From e0a689523ca4ee8e84b3cc128e13105c01aaf58c Mon Sep 17 00:00:00 2001 From: Jan Melcher Date: Fri, 13 Sep 2024 11:49:47 +0200 Subject: [PATCH] refactor: remove comment-only files in withModuleSelection() --- .../project/select-modules-in-sources.spec.ts | 47 +++++++++++++++++++ src/project/select-modules-in-sources.ts | 10 ++++ 2 files changed, 57 insertions(+) diff --git a/spec/project/select-modules-in-sources.spec.ts b/spec/project/select-modules-in-sources.spec.ts index 96b9508d..e9043643 100644 --- a/spec/project/select-modules-in-sources.spec.ts +++ b/spec/project/select-modules-in-sources.spec.ts @@ -182,6 +182,53 @@ describe('selectModulesInProjectSource', () => { expect(result.getModel().modules).to.deep.equal([]); }); + it('removes files that become comment-only', () => { + const project = new Project({ + sources: [ + new ProjectSource( + 'modules.json', + JSON.stringify({ + modules: ['module1', 'module2', 'module3', 'extra1', 'extra2'], + }), + ), + gql` + # a comment in the keeper file + type Keeper @rootEntity @modules(in: "module1", includeAllFields: true) { + key: String @key + } + `.loc!.source, + gql` + # a comment in the discard file + type Discard @rootEntity @modules(in: "extra2", includeAllFields: true) { + key: String @key + } + `.loc!.source, + // will also remove this one because we're throwing away comment-only file when + // parsing a project. Documenting that behavior in this test case, but it's + // probably fine either way + { + name: 'empty.graphqls', + body: "# a file that's already comment-only", + }, + ], + modelOptions: { withModuleDefinitions: true }, + }); + expectToBeValid(project); + + const result = project.withModuleSelection(['module1', 'module2'], { + removeModuleDeclarations: true, + }); + expectToBeValid(result); + expect(result.sources.map((s) => s.body)).to.deep.equal([ + ` + # a comment in the keeper file + type Keeper @rootEntity { + key: String @key + } + `, + ]); + }); + it('removes the modules part in an object file with modules and something else', () => { const project = new Project({ sources: [ diff --git a/src/project/select-modules-in-sources.ts b/src/project/select-modules-in-sources.ts index 81c6ce45..e09ac0d3 100644 --- a/src/project/select-modules-in-sources.ts +++ b/src/project/select-modules-in-sources.ts @@ -21,6 +21,7 @@ import { findDirectiveWithName } from '../schema/schema-utils'; import { Project, ProjectOptions } from './project'; import { ProjectSource } from './source'; import { isReadonlyArray } from '../utils/utils'; +import { isCommentOnlySource } from '../graphql/is-comment-only-source'; export interface ModuleSelectionOptions { /** @@ -255,6 +256,10 @@ function selectModulesInGraphQLSource({ } } + if (!changes) { + return source.body; + } + let currentPosition = 0; let output = ''; for (let i = 0; i <= changes.length; i++) { @@ -275,6 +280,11 @@ function selectModulesInGraphQLSource({ } } + // if we removed everything except comments, delete the file + if (isCommentOnlySource(output)) { + return undefined; + } + return output; }