Skip to content

Commit

Permalink
chore: refactor helpers and plugin init logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Convly committed Aug 13, 2024
1 parent 40b3f50 commit 69e29d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
33 changes: 19 additions & 14 deletions src/cli/commands/plugin/init/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ 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,
{ silent, debug }: ActionOptions,
{ 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 });
Expand All @@ -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.');
Expand Down Expand Up @@ -255,7 +260,7 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => {
optional: true,
}),
],
async getFiles(answers) {
async getFiles(answers = []) {
const author: string[] = [];

const files: TemplateFile[] = [];
Expand Down Expand Up @@ -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: {
Expand All @@ -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': {
Expand Down Expand Up @@ -379,7 +384,7 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => {
name: 'strapi-server.js',
contents: outdent`
'use strict';
module.exports = require('./dist/server');
`,
});
Expand Down
7 changes: 3 additions & 4 deletions src/cli/commands/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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('');
Expand All @@ -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('// ...')}
}
Expand Down

0 comments on commit 69e29d8

Please sign in to comment.