Skip to content

Commit

Permalink
✨ Feature(plugin): passing environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
fhyoga authored and Molunerfinn committed Dec 26, 2019
1 parent f9bb9fb commit 50467c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/lib/PluginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PicGo from '../core/PicGo'
import spawn from 'cross-spawn'
import { Result } from '../utils/interfaces'
import { Result, ProcessEnv } from '../utils/interfaces'

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

async install (plugins: string[], proxy: string = ''): Promise<void> {
async install (plugins: string[], proxy: string = '', env: ProcessEnv): Promise<void> {
plugins = plugins.map((item: string) => 'picgo-plugin-' + item)
const result = await this.execCommand('install', plugins, this.ctx.baseDir, proxy)
const result = await this.execCommand('install', plugins, this.ctx.baseDir, proxy, env)
if (!result.code) {
plugins.forEach((plugin: string) => {
this.ctx.pluginLoader.registerPlugin(plugin)
Expand Down Expand Up @@ -51,9 +51,9 @@ class PluginHandler {
})
}
}
async update (plugins: string[], proxy: string = ''): Promise<void> {
async update (plugins: string[], proxy: string = '', env: ProcessEnv): Promise<void> {
plugins = plugins.map((item: string) => 'picgo-plugin-' + item)
const result = await this.execCommand('update', plugins, this.ctx.baseDir, proxy)
const result = await this.execCommand('update', plugins, this.ctx.baseDir, proxy, env)
if (!result.code) {
this.ctx.log.success('插件更新成功')
this.ctx.emit('updateSuccess', {
Expand All @@ -69,7 +69,7 @@ class PluginHandler {
})
}
}
execCommand (cmd: string, modules: string[], where: string, proxy: string = ''): Promise<Result> {
execCommand (cmd: string, modules: string[], where: string, proxy: string = '', env: ProcessEnv = {}): Promise<Result> {
const registry = this.ctx.getConfig('registry')
return new Promise((resolve: any, reject: any): void => {
let args = [cmd].concat(modules).concat('--color=always').concat('--save')
Expand All @@ -80,7 +80,7 @@ class PluginHandler {
args = args.concat(`--proxy=${proxy}`)
}
try {
const npm = spawn('npm', args, { cwd: where })
const npm = spawn('npm', args, { cwd: where, env: Object.assign({}, process.env, env) })

let output = ''
npm.stdout.on('data', (data: string) => {
Expand Down
9 changes: 8 additions & 1 deletion src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ interface ClipboardImage {
isExistFile: boolean
}

/**
* for install command environment variable
*/
interface ProcessEnv {
[propName: string]: string | undefined
}
export {
PluginConfig,
ImgInfo,
Expand All @@ -98,5 +104,6 @@ export {
Result,
ImgSize,
Options,
ClipboardImage
ClipboardImage,
ProcessEnv
}

0 comments on commit 50467c7

Please sign in to comment.