Skip to content

Commit

Permalink
fix(cli): Fix maximum call stack error
Browse files Browse the repository at this point in the history
Fixes #2819
  • Loading branch information
michaelbromley committed May 2, 2024
1 parent 4eaf7ff commit 464e68b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ function createSimpleResolver(
const resolverSourceFile = createFile(
project,
path.join(__dirname, 'templates/simple-resolver.template.ts'),
path.join(plugin.getPluginDir().getPath(), 'api', resolverFileName),
);
resolverSourceFile.move(path.join(plugin.getPluginDir().getPath(), 'api', resolverFileName));

const resolverClassDeclaration = resolverSourceFile
.getClasses()
Expand Down Expand Up @@ -245,8 +245,6 @@ function createCrudResolver(
const resolverSourceFile = createFile(
project,
path.join(__dirname, 'templates/crud-resolver.template.ts'),
);
resolverSourceFile.move(
path.join(
plugin.getPluginDir().getPath(),
'api',
Expand Down Expand Up @@ -638,7 +636,9 @@ function getOrCreateApiExtensionsFile(project: Project, plugin: VendurePluginRef
if (existingApiExtensionsFile) {
return existingApiExtensionsFile;
}
return createFile(project, path.join(__dirname, 'templates/api-extensions.template.ts')).move(
return createFile(
project,
path.join(__dirname, 'templates/api-extensions.template.ts'),
path.join(plugin.getPluginDir().getPath(), 'api', 'api-extensions.ts'),
);
}
7 changes: 5 additions & 2 deletions packages/cli/src/commands/add/codegen/codegen-config-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export class CodegenConfigRef {
if (fs.existsSync(codegenFilePath)) {
this.sourceFile = this.project.addSourceFileAtPath(codegenFilePath);
} else {
this.sourceFile = createFile(this.project, path.join(__dirname, 'templates/codegen.template.ts'));
this.sourceFile.move(path.join(rootDir.getPath(), 'codegen.ts'));
this.sourceFile = createFile(
this.project,
path.join(__dirname, 'templates/codegen.template.ts'),
path.join(rootDir.getPath(), 'codegen.ts'),
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/add/entity/add-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ function createEntity(plugin: VendurePluginRef, options: AddEntityOptions) {
const entityFile = createFile(
plugin.getSourceFile().getProject(),
path.join(__dirname, 'templates/entity.template.ts'),
path.join(entitiesDir, `${options.fileName}.ts`),
);
const translationFile = createFile(
plugin.getSourceFile().getProject(),
path.join(__dirname, 'templates/entity-translation.template.ts'),
path.join(entitiesDir, `${options.translationFileName}.ts`),
);
entityFile.move(path.join(entitiesDir, `${options.fileName}.ts`));
translationFile.move(path.join(entitiesDir, `${options.translationFileName}.ts`));

const entityClass = entityFile.getClass('ScaffoldEntity')?.rename(options.className);
const customFieldsClass = entityFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ describe('addEntityToPlugin', () => {
const pluginClasses = getPluginClasses(project);
expect(pluginClasses.length).toBe(1);
const entityTemplatePath = path.join(__dirname, '../../templates/entity.template.ts');
const entityFile = createFile(project, entityTemplatePath);
entityFile.move(path.join(__dirname, 'fixtures', 'entity.ts'));
const entityFile = createFile(
project,
entityTemplatePath,
path.join(__dirname, 'fixtures', 'entity.ts'),
);
const entityClass = entityFile.getClass('ScaffoldEntity');
addEntityToPlugin(new VendurePluginRef(pluginClasses[0]), entityClass!);

Expand Down
24 changes: 16 additions & 8 deletions packages/cli/src/commands/add/plugin/create-new-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,30 @@ export async function generatePlugin(
const projectSpinner = spinner();
projectSpinner.start('Generating plugin scaffold...');
await pauseForPromptDisplay();
const { project } = await getTsMorphProject({ skipAddingFilesFromTsConfig: true });
const { project } = await getTsMorphProject({ skipAddingFilesFromTsConfig: false });

const pluginFile = createFile(project, path.join(__dirname, 'templates/plugin.template.ts'));
const pluginFile = createFile(
project,
path.join(__dirname, 'templates/plugin.template.ts'),
path.join(options.pluginDir, paramCase(nameWithoutPlugin) + '.plugin.ts'),
);
const pluginClass = pluginFile.getClass('TemplatePlugin');
if (!pluginClass) {
throw new Error('Could not find the plugin class in the generated file');
}
pluginClass.rename(templateContext.pluginName);

const typesFile = createFile(project, path.join(__dirname, 'templates/types.template.ts'));
const typesFile = createFile(
project,
path.join(__dirname, 'templates/types.template.ts'),
path.join(options.pluginDir, 'types.ts'),
);

const constantsFile = createFile(project, path.join(__dirname, 'templates/constants.template.ts'));
const constantsFile = createFile(
project,
path.join(__dirname, 'templates/constants.template.ts'),
path.join(options.pluginDir, 'constants.ts'),
);
constantsFile
.getVariableDeclaration('TEMPLATE_PLUGIN_OPTIONS')
?.rename(templateContext.pluginInitOptionsName)
Expand All @@ -178,10 +190,6 @@ export async function generatePlugin(
.getVariableDeclaration('loggerCtx')
?.set({ initializer: `'${templateContext.pluginName}'` });

typesFile.move(path.join(options.pluginDir, 'types.ts'));
pluginFile.move(path.join(options.pluginDir, paramCase(nameWithoutPlugin) + '.plugin.ts'));
constantsFile.move(path.join(options.pluginDir, 'constants.ts'));

projectSpinner.stop('Generated plugin scaffold');
await project.save();
return {
Expand Down
23 changes: 16 additions & 7 deletions packages/cli/src/commands/add/service/add-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ async function addService(
}

const serviceSpinner = spinner();

const serviceFileName = paramCase(options.serviceName).replace(/-service$/, '.service');
let serviceSourceFile: SourceFile;
const serviceSourceFilePath = path.join(
vendurePlugin.getPluginDir().getPath(),
'services',
`${serviceFileName}.ts`,
);
let serviceClassDeclaration: ClassDeclaration;
if (options.type === 'basic') {
const name = await text({
Expand All @@ -102,15 +107,23 @@ async function addService(
options.serviceName = name;
serviceSpinner.start(`Creating ${options.serviceName}...`);
await pauseForPromptDisplay();
serviceSourceFile = createFile(project, path.join(__dirname, 'templates/basic-service.template.ts'));
serviceSourceFile = createFile(
project,
path.join(__dirname, 'templates/basic-service.template.ts'),
serviceSourceFilePath,
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
serviceClassDeclaration = serviceSourceFile
.getClass('BasicServiceTemplate')!
.rename(options.serviceName);
} else {
serviceSpinner.start(`Creating ${options.serviceName}...`);
await pauseForPromptDisplay();
serviceSourceFile = createFile(project, path.join(__dirname, 'templates/entity-service.template.ts'));
serviceSourceFile = createFile(
project,
path.join(__dirname, 'templates/entity-service.template.ts'),
serviceSourceFilePath,
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
serviceClassDeclaration = serviceSourceFile
.getClass('EntityServiceTemplate')!
Expand Down Expand Up @@ -151,10 +164,6 @@ async function addService(
removedUnusedConstructorArgs(serviceClassDeclaration, entityRef);
}
modifiedSourceFiles.push(serviceSourceFile);
const serviceFileName = paramCase(options.serviceName).replace(/-service$/, '.service');
serviceSourceFile?.move(
path.join(vendurePlugin.getPluginDir().getPath(), 'services', `${serviceFileName}.ts`),
);

serviceSpinner.message(`Registering service with plugin...`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ async function addUiExtensions(options?: AddUiExtensionsOptions): Promise<CliCom

const providersFileDest = path.join(pluginDir, 'ui', 'providers.ts');
if (!fs.existsSync(providersFileDest)) {
createFile(project, path.join(__dirname, 'templates/providers.template.ts')).move(providersFileDest);
createFile(project, path.join(__dirname, 'templates/providers.template.ts'), providersFileDest);
}
const routesFileDest = path.join(pluginDir, 'ui', 'routes.ts');
if (!fs.existsSync(routesFileDest)) {
createFile(project, path.join(__dirname, 'templates/routes.template.ts')).move(routesFileDest);
createFile(project, path.join(__dirname, 'templates/routes.template.ts'), routesFileDest);
}

log.success('Created UI extension scaffold');
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/utilities/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export async function getTsMorphProject(options: ProjectOptions = {}, providedTs
const project = new Project({
tsConfigFilePath: tsConfigPath,
manipulationSettings: defaultManipulationSettings,
skipFileDependencyResolution: true,
compilerOptions: {
skipLibCheck: true,
},
Expand Down Expand Up @@ -123,14 +122,15 @@ export function getRelativeImportPath(locations: {
return convertPathToRelativeImport(path.relative(fromDir, toPath));
}

export function createFile(project: Project, templatePath: string) {
export function createFile(project: Project, templatePath: string, filePath: string) {
const template = fs.readFileSync(templatePath, 'utf-8');
const tempFilePath = path.join('/.vendure-cli-temp/', path.basename(templatePath));
try {
return project.createSourceFile(path.join('/.vendure-cli-temp/', tempFilePath), template, {
const file = project.createSourceFile(filePath, template, {
overwrite: true,
scriptKind: ScriptKind.TS,
});
project.resolveSourceFileDependencies();
return file;
} catch (e: any) {
log.error(e.message);
process.exit(1);
Expand Down

0 comments on commit 464e68b

Please sign in to comment.