-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport_npm_tarballs.js
More file actions
executable file
·135 lines (114 loc) · 3.16 KB
/
import_npm_tarballs.js
File metadata and controls
executable file
·135 lines (114 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
const { execSync } = require("node:child_process");
const { existsSync, mkdirSync } = require("node:fs");
const path = require("node:path");
const filename = path.basename(__filename);
function showHelp() {
console.log(`
Usage: ${filename} [options]
Packages all dependencies into tarballs in specified directory.
Options:
-h, --help Show this help message
-p, --path Output directory for tarballs (required)
-t, --target Path to package.json (default: ./package.json)
Examples:
${filename} -p ./artifacts
${filename} -p ./output -t ../project/package.json
`);
process.exit(0);
}
function parseArgs() {
const args = process.argv.slice(2);
if (args.length === 0) showHelp();
let outputPath = null;
let packageJsonPath = "./package.json";
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case "-h":
case "--help":
showHelp();
break;
case "-p":
case "--path":
outputPath = args[i + 1];
if (!outputPath) {
console.error("[Error]: Output path not specified");
process.exit(1);
}
i++;
break;
case "-t":
case "--target":
packageJsonPath = args[i + 1];
if (!packageJsonPath) {
console.error("[Error]: package.json path not specified");
process.exit(1);
}
i++;
break;
default:
console.error(`[Error]: Unknown option '${arg}'`);
showHelp();
}
}
if (!outputPath) {
console.error("[Error]: Output path is required");
showHelp();
}
return { outputPath, packageJsonPath };
}
function ensureDirectoryExists(dirPath) {
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
console.log(`Created directory: ${dirPath}`);
}
return path.resolve(dirPath);
}
function loadPackageJson(packageJsonPath) {
try {
const absolutePath = path.resolve(packageJsonPath);
if (!existsSync(absolutePath)) {
console.error(`[Error]: package.json not found at ${absolutePath}`);
process.exit(1);
}
return require(absolutePath);
} catch (error) {
console.error("Error loading package.json:", error.message);
process.exit(1);
}
}
function packDependencies(outputDir, dependencies) {
console.log(`Saving tarballs to: ${outputDir}`);
for (const dep of Object.keys(dependencies)) {
try {
console.log(`Packing ${dep}...`);
execSync(`npm pack ${dep}`, {
stdio: "inherit",
cwd: outputDir,
});
} catch (error) {
console.error(`Failed to pack ${dep}:`, error.message);
}
}
// Object.keys(dependencies).forEach((dep) => {
// console.log("Fetching tarball for ${dep}...");
// execSync(`npm pack ${dep}`, { stdio: "inherit" });
// });
// for (const [dep, version] of Object.entries(dependencies)) {
// console.log(`Fetching ${dep}@${version}...`);
// execSync(`npm pack ${dep}`, { stdio: "inherit" });
// }
}
function main() {
const { outputPath, packageJsonPath } = parseArgs();
const packageJson = loadPackageJson(packageJsonPath);
const absoluteOutputPath = ensureDirectoryExists(outputPath);
const dependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
};
packDependencies(absoluteOutputPath, dependencies);
console.log("All packages processed");
}
main();