| 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 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
3×
2×
1×
1×
2×
1×
1×
2×
1×
1×
2×
2×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| /**
* @file
* @desc scaffold handlers
* @author https://github.com/hoperyy
* @date 2017/08/11
*/
const fs = require('fs');
const path = require('path');
const fse = require('fs-extra');
const mergeDirs = require('merge-dirs');
const pathUtil = require('./path');
const fileUtil = require('./file');
const npm = require('./npm');
const getNpmPackageVersion = require('get-npm-package-version');
const createExecPackageJsonFile = (execInstallFolder, scaffoldName) => {
const pkgJsonPath = path.join(execInstallFolder, 'package.json');
fse.ensureFileSync(pkgJsonPath);
fse.writeFileSync(pkgJsonPath, JSON.stringify({
name: `installing-${scaffoldName}`,
version: '1.0.0',
}));
};
const getMaps = (() => {
let shortKeyMap = null;
let fullKeyMap = null;
return (scaffoldList) => {
if (shortKeyMap && fullKeyMap) {
return {
shortKeyMap,
fullKeyMap,
};
}
fullKeyMap = {};
scaffoldList.forEach((item) => {
fullKeyMap[item.fullName] = {
otherName: item.value,
version: item.version,
};
});
shortKeyMap = {};
scaffoldList.forEach((item) => {
shortKeyMap[item.value] = {
otherName: item.fullName,
version: item.version,
};
});
return {
shortKeyMap,
fullKeyMap,
};
};
})();
module.exports = {
checkOutdated: true,
beforeScaffoldInstall() {},
writeScaffoldInConfigFile({ scaffoldName }) {
const cwd = process.cwd();
/**
* 1. .biorc will be read firstly
* 2. package.json "bio-scaffold" will be read secondly
*/
const stage0Config = path.join(cwd, pathUtil.configName);
const stage1Config = path.join(cwd, 'package.json');
const writeFile = () => {
fileUtil.writeFileSync(stage0Config, JSON.stringify({
scaffold: this.getFullName(scaffoldName),
}, null, '\t'));
};
if (fs.existsSync(stage0Config)) {
writeFile();
} else { // then write package.json
if (fs.existsSync(stage1Config)) {
const pkgContent = fs.readFileSync(stage1Config, 'utf-8');
try {
const obj = JSON.parse(pkgContent);
obj['bio-scaffold'] = scaffoldName;
fs.writeFileSync(stage1Config, JSON.stringify(obj, null, '\t'));
} catch (err) {
writeFile();
}
} else {
writeFile();
}
}
},
getScaffoldNameFromConfigFile() {
const cwd = process.cwd();
let scaffoldName = '';
const stage0Config = path.join(cwd, pathUtil.configName);
const stage1Config = path.join(cwd, 'package.json');
// prefer .biorc
try {
scaffoldName = JSON.parse(fs.readFileSync(stage0Config).toString()).scaffold;
} catch (err) {
// eslint-disable-no-empty
}
// then get package.json "bio-scaffold"
if (!scaffoldName) {
try {
scaffoldName = JSON.parse(fs.readFileSync(stage1Config).toString())['bio-scaffold'];
} catch (err) {
// eslint-disable-no-empty
}
}
if (!scaffoldName) {
console.log('\nno scaffold info found at current directory, please run "bio init <scaffoldName>" first\n'.red);
}
return scaffoldName;
},
getFullName(scaffoldName) {
const maps = getMaps(this.scaffoldList);
const { shortKeyMap } = maps;
return shortKeyMap[scaffoldName] ? shortKeyMap[scaffoldName].otherName : scaffoldName;
},
getShortName(scaffoldName) {
const maps = getMaps(this.scaffoldList);
const { fullKeyMap } = maps;
return fullKeyMap[scaffoldName] ? fullKeyMap[scaffoldName].otherName : scaffoldName;
},
getHopedVersion(scaffoldName) {
const maps = getMaps(this.scaffoldList);
const fullName = this.getFullName(scaffoldName);
const hopedVersion = maps.fullKeyMap[fullName] ? maps.fullKeyMap[fullName].version : 'latest';
return hopedVersion;
},
scaffoldList: [], // TODO: add default scaffolds
/**
* @func
* @desc get scaffold name for current project from config file
* @param {String} cwd: current project dir path
* @return {String} scaffold name
*/
getScaffoldName(cwd) {
const { configName } = pathUtil;
const configPath = path.join(cwd, configName);
const content = fs.readFileSync(configPath).toString();
const contentObj = JSON.parse(content);
return contentObj.scaffold;
},
/**
* @func
* @desc ensure scaffold latest
* @param {String} scaffoldName
*/
ensureScaffoldLatest(scaffoldName) {
// move cached scaffold file, if exists
this.moveScaffoldCache(scaffoldName);
if (!this.isScaffoldExists(scaffoldName)) {
console.log(`installing scaffold ${scaffoldName}...`);
this.installScaffold(scaffoldName, { async: false });
console.log(`scaffold ${scaffoldName} installed successfully`);
return;
}
if (this._isScaffoldOutdate(scaffoldName)) {
console.log(`\nupdating scaffold ${scaffoldName} silently...\n`);
this.installScaffold(scaffoldName, { async: true });
}
},
/**
* @func
* @desc check whether scaffold exists
* @param {String} scaffoldName
* @return {Boolean}
*/
isScaffoldExists(scaffoldName) {
const pkg = path.join(pathUtil.getScaffoldFolder(scaffoldName), 'package.json');
Eif (!fs.existsSync(pkg)) {
console.log(`\n${scaffoldName}/package.json is not found at local\n`);
return false;
}
return true;
},
/**
* @func
* @private
* @desc check whether scaffold is outdated
* @param {String} scaffoldName
* @return {Boolean}
*/
_isScaffoldOutdate(scaffoldName) {
if (!this.checkOutdated) {
return false;
}
const packagejsonFilePath = path.join(pathUtil.getScaffoldFolder(scaffoldName), 'package.json');
const obj = JSON.parse(fs.readFileSync(packagejsonFilePath).toString());
const currentVersion = obj.version;
const hopedVersion = this.getHopedVersion(scaffoldName);
if (hopedVersion !== 'latest') {
if (hopedVersion !== currentVersion) {
console.log(`\nscaffold ${scaffoldName} is outdated, details as below:\n`);
console.log(' - scaffoldName: ', scaffoldName);
console.log(' - currentVersion: ', currentVersion);
console.log(' - hopedVersion: ', hopedVersion);
return true;
} else {
return false;
}
} else {
const latestVersion = getNpmPackageVersion(scaffoldName, { registry: npm.registry, timeout: 2000 });
if (latestVersion) {
if (currentVersion !== latestVersion) {
console.log(`\nscaffold ${scaffoldName} is outdated, details as below:\n`);
console.log(' - scaffoldName: ', scaffoldName);
console.log(' - currentVersion: ', currentVersion);
console.log(' - hopedVersion: ', hopedVersion, latestVersion, '\n');
return true;
}
return false;
}
return false;
}
},
moveScaffoldCache(scaffoldName) {
const execInstallFolder = pathUtil.getScaffoldExecInstallFolder(scaffoldName);
const scaffoldFolder = pathUtil.getScaffoldFolder(scaffoldName);
const scaffoldWrapper = pathUtil.getScaffoldWrapper(scaffoldName);
const srcScaffold = path.join(execInstallFolder, 'node_modules', scaffoldName);
const srcScaffoldDep = path.join(execInstallFolder, 'node_modules');
if (!fs.existsSync(srcScaffold) || !fs.existsSync(srcScaffoldDep)) {
return;
}
if (fs.existsSync(scaffoldFolder)) {
fse.removeSync(scaffoldFolder);
}
// move node_modules
fse.moveSync(srcScaffold, path.join(scaffoldWrapper, scaffoldName), {
overwrite: true,
});
// merge node_modules
console.log('\nreplacing scaffold...');
mergeDirs.default(srcScaffoldDep, path.join(scaffoldFolder, 'node_modules'), 'overwrite');
console.log('replecement done.');
fse.removeSync(execInstallFolder);
},
/**
* @func
* @desc install scaffold
* @param {String} scaffoldName
*/
installScaffold(scaffoldName, options) {
const { async } = options;
const execInstallFolder = pathUtil.getScaffoldExecInstallFolder(scaffoldName);
const child = require('child_process');
const hopedVersion = this.getHopedVersion(scaffoldName);
// ensure exec dir
fse.ensureDirSync(execInstallFolder);
// ensure package.json exists
createExecPackageJsonFile(execInstallFolder, scaffoldName);
this.beforeScaffoldInstall(execInstallFolder);
const order = `cd ${execInstallFolder} && npm --registry ${npm.registry} install ${scaffoldName}@${hopedVersion}`;
if (async) {
child.exec(order, (error) => {
if (error) {
// remove exec dir
fse.removeSync(execInstallFolder);
return;
}
console.log(`scaffold "${scaffoldName}" updated successfully!`);
});
} else {
try {
child.execSync(order, {
stdio: 'inherit',
});
this.moveScaffoldCache(scaffoldName);
} catch (err) {
// throw Error(err);
console.log(`\nrun "npm --registry ${npm.registry} install ${scaffoldName}@${hopedVersion}" failed and skipped\n`.red);
}
}
},
};
|