allow the choice of the new API name

This commit is contained in:
Dieter Stinglhamber 2021-11-16 09:39:29 +01:00
parent c32a1921e9
commit ba4072fdec
2 changed files with 33 additions and 4 deletions

View File

@ -129,6 +129,7 @@ const generateAPI = ({ singularName, kind = 'collectionType', pluralName, displa
{
kind,
singularName,
id: singularName,
pluralName,
displayName,
destination: 'new',

View File

@ -1,6 +1,8 @@
'use strict';
const { join } = require('path');
const slugify = require('@sindresorhus/slugify');
const fs = require('fs-extra');
const getDestinationPrompts = require('./prompts/get-destination-prompts');
const getFilePath = require('./utils/get-file-path');
@ -18,14 +20,42 @@ module.exports = plop => {
const config = await inquirer.prompt([
...ctNamesPrompts,
...kindPrompts,
...getDestinationPrompts('model', plop.getDestBasePath()),
...draftAndPublishPrompts,
...bootstrapApiPrompts,
]);
const attributes = await getAttributesPrompts(inquirer);
const api = await inquirer.prompt([
...getDestinationPrompts('model', plop.getDestBasePath()),
{
when: answers => answers.destination === 'new',
type: 'input',
name: 'id',
default: config.singularName,
message: 'Name of the new API?',
async validate(input) {
const apiPath = join(plop.getDestBasePath(), 'api');
const exists = await fs.pathExists(apiPath);
if (!exists) {
return true;
}
const apiDir = await fs.readdir(apiPath, { withFileTypes: true });
const apiDirContent = apiDir.filter(fd => fd.isDirectory());
if (apiDirContent.findIndex(api => api.name === input) !== -1) {
throw new Error('This name is already taken.');
}
return true;
},
},
...bootstrapApiPrompts,
]);
return {
...config,
...api,
attributes,
};
},
@ -47,8 +77,6 @@ module.exports = plop => {
const filePath = getFilePath(answers.destination);
answers.id = answers.singularName;
const baseActions = [
{
type: 'add',