Skip to content

Commit

Permalink
feature: npm命令可配置化。 (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 authored Jul 8, 2024
1 parent 5e6e7b2 commit face64b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/core/src/modules/plugin/node/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
variables: true
},
setting: {
command: 'npm',
'strict-ssl': true,
cafile: false,
NODE_EXTRA_CA_CERTS: false,
Expand Down
50 changes: 28 additions & 22 deletions packages/core/src/modules/plugin/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const NodePlugin = function (context) {
nodeApi.setVariables()
},
async getNpmEnv () {
const ret = await shell.exec(['npm config list --json'], { type: 'cmd' })
const command = config.get().plugin.node.setting.command || 'npm'

const ret = await shell.exec([`${command} config list --json`], { type: 'cmd' })
if (ret != null) {
const json = ret.substring(ret.indexOf('{'))
return jsonApi.parse(json)
Expand All @@ -38,25 +40,27 @@ const NodePlugin = function (context) {
},

async setNpmEnv (list) {
const command = config.get().plugin.node.setting.command || 'npm'

const cmds = []
for (const item of list) {
if (item.value != null) {
cmds.push(`npm config set ${item.key} ${item.value}`)
cmds.push(`${command} config set ${item.key} ${item.value}`)
} else {
cmds.push(`npm config delete ${item.key}`)
cmds.push(`${command} config delete ${item.key}`)
}
}
const ret = await shell.exec(cmds, { type: 'cmd' })
return ret
return await shell.exec(cmds, { type: 'cmd' })
},

async unsetNpmEnv (list) {
const command = config.get().plugin.node.setting.command || 'npm'

const cmds = []
for (const item of list) {
cmds.push(`npm config delete ${item} `)
cmds.push(`${command} config delete ${item} `)
}
const ret = await shell.exec(cmds, { type: 'cmd' })
return ret
return await shell.exec(cmds, { type: 'cmd' })
},

async setYarnEnv (list) {
Expand All @@ -69,17 +73,15 @@ const NodePlugin = function (context) {
cmds.push(`yarn config delete ${item.key}`)
}
}
const ret = await shell.exec(cmds, { type: 'cmd' })
return ret
return await shell.exec(cmds, { type: 'cmd' })
},

async unsetYarnEnv (list) {
const cmds = []
for (const item of list) {
cmds.push(`yarn config delete ${item} `)
}
const ret = await shell.exec(cmds, { type: 'cmd' })
return ret
return await shell.exec(cmds, { type: 'cmd' })
},

async getVariables () {
Expand Down Expand Up @@ -119,9 +121,11 @@ const NodePlugin = function (context) {
},

async setProxy (ip, port) {
const command = config.get().plugin.node.setting.command || 'npm'

const cmds = [
`npm config set proxy=http://${ip}:${port}`,
`npm config set https-proxy=http://${ip}:${port}`
`${command} config set proxy=http://${ip}:${port}`,
`${command} config set https-proxy=http://${ip}:${port}`
]

const env = []
Expand All @@ -135,19 +139,19 @@ const NodePlugin = function (context) {
const nodeConfig = config.get().plugin.node
const rootCaCertFile = config.get().server.setting.rootCaFile.certPath
if (nodeConfig.setting['strict-ssl']) {
cmds.push('npm config set strict-ssl false')
cmds.push(`${command} config set strict-ssl false`)
}
if (nodeConfig.setting.cafile) {
cmds.push(`npm config set cafile "${rootCaCertFile}"`)
cmds.push(`${command} config set cafile "${rootCaCertFile}"`)
}

if (nodeConfig.setting.NODE_EXTRA_CA_CERTS) {
cmds.push(`npm config set NODE_EXTRA_CA_CERTS "${rootCaCertFile}"`)
cmds.push(`${command} config set NODE_EXTRA_CA_CERTS "${rootCaCertFile}"`)
env.push({ key: 'NODE_EXTRA_CA_CERTS', value: rootCaCertFile })
}

if (nodeConfig.setting.NODE_TLS_REJECT_UNAUTHORIZED) {
cmds.push('npm config set NODE_TLS_REJECT_UNAUTHORIZED 0')
cmds.push(`${command} config set NODE_TLS_REJECT_UNAUTHORIZED 0`)
env.push({ key: 'NODE_TLS_REJECT_UNAUTHORIZED', value: '0' })
}

Expand All @@ -162,11 +166,13 @@ const NodePlugin = function (context) {
},

async unsetProxy () {
const command = config.get().plugin.node.setting.command || 'npm'

const cmds = [
'npm config delete proxy',
'npm config delete https-proxy',
'npm config delete NODE_EXTRA_CA_CERTS',
'npm config delete strict-ssl'
`${command} config delete proxy`,
`${command} config delete https-proxy`,
`${command} config delete NODE_EXTRA_CA_CERTS`,
`${command} config delete strict-ssl`
]
const ret = await shell.exec(cmds, { type: 'cmd' })
event.fire('status', { key: 'plugin.node.enabled', value: false })
Expand Down
4 changes: 4 additions & 0 deletions packages/gui/src/view/pages/plugin/node.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
当前未启动
</a-tag>
</a-form-item>
<a-form-item label="npm命令名" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input v-model="config.plugin.node.setting.command"></a-input>
<div class="form-help">如果你的npm命令改成了其他名字,或者想设置绿色版npm程序路径,可在此处修改</div>
</a-form-item>
<a-form-item label="SSL校验" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-checkbox v-model="config.plugin.node.setting['strict-ssl']">
关闭strict-ssl
Expand Down
2 changes: 1 addition & 1 deletion packages/gui/src/view/pages/plugin/pip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<!-- </a-form-item>-->
<a-form-item label="pip命令名" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input v-model="config.plugin.pip.setting.command"></a-input>
<div class="form-help">如果你的pip命令改成了其他名字(如pip3),可以在此处修改</div>
<div class="form-help">如果你的pip命令改成了其他名字(如pip3),或者想设置绿色版pip程序路径,可在此处修改</div>
</a-form-item>
<a-form-item label="仓库镜像" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-radio-group v-model="config.plugin.pip.setting.registry" @change="onSwitchRegistry"
Expand Down

0 comments on commit face64b

Please sign in to comment.