-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·81 lines (64 loc) · 1.89 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env node
const fse = require("fs-extra");
const shell = require("shelljs");
const writeTemplateFile = (fileName, dest, parameters) => {
const content = fse.readFileSync(fileName, "utf8").toString();
const processedTemplate = Object.entries(parameters).reduce(
(acc, [key, value]) => {
return acc.replace(new RegExp(`{{\\s*${key}\\s*}}`, "g"), value);
},
content
);
fse.writeFileSync(dest, processedTemplate);
};
const writeTemplate = (parameters) => {
fse.copySync(__dirname + "/template", parameters.projectDirectory, {
filter: (src, dest) => {
if (fse.lstatSync(src).isDirectory()) {
return true;
}
writeTemplateFile(src, dest, parameters);
return false;
},
});
// Rename _gitignore to .gitignore (needed for npm publish)
fse.renameSync(
`${parameters.projectDirectory}/_gitignore`,
`${parameters.projectDirectory}/.gitignore`
);
};
const run = async () => {
// Check dependencies
if (!shell.which("git")) {
shell.echo("Sorry, this script requires git");
shell.exit(1);
}
const projectDirectory = process.argv[2];
if (!projectDirectory) {
console.log("Please specify a project name:");
console.log("create-base-cli my-project");
process.exit(1);
}
// remove all invalid chars
const packageName = projectDirectory.replace(/[^a-z0-9-]/gi, "-");
const parameters = {
projectDirectory,
packageName: projectDirectory,
packageDescription: "Base CLI",
bin: projectDirectory,
};
writeTemplate(parameters);
// move to project directory
shell.cd(projectDirectory);
// init git repo
shell.exec("git init");
// install dependencies
shell.exec("npm install");
console.log("Done! 🎉");
console.log("");
console.log("Next steps:");
console.log(`cd ${projectDirectory}`);
console.log("npm link");
console.log(`${projectDirectory} --help`);
};
run();