Junyi fceebaf50e
Doc: command (#869)
* docs: add command dev doc

* feat: update doc

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-10-01 22:18:26 +08:00

44 lines
1.1 KiB
TypeScript

import path from 'path';
import * as fs from 'fs/promises';
import { InstallOptions, Plugin } from '@nocobase/server';
export class CommandPlugin extends Plugin {
getName(): string {
return this.getPackageName(__dirname);
}
beforeLoad() {
// TODO
}
async load() {
this.app
.command('export')
.option('-o, --output-dir')
.action(async (options, ...collections) => {
const { outputDir = path.join(process.env.PWD, 'storage') } = options;
await collections.reduce((promise, collection) => promise.then(async () => {
if (!this.db.hasCollection(collection)) {
console.warn('No such collection:', collection);
return;
}
const repo = this.db.getRepository(collection);
const data = repo.find();
await fs.writeFile(path.join(outputDir, `${collection}.json`), JSON.stringify(data), { mode: 0o644 });
}), Promise.resolve());
});
}
async disable() {
// this.app.resourcer.removeResource('testHello');
}
async install(options: InstallOptions) {
// TODO
}
}
export default CommandPlugin;