-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
192 lines (170 loc) · 6.73 KB
/
create.js
File metadata and controls
192 lines (170 loc) · 6.73 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const inquirer = require('inquirer');
const fs = require('fs-extra');
const commander = require('commander');
const chalk = require('chalk');
const cp = require("child_process");
const rimraf = require("rimraf");
const Listr = require('listr');
const packageJson = require('./package.json');
let projectName;
new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<project-directory>')
.usage(`${chalk.green('<project-directory>')} [options]`)
.action(name => {
projectName = name;
}).parse(process.argv);
if (!projectName) {
const projectQuestions = [
{
name: 'project-name',
type: 'input',
message: 'Project name:',
validate: function (input) {
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
else return 'Project name may only include letters, numbers, underscores and hashes.';
}
}
];
inquirer.prompt(projectQuestions)
.then(answers => {
createTemplate(answers['project-name']);
});
} else {
createTemplate(projectName);
}
function getCurrentWorkingDirectory() {
return process.cwd();
}
function createTemplate(projectName) {
const templatePath = `${__dirname}/templates`;
const projectPath = `${getCurrentWorkingDirectory()}/${projectName}`;
const performCreate = (replaceExisting) => {
const tasks = new Listr([
{
title: "Initialize Directory",
task: () => {
return new Listr([
{
title: "Removing existing directory",
enabled: () => replaceExisting,
task: () => {
return new Promise((resolve, reject) => {
try {
rimraf(projectPath, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
} catch (e) {
reject(e);
}
});
}
},
{
title: "Initialize directory",
task:
() => {
return new Promise((resolve, reject) => {
try {
fs.mkdirp(projectPath).then(_ => {
resolve();
}).catch(err => {
reject(err);
});
} catch (e) {
reject(e);
}
})
}
}
], { concurrent: false })
}
},
{
title: "Initialize template",
task: () => {
return new Promise((resolve, reject) => {
try {
createDirectoryContents(templatePath, projectName);
const fileName = `${getCurrentWorkingDirectory()}/${projectName}/package.json`;
const file = require(fileName);
file.name = projectName;
fs.writeFileSync(fileName, JSON.stringify(file));
resolve();
} catch (e) {
reject(e);
}
})
}
},
{
title: "Initializing Packages (this may take a few moments)",
task: () => {
return new Promise((resolve, reject) => {
try {
cp.exec("npm install", { cwd: projectPath }, function (error, stdout, stderr) { }).on("close", () => {
resolve();
});
} catch (e) {
reject(e);
}
});
}
}
], {
exitOnError: true
});
tasks.run()
.then(_ => {
console.log('');
console.log("Success!");
console.log("Let's get started by using the following commands:");
console.log('');
console.log("\t" + chalk.blueBright(`cd ${projectName}`));
console.log("\t" + chalk.blueBright(`npm start`));
})
.catch(err => {
console.error(err);
});
};
if (fs.existsSync(projectPath)) {
const replacementChoices = ['No', 'Yes'];
const replacementQuestions = [{
name: 'replace-existing-directory',
type: 'list',
message: `${projectPath} already exists. Would you like to replace it?`,
choices: replacementChoices
}];
inquirer.prompt(replacementQuestions)
.then(answers => {
if (answers['replace-existing-directory'] === "Yes") {
performCreate(true);
} else {
console.log(chalk.red('Unable to continue due to project name conflict.'));
}
});
} else {
performCreate();
}
}
function createDirectoryContents(templatePath, newProjectPath) {
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach(file => {
const origFilePath = `${templatePath}/${file}`;
// get stats about the current file
const stats = fs.statSync(origFilePath);
if (stats.isFile()) {
const contents = fs.readFileSync(origFilePath, 'utf8');
const writePath = `${getCurrentWorkingDirectory()}/${newProjectPath}/${file}`;
fs.writeFileSync(writePath, contents, 'utf8');
} else if (stats.isDirectory()) {
fs.mkdirSync(`${getCurrentWorkingDirectory()}/${newProjectPath}/${file}`);
// recursive call
createDirectoryContents(`${templatePath}/${file}`, `${newProjectPath}/${file}`);
}
});
}