Skip to content

Commit 50467c7

Browse files
fhyogaMolunerfinn
authored andcommitted
✨ Feature(plugin): passing environment variables
1 parent f9bb9fb commit 50467c7

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/lib/PluginHandler.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PicGo from '../core/PicGo'
22
import spawn from 'cross-spawn'
3-
import { Result } from '../utils/interfaces'
3+
import { Result, ProcessEnv } from '../utils/interfaces'
44

55
class PluginHandler {
66
// Thanks to feflow -> https://github.com/feflow/feflow/blob/master/lib/internal/install/plugin.js
@@ -9,9 +9,9 @@ class PluginHandler {
99
this.ctx = ctx
1010
}
1111

12-
async install (plugins: string[], proxy: string = ''): Promise<void> {
12+
async install (plugins: string[], proxy: string = '', env: ProcessEnv): Promise<void> {
1313
plugins = plugins.map((item: string) => 'picgo-plugin-' + item)
14-
const result = await this.execCommand('install', plugins, this.ctx.baseDir, proxy)
14+
const result = await this.execCommand('install', plugins, this.ctx.baseDir, proxy, env)
1515
if (!result.code) {
1616
plugins.forEach((plugin: string) => {
1717
this.ctx.pluginLoader.registerPlugin(plugin)
@@ -51,9 +51,9 @@ class PluginHandler {
5151
})
5252
}
5353
}
54-
async update (plugins: string[], proxy: string = ''): Promise<void> {
54+
async update (plugins: string[], proxy: string = '', env: ProcessEnv): Promise<void> {
5555
plugins = plugins.map((item: string) => 'picgo-plugin-' + item)
56-
const result = await this.execCommand('update', plugins, this.ctx.baseDir, proxy)
56+
const result = await this.execCommand('update', plugins, this.ctx.baseDir, proxy, env)
5757
if (!result.code) {
5858
this.ctx.log.success('插件更新成功')
5959
this.ctx.emit('updateSuccess', {
@@ -69,7 +69,7 @@ class PluginHandler {
6969
})
7070
}
7171
}
72-
execCommand (cmd: string, modules: string[], where: string, proxy: string = ''): Promise<Result> {
72+
execCommand (cmd: string, modules: string[], where: string, proxy: string = '', env: ProcessEnv = {}): Promise<Result> {
7373
const registry = this.ctx.getConfig('registry')
7474
return new Promise((resolve: any, reject: any): void => {
7575
let args = [cmd].concat(modules).concat('--color=always').concat('--save')
@@ -80,7 +80,7 @@ class PluginHandler {
8080
args = args.concat(`--proxy=${proxy}`)
8181
}
8282
try {
83-
const npm = spawn('npm', args, { cwd: where })
83+
const npm = spawn('npm', args, { cwd: where, env: Object.assign({}, process.env, env) })
8484

8585
let output = ''
8686
npm.stdout.on('data', (data: string) => {

src/utils/interfaces.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ interface ClipboardImage {
8989
isExistFile: boolean
9090
}
9191

92+
/**
93+
* for install command environment variable
94+
*/
95+
interface ProcessEnv {
96+
[propName: string]: string | undefined
97+
}
9298
export {
9399
PluginConfig,
94100
ImgInfo,
@@ -98,5 +104,6 @@ export {
98104
Result,
99105
ImgSize,
100106
Options,
101-
ClipboardImage
107+
ClipboardImage,
108+
ProcessEnv
102109
}

0 commit comments

Comments
 (0)