-
Notifications
You must be signed in to change notification settings - Fork 3
/
postInstall.js
69 lines (56 loc) · 1.74 KB
/
postInstall.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
var fs = require('fs');
var spath = require('path');
var os = require('os');
function getAllfiles(dir, findOne) {
// if (arguments.length < 2) throw new TypeError('Bad arguments number');
if (typeof findOne !== 'function') {
throw new TypeError('The argument "findOne" must be a function');
}
eachFileSync(spath.resolve(dir), findOne);
}
function eachFileSync (dir, findOne) {
var stats = fs.statSync(dir);
var files = fullPath(dir, fs.readdirSync(dir));
files.forEach(function (item) {
findOne(item, stats);
});
}
function fullPath (dir, files) {
return files.map(function (f) {
return spath.join(dir, f);
});
}
getPackageJson("./../..", function (f, s) {
var isPackageJson = f.match(/package\.json/);
if (isPackageJson != null) {
console.log("find package.json file: " + f);
if (isFile(f) == false) {
console.log("configure package.json error!!");
return;
}
var rf = fs.readFileSync(f, "utf-8");
var searchKey = rf.match(/\n.*\"scripts\"\: \{\n/);
if (/configureReactBackHome/.test(rf)) {
return;
}
if (searchKey != null) {
rf = rf.replace(searchKey[0], searchKey[0] + " \"configureReactBackHome\"\: \"node node_modules\/react-back-home\/configureReactBackHome\.js\"\,\n");
fs.writeFileSync(f, rf, "utf-8");
}
}
});
function getPackageJson(dir, findOne) {
if (typeof findOne !== 'function') {
throw new TypeError('The argument "findOne" must be a function');
}
eachFileSync(spath.resolve(dir), findOne);
}
function isFile(path){
return exists(path) && fs.statSync(path).isFile();
}
function exists(path){
return fs.existsSync(path) || path.existsSync(path);
}
function isDir(path){
return exists(path) && fs.statSync(path).isDirectory();
}