UNPKG

2.91 kBJavaScriptView Raw
1(function() {
2 module.exports = function($) {
3 var Updater, kleur;
4 kleur = require('kleur');
5 Updater = (function() {
6 class Updater {
7 /*
8 clean_()
9 execute_(arg...)
10 getLatestVersion_(name)
11 listPkg_(list, isDev)
12 */
13 async clean_() {
14 return (await $.clean_(this.pathCache));
15 }
16
17 async execute_(...arg) {
18 var data;
19 data = (await $.read_('./package.json'));
20 await $.chain(this).listPkg_(data.dependencies, false).listPkg_(data.devDependencies, true);
21 if (!this.listCmd.length) {
22 $.info('update', 'everything is ok');
23 await this.clean_();
24 return this;
25 }
26 await $.exec_(this.listCmd);
27 await this.clean_();
28 return this;
29 }
30
31 async getLatestVersion_(name) {
32 var status, version;
33 this.cache || (this.cache = (await $.read_(this.pathCache)));
34 this.cache || (this.cache = {});
35 version = this.cache[name];
36 if (version) {
37 return version;
38 }
39 [status, version] = (await $.exec_(`npm view ${name} version`, {
40 silent: true
41 }));
42 if (!status) {
43 throw new Error(version);
44 }
45 this.cache[name] = version;
46 await $.write_(this.pathCache, this.cache);
47 return version; // return
48 }
49
50 async listPkg_(list, isDev) {
51 var lineCmd, name, version, versionCurrent, versionLatest;
52 for (name in list) {
53 version = list[name];
54 versionCurrent = version.replace(/[~^]/, '');
55 $.info.pause(this.namespace);
56 versionLatest = (await this.getLatestVersion_(name));
57 $.info.resume(this.namespace);
58 if (versionCurrent === versionLatest) {
59 $.info('update', `'${name}': '${versionCurrent}' == '${versionLatest}'`);
60 continue;
61 }
62 $.info('update', `'${name}': '${versionCurrent}' ${kleur.green('->')} '${versionLatest}'`);
63 lineCmd = ['npm install', `${name}@${versionLatest}`, isDev ? '' : '--production', isDev ? '--save-dev' : '--save'].join(' ');
64 lineCmd = lineCmd.replace(/\s{2,}/g, ' ');
65 this.listCmd.push(lineCmd);
66 }
67 return this;
68 }
69
70 };
71
72 /*
73 cache
74 listCmd
75 namespace
76 pathCache
77 */
78 Updater.prototype.cache = null;
79
80 Updater.prototype.listCmd = [];
81
82 Updater.prototype.namespace = '$.update_';
83
84 Updater.prototype.pathCache = './temp/cache-update.json';
85
86 return Updater;
87
88 }).call(this);
89
90 // return
91 return $.update_ = async function(...arg) {
92 var m;
93 m = new Updater();
94 await m.execute_(...arg);
95 return $; // return
96 };
97 };
98
99}).call(this);