fixed NPM detection in cases when node is not on PATH

This commit is contained in:
Eugene Pankov
2018-04-03 13:31:56 +02:00
parent 30f221d05e
commit 128fe24003

View File

@@ -29,6 +29,7 @@ export class PluginManagerService {
userPluginsPath: string = (window as any).userPluginsPath userPluginsPath: string = (window as any).userPluginsPath
installedPlugins: IPluginInfo[] = (window as any).installedPlugins installedPlugins: IPluginInfo[] = (window as any).installedPlugins
npmPath: string npmPath: string
private envPath: string
constructor ( constructor (
log: LogService, log: LogService,
@@ -41,11 +42,13 @@ export class PluginManagerService {
async detectPath () { async detectPath () {
this.npmPath = this.config.store.npm this.npmPath = this.config.store.npm
this.envPath = process.env.PATH
if (await fs.exists(this.npmPath)) { if (await fs.exists(this.npmPath)) {
return return
} }
if (this.hostApp.platform !== Platform.Windows) { if (this.hostApp.platform !== Platform.Windows) {
let searchPaths = (await exec('$SHELL -c -i \'echo $PATH\''))[0].toString().trim().split(':') this.envPath = (await exec('$SHELL -c -i \'echo $PATH\''))[0].toString().trim()
let searchPaths = this.envPath.split(':')
for (let searchPath of searchPaths) { for (let searchPath of searchPaths) {
if (await fs.exists(path.join(searchPath, 'npm'))) { if (await fs.exists(path.join(searchPath, 'npm'))) {
this.logger.debug('Found npm in', searchPath) this.logger.debug('Found npm in', searchPath)
@@ -59,7 +62,7 @@ export class PluginManagerService {
async isNPMInstalled (): Promise<boolean> { async isNPMInstalled (): Promise<boolean> {
await this.detectPath() await this.detectPath()
try { try {
await exec(`${this.npmPath} -v`) await exec(`${this.npmPath} -v`, { env: this.getEnv() })
return true return true
} catch (_) { } catch (_) {
return false return false
@@ -88,13 +91,17 @@ export class PluginManagerService {
} }
async installPlugin (plugin: IPluginInfo) { async installPlugin (plugin: IPluginInfo) {
await exec(`${this.npmPath} --prefix "${this.userPluginsPath}" install ${plugin.packageName}@${plugin.version}`) await exec(`${this.npmPath} --prefix "${this.userPluginsPath}" install ${plugin.packageName}@${plugin.version}`, { env: this.getEnv() })
this.installedPlugins = this.installedPlugins.filter(x => x.packageName !== plugin.packageName) this.installedPlugins = this.installedPlugins.filter(x => x.packageName !== plugin.packageName)
this.installedPlugins.push(plugin) this.installedPlugins.push(plugin)
} }
async uninstallPlugin (plugin: IPluginInfo) { async uninstallPlugin (plugin: IPluginInfo) {
await exec(`${this.npmPath} --prefix "${this.userPluginsPath}" remove ${plugin.packageName}`) await exec(`${this.npmPath} --prefix "${this.userPluginsPath}" remove ${plugin.packageName}`, { env: this.getEnv() })
this.installedPlugins = this.installedPlugins.filter(x => x.packageName !== plugin.packageName) this.installedPlugins = this.installedPlugins.filter(x => x.packageName !== plugin.packageName)
} }
private getEnv (): any {
return Object.assign(process.env, { PATH: this.envPath })
}
} }