|
1 |
| -import { gray, green } from 'kolorist'; |
2 |
| -import prompts from 'prompts'; |
3 |
| -import { registriesPath } from '../../config/path'; |
4 |
| -import { getFileUser, writeFileUser } from '../../utils/getUserList'; |
5 |
| -import { defaultNpmMirror } from '../../config/registry'; |
6 |
| -import { execCommand } from '../../utils/shell'; |
7 |
| -import { geneDashLine, printMessages } from '../../utils/tools'; |
8 |
| -import { log } from '../../utils/log'; |
9 |
| -import { insertRegistry } from '../../utils/helper'; |
10 |
| -import type { NrmCmd } from '../../type/shell.type'; |
11 |
| - |
12 |
| -export const useLs = async (cmd: NrmCmd) => { |
13 |
| - const userConfig = await getFileUser(registriesPath); |
14 |
| - let registryList = defaultNpmMirror; |
15 |
| - if (userConfig) { |
16 |
| - if (!userConfig.registry || userConfig.registry.length === 0) { |
17 |
| - userConfig.registry = registryList; |
18 |
| - writeFileUser(registriesPath, userConfig); |
19 |
| - } else registryList = userConfig.registry; |
20 |
| - } |
21 |
| - |
22 |
| - let packageManager = 'npm'; |
23 |
| - if (cmd.packageManager) packageManager = cmd.packageManager; |
24 |
| - // npm config get registry |
25 |
| - |
26 |
| - let currectRegistry = ''; |
27 |
| - try { |
28 |
| - currectRegistry = await execCommand(packageManager, [ |
29 |
| - 'config', |
30 |
| - 'get', |
31 |
| - 'registry', |
32 |
| - ]); |
33 |
| - } catch (error) { |
34 |
| - log.error(`${packageManager} is not found`); |
35 |
| - return; |
36 |
| - } |
37 |
| - |
38 |
| - if (registryList.every((x) => x.registry !== currectRegistry)) { |
39 |
| - // 默认添加本地源 |
40 |
| - try { |
41 |
| - const { name } = await prompts({ |
42 |
| - type: 'text', |
43 |
| - name: 'name', |
44 |
| - message: `find new registry:${currectRegistry}, please give it a name`, |
45 |
| - }); |
46 |
| - await insertRegistry(name, name, currectRegistry); |
47 |
| - |
48 |
| - log.info(`[found new registry]: ${currectRegistry}`); |
49 |
| - |
50 |
| - registryList.push({ |
51 |
| - name, |
52 |
| - registry: currectRegistry, |
53 |
| - home: '', |
54 |
| - alias: name, |
55 |
| - }); |
56 |
| - } catch (error) {} |
57 |
| - } |
58 |
| - |
59 |
| - const length = |
60 |
| - Math.max( |
61 |
| - ...registryList.map((x) => { |
62 |
| - return x.alias.length + (x.alias !== x.name ? x.name.length : 0); |
63 |
| - }) |
64 |
| - ) + 3; |
65 |
| - const prefix = ' '; |
66 |
| - |
67 |
| - const messages = registryList.map((item) => { |
68 |
| - const currect = item.registry === currectRegistry ? `${green('*')}` : ' '; |
69 |
| - |
70 |
| - const isSame = item.alias === item.name; |
71 |
| - |
72 |
| - return `${prefix + currect}${ |
73 |
| - isSame ? item.alias : `${item.alias}(${gray(item.name)})` |
74 |
| - }${geneDashLine(item.name, length)}${item.registry}`; |
75 |
| - }); |
76 |
| - |
77 |
| - printMessages(messages); |
78 |
| -}; |
| 1 | +import { gray, green } from 'kolorist'; |
| 2 | +import prompts from 'prompts'; |
| 3 | +import { registriesPath } from '../../config/path'; |
| 4 | +import { getFileUser, writeFileUser } from '../../utils/getUserList'; |
| 5 | +import { defaultNpmMirror } from '../../config/registry'; |
| 6 | +import { execCommand } from '../../utils/shell'; |
| 7 | +import { geneDashLine, printMessages } from '../../utils/tools'; |
| 8 | +import { log } from '../../utils/log'; |
| 9 | +import { insertRegistry } from '../../utils/helper'; |
| 10 | +import type { NrmCmd } from '../../type/shell.type'; |
| 11 | + |
| 12 | +export const useLs = async (cmd: NrmCmd) => { |
| 13 | + const userConfig = await getFileUser(registriesPath); |
| 14 | + let registryList = defaultNpmMirror; |
| 15 | + if (userConfig) { |
| 16 | + if (!userConfig.registry || userConfig.registry.length === 0) { |
| 17 | + userConfig.registry = registryList; |
| 18 | + writeFileUser(registriesPath, userConfig); |
| 19 | + } else registryList = userConfig.registry; |
| 20 | + } |
| 21 | + |
| 22 | + let packageManager = 'npm'; |
| 23 | + if (cmd.packageManager) packageManager = cmd.packageManager; |
| 24 | + // npm config get registry |
| 25 | + |
| 26 | + let currectRegistry = ''; |
| 27 | + try { |
| 28 | + currectRegistry = await execCommand(packageManager, [ |
| 29 | + 'config', |
| 30 | + 'get', |
| 31 | + 'registry', |
| 32 | + ]); |
| 33 | + } catch (error) { |
| 34 | + log.error(`${packageManager} is not found`); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (registryList.every((x) => x.registry !== currectRegistry)) { |
| 39 | + // 默认添加本地源 |
| 40 | + try { |
| 41 | + const { name } = await prompts({ |
| 42 | + type: 'text', |
| 43 | + name: 'name', |
| 44 | + message: `find new registry:${currectRegistry}, please give it a name`, |
| 45 | + }); |
| 46 | + await insertRegistry(name, name, currectRegistry); |
| 47 | + |
| 48 | + log.info(`[found new registry]: ${currectRegistry}`); |
| 49 | + |
| 50 | + registryList.push({ |
| 51 | + name, |
| 52 | + registry: currectRegistry, |
| 53 | + home: '', |
| 54 | + alias: name, |
| 55 | + }); |
| 56 | + } catch (error) {} |
| 57 | + } |
| 58 | + |
| 59 | + const length = |
| 60 | + Math.max( |
| 61 | + ...registryList.map((x) => { |
| 62 | + return x.alias.length + (x.alias !== x.name ? x.name.length : 0); |
| 63 | + }) |
| 64 | + ) + 3; |
| 65 | + const prefix = ' '; |
| 66 | + |
| 67 | + const messages = registryList.map((item) => { |
| 68 | + const currect = item.registry === currectRegistry ? `${green('*')}` : ' '; |
| 69 | + |
| 70 | + const isSame = item.alias === item.name; |
| 71 | + |
| 72 | + return `${prefix + currect}${ |
| 73 | + isSame ? item.alias : `${item.alias}(${gray(item.name)})` |
| 74 | + }${geneDashLine(item.name, length)}${item.registry}`; |
| 75 | + }); |
| 76 | + |
| 77 | + printMessages(messages); |
| 78 | +}; |
0 commit comments