Skip to content

Commit 08874a9

Browse files
committed
fix(build): use known working node version
- fix npm links fallback - fix npm links handling for installer too - use matching npm version for node version when building - GitHub workflows: allow triggering of installer creation without tagging
1 parent e4e67cd commit 08874a9

File tree

4 files changed

+72
-66
lines changed

4 files changed

+72
-66
lines changed

.github/workflows/release.yml

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
tags:
66
- 'v[0-9]+.[0-9]+.[0-9]+'
77
- 'v[0-9]+.[0-9]+.[0-9]+-*'
8+
workflow_dispatch:
9+
810

911
jobs:
1012
run-build:
@@ -23,7 +25,14 @@ jobs:
2325
run: Copy-Item (Get-Command node.exe | Select-Object -ExpandProperty Definition) .
2426
- run: npm test
2527
- run: npm run build
28+
- name: Archive build artifact
29+
if: github.ref_type == 'branch'
30+
uses: actions/upload-artifact@v3
31+
with:
32+
name: Installer
33+
path: "build/out/NodistSetup-*.exe"
2634
- name: Create release draft
35+
if: github.ref_type == 'tag'
2736
uses: ncipollo/release-action@v1
2837
with:
2938
artifacts: "build/out/NodistSetup-*.exe"

build/build.js

+10-30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var fs = require('fs');
55
var mkdirp = require('mkdirp');
66
var ncp = require('ncp');
77
var path = require('path');
8+
var semver = require('semver');
89
var recursiveReaddir = require('recursive-readdir');
910
var request = require('request');
1011
var rimraf = require('rimraf');
@@ -83,6 +84,7 @@ var npm = new (require('../lib/npm'))({nodistDir: stagingDir});
8384
//default npm version to the latest at the time of writing
8485
var npmVersion = '6.14.16';
8586
var nodeVersion = '16.15.0';
87+
var maxNodeMainVersion = '^20';
8688

8789
var versionPathx86 = '';
8890
var versionPathx64 = '';
@@ -102,31 +104,14 @@ console.log('Welcome to the Nodist Builder');
102104
console.log(' before going further we need to prep our staging folder');
103105

