UNPKG

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