86 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-07-21 20:08:17 +02:00
'use strict';
const { join } = require('path');
2021-07-21 20:08:17 +02:00
const { flow, camelCase, upperFirst, lowerCase } = require('lodash');
2021-07-22 14:10:54 +02:00
const fileExistsInPackages = require('../utils/fileExistsInPackages');
2021-07-21 20:08:17 +02:00
const getPluginList = require('../utils/getPluginList');
const packagesFolder = require('../utils/packagesFolder');
2021-07-21 20:08:17 +02:00
const pascalCase = flow(
camelCase,
upperFirst
);
2021-07-21 20:08:17 +02:00
const prompts = [
{
type: 'list',
name: 'plugin',
message: 'Which plugin should be targeted?',
choices: getPluginList,
},
{
type: 'input',
name: 'name',
message: 'What should be the name of the component?',
async validate(name, answers) {
2021-07-22 14:10:54 +02:00
if (!name) {
return 'The name cannot be empty.';
}
return (await fileExistsInPackages(`${answers.plugin}/admin/src/components/${name}`))
? 'This component already exists.'
: true;
},
2021-07-21 20:08:17 +02:00
filter: pascalCase,
},
{
type: 'confirm',
name: 'styled',
message: 'Is this a styled component?',
},
{
type: 'input',
name: 'htmlTag',
message: 'Which HTML tag should be used as a base?',
2021-07-22 14:10:54 +02:00
when: answers => answers.styled,
validate: htmlTag => (!htmlTag ? 'The HTML tag cannot be empty.' : true),
2021-07-21 20:08:17 +02:00
filter: lowerCase,
},
{
type: 'confirm',
name: 'useI18n',
message: 'Will it use i18n?',
2021-07-22 14:10:54 +02:00
when: answers => !answers.styled,
2021-07-21 20:08:17 +02:00
},
{
type: 'confirm',
name: 'useRedux',
message: 'Will it use Redux?',
2021-07-22 14:10:54 +02:00
when: answers => !answers.styled,
2021-07-21 20:08:17 +02:00
},
];
const actions = answers => {
const { useRedux } = answers;
const [pluginFolder, plugin] = answers.plugin.split('/');
answers.plugin = plugin;
const templatesFolder = 'component/templates';
2021-07-21 20:08:17 +02:00
const pattern = useRedux ? '**/**.hbs' : '**/index.*.hbs';
const path = join(packagesFolder, pluginFolder, '{{plugin}}/admin/src/components/{{name}}');
2021-07-21 20:08:17 +02:00
return [
{
type: 'addMany',
destination: path,
templateFiles: [`${templatesFolder}/${pattern}`],
base: templatesFolder,
2021-07-21 20:08:17 +02:00
},
// TODO: If redux will be used then 'append' created reducer inside 'reducers.js'
{
type: 'lint',
files: [path],
},
2021-07-21 20:08:17 +02:00
];
};
module.exports = { prompts, actions };