From 69e29d8e5fbc822e2bb9da00d7749d8b6e477f05 Mon Sep 17 00:00:00 2001 From: Convly Date: Tue, 13 Aug 2024 11:46:54 +0200 Subject: [PATCH] chore: refactor helpers and plugin init logic --- src/cli/commands/plugin/init/action.ts | 33 +++++++++++++++----------- src/cli/commands/utils/helpers.ts | 7 +++--- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/cli/commands/plugin/init/action.ts b/src/cli/commands/plugin/init/action.ts index 6d76434..efa8fef 100644 --- a/src/cli/commands/plugin/init/action.ts +++ b/src/cli/commands/plugin/init/action.ts @@ -25,7 +25,7 @@ const USE_RC_VERSIONS: string[] = [ // Store results of prompt answers (run by pack-up init) // This is a limitation of pack-up; we cannot run the prompt and pass the answers in -let promptAnswers: any = []; +let promptAnswers: { name: string; answer: string | boolean }[] = []; export default async ( packagePath: string, @@ -33,13 +33,17 @@ export default async ( { logger, cwd }: CLIContext ) => { try { - const isStrapi = dirContainsStrapiProject(cwd); + // Make sure prompt answers are reset + promptAnswers = []; + + const isStrapiProject = dirContainsStrapiProject(cwd); // If the user entered a path, we will try to parse the plugin name from it so we can provide it as a suggestion for consistency const parsedPath = path.parse(packagePath); const suggestedPackageName = parsedPath.base; const isPathPackageName = !packagePath.includes('/'); - const pluginPath = isStrapi && isPathPackageName ? `./src/plugins/${packagePath}` : packagePath; + const pluginPath = + isStrapiProject && isPathPackageName ? `./src/plugins/${packagePath}` : packagePath; // const template = getPluginTemplate({ suggestedPackageName }); @@ -55,14 +59,15 @@ export default async ( template, }); - if (isStrapi) { - const pkgName = promptAnswers.find((option: any) => option?.name === 'pkgName')?.answer; - - const language = promptAnswers.find((option: any) => option?.name === 'typescript')?.answer + if (isStrapiProject) { + const pkgName = promptAnswers.find((option) => option.name === 'pkgName')?.answer; + const language = promptAnswers.find((option) => option.name === 'typescript')?.answer ? 'ts' : 'js'; - logger.info(logInstructions(pkgName, { language, path: pluginPath })); + if (typeof pkgName === 'string' && ['ts', 'js'].includes(language)) { + logger.info(logInstructions(pkgName, { language, path: pluginPath })); + } } logger.info('Plugin generated successfully.'); @@ -255,7 +260,7 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => { optional: true, }), ], - async getFiles(answers) { + async getFiles(answers = []) { const author: string[] = []; const files: TemplateFile[] = []; @@ -288,7 +293,7 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => { }, peerDependencies: { // TODO: set this to 5.0.0 when Strapi 5 is released - '@strapi/strapi': '^5.0.0-beta', + '@strapi/strapi': '^5.0.0-rc', '@strapi/sdk-plugin': '^5.0.0', }, strapi: { @@ -307,12 +312,12 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => { break; } case 'description': { - pkgJson.description = String(answer) ?? undefined; - pkgJson.strapi.description = String(answer) ?? undefined; + pkgJson.description = String(answer); + pkgJson.strapi.description = String(answer); break; } case 'displayName': { - pkgJson.strapi.displayName = String(answer) ?? undefined; + pkgJson.strapi.displayName = String(answer); break; } case 'authorName': { @@ -379,7 +384,7 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => { name: 'strapi-server.js', contents: outdent` 'use strict'; - + module.exports = require('./dist/server'); `, }); diff --git a/src/cli/commands/utils/helpers.ts b/src/cli/commands/utils/helpers.ts index 1d6e51d..f2f2f35 100644 --- a/src/cli/commands/utils/helpers.ts +++ b/src/cli/commands/utils/helpers.ts @@ -23,8 +23,7 @@ export const dirContainsStrapiProject = (dir: string) => { const packageJsonPath = path.join(dir, 'package.json'); const pkgJSON = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); return Boolean( - (pkgJSON.dependencies && pkgJSON.dependencies['@strapi/strapi']) || - (pkgJSON.devDependencies && pkgJSON.devDependencies['@strapi/strapi']) + pkgJSON.dependencies?.['@strapi/strapi'] || pkgJSON.devDependencies?.['@strapi/strapi'] ); } catch (err) { return false; @@ -33,7 +32,7 @@ export const dirContainsStrapiProject = (dir: string) => { export const logInstructions = ( pluginName: string, - { language, path: pluginPath }: { language: string; path?: string } + { language, path: pluginPath }: { language: string; path: string } ) => { const maxLength = ` resolve: './src/plugins/${pluginName}'`.length; const separator = Array(maxLength).fill('─').join(''); @@ -49,7 +48,7 @@ ${exportInstruction} { ${chalk.gray('// ...')} ${chalk.green(`'${pluginName}'`)}: { enabled: ${chalk.yellow(true)}, - resolve: '${chalk.yellow(pluginPath || `./src/plugins/${pluginName}`)}' + resolve: '${chalk.yellow(pluginPath)}' }, ${chalk.gray('// ...')} }