Skip to content

Commit 372fd2e

Browse files
committed
load versions on build
1 parent 0272e0a commit 372fd2e

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

build.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const ncp = require('ncp');
1717

1818
const filterStylusPartials = require('./scripts/plugins/filter-stylus-partials');
1919
const mapHandlebarsPartials = require('./scripts/plugins/map-handlebars-partials');
20-
const versions = require('./source/versions');
20+
const loadVersions = require('./scripts/load-versions')
2121

2222
/** Build **/
2323

@@ -43,18 +43,7 @@ function i18nJSON (lang) {
4343
return finalJSON;
4444
}
4545

46-
const source = {
47-
project: {
48-
versions,
49-
currentVersion: versions[0].version,
50-
banner: {
51-
visible: false,
52-
content: 'Important <a href="#">security release</a>, please update now!'
53-
}
54-
}
55-
};
56-
57-
function buildlocale (locale) {
46+
function buildlocale (source, locale) {
5847
console.time('[metalsmith] build/' + locale + ' finished');
5948
const siteJSON = path.join(__dirname, 'locale', locale, 'site.json');
6049
const metalsmith = Metalsmith(__dirname);
@@ -177,9 +166,23 @@ function copystatic () {
177166

178167
function fullbuild () {
179168
copystatic();
180-
fs.readdir(path.join(__dirname, 'locale'), function (e, locales) {
181-
locales.forEach(function (locale) {
182-
buildlocale(locale);
169+
loadVersions(function (err, versions) {
170+
if (err) { throw err; }
171+
const source = {
172+
project: {
173+
versions,
174+
currentVersion: versions[0].version,
175+
banner: {
176+
visible: false,
177+
content: 'Important <a href="#">security release</a>, please update now!'
178+
}
179+
}
180+
};
181+
182+
fs.readdir(path.join(__dirname, 'locale'), function (e, locales) {
183+
locales.forEach(function (locale) {
184+
buildlocale(source, locale);
185+
});
183186
});
184187
});
185188
}

scripts/load-versions.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ const semver = require('semver');
77
const map = require('map-async');
88
const https = require('https');
99

10-
map([ 'https://nodejs.org/dist/index.json', 'https://iojs.org/dist/index.json' ], download, munge);
10+
function loadVersions (callback) {
11+
map(
12+
[ 'https://nodejs.org/dist/index.json', 'https://iojs.org/dist/index.json' ],
13+
download,
14+
function (err, versions) {
15+
if (err)
16+
return callback(err);
17+
versions = munge(versions);
18+
callback(null, versions);
19+
}
20+
);
21+
}
1122

1223
function download (url, cb) {
1324
let data = '';
@@ -26,13 +37,7 @@ function download (url, cb) {
2637
});
2738
}
2839

29-
function munge (err, versions) {
30-
if (err) {
31-
console.error('Aborting due to download error from node or iojs');
32-
console.error(err.stack)
33-
return process.exit(1);
34-
}
35-
40+
function munge (versions) {
3641
versions[0].forEach(function (v) {
3742
v.url = 'https://nodejs.org/dist/' + v.version + '/'
3843
v.name = 'Node.js'
@@ -48,5 +53,19 @@ function munge (err, versions) {
4853
return semver.compare(b.version, a.version);
4954
});
5055

51-
fs.writeFileSync(__dirname + '/../source/versions.json', JSON.stringify(allVersions, null, 2));
56+
return allVersions;
5257
}
58+
59+
module.exports = loadVersions;
60+
61+
if (require.main === module) {
62+
loadVersions(function (err, versions) {
63+
if (err) {
64+
console.error('Aborting due to download error from node or iojs');
65+
console.error(err.stack);
66+
return process.exit(1);
67+
}
68+
69+
fs.writeFileSync(__dirname + '/../source/versions.json', JSON.stringify(versions, null, 2));
70+
})
71+
}

0 commit comments

Comments
 (0)