2019-08-07 00:26:04 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as github from '@actions/github';
|
|
|
|
|
2019-09-03 16:57:45 +02:00
|
|
|
export function configAuthentication(registryUrl: string, alwaysAuth: string) {
|
2019-08-07 00:26:04 +02:00
|
|
|
const npmrc: string = path.resolve(
|
|
|
|
process.env['RUNNER_TEMP'] || process.cwd(),
|
|
|
|
'.npmrc'
|
|
|
|
);
|
|
|
|
if (!registryUrl.endsWith('/')) {
|
|
|
|
registryUrl += '/';
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:57:45 +02:00
|
|
|
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
|
2019-08-07 00:26:04 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 16:57:45 +02:00
|
|
|
function writeRegistryToFile(
|
|
|
|
registryUrl: string,
|
|
|
|
fileLocation: string,
|
|
|
|
alwaysAuth: string
|
|
|
|
) {
|
2019-08-07 00:26:04 +02:00
|
|
|
let scope: string = core.getInput('scope');
|
|
|
|
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
|
|
|
|
scope = github.context.repo.owner;
|
|
|
|
}
|
|
|
|
if (scope && scope[0] != '@') {
|
|
|
|
scope = '@' + scope;
|
|
|
|
}
|
2019-08-22 04:46:35 +02:00
|
|
|
if (scope) {
|
2022-12-05 13:32:26 +01:00
|
|
|
scope = scope.toLowerCase() + ':';
|
2019-08-22 04:46:35 +02:00
|
|
|
}
|
2019-08-07 00:26:04 +02:00
|
|
|
|
|
|
|
core.debug(`Setting auth in ${fileLocation}`);
|
2023-03-08 09:47:38 +01:00
|
|
|
let newContents = '';
|
2019-08-07 00:26:04 +02:00
|
|
|
if (fs.existsSync(fileLocation)) {
|
|
|
|
const curContents: string = fs.readFileSync(fileLocation, 'utf8');
|
|
|
|
curContents.split(os.EOL).forEach((line: string) => {
|
|
|
|
// Add current contents unless they are setting the registry
|
2022-12-05 13:32:26 +01:00
|
|
|
if (!line.toLowerCase().startsWith(`${scope}registry`)) {
|
2019-08-07 00:26:04 +02:00
|
|
|
newContents += line + os.EOL;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Remove http: or https: from front of registry.
|
|
|
|
const authString: string =
|
|
|
|
registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
|
2023-03-08 09:47:38 +01:00
|
|
|
const registryString = `${scope}registry=${registryUrl}`;
|
|
|
|
const alwaysAuthString = `always-auth=${alwaysAuth}`;
|
2019-09-03 16:57:45 +02:00
|
|
|
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
|
2019-08-07 00:26:04 +02:00
|
|
|
fs.writeFileSync(fileLocation, newContents);
|
|
|
|
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
|
2020-10-22 02:02:40 +02:00
|
|
|
// Export empty node_auth_token if didn't exist so npm doesn't complain about not being able to find it
|
2020-10-22 01:22:21 +02:00
|
|
|
core.exportVariable(
|
|
|
|
'NODE_AUTH_TOKEN',
|
|
|
|
process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX'
|
|
|
|
);
|
2019-08-07 00:26:04 +02:00
|
|
|
}
|