Browse Source

fix(cli): Improve support for multiple tsconfig files

Michael Bromley 1 year ago
parent
commit
d871eb7227

+ 3 - 4
packages/cli/src/commands/add/codegen/add-codegen.ts

@@ -62,7 +62,7 @@ async function addCodegen(options?: AddCodegenOptions): Promise<CliCommandReturn
     configSpinner.start('Configuring codegen file...');
     configSpinner.start('Configuring codegen file...');
     await pauseForPromptDisplay();
     await pauseForPromptDisplay();
 
 
-    const codegenFile = new CodegenConfigRef(packageJson.getPackageRootDir());
+    const codegenFile = new CodegenConfigRef(project, packageJson.getPackageRootDir());
 
 
     const rootDir = project.getDirectory('.');
     const rootDir = project.getDirectory('.');
     if (!rootDir) {
     if (!rootDir) {
@@ -85,9 +85,9 @@ async function addCodegen(options?: AddCodegenOptions): Promise<CliCommandReturn
             codegenFile.addEntryToGeneratesObject({
             codegenFile.addEntryToGeneratesObject({
                 name: `'${uiExtensionsPath}/gql/'`,
                 name: `'${uiExtensionsPath}/gql/'`,
                 kind: StructureKind.PropertyAssignment,
                 kind: StructureKind.PropertyAssignment,
-                initializer: `{ 
+                initializer: `{
                         preset: 'client',
                         preset: 'client',
-                        documents: '${uiExtensionsPath}/**/*.ts', 
+                        documents: '${uiExtensionsPath}/**/*.ts',
                         presetConfig: {
                         presetConfig: {
                             fragmentMasking: false,
                             fragmentMasking: false,
                         },
                         },
@@ -101,7 +101,6 @@ async function addCodegen(options?: AddCodegenOptions): Promise<CliCommandReturn
     configSpinner.stop('Configured codegen file');
     configSpinner.stop('Configured codegen file');
 
 
     await project.save();
     await project.save();
-    await codegenFile.save();
 
 
     const nextSteps = [
     const nextSteps = [
         `You can run codegen by doing the following:`,
         `You can run codegen by doing the following:`,

+ 7 - 9
packages/cli/src/commands/add/codegen/codegen-config-ref.ts

@@ -13,19 +13,17 @@ import {
 import { createFile, getTsMorphProject } from '../../../utilities/ast-utils';
 import { createFile, getTsMorphProject } from '../../../utilities/ast-utils';
 
 
 export class CodegenConfigRef {
 export class CodegenConfigRef {
-    private readonly tempProject: Project;
     public readonly sourceFile: SourceFile;
     public readonly sourceFile: SourceFile;
     private configObject: ObjectLiteralExpression | undefined;
     private configObject: ObjectLiteralExpression | undefined;
-    constructor(rootDir: Directory) {
-        this.tempProject = getTsMorphProject({ skipAddingFilesFromTsConfig: true });
+    constructor(
+        private readonly project: Project,
+        rootDir: Directory,
+    ) {
         const codegenFilePath = path.join(rootDir.getPath(), 'codegen.ts');
         const codegenFilePath = path.join(rootDir.getPath(), 'codegen.ts');
         if (fs.existsSync(codegenFilePath)) {
         if (fs.existsSync(codegenFilePath)) {
-            this.sourceFile = this.tempProject.addSourceFileAtPath(codegenFilePath);
+            this.sourceFile = this.project.addSourceFileAtPath(codegenFilePath);
         } else {
         } else {
-            this.sourceFile = createFile(
-                this.tempProject,
-                path.join(__dirname, 'templates/codegen.template.ts'),
-            );
+            this.sourceFile = createFile(this.project, path.join(__dirname, 'templates/codegen.template.ts'));
             this.sourceFile.move(path.join(rootDir.getPath(), 'codegen.ts'));
             this.sourceFile.move(path.join(rootDir.getPath(), 'codegen.ts'));
         }
         }
     }
     }
@@ -58,6 +56,6 @@ export class CodegenConfigRef {
     }
     }
 
 
     save() {
     save() {
-        return this.tempProject.save();
+        return this.project.save();
     }
     }
 }
 }

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

@@ -109,7 +109,7 @@ export async function createNewPlugin(): Promise<CliCommandReturnVal> {
         if (featureType === 'no') {
         if (featureType === 'no') {
             done = true;
             done = true;
         } else {
         } else {
-            const newProject = getTsMorphProject();
+            const newProject = await getTsMorphProject();
             workingProject = newProject;
             workingProject = newProject;
             const newPlugin = newProject
             const newPlugin = newProject
                 .getSourceFile(workingPlugin.getSourceFile().getFilePath())
                 .getSourceFile(workingPlugin.getSourceFile().getFilePath())
@@ -158,7 +158,7 @@ export async function generatePlugin(
     const projectSpinner = spinner();
     const projectSpinner = spinner();
     projectSpinner.start('Generating plugin scaffold...');
     projectSpinner.start('Generating plugin scaffold...');
     await pauseForPromptDisplay();
     await pauseForPromptDisplay();
-    const project = getTsMorphProject({ skipAddingFilesFromTsConfig: true });
+    const project = await getTsMorphProject({ skipAddingFilesFromTsConfig: true });
 
 
     const pluginFile = createFile(project, path.join(__dirname, 'templates/plugin.template.ts'));
     const pluginFile = createFile(project, path.join(__dirname, 'templates/plugin.template.ts'));
     const pluginClass = pluginFile.getClass('TemplatePlugin');
     const pluginClass = pluginFile.getClass('TemplatePlugin');

+ 1 - 1
packages/cli/src/shared/shared-prompts.ts

@@ -20,7 +20,7 @@ export async function analyzeProject(options: {
         const projectSpinner = spinner();
         const projectSpinner = spinner();
         projectSpinner.start('Analyzing project...');
         projectSpinner.start('Analyzing project...');
         await pauseForPromptDisplay();
         await pauseForPromptDisplay();
-        project = getTsMorphProject();
+        project = await getTsMorphProject();
         projectSpinner.stop('Project analyzed');
         projectSpinner.stop('Project analyzed');
     }
     }
     return project as Project;
     return project as Project;

+ 3 - 2
packages/cli/src/utilities/ast-utils.ts

@@ -29,8 +29,9 @@ export async function selectTsConfigFile() {
     return selectedConfigFile as string;
     return selectedConfigFile as string;
 }
 }
 
 
-export function getTsMorphProject(options: ProjectOptions = {}) {
-    const tsConfigPath = path.join(process.cwd(), 'tsconfig.json');
+export async function getTsMorphProject(options: ProjectOptions = {}) {
+    const tsConfigFile = await selectTsConfigFile();
+    const tsConfigPath = path.join(process.cwd(), tsConfigFile);
     if (!fs.existsSync(tsConfigPath)) {
     if (!fs.existsSync(tsConfigPath)) {
         throw new Error('No tsconfig.json found in current directory');
         throw new Error('No tsconfig.json found in current directory');
     }
     }