mirror of
https://github.com/actions/setup-node
synced 2025-04-10 02:45:50 +00:00
.
This commit is contained in:
parent
bf34d46487
commit
90d3f7502b
4 changed files with 15755 additions and 356 deletions
15755
dist/index.js
vendored
Normal file
15755
dist/index.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,56 +0,0 @@
|
|||
"use strict";
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs = __importStar(require("fs"));
|
||||
const os = __importStar(require("os"));
|
||||
const path = __importStar(require("path"));
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const github = __importStar(require("@actions/github"));
|
||||
function configAuthentication(registryUrl, alwaysAuth) {
|
||||
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
|
||||
if (!registryUrl.endsWith('/')) {
|
||||
registryUrl += '/';
|
||||
}
|
||||
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
|
||||
}
|
||||
exports.configAuthentication = configAuthentication;
|
||||
function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) {
|
||||
let scope = core.getInput('scope');
|
||||
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
|
||||
scope = github.context.repo.owner;
|
||||
}
|
||||
if (scope && scope[0] != '@') {
|
||||
scope = '@' + scope;
|
||||
}
|
||||
if (scope) {
|
||||
scope = scope.toLowerCase();
|
||||
}
|
||||
core.debug(`Setting auth in ${fileLocation}`);
|
||||
let newContents = '';
|
||||
if (fs.existsSync(fileLocation)) {
|
||||
const curContents = fs.readFileSync(fileLocation, 'utf8');
|
||||
curContents.split(os.EOL).forEach((line) => {
|
||||
// Add current contents unless they are setting the registry
|
||||
if (!line.toLowerCase().startsWith('registry')) {
|
||||
newContents += line + os.EOL;
|
||||
}
|
||||
});
|
||||
}
|
||||
// Remove http: or https: from front of registry.
|
||||
const authString = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
|
||||
const registryString = scope
|
||||
? `${scope}:registry=${registryUrl}`
|
||||
: `registry=${registryUrl}`;
|
||||
const alwaysAuthString = `always-auth=${alwaysAuth}`;
|
||||
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
|
||||
fs.writeFileSync(fileLocation, newContents);
|
||||
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
|
||||
// Export empty node_auth_token so npm doesn't complain about not being able to find it
|
||||
core.exportVariable('NODE_AUTH_TOKEN', 'XXXXX-XXXXX-XXXXX-XXXXX');
|
||||
}
|
247
lib/installer.js
247
lib/installer.js
|
@ -1,247 +0,0 @@
|
|||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
// Load tempDirectory before it gets wiped by tool-cache
|
||||
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
|
||||
const assert = __importStar(require("assert"));
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const hc = __importStar(require("@actions/http-client"));
|
||||
const io = __importStar(require("@actions/io"));
|
||||
const tc = __importStar(require("@actions/tool-cache"));
|
||||
const os = __importStar(require("os"));
|
||||
const path = __importStar(require("path"));
|
||||
const semver = __importStar(require("semver"));
|
||||
let osPlat = os.platform();
|
||||
let osArch = translateArchToDistUrl(os.arch());
|
||||
if (!tempDirectory) {
|
||||
let baseLocation;
|
||||
if (process.platform === 'win32') {
|
||||
// On windows use the USERPROFILE env variable
|
||||
baseLocation = process.env['USERPROFILE'] || 'C:\\';
|
||||
}
|
||||
else {
|
||||
if (process.platform === 'darwin') {
|
||||
baseLocation = '/Users';
|
||||
}
|
||||
else {
|
||||
baseLocation = '/home';
|
||||
}
|
||||
}
|
||||
tempDirectory = path.join(baseLocation, 'actions', 'temp');
|
||||
}
|
||||
function getNode(versionSpec) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// check cache
|
||||
let toolPath;
|
||||
toolPath = tc.find('node', versionSpec);
|
||||
// If not found in cache, download
|
||||
if (!toolPath) {
|
||||
let version;
|
||||
const c = semver.clean(versionSpec) || '';
|
||||
// If explicit version
|
||||
if (semver.valid(c) != null) {
|
||||
// version to download
|
||||
version = versionSpec;
|
||||
}
|
||||
else {
|
||||
// query nodejs.org for a matching version
|
||||
version = yield queryLatestMatch(versionSpec);
|
||||
if (!version) {
|
||||
throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
|
||||
}
|
||||
// check cache
|
||||
toolPath = tc.find('node', version);
|
||||
}
|
||||
if (!toolPath) {
|
||||
// download, extract, cache
|
||||
toolPath = yield acquireNode(version);
|
||||
}
|
||||
}
|
||||
//
|
||||
// a tool installer initimately knows details about the layout of that tool
|
||||
// for example, node binary is in the bin folder after the extract on Mac/Linux.
|
||||
// layouts could change by version, by platform etc... but that's the tool installers job
|
||||
//
|
||||
if (osPlat != 'win32') {
|
||||
toolPath = path.join(toolPath, 'bin');
|
||||
}
|
||||
//
|
||||
// prepend the tools path. instructs the agent to prepend for future tasks
|
||||
core.addPath(toolPath);
|
||||
});
|
||||
}
|
||||
exports.getNode = getNode;
|
||||
function queryLatestMatch(versionSpec) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// node offers a json list of versions
|
||||
let dataFileName;
|
||||
switch (osPlat) {
|
||||
case 'linux':
|
||||
dataFileName = `linux-${osArch}`;
|
||||
break;
|
||||
case 'darwin':
|
||||
dataFileName = `osx-${osArch}-tar`;
|
||||
break;
|
||||
case 'win32':
|
||||
dataFileName = `win-${osArch}-exe`;
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unexpected OS '${osPlat}'`);
|
||||
}
|
||||
let versions = [];
|
||||
let dataUrl = 'https://nodejs.org/dist/index.json';
|
||||
let httpClient = new hc.HttpClient('setup-node', [], {
|
||||
allowRetries: true,
|
||||
maxRetries: 3
|
||||
});
|
||||
let response = yield httpClient.get(dataUrl);
|
||||
assert.ok(response.message.statusCode === 200, `Unexpected HTTP status code '${response.message.statusCode}'`);
|
||||
let body = yield response.readBody();
|
||||
let nodeVersions = JSON.parse(body);
|
||||
nodeVersions.forEach((nodeVersion) => {
|
||||
// ensure this version supports your os and platform
|
||||
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
|
||||
versions.push(nodeVersion.version);
|
||||
}
|
||||
});
|
||||
// get the latest version that matches the version spec
|
||||
let version = evaluateVersions(versions, versionSpec);
|
||||
return version;
|
||||
});
|
||||
}
|
||||
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
|
||||
function evaluateVersions(versions, versionSpec) {
|
||||
let version = '';
|
||||
core.debug(`evaluating ${versions.length} versions`);
|
||||
versions = versions.sort((a, b) => {
|
||||
if (semver.gt(a, b)) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
for (let i = versions.length - 1; i >= 0; i--) {
|
||||
const potential = versions[i];
|
||||
const satisfied = semver.satisfies(potential, versionSpec);
|
||||
if (satisfied) {
|
||||
version = potential;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (version) {
|
||||
core.debug(`matched: ${version}`);
|
||||
}
|
||||
else {
|
||||
core.debug('match not found');
|
||||
}
|
||||
return version;
|
||||
}
|
||||
function acquireNode(version) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
//
|
||||
// Download - a tool installer intimately knows how to get the tool (and construct urls)
|
||||
//
|
||||
version = semver.clean(version) || '';
|
||||
let fileName = osPlat == 'win32'
|
||||
? `node-v${version}-win-${osArch}`
|
||||
: `node-v${version}-${osPlat}-${osArch}`;
|
||||
let urlFileName = osPlat == 'win32' ? `${fileName}.7z` : `${fileName}.tar.gz`;
|
||||
let downloadUrl = `https://nodejs.org/dist/v${version}/${urlFileName}`;
|
||||
let downloadPath;
|
||||
try {
|
||||
downloadPath = yield tc.downloadTool(downloadUrl);
|
||||
}
|
||||
catch (err) {
|
||||
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
|
||||
return yield acquireNodeFromFallbackLocation(version);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
//
|
||||
// Extract
|
||||
//
|
||||
let extPath;
|
||||
if (osPlat == 'win32') {
|
||||
let _7zPath = path.join(__dirname, '..', 'externals', '7zr.exe');
|
||||
extPath = yield tc.extract7z(downloadPath, undefined, _7zPath);
|
||||
}
|
||||
else {
|
||||
extPath = yield tc.extractTar(downloadPath);
|
||||
}
|
||||
//
|
||||
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
|
||||
//
|
||||
let toolRoot = path.join(extPath, fileName);
|
||||
return yield tc.cacheDir(toolRoot, 'node', version);
|
||||
});
|
||||
}
|
||||
// For non LTS versions of Node, the files we need (for Windows) are sometimes located
|
||||
// in a different folder than they normally are for other versions.
|
||||
// Normally the format is similar to: https://nodejs.org/dist/v5.10.1/node-v5.10.1-win-x64.7z
|
||||
// In this case, there will be two files located at:
|
||||
// /dist/v5.10.1/win-x64/node.exe
|
||||
// /dist/v5.10.1/win-x64/node.lib
|
||||
// If this is not the structure, there may also be two files located at:
|
||||
// /dist/v0.12.18/node.exe
|
||||
// /dist/v0.12.18/node.lib
|
||||
// This method attempts to download and cache the resources from these alternative locations.
|
||||
// Note also that the files are normally zipped but in this case they are just an exe
|
||||
// and lib file in a folder, not zipped.
|
||||
function acquireNodeFromFallbackLocation(version) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Create temporary folder to download in to
|
||||
let tempDownloadFolder = 'temp_' + Math.floor(Math.random() * 2000000000);
|
||||
let tempDir = path.join(tempDirectory, tempDownloadFolder);
|
||||
yield io.mkdirP(tempDir);
|
||||
let exeUrl;
|
||||
let libUrl;
|
||||
try {
|
||||
exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`;
|
||||
libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`;
|
||||
const exePath = yield tc.downloadTool(exeUrl);
|
||||
yield io.cp(exePath, path.join(tempDir, 'node.exe'));
|
||||
const libPath = yield tc.downloadTool(libUrl);
|
||||
yield io.cp(libPath, path.join(tempDir, 'node.lib'));
|
||||
}
|
||||
catch (err) {
|
||||
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
|
||||
exeUrl = `https://nodejs.org/dist/v${version}/node.exe`;
|
||||
libUrl = `https://nodejs.org/dist/v${version}/node.lib`;
|
||||
const exePath = yield tc.downloadTool(exeUrl);
|
||||
yield io.cp(exePath, path.join(tempDir, 'node.exe'));
|
||||
const libPath = yield tc.downloadTool(libUrl);
|
||||
yield io.cp(libPath, path.join(tempDir, 'node.lib'));
|
||||
}
|
||||
else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return yield tc.cacheDir(tempDir, 'node', version);
|
||||
});
|
||||
}
|
||||
// os.arch does not always match the relative download url, e.g.
|
||||
// os.arch == 'arm' != node-v12.13.1-linux-armv7l.tar.gz
|
||||
// All other currently supported architectures match, e.g.:
|
||||
// os.arch = arm64 => https://nodejs.org/dist/v{VERSION}/node-v{VERSION}-{OS}-arm64.tar.gz
|
||||
// os.arch = x64 => https://nodejs.org/dist/v{VERSION}/node-v{VERSION}-{OS}-x64.tar.gz
|
||||
function translateArchToDistUrl(arch) {
|
||||
switch (arch) {
|
||||
case 'arm':
|
||||
return 'armv7l';
|
||||
default:
|
||||
return arch;
|
||||
}
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const installer = __importStar(require("./installer"));
|
||||
const auth = __importStar(require("./authutil"));
|
||||
const path = __importStar(require("path"));
|
||||
function run() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
//
|
||||
// Version is optional. If supplied, install / use from the tool cache
|
||||
// If not supplied then task is still used to setup proxy, auth, etc...
|
||||
//
|
||||
let version = core.getInput('version');
|
||||
if (!version) {
|
||||
version = core.getInput('node-version');
|
||||
}
|
||||
if (version) {
|
||||
// TODO: installer doesn't support proxy
|
||||
yield installer.getNode(version);
|
||||
}
|
||||
const registryUrl = core.getInput('registry-url');
|
||||
const alwaysAuth = core.getInput('always-auth');
|
||||
if (registryUrl) {
|
||||
auth.configAuthentication(registryUrl, alwaysAuth);
|
||||
}
|
||||
// TODO: setup proxy from runner proxy config
|
||||
const matchersPath = path.join(__dirname, '..', '.github');
|
||||
console.log(`##[add-matcher]${path.join(matchersPath, 'tsc.json')}`);
|
||||
console.log(`##[add-matcher]${path.join(matchersPath, 'eslint-stylish.json')}`);
|
||||
console.log(`##[add-matcher]${path.join(matchersPath, 'eslint-compact.json')}`);
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
run();
|
Loading…
Add table
Reference in a new issue