This repository was archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateProject.js
More file actions
145 lines (123 loc) · 3.97 KB
/
createProject.js
File metadata and controls
145 lines (123 loc) · 3.97 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
const fs = require('fs');
const commander = require('commander');
const packageJson = require('./package.json');
const path = require('path');
const ncp = require('ncp');
const spawn = require('cross-spawn');
const validateNpmPackageName = require('validate-npm-package-name');
const program = new commander.Command(packageJson.name);
const steps = [
{
step: 'Make project skeleton',
run: async (rootPath, name) => {
fs.mkdirSync(rootPath, { recursive: true });
await createSkeleton(rootPath, name);
createPackageJson(rootPath, name);
},
},
{
step: 'Install dependencies',
run: async (rootPath, name) => {
await runCmd(rootPath,'npm', ['install']);
}
}
];
function createSkeleton(root, projectName) {
return new Promise((resolve, reject) => {
const skeletonFiles = ['src', 'gulpfile.js', '.gitignore', '.babelrc'];
fs.readdir(__dirname, (error, files) => {
if (error) {
reject(error);
}
for (let file of files) {
if (skeletonFiles.includes(file)) {
ncp(path.join(__dirname, file), path.join(root, file));
}
}
resolve();
});
});
}
function createPackageJson(root, projectName) {
let projectPackageJson = {
name: projectName,
description: "",
author: "",
license: "MIT",
version: "0.0.1",
main: "./dist/index.js",
scripts: {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "gulp",
"build:watch": "gulp watch",
"start": "node ./dist/index.js",
"debug": "node --inspect ./dist/index.js",
"build:debug": "npm run build && npm run debug"
},
dependencies: {},
devDependencies: {
"@babel/core": "^7.10.4",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-proposal-private-methods": "^7.10.4",
"@babel/plugin-syntax-class-properties": "^7.10.4",
"@babel/plugin-transform-runtime": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/register": "^7.10.4",
"babel-cli": "^6.26.0",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-plumber": "^1.2.1",
"gulp-sourcemaps": "^2.6.5"
}
};
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(projectPackageJson, null, 2));
}
function runCmd(root, command, args) {
return new Promise((resolve, reject) => {
let child = spawn.sync(command, args, { cwd: root, stdio: 'inherit' });
child.on('error', (error) => {
reject(error);
});
child.on('close', (code) => {
resolve(code);
});
});
}
function validateName(name) {
const nameValidation = validateNpmPackageName(name);
if (!nameValidation.validForNewPackages) {
if (nameValidation.errors.length > 0) {
console.log('Invalid name errors:');
console.log(nameValidation.errors.join('\n'));
}
return false;
}
return true;
}
async function creaateProject(name) {
const root = path.resolve(name);
const appName = path.basename(root);
if (!validateName(appName)) {
return;
}
if (fs.existsSync(name)) {
console.log('project at path ' + name + ' already exists!');
return;
}
try {
let step_num = 1;
for (let step of steps) {
console.log(`Step (${step_num}/${steps.length}): ${step.step}`);
await step.run(root, appName);
console.log('done!');
step_num++;
}
} catch (exception) {
console.log(exception.toString());
}
}
program.version(packageJson.version)
.arguments('<project-name>')
.action(creaateProject)
.parse(process.argv);