Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure indent is covered via test #3162

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-apes-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/code-file-loader': minor
---

allow supplying config via constructor
29 changes: 25 additions & 4 deletions packages/loaders/code-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ import { createRequire } from 'module';

const { readFile, access } = fsPromises;

export type CodeFileLoaderConfig = {
pluckConfig?: GraphQLTagPluckOptions;
noPluck?: boolean;
noRequire?: boolean;
};

/**
* Additional options for loading from a code file
*/
export type CodeFileLoaderOptions = {
require?: string | string[];
pluckConfig?: GraphQLTagPluckOptions;
noPluck?: boolean;
noRequire?: boolean;
} & BaseLoaderOptions;
} & CodeFileLoaderConfig &
BaseLoaderOptions;

const FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.vue'];

Expand All @@ -57,11 +61,21 @@ function createGlobbyOptions(options: CodeFileLoaderOptions): GlobbyOptions {
* Supported extensions include: `.ts`, `.tsx`, `.js`, `.jsx`, `.vue`
*/
export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
private config: CodeFileLoaderConfig;
constructor(config?: CodeFileLoaderConfig) {
this.config = config ?? {};
}
ardatan marked this conversation as resolved.
Show resolved Hide resolved

private getMergedOptions(options: CodeFileLoaderOptions): CodeFileLoaderOptions {
return { ...this.config, ...options };
}

loaderId(): string {
return 'code-file';
}

async canLoad(pointer: string, options: CodeFileLoaderOptions): Promise<boolean> {
options = this.getMergedOptions(options);
if (isGlob(pointer)) {
// FIXME: parse to find and check the file extensions?
return true;
Expand All @@ -83,6 +97,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}

canLoadSync(pointer: string, options: CodeFileLoaderOptions): boolean {
options = this.getMergedOptions(options);
if (isGlob(pointer)) {
// FIXME: parse to find and check the file extensions?
return true;
Expand All @@ -99,16 +114,19 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}

async resolveGlobs(glob: string, options: CodeFileLoaderOptions) {
options = this.getMergedOptions(options);
const ignores = asArray(options.ignore || []);
return globby([glob, ...ignores.map(v => `!(${v})`).map(v => unixify(v))], createGlobbyOptions(options));
}

resolveGlobsSync(glob: string, options: CodeFileLoaderOptions) {
options = this.getMergedOptions(options);
const ignores = asArray(options.ignore || []);
return globby.sync([glob, ...ignores.map(v => `!(${v})`).map(v => unixify(v))], createGlobbyOptions(options));
}

async load(pointer: string, options: CodeFileLoaderOptions): Promise<Source[] | null> {
options = this.getMergedOptions(options);
if (isGlob(pointer)) {
const resolvedPaths = await this.resolveGlobs(pointer, options);
const finalResult: Source[] = [];
Expand All @@ -127,6 +145,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}

loadSync(pointer: string, options: CodeFileLoaderOptions): Source[] | null {
options = this.getMergedOptions(options);
if (isGlob(pointer)) {
const resolvedPaths = this.resolveGlobsSync(pointer, options);
const finalResult: Source[] = [];
Expand All @@ -143,6 +162,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}

async handleSinglePath(location: string, options: CodeFileLoaderOptions): Promise<Source[] | null> {
options = this.getMergedOptions(options);
const normalizedFilePath = ensureAbsolutePath(location, options);

const errors: Error[] = [];
Expand Down Expand Up @@ -192,6 +212,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}

handleSinglePathSync(location: string, options: CodeFileLoaderOptions): Source[] | null {
options = this.getMergedOptions(options);
const normalizedFilePath = ensureAbsolutePath(location, options);

const errors: Error[] = [];
Expand Down
61 changes: 46 additions & 15 deletions packages/loaders/code-file/tests/load-from-code-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,61 @@ describe('loadFromCodeFileSync', () => {
it('should support loading many in same file', () => {
const loaded = loader.loadSync('./test-files/multiple-from-file.ts', {
cwd: __dirname,
pluckConfig: {
skipIndent: true,
},
});
expect(loaded?.length).toEqual(3);
expect(loaded?.[0].rawSDL).toBeDefined();
expect(loaded?.[0].rawSDL).toMatchInlineSnapshot(`
"query Foo {
Tweets {
id
"
query Foo {
Tweets {
id
}
}
}"
"
`);
expect(loaded?.[1].rawSDL).toBeDefined();
expect(loaded?.[1].rawSDL).toMatchInlineSnapshot(`
"fragment Lel on Tweet {
id
body
}"
`);
"
fragment Lel on Tweet {
id
body
}
"
`);
expect(loaded?.[2].rawSDL).toBeDefined();
expect(loaded?.[2].rawSDL).toMatchInlineSnapshot(`
"query Bar {
Tweets {
...Lel
}
}"
`);
"
query Bar {
Tweets {
...Lel
}
}
"
`);
});

it('can inherit config options from constructor', () => {
const loader = new CodeFileLoader({
pluckConfig: {
skipIndent: true
}
})
const loaded = loader.loadSync('./test-files/multiple-from-file.ts', {
cwd: __dirname,
});
expect(loaded?.length).toEqual(3);
expect(loaded?.[0].rawSDL).toBeDefined();
expect(loaded?.[0].rawSDL).toMatchInlineSnapshot(`
"
query Foo {
Tweets {
id
}
}
"
`);
})
});