Skip to content

Commit 1f9e496

Browse files
committed
fix(token): update token loading to read from .npmrc file
1 parent a9bca5c commit 1f9e496

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/token.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
import type { IConfigOptions } from '@/types'
2-
import { writeFile } from 'node:fs/promises'
2+
import { readFile, writeFile } from 'node:fs/promises'
33
import { resolve } from 'node:path'
4+
import process from 'node:process'
5+
import boxen from 'boxen'
6+
import { x } from 'tinyexec'
47
import { CACHE_FOLDER_NAME } from '@/constant.ts'
5-
import { createDir, loaderTs } from '@/utils.ts'
8+
import { createDir } from '@/utils.ts'
69

710
export const setToken = async (config: IConfigOptions, token: string): Promise<void> => {
811
createDir(resolve(config.cwd, CACHE_FOLDER_NAME))
912
await writeFile(config.token.file, `export default "${token}"`)
1013
}
1114

1215
export const loaderToken = async (config: IConfigOptions): Promise<string> => {
13-
return await loaderTs(config.token.file)
16+
const npmrcPath = await x('npm', ['config', 'get', 'userconfig'], {
17+
nodeOptions: {
18+
cwd: config.cwd,
19+
stdio: 'pipe',
20+
},
21+
})
22+
23+
const content = await readFile(npmrcPath.stdout.trim(), 'utf-8')
24+
const [, tokenValue] = content.match(/\/\/registry.npmjs.org\/:_authToken=(.*)/) || []
25+
const token = tokenValue?.trim() || null
26+
if (!token) {
27+
console.log(boxen(
28+
`_authToken not found in your .npmrc file,
29+
you can use the following codemod:
30+
Run "dnmp set <token> or npm config set //registry.npmjs.org/:_authToken=<token>"`,
31+
{
32+
title: 'Warning',
33+
padding: 1,
34+
margin: 0,
35+
borderStyle: 'round',
36+
borderColor: 'yellow',
37+
},
38+
))
39+
process.exit(0)
40+
}
41+
return token
1442
}

0 commit comments

Comments
 (0)