forked from cdnjs/cdnjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm-auto-update.js
95 lines (82 loc) · 3.36 KB
/
npm-auto-update.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
var path = require("path"),
fs = require("fs-extra"),
glob = require("glob"),
_ = require('lodash'),
request = require("superagent"),
async = require("async"),
tarball = require('tarball-extract'),
mkdirp = require('mkdirp');
var parse = function (json_file, ignore_missing, ignore_parse_fail) {
var content;
try {
content = fs.readFileSync(json_file, 'utf8');
} catch (err1) {
if (!ignore_missing) {
assert.ok(0, json_file + " doesn't exist!");
}
return null;
}
try {
return JSON.parse(content);
} catch (err2) {
if (!ignore_parse_fail) {
assert.ok(0, json_file + " failed to parse");
}
return null;
}
}
var updateLibrary = function (pkg, callback) {
console.log('Checking versions for ' + pkg.npmName);
request.get('http://registry.npmjs.org/' + pkg.npmName, function(result) {
//console.log(result.body);
_.each(result.body.versions, function(data, version) {
var path = './ajax/libs/' + pkg.name + '/' + version;
if(!fs.existsSync(path)) {
fs.mkdirSync(path);
var url = data.dist.tarball;
var download_file = path + '/dist.tar.gz';
tarball.extractTarballDownload(url , download_file, path, {}, function(err, result) {
fs.unlinkSync(download_file);
var folderName = fs.readdirSync(path)[0];
var npmFileMap = pkg.npmFileMap;
_.each(npmFileMap, function(fileSpec) {
var basePath = fileSpec.basePath || "";
_.each(fileSpec.files, function(file) {
var extractPath = basePath + "/" + file;
var files = glob.sync(path + "/" + folderName + "/" + basePath + "/" + file);
_.each(files, function(extractFilePath) {
var replacePath = folderName + "/" + basePath + "/";
replacePath = replacePath.replace(/\/\//g, "/");
var actualPath = extractFilePath.replace(replacePath, "");
fs.renameSync(extractFilePath, actualPath);
});
});
});
fs.removeSync(path + '/' + folderName);
});
console.log("Do not have version", version, "of", pkg.npmName);
}
});
var npmVersion = result.body['dist-tags'].latest;
pkg.version = npmVersion;
fs.writeFileSync('ajax/libs/' + pkg.name + '/package.json', JSON.stringify(pkg, null, 2), 'utf8');
callback(null, pkg['npm-name']);
});
}
console.log('Looking for npm enabled libraries...');
// load up those files
var packages = glob.sync("./ajax/libs/**/package.json");
packages = _(packages).map(function (pkg) {
var parsedPkg = parse(pkg);
return parsedPkg.npmName ? parsedPkg : null;
}).compact().value();
console.log('Found ' + packages.length + ' npm enabled libraries');
var libraryUpdates = [];
_.each(packages, function(pkg) {
libraryUpdates.push(function (callback) {
updateLibrary(pkg, callback);
});;
});
async.series(libraryUpdates, function(err, results) {
console.log('Script completed');
});