-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
155 lines (148 loc) · 4.39 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
var winston = require('winston');
var semver = require('semver');
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var exec = require('sync-exec');
var cacheFile = 'cache.json';
var fullPath = path.join(__dirname, cacheFile);
winston.loggers.add('colored', {
console: {
colorize: true,
},
});
var initCache = function () {
if (fs.existsSync(fullPath)) {
return;
}
fs.writeFile(fullPath, '');
};
initCache();
var logger = winston.loggers.get('colored');
function anonymous() {
var exports = {
options: {},
};
var devDeps = {
type: 'devDependencies',
installType: '--save-dev'
};
var prodDeps = {
type: 'dependencies',
installType: '--save'
};
var getSpawnOptions = function (type, dep, saveType) {
var options;
switch(type) {
case 'update':
options = ['npm', 'update', dep];
break;
case 'install':
options = ['npm', 'install', dep, saveType];
break;
}
return options;
};
var getPackageJsonPath = function () {
if (exports.options.packageJson) {
return path.join(process.cwd(), exports.options.packageJson);
} else {
logger.log('error', 'option.package is needed');
}
};
var getPackageJson = function (from) {
var pkg;
try {
//load package json
pkg = require(from);
} catch (e) {
logger.log('error', e);
}
return pkg;
};
var isValidModule = function (mod) {
var modulesLimit = exports.options.moduleUpdateOnlyLimit || [];
if (modulesLimit.length > 0) {
return modulesLimit.indexOf(mod.name) >= 0;
}
return true;
};
var getCheckList = function (packages) {
var moduleSet = [];
var options = exports.options;
var packageJson = getPackageJson(getPackageJsonPath());
_.forEach(packages, function (pkg) {
var modules = packageJson[pkg.type];
_.forEach(modules, function (version, name) {
moduleSet.push({
name: name,
v: version,
type: pkg.type,
installType: pkg.installType,
});
});
});
return moduleSet;
};
var isInstalled = function (mod) {
if (fs.existsSync(path.join(process.cwd(), 'node_modules', mod))) {
return true;
}
return false;
}
var getUpdateType = function (mod) {
return isInstalled(mod.name) ? 'update' : 'install';
}
var updatePackage = function (packages) {
var mappedModules = getCheckList(packages);
_.forEach(mappedModules, function (mod) {
var updateType = getUpdateType(mod);
var spawnCode;
if (updateType === 'update' && !isValidModule(mod)) {
return;
}
spawnCode = getSpawnOptions(updateType, mod.name, mod.installType).join(' ');
logger.log('info', spawnCode);
var processInfo = exec(spawnCode, {
cwd: process.cwd()
})
if (processInfo.stdout)
logger.log('info', processInfo.stdout);
if (processInfo.stderr)
logger.log('warn', processInfo.stderr);
});
};
var checkVersion = function (tv) {
var v;
v = fs.readFileSync(fullPath).toString();
if (!tv || !v) {
//tv != '' && v = '' 第一次运行
//tv = '' && v != '' 每次都check
if (tv) {
fs.writeFileSync(fullPath, tv);
}
return true;
}
if (semver.gt(tv, v)) {
fs.writeFileSync(fullPath, tv);
return true;
}
};
exports.run = function () {
var options = exports.options;
if (checkVersion(options.version)) {
var packages = _.filter([devDeps, prodDeps], function (obj) {
return options.packages[obj.type]
});
}
updatePackage(packages);
};
exports.config = function (config) {
exports.options = _.assign(exports.options, config);
};
exports.updateModules = function () {
getModulesList();
};
return exports;
}
module.exports = anonymous();