forked from jaredpalmer/razzle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-example.js
More file actions
53 lines (49 loc) · 1.6 KB
/
Copy pathnew-example.js
File metadata and controls
53 lines (49 loc) · 1.6 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
const yargs = require('yargs');
const execa = require('execa');
const fs = require('fs-extra');
const path = require('path');
const rootDir = process.cwd();
let argv = yargs
.usage(
'$0 <from> <to> [-b|--bootstrap]')
.command({
command: '*',
builder: yargs => {
return yargs
.option('b', {
alias: 'bootstrap',
describe: 'bootstrap example',
type: 'boolean',
default: true,
});
},
handler: async argv => {
console.log(argv);
const templatePath = path.join(rootDir, `examples/${argv._[0]}`);
const projectPath = path.join(rootDir, `examples/${argv._[1]}`);
const relativePath = `examples/${argv._[1]}`;
fs.copy(templatePath, projectPath)
.then(async function() {
const packageJsonData = JSON.parse(
await fs.readFile(path.join(projectPath, 'package.json'))
);
packageJsonData.name = `razzle-examples-${argv._[1]}`
const jsonString = JSON.stringify(packageJsonData, null, ' ') + '\n';
if (jsonString) {
try {
fs.writeFileSync(path.join(projectPath, 'package.json'), jsonString);
} catch {
console.log(`failed to write json ${item}`);
}
} else {
console.log(`not writing empty json ${item}`);
}
if (argv.bootstrap) {
execa.sync(`yarn`, ['bootstrap-examples', relativePath],
{ stdio: 'inherit', cwd: rootDir })
}
console.log(`Type\ncd ${relativePath}\nTo work on the new example`);
})
},
})
.help().argv;