Skip to content

Commit

Permalink
feat: extension system (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese authored Feb 8, 2024
1 parent 9681072 commit 836139d
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 7 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/usr/bin/env node

import program from './src/executer/cli.js';
import rescue from './src/executer/rescue.js';
import ExtensionHandler from './src/handlers/extension.js';

program.parse(process.argv);
if (['rescue', 'r'].includes(process.argv[2])) await rescue();
else {
ExtensionHandler.init();
ExtensionHandler.load(process);
program.parse(process.argv);
}
11 changes: 8 additions & 3 deletions raw/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// === REFRESH TAG = ERROR
// === REFRESH TAG = ERROR
// === CONFIGURABLE VARIABLES
import config from './.serein.json' assert { type: 'json' };
const output = config.output;
Expand Down Expand Up @@ -207,13 +207,18 @@ const deploy = gulp.series(

const default_action = gulp.series(build, deploy);

async function extension() {
const [name, task] = [process.argv[3], process.argv[4]];
await (await import(name))[task](config);
}

export {
build,
bundle,
clean_and_copy as cc,
deploy,
default_action as default,
watch,
compile_scripts as cs
compile_scripts as cs,
extension
};

2 changes: 2 additions & 0 deletions src/base/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const IO = {
}
},

exists: (filename) => existsSync(filename),

writeText: (filename, text) => {
writeFileSync(filename, text);
done('Create ' + filename);
Expand Down
20 changes: 19 additions & 1 deletion src/executer/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import adaptProject from './adapt.js';
import moduleManage from './module.js';
import switchVersion from './switch.js';
import { CLI_VERSION } from '../base/constants.js';
import { install, uninstall } from './ext.js';

program
.name('serein')
Expand All @@ -30,7 +31,7 @@ program
.alias('m')
.description('managing current dependencies')
.option('-y --yes', 'switch to latest version directly')
.action((option) => moduleManage());
.action(() => moduleManage());

program
.command('adapt')
Expand Down Expand Up @@ -62,4 +63,21 @@ program
.description('listen for file changes and deploy project automatically')
.action(() => IO.exec('gulp watch'));

program
.command('install <packageName>')
.alias('ins')
.description('install an extension for serein')
.action((...packageName) => install(packageName));

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

program
.command('rescue')
.alias('r')
.description('recovering a project with an exception dependency');

export default program;
16 changes: 16 additions & 0 deletions src/executer/ext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import InfoHandler from '../handlers/information.js';
import ExtensionHandler from '../handlers/extension.js';

async function install(packageName) {
InfoHandler.bind('ext');

await ExtensionHandler.install(packageName);
}

async function uninstall(packageName) {
InfoHandler.bind('ext');

await ExtensionHandler.uninstall(packageName);
}

export { install, uninstall };
19 changes: 19 additions & 0 deletions src/executer/rescue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import IO from '../base/io.js';
import NpmHandler from '../handlers/npm.js';
import { error, start } from '../base/console.js';

async function rescue() {
start('project rescue...');
if (!(IO.exists('package.json') && IO.exists('.serein.json')))
console.log(
error(
'Fix failed, make sure you have serein.json with package.json!'
)
);
else {
await NpmHandler.install();
done('project rescue.');
}
}

export default rescue;
58 changes: 58 additions & 0 deletions src/handlers/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { done, error, start } from '../base/console.js';
import IO from '../base/io.js';
import NpmHandler from './npm.js';
import InfoHandler from './information.js';

class ExtensionClass {
constructor() {
this.context = {};
this.extList = [];
}

init() {
if (IO.exists('.serein.json')) {
this.context = IO.readJSON('.serein.json');
if (!this.context['extensions']) this.context['extension'] = [];
else this.extList = this.context['extension'];
}
}

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

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

done('Intstall extension.');
}

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

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

done('Uninstall extensions.');
}

async load(program) {
if (this.extList.length) {
start('Load extensions...');
try {
for (const packageName in this.extList)
(await import(packageName)).cli((program, InfoHandler));
} catch (e) {
console.log(error('Failed to load extension!'), e);
}
done('Load extensions.');
}
}
}

const ExtensionHandler = new ExtensionClass();

export default ExtensionHandler;
7 changes: 6 additions & 1 deletion src/handlers/information.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ class InfoClass extends DelayHanlder {
behManifestPath: 'behavior_packs/manifest.json',
resManifestPath: 'resource_packs/manifest.json'
});
} else if (this.info.mode === 'switch' || this.info.mode === 'module') {
} else if (
this.info.mode === 'switch' ||
this.info.mode === 'module' ||
this.info.mode === 'ext' ||
this.info.mode === 'rescue'
) {
Object.assign(this.info, {
mode: this.info.mode,
...(await ConfigRender.getConfig())
Expand Down
20 changes: 19 additions & 1 deletion src/handlers/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { existsSync } from 'fs';
import { deleteSync } from 'del';
import MirrorHandler from './mirror.js';
import { ALL } from '../base/constants.js';
import { accept } from '../base/console.js';
import DelayHanlderWithInfo from './delayInfo.js';
import { accept, start, done } from '../base/console.js';

function checkPnpm() {
try {
Expand Down Expand Up @@ -140,6 +140,24 @@ class NpmClass extends DelayHanlderWithInfo {
} else
IO.exec(`npm install --registry=${this.mirror} ${android_suffix}`);
}

async add(packageName) {
await this.check();
start(`Install extension "${packageName}"...`);
if (this.pnpm) {
IO.exec(`pnpm install ${packageName}`);
} else IO.exec(`npm install ${packageName}`);
done(`Install extension "${packageName}".`);
}

async del(packageName) {
await this.check();
start(`Uninstall extension "${packageName}"...`);
if (this.pnpm) {
IO.exec(`pnpm remove ${packageName}`);
} else IO.exec(`npm uninstall ${packageName}`);
done(`Uninstall extension "${packageName}".`);
}
}

const NpmHandler = new NpmClass();
Expand Down

0 comments on commit 836139d

Please sign in to comment.