diff --git a/packages/core/content-type-builder/server/services/content-types.js b/packages/core/content-type-builder/server/services/content-types.js index 843464aaba..ee3ce5cf30 100644 --- a/packages/core/content-type-builder/server/services/content-types.js +++ b/packages/core/content-type-builder/server/services/content-types.js @@ -129,6 +129,7 @@ const generateAPI = ({ singularName, kind = 'collectionType', pluralName, displa { kind, singularName, + id: singularName, pluralName, displayName, destination: 'new', diff --git a/packages/generators/generators/lib/plops/content-type.js b/packages/generators/generators/lib/plops/content-type.js index 9710266230..17a56b0bfd 100644 --- a/packages/generators/generators/lib/plops/content-type.js +++ b/packages/generators/generators/lib/plops/content-type.js @@ -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',