Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathauthutil.ts
More file actions
Latest commit
58 lines (54 loc) · 1.92 KB
/
Copy pathauthutil.ts
File metadata and controls
58 lines (54 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import*asfsfrom'fs';
import*asosfrom'os';
import*aspathfrom'path';
import*ascorefrom'@actions/core';
import*asgithubfrom'@actions/github';
exportfunctionconfigAuthentication(registryUrl: string,alwaysAuth: string){
constnpmrc: string=path.resolve(
process.env['RUNNER_TEMP']||process.cwd(),
'.npmrc'
);
if(!registryUrl.endsWith('/')){
registryUrl+='/';
}
writeRegistryToFile(registryUrl,npmrc,alwaysAuth);
}
functionwriteRegistryToFile(
registryUrl: string,
fileLocation: string,
alwaysAuth: string
){
letscope: string=core.getInput('scope');
if(!scope&®istryUrl.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}`);
letnewContents: string='';
if(fs.existsSync(fileLocation)){
constcurContents: string=fs.readFileSync(fileLocation,'utf8');
curContents.split(os.EOL).forEach((line: string)=>{
// 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.
constauthString: string=
registryUrl.replace(/(^\w+:|^)/,'')+':_authToken=${NODE_AUTH_TOKEN}';
constregistryString: string=scope
? `${scope}:registry=${registryUrl}`
: `registry=${registryUrl}`;
constalwaysAuthString: string=`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');
}