setup-node/src/authutil.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-08-05 11:51:04 -04:00
import * as fs from 'fs';
import * as os from 'os';
2019-08-05 11:35:39 -04:00
import * as path from 'path';
2019-08-05 11:51:04 -04:00
import * as core from '@actions/core';
2019-08-05 11:35:39 -04:00
2019-08-05 13:12:37 -04:00
export function configAuthentication(registryUrl: string) {
const npmrc: string = path.resolve(process.cwd(), '.npmrc');
2019-08-05 11:35:39 -04:00
writeRegistryToFile(registryUrl, npmrc);
2019-08-05 11:35:39 -04:00
}
function writeRegistryToFile(registryUrl: string, fileLocation: string) {
2019-08-05 11:51:04 -04:00
core.debug(`Setting auth in ${fileLocation}`);
2019-08-05 13:15:47 -04:00
let newContents: string = '';
2019-08-05 11:51:04 -04:00
if (fs.existsSync(fileLocation)) {
2019-08-05 13:12:37 -04:00
const curContents: string = fs.readFileSync(fileLocation, 'utf8');
2019-08-05 11:51:04 -04:00
curContents.split(os.EOL).forEach((line: string) => {
// Add current contents unless they are setting the registry
2019-08-05 13:12:37 -04:00
if (!line.toLowerCase().startsWith('registry')) {
2019-08-05 11:51:04 -04:00
newContents += line + os.EOL;
}
});
}
newContents +=
'registry=' +
registryUrl +
os.EOL +
'always-auth=true' +
os.EOL +
2019-08-05 13:12:37 -04:00
registryUrl.replace(/(^\w+:|^)/, '') + // Remove http: or https: from front of registry.
':_authToken=${NODE_AUTH_TOKEN}';
2019-08-05 11:51:04 -04:00
fs.writeFileSync(fileLocation, newContents);
2019-08-05 11:35:39 -04:00
}