This repository has been archived by the owner on Jun 1, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
autoupdate.js
executable file
·85 lines (76 loc) · 2.47 KB
/
autoupdate.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
#!/usr/bin/env node
var _ = require('lodash');
var fs = require('fs-extra');
var async = require('async');
var config = require('./config');
var path = require('path');
var glob = require('glob');
var GIT_REPO_LOCAL_FOLDER = config.GIT_REPO_LOCAL_FOLDER;
var gitUpdater = require('./updaters/git');
var npmUpdater = require('./updaters/npm');
if (process.env.AUTOUPDATE_CONCURRENT_LIMIT === undefined) {
throw 'AUTOUPDATE_CONCURRENT_LIMIT is missing';
}
if (process.env.BOT_BASE_PATH === undefined) {
throw 'BOT_BASE_PATH is missing';
}
const BOT_BASE_PATH = process.env.BOT_BASE_PATH;
var asyncLimit = parseInt(process.env.AUTOUPDATE_CONCURRENT_LIMIT);
var startAutoUpdate = function (library, callback) {
// if the package has a .do_not_update file we ignore the update process for
// it and move on.
const doNotUpdatePath = path.join(
process.env.BOT_BASE_PATH, "cdnjs", 'ajax', 'libs', library.name, '.do_not_update'
)
if (fs.existsSync(doNotUpdatePath)) {
console.log('package has .do_not_update; ignore');
return callback(null, 0)
}
console.log('\n');
console.log(library.name.yellow);
var source = library.autoupdate.source;
switch (source) {
case 'git':
gitUpdater.update(library, callback);
break;
case 'npm':
npmUpdater.update(library, callback);
break;
default:
console.log('Autoupdate type not supportted'.red);
callback(null, 0);
}
};
var initialize = function (err) {
if (err) {
console.error('Got an error: ' + err);
} else {
console.log('Starting Auto Update'.cyan);
console.log('-----------------------');
var args = process.argv.slice(2);
var globPattern = (args.length === 1) ? args[0] : '*';
var filenames = glob.sync(path.normalize(path.join(
process.env.BOT_BASE_PATH, "cdnjs", 'ajax', 'libs', globPattern, 'package.json'
)));
var librarys = _.chain(filenames)
.map(function (filename) {
return JSON.parse(fs.readFileSync(filename, 'utf8'));
})
.filter(function (library) {
return typeof library.autoupdate === 'object';
})
.value();
async.eachLimit(librarys, asyncLimit, function (library, callback) {
startAutoUpdate(library, callback);
}, function () {
console.log('\n');
console.log('-----------------------');
console.log('Auto Update Completed'.green);
});
}
};
fs.mkdirpSync(GIT_REPO_LOCAL_FOLDER);
initialize();
process.on('uncaughtException', (err) => {
console.log(err);
});