forked from santimendoza/craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·398 lines (354 loc) · 10.5 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env node
'use strict';
const program = require('commander');
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const spawn = require('cross-spawn');
const execSync = require('child_process').execSync;
const tmp = require('tmp');
const yaml = require('js-yaml');
const packageJson = require('./package.json');
const defaultSpec = {
node_modules: 'ignore',
'package.json': 'merge',
'package-lock.json': 'ignore',
'craft.yaml': 'ignore',
'craft.yml': 'ignore',
'.git': 'ignore',
};
let projectName;
let projectTemplate;
program
.name('craft')
.version(packageJson.version)
.arguments('<project-directory> [template-url]')
.usage(
`${chalk.green('<project-directory>')} ${chalk.cyan(
'<template-url>'
)} [options]`
)
.action(function(name, template) {
projectName = name;
projectTemplate = template;
})
.parse(process.argv);
if (
typeof projectName === 'undefined' ||
typeof projectTemplate === 'undefined'
) {
console.error('Please specify the project directory and template url:');
console.log(
` ${chalk.cyan(program.name())} ${chalk.green(
'<project-directory>'
)} ${chalk.yellow('<template-url>')}`
);
console.log();
console.log('For example:');
console.log(
` ${chalk.cyan(program.name())} ${chalk.green(
'my-react-app'
)} ${chalk.yellow('https://github.com/cebroker/react-foundation')}`
);
console.log();
console.log(
`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`
);
process.exit(1);
}
const root = path.resolve(projectName);
const npx = isNPXAvailable();
if (!npx && !isCRAInstalled()) {
console.error('No create-react-app installation has been detected.');
console.log('Please install create-react-app to continue.');
console.log();
console.log(
` ${chalk.cyan('npm')} install -g ${chalk.bold('create-react-app')}`
);
process.exit(1);
}
createApp(projectName, npx)
.then(() => {
console.log();
console.log(chalk.magenta('Applying custom template...'));
console.log();
// Clone template to a temp directory and read the spec file
return getTemporaryDirectory().then(obj => {
return new Promise((resolve, reject) => {
const command = 'git';
const args = ['clone', projectTemplate, obj.tmpdir];
const child = spawn(command, args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject({
command: `${command} ${args.join(' ')}`
});
}
Object.assign(obj, readConfig(obj.tmpdir));
resolve(obj);
});
});
});
})
.then(obj => {
// Delete folders and files
console.log();
console.log('Deleting files...');
const files = Object.keys(obj.spec).filter(file => obj.spec[file] === 'delete');
const promises = [];
for (const file of files) {
const target = path.join(root, file);
promises.push(
new Promise((resolve, reject) => {
fs.remove(target, err => {
if (err) {
console.log(chalk.red(`! ${file}`));
} else {
console.log(`- ${file}`);
}
resolve();
});
})
);
}
return Promise.all(promises).then(() => obj);
})
.then(obj => {
// Copy folders and files
console.log();
console.log('Copying files...');
const files = fs.readdirSync(obj.tmpdir);
const skip = file => {
const directive = obj.spec[file];
return directive && directive !== 'replace';
};
const prefixLength = obj.tmpdir.length + 1;
const filter = (src, dest) => !skip(src.substring(prefixLength));
const promises = [];
for (const file of files) {
if (skip(file)) {
continue;
}
const src = path.join(obj.tmpdir, file);
const dest = path.join(root, file);
promises.push(
new Promise((resolve, reject) => {
fs.copy(src, dest, { filter }, err => {
if (err) {
console.log(chalk.red(`! ${file}`));
} else {
console.log(`+ ${file}`);
}
resolve();
});
})
);
}
return Promise.all(promises).then(() => obj);
})
.then(obj => {
if (obj.spec['package.json'] !== 'merge') {
return obj;
}
console.log();
console.log('Installing template packages...');
const originalDirectory = process.cwd();
process.chdir(root);
// Get dependencies to install
const templatePackageJsonPath = path.resolve(obj.tmpdir, 'package.json');
let templatePackageJson;
try {
templatePackageJson = fs.readJsonSync(templatePackageJsonPath);
} catch (error) {
return Promise.resolve(obj);
}
const templateDependencies = templatePackageJson.dependencies || {};
// Exclude dependencies that were already installed when the app was created
const appPackageJsonPath = path.join(root, 'package.json');
let appPackageJson = fs.readJsonSync(appPackageJsonPath);
Object.keys(appPackageJson.dependencies || {}).forEach(key => {
delete templateDependencies[key];
});
// Install additional dependencies
return install(templateDependencies).then(() => {
// Re-read the app package.json to get updated dependencies
appPackageJson = fs.readJsonSync(appPackageJsonPath)
// Dependencies are already available in app package.json so we can safely
// replace them. However we cannot replace template scripts with app
// scripts since the template could contains scripts customization
const scripts = Object.assign(
{},
appPackageJson.scripts,
templatePackageJson.scripts
);
// As the default ESLint configuration is fully captured in the sharable
// eslint-config-react-app package, we don't expect any changes in the
// app package.json. So, we allow any customizations in the template to
// take precendence.
const eslintConfig = templatePackageJson.eslintConfig || appPackageJson.eslintConfig;
const packageJson = Object.assign(
{},
templatePackageJson,
appPackageJson,
{ scripts, eslintConfig }
);
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
return obj;
});
})
.then(obj => {
// Perform cleanup
console.log();
console.log(chalk.green('Template applied successfully!'));
obj.cleanup();
})
.catch(reason => {
console.log();
console.log('Aborting installation.');
if (reason.command) {
console.log(` ${chalk.cyan(reason.command)} has failed.`);
} else {
console.log(chalk.red('Unexpected error. Please report it as a bug:'));
console.log(reason);
}
console.log();
});
function createApp(name, npx) {
return new Promise((resolve, reject) => {
const command = npx ? 'npx' : 'create-react-app';
const args = npx ? ['create-react-app', name] : [name];
const child = spawn(command, args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject({
command: `${command} ${args.join(' ')}`
});
return;
}
resolve();
});
});
}
function isNPXAvailable() {
try {
execSync('npx --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}
function isCRAInstalled() {
try {
execSync('create-react-app --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}
function getTemporaryDirectory() {
return new Promise((resolve, reject) => {
// Unsafe cleanup lets us recursively delete the directory if it contains
// contents; by default it only allows removal if it's empty
tmp.dir({ unsafeCleanup: true }, (err, tmpdir, callback) => {
if (err) {
reject(err);
} else {
resolve({
tmpdir: tmpdir,
cleanup: () => {
try {
callback();
} catch (ignored) {
// Callback might throw and fail, since it's a temp directory the
// OS will clean it up eventually...
}
}
});
}
});
});
}
function readConfig(templateDir) {
const yamlFile = 'craft.yaml';
const yamlPath = path.join(templateDir, yamlFile);
const ymlFile = 'craft.yml'
const ymlPath = path.join(templateDir, ymlFile);
let configFile, configPath;
let config = {};
if (fs.existsSync(yamlPath)) {
configFile = yamlFile;
configPath = yamlPath;
} else if (fs.existsSync(ymlPath)) {
configFile = ymlFile;
configPath = ymlPath;
}
if (configPath) {
console.log(`Using craft configuration from ${configFile}.`);
try {
const configContent = fs.readFileSync(configPath, 'utf8');
config = normalizeConfig(yaml.safeLoad(configContent, { json: true }));
} catch (err) {
console.log(chalk.red(`Failed to read ${configFile}. Using defaults.`));
console.log(err);
}
}
return config;
}
function normalizeConfig(config = {}) {
const directives = [ 'ignore', 'delete', 'replace'];
const spec = Object.assign({}, defaultSpec);
const add = (directive, value) => {
const t = typeof value;
if (t === 'string' || t === 'number' || t === 'boolean') {
const filePath = value.toString().replace(/\//g, path.sep);
spec[filePath] = directive;
} else {
const dir = chalk.red(directive);
console.log(`Invalid value for directive ${dir}: ${value}.`);
}
}
if (config.spec) {
Object.keys(config.spec).forEach(directive => {
if (directives.includes(directive)) {
const value = config.spec[directive];
if (Array.isArray(value)) {
value.forEach(val => add(directive, val));
} else {
add(directive, value);
}
} else {
console.log(`Invalid directive: ${chalk.red(directive)}.`);
}
});
}
return { spec };
}
function install(dependencies) {
return new Promise((resolve, reject) => {
let args = [
'install',
'--save',
//'--save-exact',
'--loglevel',
'error'
];
args = args.concat(
Object.keys(dependencies).map(key => {
return `${key}@${dependencies[key]}`;
})
);
const child = spawn('npm', args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject({
command: `${command} ${args.join(' ')}`
});
return;
}
resolve();
});
});
}