nocobase/packages/core/cli/src/plugin-generator.js
chenos a6eebb940f
feat: update docs (#990)
* feat: improve code

* feat: update docs

* feat: update docs

* Update index.md

* Update features.md

* Update when.md

* Update contributing.md

* Update translations.md

* feat: clean up

* Add files via upload

* Update the-first-app.md

* Update plugins.md

* Update a-b-c.md

* Update blocks.md

* feat: update docs

* Add files via upload

* Update charts.md

* feat: update navs

* Update index.md

* Update index.md

* Update features.md

* Update index.md

* Update docker-compose.md

* Update create-nocobase-app.md

* Update git-clone.md

* Update contributing.md

* Update translations.md

* Update plugins.md

* Update the-first-app.md

* Add files via upload

* Update charts.md

* Update charts.md

* Update a-b-c.md

* Update collections.md

* Update menus.md

* Update menus.md

Co-authored-by: Zhou <zhou.working@gmail.com>
2022-10-31 11:52:17 +08:00

67 lines
1.9 KiB
JavaScript

const chalk = require('chalk');
const { existsSync } = require('fs');
const { join, resolve } = require('path');
const { Generator } = require('@umijs/utils');
const { readFile } = require('fs').promises;
function camelize(str) {
return str.trim().replace(/[-_\s]+(.)?/g, (match, c) => c.toUpperCase());
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
async function getProjectName() {
const content = await readFile(resolve(process.cwd(), 'package.json'), 'utf-8');
const json = JSON.parse(content);
return json.name;
}
async function getProjectVersion() {
const content = await readFile(resolve(process.cwd(), 'lerna.json'), 'utf-8');
const json = JSON.parse(content);
return json.version || '0.1.0';
}
class PluginGenerator extends Generator {
constructor(options) {
const { context = {}, ...opts } = options;
super(opts);
this.context = context;
}
async getContext() {
const { name } = this.context;
const packageName = await getProjectName();
const nocobaseVersion = require('@nocobase/server/package.json').version;
const packageVersion = await getProjectVersion();
return {
...this.context,
packageName: `@${packageName}/plugin-${name}`,
packageVersion,
nocobaseVersion,
pascalCaseName: capitalize(camelize(name)),
};
}
async writing() {
const { name } = this.context;
const target = resolve(process.cwd(), 'packages/plugins/', name);
if (existsSync(target)) {
console.log(chalk.red(`[${name}] plugin already exists.`));
return;
}
console.log('Creating plugin');
this.copyDirectory({
target,
context: await this.getContext(),
path: join(__dirname, '../templates/plugin'),
});
console.log('');
console.log(`The plugin folder is in ${chalk.green(`packages/plugins/${name}`)}`);
}
}
exports.PluginGenerator = PluginGenerator;