mirror of
https://github.com/actions/setup-node.git
synced 2026-03-04 06:51:04 +08:00
Compare commits
5 Commits
20ae17ba9e
...
b6325b9a1f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6325b9a1f | ||
|
|
39370e3970 | ||
|
|
c955c43bf4 | ||
|
|
8d3d0041fe | ||
|
|
e54c83ad43 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -93,3 +93,6 @@ typings/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# Built artifacts
|
||||
dist/
|
||||
@ -76,6 +76,10 @@ See [action.yml](action.yml)
|
||||
# Set always-auth option in npmrc file.
|
||||
# Default: ''
|
||||
always-auth: ''
|
||||
|
||||
# Set ignore-scripts in npmrc file to prevent pre and postinstall scripts from running as they are a potential security problem.
|
||||
# Default: false
|
||||
ignore-scripts: false
|
||||
```
|
||||
<!-- end usage -->
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ import * as core from '@actions/core';
|
||||
import * as cache from '@actions/cache';
|
||||
import * as path from 'path';
|
||||
import * as glob from '@actions/glob';
|
||||
import osm from 'os';
|
||||
|
||||
import * as utils from '../src/cache-utils';
|
||||
import {restoreCache} from '../src/cache-restore';
|
||||
@ -12,6 +13,7 @@ describe('cache-restore', () => {
|
||||
process.env.RUNNER_OS = 'Linux';
|
||||
}
|
||||
const platform = process.env.RUNNER_OS;
|
||||
const arch = 'arm64';
|
||||
const commonPath = '/some/random/path';
|
||||
const npmCachePath = `${commonPath}/npm`;
|
||||
const pnpmCachePath = `${commonPath}/pnpm`;
|
||||
@ -52,6 +54,7 @@ describe('cache-restore', () => {
|
||||
let getCommandOutputSpy: jest.SpyInstance;
|
||||
let restoreCacheSpy: jest.SpyInstance;
|
||||
let hashFilesSpy: jest.SpyInstance;
|
||||
let archSpy: jest.SpyInstance;
|
||||
|
||||
beforeEach(() => {
|
||||
// core
|
||||
@ -102,6 +105,10 @@ describe('cache-restore', () => {
|
||||
|
||||
// cache-utils
|
||||
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
|
||||
|
||||
// os
|
||||
archSpy = jest.spyOn(osm, 'arch');
|
||||
archSpy.mockImplementation(() => arch);
|
||||
});
|
||||
|
||||
describe('Validate provided package manager', () => {
|
||||
@ -135,7 +142,7 @@ describe('cache-restore', () => {
|
||||
await restoreCache(packageManager, '');
|
||||
expect(hashFilesSpy).toHaveBeenCalled();
|
||||
expect(infoSpy).toHaveBeenCalledWith(
|
||||
`Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}`
|
||||
`Cache restored from key: node-cache-${platform}-${arch}-${packageManager}-${fileHash}`
|
||||
);
|
||||
expect(infoSpy).not.toHaveBeenCalledWith(
|
||||
`${packageManager} cache is not found`
|
||||
|
||||
46
__tests__/ignore-scripts.test.ts
Normal file
46
__tests__/ignore-scripts.test.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import * as ignorescripts from '../src/ignore-scripts';
|
||||
import {getNpmrcLocation} from '../src/util';
|
||||
|
||||
let rcFile: string;
|
||||
|
||||
describe('ignore-scripts tests', () => {
|
||||
const runnerDir = path.join(__dirname, 'runner');
|
||||
|
||||
beforeEach(async () => {
|
||||
rcFile = getNpmrcLocation();
|
||||
}, 5000);
|
||||
|
||||
afterEach(async () => {
|
||||
fs.unlinkSync(rcFile);
|
||||
rcFile = getNpmrcLocation();
|
||||
}, 10000);
|
||||
|
||||
it('sets the value to true according to input', async () => {
|
||||
ignorescripts.ignoreScriptsInNpmConfig('true');
|
||||
const rcContents = fs.readFileSync(rcFile).toString();
|
||||
expect(rcContents).toMatch('\nignore-scripts=true\n');
|
||||
});
|
||||
|
||||
it('sets the value to false according to input', async () => {
|
||||
ignorescripts.ignoreScriptsInNpmConfig('false');
|
||||
const rcContents = fs.readFileSync(rcFile).toString();
|
||||
expect(rcContents).toMatch('\nignore-scripts=false\n');
|
||||
});
|
||||
|
||||
it('defaults to false on empty input', async () => {
|
||||
ignorescripts.ignoreScriptsInNpmConfig('');
|
||||
const rcContents = fs.readFileSync(rcFile).toString();
|
||||
expect(rcContents).toMatch('\nignore-scripts=false\n');
|
||||
});
|
||||
|
||||
it('preserves existing npmrc file contents', async () => {
|
||||
fs.writeFileSync(getNpmrcLocation(), 'something\nwhatever\nstuff');
|
||||
ignorescripts.ignoreScriptsInNpmConfig('true');
|
||||
const rcContents = fs.readFileSync(rcFile).toString();
|
||||
expect(rcContents).toMatch(
|
||||
'something\nwhatever\nstuff\nignore-scripts=true\n'
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -25,6 +25,9 @@ inputs:
|
||||
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
|
||||
cache-dependency-path:
|
||||
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
|
||||
ignore-scripts:
|
||||
description: 'Set ignore-scripts in npmrc to prevent pre and postinstall scripts from running as they are a potential security problem.'
|
||||
default: 'false'
|
||||
# TODO: add input to control forcing to pull from cloud or dist.
|
||||
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
|
||||
outputs:
|
||||
|
||||
8
dist/cache-save/index.js
vendored
8
dist/cache-save/index.js
vendored
@ -84068,7 +84068,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.unique = exports.printEnvDetailsAndSetOutput = exports.getNodeVersionFromFile = void 0;
|
||||
exports.defaultIfEmpty = exports.getNpmrcLocation = exports.unique = exports.printEnvDetailsAndSetOutput = exports.getNodeVersionFromFile = void 0;
|
||||
const core = __importStar(__nccwpck_require__(2186));
|
||||
const exec = __importStar(__nccwpck_require__(1514));
|
||||
const io = __importStar(__nccwpck_require__(7436));
|
||||
@ -84166,6 +84166,12 @@ const unique = () => {
|
||||
};
|
||||
};
|
||||
exports.unique = unique;
|
||||
const getNpmrcLocation = () => {
|
||||
return path_1.default.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
|
||||
};
|
||||
exports.getNpmrcLocation = getNpmrcLocation;
|
||||
const defaultIfEmpty = (input, defaultValue) => input.length === 0 ? defaultValue : input;
|
||||
exports.defaultIfEmpty = defaultIfEmpty;
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
38
dist/setup/index.js
vendored
38
dist/setup/index.js
vendored
@ -93303,6 +93303,7 @@ const core = __importStar(__nccwpck_require__(2186));
|
||||
const glob = __importStar(__nccwpck_require__(8090));
|
||||
const path_1 = __importDefault(__nccwpck_require__(1017));
|
||||
const fs_1 = __importDefault(__nccwpck_require__(7147));
|
||||
const os_1 = __importDefault(__nccwpck_require__(2037));
|
||||
const constants_1 = __nccwpck_require__(9042);
|
||||
const cache_utils_1 = __nccwpck_require__(1678);
|
||||
const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
@ -93311,6 +93312,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
|
||||
throw new Error(`Caching for '${packageManager}' is not supported`);
|
||||
}
|
||||
const platform = process.env.RUNNER_OS;
|
||||
const arch = os_1.default.arch();
|
||||
const cachePaths = yield (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath);
|
||||
core.saveState(constants_1.State.CachePaths, cachePaths);
|
||||
const lockFilePath = cacheDependencyPath
|
||||
@ -93320,7 +93322,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
|
||||
if (!fileHash) {
|
||||
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
|
||||
}
|
||||
const keyPrefix = `node-cache-${platform}-${packageManager}`;
|
||||
const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`;
|
||||
const primaryKey = `${keyPrefix}-${fileHash}`;
|
||||
core.debug(`primary key is ${primaryKey}`);
|
||||
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
|
||||
@ -94375,6 +94377,29 @@ class CanaryBuild extends base_distribution_prerelease_1.default {
|
||||
exports["default"] = CanaryBuild;
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 6572:
|
||||
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.ignoreScriptsInNpmConfig = void 0;
|
||||
const fs_1 = __nccwpck_require__(7147);
|
||||
const util_1 = __nccwpck_require__(2629);
|
||||
const ignoreScriptsInNpmConfig = (ignore) => {
|
||||
const nonEmptyInput = (0, util_1.defaultIfEmpty)(ignore, 'false');
|
||||
const ignored = JSON.parse(nonEmptyInput);
|
||||
appendToNpmrc(ignored);
|
||||
};
|
||||
exports.ignoreScriptsInNpmConfig = ignoreScriptsInNpmConfig;
|
||||
const appendToNpmrc = (ignoreScripts) => {
|
||||
const npmrc = (0, util_1.getNpmrcLocation)();
|
||||
(0, fs_1.writeFileSync)(npmrc, `\nignore-scripts=${ignoreScripts}\n`, { flag: 'a' });
|
||||
};
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 399:
|
||||
@ -94428,6 +94453,7 @@ const cache_utils_1 = __nccwpck_require__(1678);
|
||||
const installer_factory_1 = __nccwpck_require__(5617);
|
||||
const util_1 = __nccwpck_require__(2629);
|
||||
const constants_1 = __nccwpck_require__(9042);
|
||||
const ignore_scripts_1 = __nccwpck_require__(6572);
|
||||
function run() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
@ -94467,6 +94493,8 @@ function run() {
|
||||
if (registryUrl) {
|
||||
auth.configAuthentication(registryUrl, alwaysAuth);
|
||||
}
|
||||
const ignoreScripts = core.getInput('ignore-scripts');
|
||||
(0, ignore_scripts_1.ignoreScriptsInNpmConfig)(ignoreScripts);
|
||||
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
|
||||
core.saveState(constants_1.State.CachePackageManager, cache);
|
||||
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
||||
@ -94550,7 +94578,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.unique = exports.printEnvDetailsAndSetOutput = exports.getNodeVersionFromFile = void 0;
|
||||
exports.defaultIfEmpty = exports.getNpmrcLocation = exports.unique = exports.printEnvDetailsAndSetOutput = exports.getNodeVersionFromFile = void 0;
|
||||
const core = __importStar(__nccwpck_require__(2186));
|
||||
const exec = __importStar(__nccwpck_require__(1514));
|
||||
const io = __importStar(__nccwpck_require__(7436));
|
||||
@ -94648,6 +94676,12 @@ const unique = () => {
|
||||
};
|
||||
};
|
||||
exports.unique = unique;
|
||||
const getNpmrcLocation = () => {
|
||||
return path_1.default.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
|
||||
};
|
||||
exports.getNpmrcLocation = getNpmrcLocation;
|
||||
const defaultIfEmpty = (input, defaultValue) => input.length === 0 ? defaultValue : input;
|
||||
exports.defaultIfEmpty = defaultIfEmpty;
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
@ -3,6 +3,7 @@ import * as core from '@actions/core';
|
||||
import * as glob from '@actions/glob';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
|
||||
import {State} from './constants';
|
||||
import {
|
||||
@ -21,6 +22,7 @@ export const restoreCache = async (
|
||||
throw new Error(`Caching for '${packageManager}' is not supported`);
|
||||
}
|
||||
const platform = process.env.RUNNER_OS;
|
||||
const arch = os.arch();
|
||||
|
||||
const cachePaths = await getCacheDirectories(
|
||||
packageManagerInfo,
|
||||
@ -38,7 +40,7 @@ export const restoreCache = async (
|
||||
);
|
||||
}
|
||||
|
||||
const keyPrefix = `node-cache-${platform}-${packageManager}`;
|
||||
const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`;
|
||||
const primaryKey = `${keyPrefix}-${fileHash}`;
|
||||
core.debug(`primary key is ${primaryKey}`);
|
||||
|
||||
|
||||
13
src/ignore-scripts.ts
Normal file
13
src/ignore-scripts.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import {writeFileSync} from 'fs';
|
||||
import {defaultIfEmpty, getNpmrcLocation} from './util';
|
||||
|
||||
export const ignoreScriptsInNpmConfig = (ignore: string): void => {
|
||||
const nonEmptyInput: string = defaultIfEmpty(ignore, 'false');
|
||||
const ignored: boolean = JSON.parse(nonEmptyInput);
|
||||
appendToNpmrc(ignored);
|
||||
};
|
||||
|
||||
const appendToNpmrc = (ignoreScripts: boolean): void => {
|
||||
const npmrc = getNpmrcLocation();
|
||||
writeFileSync(npmrc, `\nignore-scripts=${ignoreScripts}\n`, {flag: 'a'});
|
||||
};
|
||||
@ -9,6 +9,7 @@ import {isCacheFeatureAvailable} from './cache-utils';
|
||||
import {getNodejsDistribution} from './distributions/installer-factory';
|
||||
import {getNodeVersionFromFile, printEnvDetailsAndSetOutput} from './util';
|
||||
import {State} from './constants';
|
||||
import {ignoreScriptsInNpmConfig} from './ignore-scripts';
|
||||
|
||||
export async function run() {
|
||||
try {
|
||||
@ -59,6 +60,9 @@ export async function run() {
|
||||
auth.configAuthentication(registryUrl, alwaysAuth);
|
||||
}
|
||||
|
||||
const ignoreScripts: string = core.getInput('ignore-scripts');
|
||||
ignoreScriptsInNpmConfig(ignoreScripts);
|
||||
|
||||
if (cache && isCacheFeatureAvailable()) {
|
||||
core.saveState(State.CachePackageManager, cache);
|
||||
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
||||
|
||||
@ -106,3 +106,10 @@ export const unique = () => {
|
||||
return true;
|
||||
};
|
||||
};
|
||||
|
||||
export const getNpmrcLocation: () => string = () => {
|
||||
return path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
|
||||
};
|
||||
|
||||
export const defaultIfEmpty = (input: string, defaultValue: string): string =>
|
||||
input.length === 0 ? defaultValue : input;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user