Skip to content

Commit 609b066

Browse files
buzzie-beemsutkowskiphryneas
authored
Fix #32 by appending ./ to relative imports from the same folder (#38)
Co-authored-by: Matt Sutkowski <[email protected]> Co-authored-by: Lenz Weber <[email protected]>
1 parent 2083937 commit 609b066

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/utils/resolveImportPath.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ import { stripFileExtension } from './stripFileExtension';
33

44
export function resolveImportPath(modulePath: string, containingFile: string) {
55
containingFile = path.resolve(containingFile, '..');
6-
return stripFileExtension(path.relative(containingFile, modulePath));
6+
const strippedFile = stripFileExtension(path.relative(containingFile, modulePath));
7+
if (strippedFile.charAt(0) !== '.') {
8+
return `./${strippedFile}`;
9+
}
10+
return strippedFile;
711
}

test/cli.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ function getTmpFileName() {
1212
return path.resolve(tmpDir, `${++id}.test.generated.ts`);
1313
}
1414

15+
function copyAndGetTmpFileName(fileToCopyPath: string, newFileName: string): string {
16+
newFileName = `${++id}.test.${newFileName}`;
17+
const newFilePath = path.resolve(tmpDir, newFileName);
18+
fs.copyFileSync(fileToCopyPath, newFilePath, fs.constants.COPYFILE_EXCL);
19+
return newFileName;
20+
}
21+
1522
function cli(args: string[], cwd: string): Promise<{ error: ExecException | null; stdout: string; stderr: string }> {
1623
return new Promise((resolve) => {
1724
exec(
@@ -171,6 +178,34 @@ describe('CLI options testing', () => {
171178
expect(numberOfErrors).toEqual(0);
172179
});
173180

181+
it("should import { default as customBaseQuery } from './customBaseQuery' when a local customBaseQuery is provided to --baseQuery", async () => {
182+
const localBaseQueryName = copyAndGetTmpFileName('./test/fixtures/customBaseQuery.ts', 'localCustomBaseQuery.ts');
183+
const fileName = getTmpFileName();
184+
const result = await cli(
185+
[
186+
'-h',
187+
`--baseQuery`,
188+
`./test/tmp/${localBaseQueryName}:namedBaseQuery`,
189+
'--file',
190+
fileName,
191+
`./test/fixtures/petstore.json`,
192+
],
193+
'.'
194+
);
195+
196+
const output = fs.readFileSync(fileName, { encoding: 'utf-8' });
197+
198+
const strippedLocalBaseQueryName = path.parse(localBaseQueryName).name;
199+
200+
expect(output).not.toContain('fetchBaseQuery');
201+
expect(output).toContain(`import { namedBaseQuery } from './${strippedLocalBaseQueryName}'`);
202+
203+
const expectedErrors = [MESSAGES.NAMED_EXPORT_MISSING];
204+
205+
const numberOfErrors = expectedErrors.filter((msg) => result.stderr.indexOf(msg) > -1).length;
206+
expect(numberOfErrors).toEqual(0);
207+
});
208+
174209
it('should error out when the specified with path alias is not found', async () => {
175210
const result = await cli(
176211
['-h', `--baseQuery`, `@/hoge/fuga/nonExistantFile`, `./test/fixtures/petstore.json`],

0 commit comments

Comments
 (0)