104106
//defining helper functions
105-
function getLatestNodeVersionFor(nodeVersions, fileType) {
107+
function getLatestUsableNodeVersionFor(nodeVersions, fileType) {
106108
for (var key in nodeVersions) {
107-
if (nodeVersions[key].files.includes(fileType)) {
108-
return nodeVersions[key].version;
109+
if (nodeVersions[key].files.includes(fileType) && semver.satisfies(nodeVersions[key].version, maxNodeMainVersion)) {
110+
return { nodeVersion: nodeVersions[key].version, npmVersion: nodeVersions[key].npm };
109111
}
110112
}
111113
}
112114

113-
async function resolveLinkedWorkspaces(dirPath) {
114-
let movedLinks = 0;
115-
const files = await fs.readdirAsync(dirPath, { withFileTypes: true });
116-
const dirPromises = [];
117-
for (const file of files) {
118-
const filePath = path.join(dirPath, file.name);
119-
if (file.isSymbolicLink()) {
120-
const linkTarget = await fs.readlinkAsync(filePath);
121-
await fs.renameAsync(path.join(dirPath, linkTarget), filePath);
122-
movedLinks++;
123-
} else if (file.isDirectory()) {
124-
dirPromises.push(resolveLinkedWorkspaces(filePath));
125-
}
126-
}
127-
return (await Promise.all(dirPromises)).reduce((sum, num) => sum + num, movedLinks);
128-
}
129-
130115
//start by clearing the staging and tmp folders
131116
P.all([
132117
rimraf(outDir),
@@ -217,7 +202,7 @@ P.all([
217202
});
218203
})
219204
.then(function(res){
220-
nodeVersion = getLatestNodeVersionFor(res.body, 'win-x86-exe');
205+
({ nodeVersion, npmVersion } = getLatestUsableNodeVersionFor(res.body, 'win-x86-exe'));
221206
nodeLatestUrlx86 = nodeLatestUrlx86.replace('VERSION',nodeVersion);
222207
nodeLatestUrlx64 = nodeLatestUrlx64.replace('VERSION',nodeVersion);
223208
console.log('Latest version of Node ' + nodeVersion);
@@ -253,14 +238,9 @@ P.all([
253238
);
254239
})
255240
.then(function(){
256-
console.log('Figure out the latest version of NPM');
257-
return npm.latestVersion();
258-
})
259-
.then(function(version){
260-
npmVersion = version;
261-
var downloadLink = npm.downloadUrl(version);
262-
console.log('Determined latest NPM as ' + npmVersion);
263-
console.log('Downloading latest NPM from ' + downloadLink);
241+
var downloadLink = npm.downloadUrl(npmVersion);
242+
console.log('Determined matching NPM as ' + npmVersion);
243+
console.log('Downloading matching NPM from ' + downloadLink);
264244
return Promise.resolve()
265245
.then(() => mkdirp(stagingNpmDir+'/'+npmVersion.replace('v','')))
266246
.then(() => {
@@ -291,7 +271,7 @@ P.all([
291271
})
292272
.then(function() {
293273
console.log('Installation complete');
294-
return resolveLinkedWorkspaces(path.join(stagingNpmDir, npmVersion.replace('v', ''), 'node_modules'));
274+
return helper.resolveLinkedWorkspaces(path.join(stagingNpmDir, npmVersion.replace('v', '')), false);
295275
})
296276
.then(function(movedLinks) {
297277
if (movedLinks) {

lib/build.js

+52
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ var path = require('path');
44
var promisePipe = require('promisepipe');
55
var ProgressBar = require('progress');
66
var request = require('request');
7+
const P = require('bluebird');
78
var debug = require('debug')('nodist:build')
89

10+
//make some promising APIs
11+
P.promisifyAll(fs);
12+
913
/**
1014
* Copy File
1115
* @param {string} source
@@ -141,3 +145,51 @@ exports.downloadFileStream = function downloadFileStream(url) {
141145
});
142146
return req
143147
}
148+
149+
/**
150+
* Npm version >= 18 using symlinks that do not work in windows and have to be fixed
151+
* this function replace the broken symlinks with NTFS junction or move the directory if junctions are not supported
152+
*
153+
* @param {string} dirPath
154+
* @param {boolean} preferSymlink
155+
* @returns {Promise<number>} number of changed links
156+
*/
157+
exports.resolveLinkedWorkspaces = async function resolveLinkedWorkspaces(dirPath, preferSymlink = true) {
158+
let fixedLinks = 0;
159+
const packageLockJson = JSON.parse(fs.readFileSync(path.join(dirPath, 'package-lock.json')).toString());
160+
await Promise.all(Object.entries(packageLockJson.packages)
161+
.filter(([pkgPath, pkg]) => pkg.link === true)
162+
.map(async ([pkgPath, pkg]) => {
163+
164+
const linkPath = path.join(dirPath, pkgPath);
165+
166+
167+
if (await fs.accessAsync(linkPath, fs.constants.F_OK).then(() => true).catch(() => false)) {
168+
await fs.unlinkAsync(linkPath);
169+
}
170+
171+
let linkCreated = false;
172+
if (preferSymlink) {
173+
const linkTarget = path.join(
174+
...pkgPath.split('/').slice(0, -1).map(() => '..'),
175+
pkg.resolved
176+
);
177+
debug('Create symlink for ', linkPath, 'with target', linkTarget);
178+
try {
179+
await fs.symlinkAsync(linkTarget, linkPath, 'junction');
180+
linkCreated = true;
181+
} catch (e) {
182+
debug('Link ', linkPath, 'could not be created');
183+
}
184+
}
185+
if (!linkCreated) {
186+
const from = path.join(dirPath, pkg.resolved);
187+
debug('Move', from, 'to', linkPath);
188+
await fs.renameAsync(from, linkPath);
189+
}
190+
191+
fixedLinks++;
192+
}));
193+
194+
return fixedLinks;
195+
};

lib/npm.js

+1-36
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,6 @@ module.exports = npmist
3636

3737
var NPMIST = npmist.prototype
3838

39-
/**
40-
* Npm version >= 18 using symlinks that do not work in windows and have to be fixed
41-
* this function replace the broken symlinks with NTFS junction or move the directory if junctions are not supported
42-
*
43-
* @param {string} dirPath
44-
* @returns {Promise<number>} number of changed links
45-
*/
46-
async function resolveLinkedWorkspaces(dirPath) {
47-
let fixedLinks = 0;
48-
const packageLockJson = JSON.parse(fs.readFileSync(path.join(dirPath, 'package-lock.json')).toString());
49-
await Promise.all(Object.entries(packageLockJson.packages)
50-
.filter(([pkgPath, pkg]) => pkg.link === true)
51-
.map(async ([pkgPath, pkg]) => {
52-
const linkTarget = path.join(
53-
...pkgPath.split('/').slice(0, -1).map(() => '..'),
54-
pkg.resolved
55-
);
56-
const linkPath = path.join(dirPath, pkgPath);
57-
58-
debug('Create symlink for ', linkPath, 'with target', linkTarget);
59-
if (await fs.accessAsync(linkPath, fs.constants.F_OK).then(() => true).catch(() => false)) {
60-
await fs.unlinkAsync(linkPath);
61-
}
62-
63-
try {
64-
await fs.symlinkAsync(linkTarget, linkPath, 'junction');
65-
} catch (e) {
66-
await fs.renameAsync(path.join(dirPath, linkTarget), linkPath);
67-
}
68-
fixedLinks++;
69-
}));
70-
71-
return fixedLinks;
72-
}
73-
7439
/**
7540
* List available NPM versions
7641
* @return {string}
@@ -331,7 +296,7 @@ NPMIST.install = function(v,done){
331296
.then(() => {
332297
if (semver.gte(version, '8.0.0')) {
333298
debug('Fix symlinks for npm version >= 8');
334-
return resolveLinkedWorkspaces(path.join(archivePath))
299+
return buildHelper.resolveLinkedWorkspaces(path.join(archivePath))
335300
.then(fixedLinks => {
336301
debug(`Fixed ${fixedLinks} symlinks for npm node_modules`);
337302
});

0 commit comments

Comments
 (0)