-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
106 lines (93 loc) · 2.3 KB
/
cli.js
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
#!/usr/bin/env node
const fs = require("fs");
const https = require("https");
const meow = require("meow");
const Listr = require("listr");
const carve = require("./index.js");
const { noop } = require("./utils");
const cli = meow(
`
Usage
$ carve <package-name>... [options]
Options
--output, -o Output directory (default: packages)
--registry, -r NPM registry to use (default: https://registry.npmjs.org)
--help Displays this help text
--version Displays the version number
Examples
$ carve [email protected] react "moment@1||2||3" --output=dump
`,
{
flags: {
output: {
type: "string",
alias: "o",
default: "packages"
},
registry: {
type: "string",
alias: "r",
default: "https://registry.npmjs.org"
}
}
}
);
if (!cli.input.length === 0) {
cli.showHelp(1);
}
const tasks = new Listr([
{
title: "Resolving packages",
task: () =>
new Listr([
{
title: "",
task: async (ctx, task) => {
const logger = {
log: message => (task.title = message),
warn: noop,
error: noop
};
ctx.packages = Object.values(
await carve(cli.input, {
logger,
registry: cli.flags["registry"]
})
);
}
}
])
},
{
title: "Downloading packages",
task: () =>
new Listr([
{
title: "",
task: (ctx, task) => {
const dir = cli.flags["output"];
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
ctx.packages.forEach(packageUrl => {
const fileName = new URL(packageUrl).pathname
.slice(1)
.replace("/-/", "/")
.replace("@", "")
.split("/")
.join("-");
const file = fs.createWriteStream(`${dir}/${fileName}`);
task.title = fileName;
https.get(packageUrl, response => {
response.pipe(file);
});
});
}
}
])
}
]);
tasks.run().catch(err => {
console.error(`Error: ${err.message || "Unable to carve packages"}`);
process.exit(2);
});