Skip to content

Commit

Permalink
fix(build): fetching of latest npm version could fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Mairu committed Oct 15, 2022
1 parent 55ea5d6 commit ca57c5a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 19 deletions.
1 change: 1 addition & 0 deletions build/fetch_nsis_plugins.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ Promise.all(
fs.writeFileSync(nsisPluginsInstalledFile, 'plugins installed');
console.log('Finished downloading NSIS plugins');
plugins.forEach(plugin => console.log(` - ${plugin}`));
// TODO: remove files from tmp folder
});

2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var wantX64 = (+process.env.NODIST_X64) === 1;
var envVersion = process.env.NODIST_NODE_VERSION ?
process.env.NODIST_NODE_VERSION.replace(/"/g, '') : process.env.NODIST_NODE_VERSION;
var npmEnvVersion = process.env.NODIST_NPM_VERSION ?
process.env.NODIST_NPM_VERSION.replace(/"/g, '') : process.NODIST_NPM_VERSION
process.env.NODIST_NPM_VERSION.replace(/"/g, '') : process.env.NODIST_NPM_VERSION;

// Create a nodist instance
var n = new nodist(
Expand Down
31 changes: 21 additions & 10 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,32 @@ NPMIST.listInstalled = function listInstalled(cb) {
});
};

/**
* Get latest NPM version
* @return {string}
*/
NPMIST.latestVersion = function(){
// use list instead of getLatestRelease because there are non npm releases in the cli repo
function getNpmReleases(page) {
return octokit.rest.repos.listReleases({
owner: 'npm',
repo: 'cli',
per_page: 20
per_page: 50,
page,
}).then((response) => response.data.map(release => release.tag_name)
.filter((version) => /^v\d+\.\d+\.\d+$/.test(version))
.sort(semver.compare)
.pop()
.filter((version) => /^v\d+\.\d+\.\d+$/.test(version))
.sort(semver.compare)
);
}

/**
* Get latest NPM version
* @return {string}
*/
NPMIST.latestVersion = async function(){
// use list instead of getLatestRelease because there are non npm releases in the cli repo
let releases = [];
let page = 1;
while (releases.length === 0) {
releases = await getNpmReleases(page);
page += 1;
}

return releases.pop();
};

/**
Expand Down
10 changes: 2 additions & 8 deletions test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
var vows = require('vows');
var debug = require('debug')('nodist:test');
var assert = require('assert');
var nodist = require('../lib/nodist.js');
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var rimraf = require('rimraf');
var { testPath, createNodistInstance } = require('./helper');

var testPath = path.resolve(__dirname + '/tmp');
var testVersion = '4.2.1';

//populate proxy if we can
Expand All @@ -31,12 +30,7 @@ if ((process.env.NODIST_TESTS_CLEAN || '1') === '1') {
}

//setup new nodist
var n = new nodist(
process.env.NODIST_NODE_MIRROR || 'https://nodejs.org/dist',
process.env.NODIST_IOJS_MIRROR || 'https://iojs.org/dist',
path.resolve(testPath)
);

var n = createNodistInstance();

/**
* Exec Nodist for Testing
Expand Down
21 changes: 21 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('path');
const Nodist = require('../lib/nodist');

const testPath = path.resolve(__dirname + '/tmp');

const createNodistInstance = () => new Nodist(
process.env.NODIST_NODE_MIRROR || 'https://nodejs.org/dist',
process.env.NODIST_IOJS_MIRROR || 'https://iojs.org/dist',
path.resolve(testPath)
);

const promiseWithCallback = (promise, callback) => promise.then(
res => { callback(null, res); },
err => { callback(err); },
);

module.exports = {
createNodistInstance,
promiseWithCallback,
testPath,
};
25 changes: 25 additions & 0 deletions test/npm-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const assert = require('assert');
const debug = require('debug')('nodist:test');
const vows = require('vows');

const Npmist = require('../lib/npm');
const { createNodistInstance, promiseWithCallback } = require('./helper');

const npmBaseVersion = '6.10.1';

vows.describe('npm')
.addBatch({
'NPMIST': {
topic: new Npmist(createNodistInstance(), npmBaseVersion),
'calling `latestVersion()`': {
topic(npmist) { promiseWithCallback(npmist.latestVersion(), this.callback); },
'should return valid version number': (error, result) => {
assert.ifError(error);
debug('latestVersion: ' + result);
assert.match(result, /^v\d+\.\d+\.\d+$/);
}
}
}
})
.export(module);

0 comments on commit ca57c5a

Please sign in to comment.