This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (109 loc) · 3.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const crossSpawn = require("cross-spawn");
const chalk = require("chalk");
const validateNpmPackageName = require("validate-npm-package-name");
const convict = require("convict");
const config = convict({
isWebapp: {
doc: "is this project a webapp or not?",
format: Boolean,
default: false,
arg: "iswebapp",
env: "ISWEBAPP",
},
umdName: {
doc: "umd module name",
format: String,
default: "myApp",
arg: "umdname",
env: "UMDNAME",
},
scripts: {
doc: "ts-scaffolder-scripts module name (can possibly include version like this: `ts-scaffolder-scripts@0.0.4` can also be used to bring your own version of the scripts",
format: String,
default: "ts-scaffolder-scripts",
arg: "scripts",
}
})
function asyncify(fn, ...args) {
return new Promise((resolve, reject) => {
fn(...args, function(err, result) {
if (err != null) {
reject(err);
} else {
resolve(result);
}
});
});
}
function spawn(cmd, args, options, cb) {
const child = crossSpawn(cmd, args, options);
child.stdout.on("data", (data) => {
console.log(chalk`[{green ${cmd}}] ${data.toString().replace(/\n$/, "")}`);
});
child.stderr.on("data", (data) => {
console.error(chalk`[{red ${cmd}}] ${data.toString().replace(/\n$/, "")}`);
});
child.on("exit", (code) => {
if (code) {
cb(code);
} else {
cb();
}
});
}
async function main() {
const projectName = process.argv[2];
if (!projectName) {
console.error(chalk`{bgGreen {black Usage:}} ts-scaffolder {yellow project-name} {grey [--iswebapp] [--umdName="{yellow bundleName}"] [--scripts="{yellow ts-scaffolder-scripts@0.0.4}"]}`);
console.error("\nconfig schema:");
console.error(config.getSchemaString());
process.exit(1);
}
const validationResult = validateNpmPackageName(projectName);
if (!validationResult.validForNewPackages) {
console.error(chalk`{bgRed {black INVALID}} {yellow project-name}`);
console.error(chalk`{yellow project-name} must conform to npm's project name rules: https://www.npmjs.com/package/validate-npm-package-name`);
if (validationResult.errors) {
validationResult.errors.forEach(err => {
console.error(chalk`{grey > ${err}}`);
});
}
process.exit(2);
}
{
console.log(chalk`[{blue ts-scaffolder}] start scaffolding`);
const projectFolder = path.join(process.cwd(), projectName);
console.log(chalk`[{blue ts-scaffolder}] create project folder`);
await asyncify(fs.mkdir, projectFolder, "0750");
console.log(chalk`[{blue ts-scaffolder}] init git repo`);
await asyncify(spawn, "git", ["init"], {
cwd: projectFolder,
});
console.log(chalk`[{blue ts-scaffolder}] init npm module`);
await asyncify(spawn, "npm", ["init", "-y"], {
cwd: projectFolder,
});
console.log(chalk`[{blue ts-scaffolder}] install ts-scaffolder-scripts`);
await asyncify(spawn, "npm", ["i", "-D", config.get("scripts")], {
cwd: projectFolder,
});
console.log(chalk`[{blue ts-scaffolder}] init ts-scaffolder-scripts`);
const initArgs = [];
if (config.get("isWebapp")) {
initArgs.push("--iswebapp");
}
initArgs.push("--umdname")
initArgs.push(config.get("umdName"));
await asyncify(spawn, "node_modules/.bin/ts-scaffolder-init", initArgs, {
cwd: projectFolder,
});
}
}
main().catch(err => {
console.error(chalk.bgRed("UNCATCHED ERROR!"), err);
console.error(chalk.bgRed("exiting!"));
process.exit(3);
});