Skip to content

Commit

Permalink
allow providing config via constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jul 7, 2021
1 parent eecdffc commit 44723c5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
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 ?? {};
}

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
22 changes: 22 additions & 0 deletions packages/loaders/code-file/tests/load-from-code-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,26 @@ describe('loadFromCodeFileSync', () => {
"
`);
});

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
}
}
"
`);
})
});

0 comments on commit 44723c5

Please sign in to comment.