-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (57 loc) · 1.76 KB
/
index.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
#!/usr/bin/env node
const path = require("path");
const fs = require("fs-extra");
async function main(folder) {
const modulefolder = __dirname;
const sampleFolder = path.join(modulefolder, "node_modules/demo-pop-app");
try {
await fs.mkdir(folder);
} catch (e) {
try {
const files = await fs.readdir(folder);
const invalidFiles = files.filter((f) => ![".git"].includes(f));
if (invalidFiles.length > 0) throw new Error("directory not empty");
} catch (e2) {
console.error(e.message);
console.error(`Unable to create directory '${folder}'.`);
console.error(`Target directory must be exist and be empty.`);
process.exit(1);
}
}
try {
await fs.copy(sampleFolder, folder);
const { scripts, dependencies, devDependencies } = JSON.parse(
await fs.readFile(path.join(folder, "package.json"))
);
const packageJson = {
name: "demo-pop-app",
version: "1.0.0",
description: "What will your pop! app do?",
author: "your_name_here",
scripts,
dependencies,
devDependencies,
};
const gitignoreSample =
"# node_modules\nnode_modules\n\n# package-lock.json\npackage-lock.json\n\n# dist\ndist";
await fs.writeFile(
path.join(folder, "package.json"),
JSON.stringify(packageJson, null, " ")
);
await fs.writeFile(path.join(folder, ".gitignore"), gitignoreSample);
} catch (e) {
console.error(e);
console.error("Unable to copy app into newly created directory.");
process.exit(1);
}
console.log(`A new pop! JS app is now in your "${folder}" folder.`);
console.log("The commands below will start the app.\n");
console.log(`1. cd ${folder}`);
console.log("2. npm install");
console.log("3. npm run dev\n");
}
if (process.argv.length < 3) {
console.log("create-pop-app <foldername>");
process.exit(1);
}
main(process.argv[2]);