Skip to content

Commit 691f3e0

Browse files
committed
refactor: remove comment-only files in withModuleSelection()
1 parent 350a9f6 commit 691f3e0

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

spec/project/select-modules-in-sources.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,53 @@ describe('selectModulesInProjectSource', () => {
182182
expect(result.getModel().modules).to.deep.equal([]);
183183
});
184184

185+
it('removes files that become comment-only', () => {
186+
const project = new Project({
187+
sources: [
188+
new ProjectSource(
189+
'modules.json',
190+
JSON.stringify({
191+
modules: ['module1', 'module2', 'module3', 'extra1', 'extra2'],
192+
}),
193+
),
194+
gql`
195+
# a comment in the keeper file
196+
type Keeper @rootEntity @modules(in: "module1", includeAllFields: true) {
197+
key: String @key
198+
}
199+
`.loc!.source,
200+
gql`
201+
# a comment in the discard file
202+
type Discard @rootEntity @modules(in: "extra2", includeAllFields: true) {
203+
key: String @key
204+
}
205+
`.loc!.source,
206+
// will also remove this one because we're throwing away comment-only file when
207+
// parsing a project. Documenting that behavior in this test case, but it's
208+
// probably fine either way
209+
{
210+
name: 'empty.graphqls',
211+
body: "# a file that's already comment-only",
212+
},
213+
],
214+
modelOptions: { withModuleDefinitions: true },
215+
});
216+
expectToBeValid(project);
217+
218+
const result = project.withModuleSelection(['module1', 'module2'], {
219+
removeModuleDeclarations: true,
220+
});
221+
expectToBeValid(result);
222+
expect(result.sources.map((s) => s.body)).to.deep.equal([
223+
`
224+
# a comment in the keeper file
225+
type Keeper @rootEntity {
226+
key: String @key
227+
}
228+
`,
229+
]);
230+
});
231+
185232
it('removes the modules part in an object file with modules and something else', () => {
186233
const project = new Project({
187234
sources: [

src/project/select-modules-in-sources.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { findDirectiveWithName } from '../schema/schema-utils';
2121
import { Project, ProjectOptions } from './project';
2222
import { ProjectSource } from './source';
2323
import { isReadonlyArray } from '../utils/utils';
24+
import { isCommentOnlySource } from '../graphql/is-comment-only-source';
2425

2526
export interface ModuleSelectionOptions {
2627
/**
@@ -255,6 +256,10 @@ function selectModulesInGraphQLSource({
255256
}
256257
}
257258

259+
if (!changes) {
260+
return source.body;
261+
}
262+
258263
let currentPosition = 0;
259264
let output = '';
260265
for (let i = 0; i <= changes.length; i++) {
@@ -275,6 +280,11 @@ function selectModulesInGraphQLSource({
275280
}
276281
}
277282

283+
// if we removed everything except comments, delete the file
284+
if (isCommentOnlySource(output)) {
285+
return undefined;
286+
}
287+
278288
return output;
279289
}
280290

0 commit comments

Comments
 (0)