Browse Source

fix(cli): Fix maximum call stack error

Fixes #2819
Michael Bromley 1 year ago
parent
commit
464e68b8a4

+ 4 - 4
packages/cli/src/commands/add/api-extension/add-api-extension.ts

@@ -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()
@@ -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',
@@ -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'),
     );
 }

+ 5 - 2
packages/cli/src/commands/add/codegen/codegen-config-ref.ts

@@ -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'),
+            );
         }
     }
 

+ 2 - 2
packages/cli/src/commands/add/entity/add-entity.ts

@@ -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

+ 5 - 2
packages/cli/src/commands/add/entity/codemods/add-entity-to-plugin/add-entity-to-plugin.spec.ts

@@ -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!);
 

+ 16 - 8
packages/cli/src/commands/add/plugin/create-new-plugin.ts

@@ -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)
@@ -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 {

+ 16 - 7
packages/cli/src/commands/add/service/add-service.ts

@@ -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({
@@ -102,7 +107,11 @@ 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')!
@@ -110,7 +119,11 @@ async function addService(
     } 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')!
@@ -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...`);
 

+ 2 - 2
packages/cli/src/commands/add/ui-extensions/add-ui-extensions.ts

@@ -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');

+ 4 - 4
packages/cli/src/utilities/ast-utils.ts

@@ -38,7 +38,6 @@ export async function getTsMorphProject(options: ProjectOptions = {}, providedTs
     const project = new Project({
         tsConfigFilePath: tsConfigPath,
         manipulationSettings: defaultManipulationSettings,
-        skipFileDependencyResolution: true,
         compilerOptions: {
             skipLibCheck: true,
         },
@@ -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);