-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNpmToken.js
More file actions
55 lines (50 loc) · 1.38 KB
/
NpmToken.js
File metadata and controls
55 lines (50 loc) · 1.38 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
require('rubico/global')
const fs = require('fs')
const resolvePath = require('./internal/resolvePath')
/**
* @name NpmToken
*
* @docs
* ```coffeescript [specscript]
* NpmToken(options {
* recurse: boolean, # defaults to true
* }) -> npmToken Promise<string>
* ```
*
* Finds the [npm token](https://docs.npmjs.com/creating-and-viewing-access-tokens) from the `.npmrc` credentials file.
*
* Arguments:
* * `options`
* * `recurse` - whether to recursively look into parent directories until the `.npmrc` file is found. Defaults to `true`.
*
* Return:
* * `npmToken` - a promise of the npm token.
*
* ```javascript
* const npmToken = await NpmToken()
* ```
*/
const NpmToken = async function (options = {}) {
const recurse = options.recurse ?? true
const npmrc = '.npmrc'
let npmrcDir = resolvePath(process.cwd())
let lines = ''
while (recurse && npmrcDir != '/') {
const npmrcFilePath = resolvePath(npmrcDir, '.npmrc')
if (fs.existsSync(npmrcFilePath)) {
lines = await fs.promises.readFile(npmrcFilePath)
.then(buffer => buffer.toString('utf8').split('\n'))
break
}
npmrcDir = resolvePath(npmrcDir, '..')
}
if (lines == '') {
throw new Error('Missing .npmrc file')
}
return pipe(lines, [
get(0),
value => (/\/\/registry.npmjs.org\/:_authToken=(.+)/g).exec(value),
get(1),
])
}
module.exports = NpmToken