Skip to content

Commit

Permalink
fix: the issue that the extension could not be loaded (#146)
Browse files Browse the repository at this point in the history
* fix: the issue that the extension could not be loaded
  • Loading branch information
Lampese authored Feb 8, 2024
1 parent 836139d commit b29120c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ExtensionHandler from './src/handlers/extension.js';

if (['rescue', 'r'].includes(process.argv[2])) await rescue();
else {
ExtensionHandler.init();
ExtensionHandler.load(process);
await ExtensionHandler.init();
await ExtensionHandler.load(program);
program.parse(process.argv);
}
4 changes: 2 additions & 2 deletions src/executer/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ program
.command('install <packageName>')
.alias('ins')
.description('install an extension for serein')
.action((...packageName) => install(packageName));
.action((packageName) => install(packageName));

program
.command('uninstall <packageName>')
.alias('unins')
.description('uninstall an extension')
.action((...packageName) => uninstall(packageName));
.action((packageName) => uninstall(packageName));

program
.command('rescue')
Expand Down
31 changes: 21 additions & 10 deletions src/handlers/extension.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { done, error, start } from '../base/console.js';
import IO from '../base/io.js';
import { resolve } from 'path';
import NpmHandler from './npm.js';
import InfoHandler from './information.js';
import { done, error, start } from '../base/console.js';

class ExtensionClass {
constructor() {
Expand All @@ -12,27 +13,27 @@ class ExtensionClass {
init() {
if (IO.exists('.serein.json')) {
this.context = IO.readJSON('.serein.json');
if (!this.context['extensions']) this.context['extension'] = [];
if (!this.context['extension']) this.context['extension'] = [];
else this.extList = this.context['extension'];
}
}

async install(packageNames) {
async install(packageName) {
start('Install extensions...');

await NpmHandler.add(packageNames.join(' '));
this.context.extension.push(...packageNames);
await NpmHandler.add(packageName);
this.context.extension.push(packageName);
IO.writeJSON('.serein.json', this.context);

done('Intstall extension.');
}

async uninstall(packageNames) {
async uninstall(packageName) {
start('Uninstall extensions...');

await NpmHandler.del(packageNames.join(' '));
await NpmHandler.del(packageName);
this.context.extension = this.context.extension.filter(
(v) => !packageNames.includes(v)
(v) => v !== packageName
);
IO.writeJSON('.serein.json', this.context);

Expand All @@ -43,8 +44,18 @@ class ExtensionClass {
if (this.extList.length) {
start('Load extensions...');
try {
for (const packageName in this.extList)
(await import(packageName)).cli((program, InfoHandler));
for (const packageName of this.extList) {
(
await import(
resolve(
process.cwd(),
'node_modules',
packageName,
'index.js'
)
)
).cli(program, InfoHandler, IO);
}
} catch (e) {
console.log(error('Failed to load extension!'), e);
}
Expand Down
14 changes: 12 additions & 2 deletions src/handlers/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,18 @@ class NpmClass extends DelayHanlderWithInfo {
await this.check();
start(`Install extension "${packageName}"...`);
if (this.pnpm) {
IO.exec(`pnpm install ${packageName}`);
} else IO.exec(`npm install ${packageName}`);
if (this.platform === 'android')
IO.writeText('.npmrc', 'node-linker=hoisted');
console.log(
accept(
'Detects that you have pnpm and will automatically enable the pnpm installation dependency.'
)
);
IO.exec(`pnpm install ${packageName} --registry=${this.mirror}`);
} else
IO.exec(
`npm install ${packageName} --registry=${this.mirror} ${android_suffix}`
);
done(`Install extension "${packageName}".`);
}

Expand Down

0 comments on commit b29120c

Please sign in to comment.