setup-node/src/util.ts
Sergey Dolin 8170e22e8f
Detect cached folders from multiple directories (#735)
* Add project-dir

* Fix find lock file

* Remove package-dir input

* format & resolve conflicts

* Add unit tests

* build dist

* Apply change request fixes

* handle non-dir cache-dependency-path

* bump cache version

* run checks

* Handle globs in cacheDependencyPath

* refactor, introduce `cacheDependencyPathToProjectsDirectories`

it is necessary for the next PR related yarn optimization

* Changes requests

* Apply fixes

* review fixes

* add e2e

* Add unique

* review updates

* review updates second stage

* Review fixes 3

* imporve e2e tests
2023-06-21 17:52:17 +02:00

73 lines
1.8 KiB
TypeScript

import * as core from '@actions/core';
import * as exec from '@actions/exec';
export function parseNodeVersionFile(contents: string): string {
let nodeVersion: string | undefined;
// Try parsing the file as an NPM `package.json` file.
try {
nodeVersion = JSON.parse(contents).volta?.node;
if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node;
} catch {
core.info('Node version file is not JSON file');
}
if (!nodeVersion) {
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = found?.groups?.version;
}
// In the case of an unknown format,
// return as is and evaluate the version separately.
if (!nodeVersion) nodeVersion = contents.trim();
return nodeVersion as string;
}
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 '';
}
}
export const unique = () => {
const encountered = new Set();
return (value: unknown): boolean => {
if (encountered.has(value)) return false;
encountered.add(value);
return true;
};
};