2023-01-05 20:16:21 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
|
|
|
|
2023-12-29 17:31:21 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
export function getNodeVersionFromFile(versionFilePath: string): string | null {
|
|
|
|
if (!fs.existsSync(versionFilePath)) {
|
|
|
|
throw new Error(
|
|
|
|
`The specified node version file at: ${versionFilePath} does not exist`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const contents = fs.readFileSync(versionFilePath, 'utf8');
|
2023-01-05 20:16:21 +08:00
|
|
|
|
|
|
|
// Try parsing the file as an NPM `package.json` file.
|
|
|
|
try {
|
2023-12-14 20:53:26 +08:00
|
|
|
const manifest = JSON.parse(contents);
|
|
|
|
|
2023-12-29 17:31:21 +08:00
|
|
|
// Presume package.json file.
|
|
|
|
if (typeof manifest === 'object' && !!manifest) {
|
|
|
|
// Support Volta.
|
|
|
|
// See https://docs.volta.sh/guide/understanding#managing-your-project
|
|
|
|
if (manifest.volta?.node) {
|
|
|
|
return manifest.volta.node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manifest.engines?.node) {
|
|
|
|
return manifest.engines.node;
|
|
|
|
}
|
2023-12-14 20:53:26 +08:00
|
|
|
|
2023-12-29 17:31:21 +08:00
|
|
|
// Support Volta workspaces.
|
|
|
|
// See https://docs.volta.sh/advanced/workspaces
|
|
|
|
if (manifest.volta?.extends) {
|
|
|
|
const extendedFilePath = path.resolve(
|
|
|
|
path.dirname(versionFilePath),
|
|
|
|
manifest.volta.extends
|
|
|
|
);
|
|
|
|
core.info('Resolving node version from ' + extendedFilePath);
|
|
|
|
return getNodeVersionFromFile(extendedFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If contents are an object, we parsed JSON
|
2023-12-14 20:53:26 +08:00
|
|
|
// this can happen if node-version-file is a package.json
|
|
|
|
// yet contains no volta.node or engines.node
|
|
|
|
//
|
2023-12-29 17:31:21 +08:00
|
|
|
// If node-version file is _not_ JSON, control flow
|
2023-12-14 20:53:26 +08:00
|
|
|
// will not have reached these lines.
|
|
|
|
//
|
|
|
|
// And because we've reached here, we know the contents
|
|
|
|
// *are* JSON, so no further string parsing makes sense.
|
2023-12-29 17:31:21 +08:00
|
|
|
return null;
|
2023-12-14 20:53:26 +08:00
|
|
|
}
|
2023-01-05 20:16:21 +08:00
|
|
|
} catch {
|
|
|
|
core.info('Node version file is not JSON file');
|
|
|
|
}
|
|
|
|
|
2023-12-29 17:31:21 +08:00
|
|
|
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
|
|
|
return found?.groups?.version ?? contents.trim();
|
2023-01-05 20:16:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function printEnvDetailsAndSetOutput() {
|
|
|
|
core.startGroup('Environment details');
|
|
|
|
|
|
|
|
const promises = ['node', 'npm', 'yarn'].map(async tool => {
|
|
|
|
const output = await getToolVersion(tool, ['--version']);
|
|
|
|
|
|
|
|
return {tool, output};
|
|
|
|
});
|
|
|
|
|
|
|
|
const tools = await Promise.all(promises);
|
|
|
|
tools.forEach(({tool, output}) => {
|
|
|
|
if (tool === 'node') {
|
|
|
|
core.setOutput(`${tool}-version`, output);
|
|
|
|
}
|
|
|
|
core.info(`${tool}: ${output}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
core.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getToolVersion(tool: string, options: string[]) {
|
|
|
|
try {
|
|
|
|
const {stdout, stderr, exitCode} = await exec.getExecOutput(tool, options, {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (exitCode > 0) {
|
|
|
|
core.info(`[warning]${stderr}`);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout.trim();
|
|
|
|
} catch (err) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 23:52:17 +08:00
|
|
|
|
|
|
|
export const unique = () => {
|
|
|
|
const encountered = new Set();
|
|
|
|
return (value: unknown): boolean => {
|
|
|
|
if (encountered.has(value)) return false;
|
|
|
|
encountered.add(value);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|