Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/npm install #254

Merged
merged 6 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: test
  • Loading branch information
buqiyuan committed Mar 7, 2023
commit 81baa7d802f39c103d7c062da32e39a9a3208944
4 changes: 0 additions & 4 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ const config: Configuration = {
appId: '.postcat.io',
productName: 'Postcat',
asar: true,
asarUnpack: [
//使用asarUnpack属性,将不需要打进asar包里的文件路径指定。
'node_modules/npm'
],
directories: {
output: 'release/'
},
Expand Down
62 changes: 20 additions & 42 deletions src/platform/node/extension-manager/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Action = 'uninstall' | 'install' | 'update';
* Install npm packages in case of missing Node Environment
* ! npm version should be 6.14.8, otherwise npm function can't be used
* */
// const npmCli = require('npm');
const npmCli = require('npm');

/**
* Fix the $PATH on macOS and Linux when run from a GUI app
Expand Down Expand Up @@ -126,52 +126,30 @@ export class ModuleHandler extends CoreHandler {
}
}
private executeByAppNpm(command: string, modules: any[], resolve, reject) {
const moduleList = modules.map(({ name, version }) => (version ? `${name}@${version}` : name));
let executeCommand = ['update', 'install', 'uninstall'];
if (!executeCommand.includes(command)) {
return;
}

const npmPath = require.resolve(`npm/bin/npm${process.platform === 'win32' ? '.cmd' : ''}`).replace('app.asar', 'app.asar.unpacked');

const npm = spawn(npmPath, [command, '--prefix', this.baseDir, '--registry', this.registry, ...moduleList], {
shell: process.platform === 'win32'
});
npm.stdout.setEncoding('utf8');
npm.stdout.on('data', async data => {
console.log('data', data);
});
npm.stdout.on('error', err => {
console.log(`npm ${command} error`, err);
if (err) {
return reject(err);
// https://www.npmjs.com/package/bin-links
npmCli.load({ 'bin-links': false, verbose: true, prefix: this.baseDir, registry: this.registry }, loaderr => {
const moduleList = modules.map(({ name, version }) => (version ? `${name}@${version}` : name));
let executeCommand = ['update', 'install', 'uninstall'];
if (!executeCommand.includes(command)) {
return;
}
});
npm.stdout.on('end', () => {
process.chdir(this.baseDir);
return resolve({ code: 0, data: true });
npmCli.commands[command](moduleList, (err, data) => {
// console.log('command', command);
process.chdir(this.baseDir);
if (err) {
return reject(err);
}
this.operatePackage(data, moduleList, command as Action);
return resolve({ code: 0, data });
});
});
}
private setRegistry() {
const decoder = new TextDecoder('gbk');
return new Promise(resolve => {
const npmPath = require.resolve(`npm/bin/npm${process.platform === 'win32' ? '.cmd' : ''}`).replace('app.asar', 'app.asar.unpacked');
console.log('====》测试 npm', npmPath);
const npm1 = spawn(npmPath, ['-v', this.registry], {
shell: process.platform === 'win32'
});

npm1.stdout.on('data', async data => {
console.log('npm1', decoder.decode(data));
});

const npm = spawn(npmPath, ['config', 'set', 'registry', this.registry], {
shell: process.platform === 'win32'
});
npm.stdout.on('data', async data => {
console.log('data', decoder.decode(data));
const npm = spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['config', 'set', 'registry', this.registry], {
cwd: this.baseDir
});
npm.stdout.on('close', () => {
npm.on('close', () => {
resolve(true);
});
});
Expand All @@ -187,7 +165,7 @@ export class ModuleHandler extends CoreHandler {
// this.executeBySystemNpm(command, modules, resolve)
// * Set Proxy
// * Set registry
await this.setRegistry();
// await this.setRegistry();
this.executeByAppNpm(command, modules, resolve, reject);
});
}
Expand Down