Skip to content

Commit

Permalink
fix(cli): Improve support for multiple tsconfig files
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Apr 29, 2024
1 parent 59206d9 commit d871eb7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
7 changes: 3 additions & 4 deletions packages/cli/src/commands/add/codegen/add-codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function addCodegen(options?: AddCodegenOptions): Promise<CliCommandReturn
configSpinner.start('Configuring codegen file...');
await pauseForPromptDisplay();

const codegenFile = new CodegenConfigRef(packageJson.getPackageRootDir());
const codegenFile = new CodegenConfigRef(project, packageJson.getPackageRootDir());

const rootDir = project.getDirectory('.');
if (!rootDir) {
Expand All @@ -85,9 +85,9 @@ async function addCodegen(options?: AddCodegenOptions): Promise<CliCommandReturn
codegenFile.addEntryToGeneratesObject({
name: `'${uiExtensionsPath}/gql/'`,
kind: StructureKind.PropertyAssignment,
initializer: `{
initializer: `{
preset: 'client',
documents: '${uiExtensionsPath}/**/*.ts',
documents: '${uiExtensionsPath}/**/*.ts',
presetConfig: {
fragmentMasking: false,
},
Expand All @@ -101,7 +101,6 @@ async function addCodegen(options?: AddCodegenOptions): Promise<CliCommandReturn
configSpinner.stop('Configured codegen file');

await project.save();
await codegenFile.save();

const nextSteps = [
`You can run codegen by doing the following:`,
Expand Down
16 changes: 7 additions & 9 deletions packages/cli/src/commands/add/codegen/codegen-config-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ import {
import { createFile, getTsMorphProject } from '../../../utilities/ast-utils';

export class CodegenConfigRef {
private readonly tempProject: Project;
public readonly sourceFile: SourceFile;
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');
if (fs.existsSync(codegenFilePath)) {
this.sourceFile = this.tempProject.addSourceFileAtPath(codegenFilePath);
this.sourceFile = this.project.addSourceFileAtPath(codegenFilePath);
} 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'));
}
}
Expand Down Expand Up @@ -58,6 +56,6 @@ export class CodegenConfigRef {
}

save() {
return this.tempProject.save();
return this.project.save();
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/commands/add/plugin/create-new-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export async function createNewPlugin(): Promise<CliCommandReturnVal> {
if (featureType === 'no') {
done = true;
} else {
const newProject = getTsMorphProject();
const newProject = await getTsMorphProject();
workingProject = newProject;
const newPlugin = newProject
.getSourceFile(workingPlugin.getSourceFile().getFilePath())
Expand Down Expand Up @@ -158,7 +158,7 @@ export async function generatePlugin(
const projectSpinner = spinner();
projectSpinner.start('Generating plugin scaffold...');
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 pluginClass = pluginFile.getClass('TemplatePlugin');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/shared/shared-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function analyzeProject(options: {
const projectSpinner = spinner();
projectSpinner.start('Analyzing project...');
await pauseForPromptDisplay();
project = getTsMorphProject();
project = await getTsMorphProject();
projectSpinner.stop('Project analyzed');
}
return project as Project;
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/utilities/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export async function selectTsConfigFile() {
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)) {
throw new Error('No tsconfig.json found in current directory');
}
Expand Down

0 comments on commit d871eb7

Please sign in to comment.