forked from jaredpalmer/razzle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-examples.js
More file actions
137 lines (122 loc) · 4.46 KB
/
Copy pathbootstrap-examples.js
File metadata and controls
137 lines (122 loc) · 4.46 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
const yargs = require('yargs');
const execa = require('execa');
const fs = require('fs-extra');
const path = require('path');
const rootDir = path.resolve(__dirname, '..');
const defaultExample = 'packages/create-razzle-app/templates/default';
let argv = yargs
.usage('$0 [<example>...] [-s|--stage=<stage>] [-c|--copy-only] [-a|--all]')
.command({
command: '*',
builder: yargs => {
return yargs
.option('c', {
alias: 'copy-only',
describe: 'only copy',
type: 'boolean',
default: false,
})
.option('a', {
alias: 'all',
describe: 'bootstrap all',
type: 'boolean',
default: false,
})
.option('s', {
alias: 'stage',
describe: 'stage directory',
default: false,
type: 'string'
})
.option('d', {
alias: 'default',
describe: 'bootstrap default example',
type: 'boolean',
default: false,
});
},
handler: async argv => {
const packageJsonData = JSON.parse(
await fs.readFile(path.join(rootDir, 'package.json'))
);
const packageMetaData = JSON.parse(
await fs.readFile(path.join(rootDir, 'package.meta.json'))
);
const examples = (
await fs.readdir(path.join(rootDir, 'examples'), {
withFileTypes: true,
})
)
.filter(item => item.isDirectory())
.map(item => item.name);
const exampleNamesMap = (argv.all
? [defaultExample].concat(examples)
: argv._).map(example => {
return [example, example.includes('/') ? example : `examples/${example}`];
});
const exampleDirs = exampleNamesMap.map(example =>example[1]);
let extraWorkspaceDirs = [];
let extraNoHoist = [];
if (exampleDirs) {
const missing = exampleDirs
.map(example => {
const examplePackageJson = path.join(rootDir, example, 'package.json');
if (!fs.existsSync(examplePackageJson)) {
return example;
}
let examplePackageJsonData = {};
try {
examplePackageJsonData = JSON.parse(fs.readFileSync(examplePackageJson));
} catch {
console.log(`failed to read json ${examplePackageJson}`);
process.exit(1);
}
const razzle_meta = examplePackageJsonData.razzle_meta||{};
extraNoHoist = extraNoHoist.concat((razzle_meta.nohoist||[]).map(nohoist=>`${example}/${nohoist}`))
extraWorkspaceDirs = extraWorkspaceDirs
.concat([example])
.concat(razzle_meta.extraWorkspacedirs || []);
return false;
})
.filter(x => Boolean(x));
if (missing.length) {
console.log(`${missing.join(', ')} not found in ${rootDir}`);
process.exit(1);
}
}
packageJsonData.workspaces = packageJsonData.workspaces.concat(extraWorkspaceDirs);
packageJsonData.workspaces.nohoist = (packageJsonData.workspaces.nohoist||[]).concat(extraNoHoist);
const jsonString = JSON.stringify(packageJsonData, null, ' ') + '\n';
if (jsonString) {
try {
fs.writeFileSync(path.join(rootDir, 'package.json'), jsonString);
} catch {
console.log(`failed to write json ${item}`);
}
} else {
console.log(`not writing empty json ${item}`);
}
await execa("yarn install --no-lockfile", { shell: true, stdio: 'inherit' });
exampleDirs
.map(example => {
fs.ensureDirSync(path.join(rootDir, example, 'node_modules', '.bin'))
fs.writeFileSync(path.join(rootDir, example, 'node_modules', '.bin', 'restrap'), `#!/usr/bin/env node
'use strict';
const execa = require('execa');
execa("cd ${rootDir} && yarn bootstrap-examples ${example}", { shell: true, stdio: 'inherit' });`);
fs.chmodSync(path.join(rootDir, example, 'node_modules', '.bin', 'restrap'), 0o775)
})
packageJsonData.workspaces = packageMetaData.workspaces;
const resetJsonString = JSON.stringify(packageJsonData, null, ' ') + '\n';
if (resetJsonString) {
try {
fs.writeFileSync(path.join(rootDir, 'package.json'), resetJsonString);
} catch {
console.log(`failed to write json ${item}`);
}
} else {
console.log(`not writing empty json ${item}`);
}
},
})
.help().argv;