2019-08-06 18:26:04 -04:00
|
|
|
"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"));
|
2019-09-03 10:57:45 -04:00
|
|
|
function configAuthentication(registryUrl, alwaysAuth) {
|
2019-08-06 18:26:04 -04:00
|
|
|
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
|
|
|
|
if (!registryUrl.endsWith('/')) {
|
|
|
|
registryUrl += '/';
|
|
|
|
}
|
2019-09-03 10:57:45 -04:00
|
|
|
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
|
2019-08-06 18:26:04 -04:00
|
|
|
}
|
|
|
|
exports.configAuthentication = configAuthentication;
|
2019-09-03 10:57:45 -04:00
|
|
|
function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) {
|
2019-08-06 18:26:04 -04:00
|
|
|
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;
|
|
|
|
}
|
2019-08-21 22:46:35 -04:00
|
|
|
if (scope) {
|
|
|
|
scope = scope.toLowerCase();
|
|
|
|
}
|
2019-08-06 18:26:04 -04:00
|
|
|
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}`;
|
2019-09-03 10:57:45 -04:00
|
|
|
const alwaysAuthString = `always-auth=${alwaysAuth}`;
|
|
|
|
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
|
2019-08-06 18:26:04 -04:00
|
|
|
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');
|
|
|
|
}
|