-
Notifications
You must be signed in to change notification settings - Fork 48
/
create-package.js
68 lines (62 loc) · 2.57 KB
/
create-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
66
67
68
const { exec, spawn } = require("child_process");
const fs = require("fs");
const colors = require("colors");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const isWindows = process.platform === "win32";
const packageName = process.argv[2];
if (!packageName) {
console.log("Error: Please enter package name".red, "\nnpm run create-package {package-name}\n".yellow);
return process.exit(0);
}
try {
let packageDir = fs.readdirSync(`./packages/${packageName}`);
if (packageDir) {
console.log(`Error: Package ${packageName} already exist.\n`.red);
return process.exit(0);
}
} catch (err) {}
function execCallback(error) {
if (error) {
console.log(`\nError: package has not been created... ${error}`.red);
return process.exit(0);
}
const cmd = isWindows ? "cmd" : "npm";
const args = isWindows ? ["/c", "npm init -y --quiet > NUL"] : ["init -y &>/dev/null"];
const npmInitComman = spawn(cmd, args, {
shell: true,
stdio: "inherit",
cwd: `${__dirname}/packages/${packageName}`,
});
npmInitComman.on("close", () => {
let userName;
rl.question("Enter your name: ".cyan, (name) => {
userName = name;
rl.close();
});
rl.on("close", function () {
let packageInfo = fs.readFileSync(`./packages/${packageName}/package.json`);
packageInfo = JSON.parse(packageInfo);
packageInfo.author = userName;
packageInfo.license = "MIT";
packageInfo = JSON.stringify(packageInfo);
packageInfo = packageInfo.split(",").join(",\n ").split("{").join("{\n ").split("}").join("\n}");
fs.writeFileSync(`./packages/${packageName}/package.json`, packageInfo);
console.log(`\nSuccess! ${packageName} package has been created!\n`.green);
console.log(`You can now open`.cyan + ` packages/${packageName} `.yellow + `directory in your fav code editor.\n`.cyan);
console.log(
`If you want to test your package, run a test server`.cyan +
` cd server && npm run start `.yellow +
`and open`.cyan +
` http://localhost:4000/${packageName} `.yellow +
`in your web browser.\n`.cyan
);
process.exit(0);
});
});
}
if (isWindows) exec(`npm run create-package-windows ${packageName}`, execCallback);
else exec(`npm run create-package-linux ${packageName}`, execCallback);