This commit is contained in:
Danny McCormick 2019-08-05 11:44:04 -04:00
parent 2b9c956517
commit 409b7dfb5b
2 changed files with 42 additions and 45 deletions

View file

@ -1,39 +1,29 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as exec from '@actions/exec';
export function configAuth(registryUrl: string) {
export async function configAuth(registryUrl: string) {
let npmrc: string = path.resolve(process.cwd(), '.npmrc');
let yarnrc: string = path.resolve(process.cwd(), '.yarnrc');
writeRegistryToFile(registryUrl, npmrc, 'NPM_TOKEN');
writeRegistryToFile(registryUrl, yarnrc, 'YARN_TOKEN');
writeRegistryToFile(registryUrl, 'npm', 'NPM_TOKEN');
writeRegistryToFile(registryUrl, 'yarn', 'YARN_TOKEN');
}
function writeRegistryToFile(
async function writeRegistryToFile(
registryUrl: string,
fileLocation: string,
packageManager: string,
authTokenName: string
) {
let newContents = '';
if (fs.existsSync(fileLocation)) {
const curContents = fs.readFileSync(fileLocation, 'utf8');
curContents.split(os.EOL).forEach((line: string) => {
// Add current contents unless they are setting the registry
if (!line.startsWith('registry')) {
newContents += line + os.EOL;
}
});
}
newContents +=
'registry=' +
registryUrl +
os.EOL +
'always-auth=true' +
os.EOL +
registryUrl.replace(/(^\w+:|^)/, '') +
':_authToken=${' +
authTokenName +
'}';
fs.writeFileSync(fileLocation, newContents);
await exec.exec(`${packageManager} config set registry=${registryUrl}`);
await exec.exec(`${packageManager} config set always-auth=true`);
await exec.exec(
packageManager +
' config set ' +
registryUrl.replace(/(^\w+:|^)/, '') +
':_authToken ${' +
authTokenName +
'}'
);
}