|
1 | 1 | import type { IConfigOptions } from '@/types' |
2 | | -import { writeFile } from 'node:fs/promises' |
| 2 | +import { readFile, writeFile } from 'node:fs/promises' |
3 | 3 | import { resolve } from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | +import boxen from 'boxen' |
| 6 | +import { x } from 'tinyexec' |
4 | 7 | import { CACHE_FOLDER_NAME } from '@/constant.ts' |
5 | | -import { createDir, loaderTs } from '@/utils.ts' |
| 8 | +import { createDir } from '@/utils.ts' |
6 | 9 |
|
7 | 10 | export const setToken = async (config: IConfigOptions, token: string): Promise<void> => { |
8 | 11 | createDir(resolve(config.cwd, CACHE_FOLDER_NAME)) |
9 | 12 | await writeFile(config.token.file, `export default "${token}"`) |
10 | 13 | } |
11 | 14 |
|
12 | 15 | 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 |
14 | 42 | } |
0 commit comments