Skip to content

Commit

Permalink
feat(cli): Allow new plugin dir to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Feb 20, 2024
1 parent 8abc8e8 commit 4ae12e7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
43 changes: 27 additions & 16 deletions packages/cli/src/commands/new/plugin/new-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ import { GeneratePluginOptions, TemplateContext } from './types';
const cancelledMessage = 'Plugin setup cancelled.';

export async function newPlugin() {
const options: GeneratePluginOptions = { name: '', customEntityName: '' } as any;
const options: GeneratePluginOptions = { name: '', customEntityName: '', pluginDir: '' } as any;
intro('Scaffolding a new Vendure plugin!');
if (!options.name) {
const name = await text({
message: 'What is the name of the plugin?',
message: 'What is the name of the bobby plugin?',
initialValue: '',
validate: input => {
if (!/^[a-z][a-z-0-9]+$/.test(input)) {
return 'The plugin name must be lowercase and contain only letters, numbers and dashes';
}
const proposedPluginDir = getPluginDirName(input);
if (fs.existsSync(proposedPluginDir)) {
return `A directory named "${proposedPluginDir}" already exists. Cannot create plugin in this directory.`;
}
},
});

Expand Down Expand Up @@ -75,23 +71,27 @@ export async function newPlugin() {
}
}
const pluginDir = getPluginDirName(options.name);
const confirmation = await confirm({
message: `Create new plugin in ${pluginDir}?`,
const confirmation = await text({
message: 'Plugin location',
initialValue: pluginDir,
placeholder: '',
validate: input => {
if (fs.existsSync(input)) {
return `A directory named "${input}" already exists. Please specify a different directory.`;
}
},
});

if (isCancel(confirmation)) {
cancel(cancelledMessage);
process.exit(0);
} else {
if (confirmation === true) {
await generatePlugin(options);
} else {
cancel(cancelledMessage);
}
options.pluginDir = confirmation;
generatePlugin(options);
}
}

export async function generatePlugin(options: GeneratePluginOptions) {
export function generatePlugin(options: GeneratePluginOptions) {
const nameWithoutPlugin = options.name.replace(/-?plugin$/i, '');
const normalizedName = nameWithoutPlugin + '-plugin';
const templateContext: TemplateContext = {
Expand Down Expand Up @@ -167,7 +167,7 @@ export async function generatePlugin(options: GeneratePluginOptions) {
});
}

const pluginDir = getPluginDirName(options.name);
const pluginDir = options.pluginDir;
fs.ensureDirSync(pluginDir);
files.forEach(file => {
const filePath = path.join(pluginDir, file.path);
Expand All @@ -180,6 +180,17 @@ export async function generatePlugin(options: GeneratePluginOptions) {
}

function getPluginDirName(name: string) {
const cwd = process.cwd();
const pathParts = cwd.split(path.sep);
const currentlyInPluginsDir = pathParts[pathParts.length - 1] === 'plugins';
const currentlyInRootDir = fs.pathExistsSync(path.join(cwd, 'package.json'));
const nameWithoutPlugin = name.replace(/-?plugin$/i, '');
return path.join(process.cwd(), paramCase(nameWithoutPlugin));

if (currentlyInPluginsDir) {
return path.join(cwd, paramCase(nameWithoutPlugin));
}
if (currentlyInRootDir) {
return path.join(cwd, 'src', 'plugins', paramCase(nameWithoutPlugin));
}
return path.join(cwd, paramCase(nameWithoutPlugin));
}
1 change: 1 addition & 0 deletions packages/cli/src/commands/new/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface GeneratePluginOptions {
withApiExtensions: boolean;
withAdminUi: boolean;
customEntityName: string;
pluginDir: string;
}

export type TemplateContext = GeneratePluginOptions & {
Expand Down

0 comments on commit 4ae12e7

Please sign in to comment.