plugin-generator
Version:
core_plugin generator
82 lines (75 loc) • 3.18 kB
JavaScript
const prompts = require('prompts');
const { red, greenBright, blue, yellow } = require('chalk');
const { writeFileSync, existsSync, mkdirSync } = require('fs');
require('./templates/first_level');
require('./templates/second_level');
require('./templates/third_level');
require('./utils');
const cwd = process.cwd();
console.log(yellow('-------STARTING-------'), '\n');
const dirHelper = (dir) => {
console.log(blue(`checking if ${fwd(dir)} exists`));
if (!existsSync(dir)) {
console.log(greenBright(`creating ${fwd(dir)}`));
mkdirSync(dir, { recursive: true });
} else {
console.log(red.bold(`Note:- ${fwd(dir)} already exists. Aborting...`));
process.exit();
}
}
const questions = [
{
type: 'select',
name: 'pluginType',
message: `Select plugin type: `,
warn: 'As of now only visualization templates are supported',
choices: [
{ title: 'Visualization', description: 'In UI, it will be present in Widgets section', value: 'vis' },
{ title: 'Sidebar', description: 'In collapsible sidebar', value: 'sidebar', disabled: true }
],
},
{
type: 'text',
name: 'plugin',
message: `Enter plugin's name: `
},
{
type: prev => prev == 'pizza' ? 'text' : null,
name: 'topping',
message: 'Name a topping'
}
];
(async () => {
const { plugin, pluginType } = await prompts(questions);
if (pluginType !== 'vis') {
process.exit();
}
const snakeCased = snakeCase(plugin);
//writing first level files
const first_dir = cwd + `/test ${snakeCased}`;
dirHelper(first_dir);
console.log(blue(`starting to write files in ${fwd(first_dir)}..`));
writeFileSync(first_dir + '/package.json', package_json(plugin));
writeFileSync(first_dir + '/index.js', index(plugin));
console.log(greenBright(`files written successfully in ${fwd(first_dir)}`));
//writing second level files
const second_dir = first_dir + '/Public';
dirHelper(second_dir);
console.log(blue(`starting to write files in ${fwd(second_dir)}..`));
const second_file = second_dir + `/${snakeCased}`;
writeFileSync(second_file + '_controller.js', controller(plugin));
writeFileSync(second_file + '_params.html', params_html(plugin));
writeFileSync(second_file + '_params.js', params_js(plugin));
writeFileSync(second_file + '.html', main_html(plugin));
writeFileSync(second_file + '.js', main_js(plugin));
writeFileSync(second_file + '_sidebar.html', sidebar_html(plugin));
console.log(greenBright(`files written successfully in ${fwd(second_dir)}`));
//writing third level files
const third_dir = second_dir + '/Styles';
dirHelper(third_dir);
console.log(blue(`starting to write files in ${fwd(third_dir)}..`));
writeFileSync(third_dir + `/${snakeCased}.css`, styles_css(plugin));
console.log(greenBright(`files written successfully in ${fwd(third_dir)}`));
console.log('\n', greenBright('-------COMPLETED-------'));
})();