-
-
Notifications
You must be signed in to change notification settings - Fork 571
/
sync-package.js
65 lines (53 loc) · 2.03 KB
/
sync-package.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
// Pulls a dependency's code into the repo, based on the package.json "files" field.
const fs = require("fs/promises");
const path = require("path");
(async () => {
const packageName = process.argv[2];
if (!packageName) {
console.log("Usage: node sync-package.js <package-name>");
process.exit(1);
}
const alwaysIncludedFiles = []; // ["README.md", "LICENSE", "CHANGELOG.md", "package.json"];
const alwaysExclude = ["demo/", "index.html", "$MenuBar.js"];
const packageDir = path.join(__dirname, "node_modules", packageName);
const outputDir = path.join(__dirname, "lib", packageName);
console.log(`Syncing ${packageName}...`);
console.log(`Source directory: ${packageDir}`);
console.log(`Output directory: ${outputDir}`);
const packageJson = JSON.parse(await fs.readFile(path.join(packageDir, "package.json"), "utf8"));
const { files } = packageJson;
if (!files) {
console.log(`No "files" field in ${packageDir}`);
process.exit(1);
}
if (!Array.isArray(files)) {
console.log(`"files" field is not an array in ${packageDir}`);
process.exit(1);
}
if (files.length === 0) {
console.log(`"files" field is empty in ${packageDir}`);
process.exit(1);
}
// TODO: Use more npm machinery, in order to include things like README.md without being specific to the variations used.
// For example see https://www.npmjs.com/package/npm-packlist
// Icing on the cake would be to rewrite README.md so that relative links work.
for (const extraFile of alwaysIncludedFiles) {
if (!files.includes(extraFile)) {
files.push(extraFile);
}
}
// TODO: Command line option to exclude files, and use glob instead of exact match on "files" field item.
for (const exclude of alwaysExclude) {
const index = files.indexOf(exclude);
if (index !== -1) {
files.splice(index, 1);
}
}
await fs.rm(outputDir, { recursive: true });
for (const file of files) {
const from = path.join(packageDir, file);
const to = path.join(outputDir, file);
console.log("Copying", from, "to", to);
await fs.cp(from, to, { recursive: true });
}
})();