-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprogram-kit-init.cjs
More file actions
58 lines (50 loc) · 1.64 KB
/
program-kit-init.cjs
File metadata and controls
58 lines (50 loc) · 1.64 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const SKIP = new Set(["node_modules", ".git", ".DS_Store"]);
function copyRecursive(src, dst) {
fs.mkdirSync(dst, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
if (SKIP.has(entry.name)) continue;
const from = path.join(src, entry.name);
const to = path.join(dst, entry.name);
if (entry.isDirectory()) {
copyRecursive(from, to);
} else if (entry.isSymbolicLink()) {
const real = fs.realpathSync(from);
const st = fs.statSync(real);
if (st.isDirectory()) {
copyRecursive(real, to);
} else {
fs.mkdirSync(path.dirname(to), { recursive: true });
fs.copyFileSync(real, to);
}
} else {
fs.mkdirSync(path.dirname(to), { recursive: true });
fs.copyFileSync(from, to);
}
}
}
function isEmptyDir(dir) {
try {
return fs.readdirSync(dir).length === 0;
} catch (_) {
return true;
}
}
const args = process.argv.slice(2);
const force = args.includes("--force");
const targetArg = args.find((a) => !a.startsWith("-")) || ".";
const target = path.resolve(process.cwd(), targetArg);
const packageRoot = path.resolve(__dirname, "..");
if (fs.existsSync(target) && !isEmptyDir(target) && !force) {
console.error(`Target is not empty: ${target}`);
console.error("Re-run with --force to overwrite.");
process.exit(1);
}
fs.mkdirSync(target, { recursive: true });
copyRecursive(packageRoot, target);
console.log(`Program kit scaffold created at ${target}`);
console.log("Next steps:");
console.log(` cd ${targetArg}`);
console.log(" npm install");