Skip to content

Commit

Permalink
fix(core): fix wrong import path in dts on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Marviuz authored and sdorra committed Apr 1, 2024
1 parent 762e03f commit 73dff85
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-ants-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@content-collections/core": patch
---

Fix wrong import path on windows
84 changes: 83 additions & 1 deletion packages/core/src/writer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { describe, it, expect } from "vitest";
import {
describe,
expect,
vitest,
} from "vitest";
import { createWriter } from "./writer";
import fs from "node:fs/promises";
import { existsSync } from "node:fs";
Expand Down Expand Up @@ -123,4 +127,82 @@ describe("writer", () => {

expect(existsSync(path.join(tmpdir, "index.d.ts"))).toBe(false);
});

describe("os specific", () => {
tmpdirTest(
"should write import with / instead of \\ on windows",
async ({ tmpdir }) => {
vitest.mock("node:path", async (importOriginal) => {
const origin = await importOriginal<typeof import("node:path")>();
return {
default: {
...origin,
sep: origin.win32.sep,
relative: origin.win32.relative,
},
};
});

const collections = [
{
name: "test",
typeName: "Test",
},
];

const writer = await createWriter(tmpdir);
await writer.createTypeDefinitionFile({
collections,
path: path.join(tmpdir, "sub", "config.ts"),
generateTypes: true,
});

const content = await fs.readFile(
path.join(tmpdir, "index.d.ts"),
"utf-8"
);
expect(content).toContain(
'import configuration from "./sub/config.ts";'
);
}
);

tmpdirTest(
"should write import with / on posix based systems",
async ({ tmpdir }) => {
vitest.mock("node:path", async (importOriginal) => {
const origin = await importOriginal<typeof import("node:path")>();
return {
default: {
...origin,
sep: origin.posix.sep,
relative: origin.posix.relative,
},
};
});

const collections = [
{
name: "test",
typeName: "Test",
},
];

const writer = await createWriter(tmpdir);
await writer.createTypeDefinitionFile({
collections,
path: path.join(tmpdir, "sub", "config.ts"),
generateTypes: true,
});

const content = await fs.readFile(
path.join(tmpdir, "index.d.ts"),
"utf-8"
);
expect(content).toContain(
'import configuration from "./sub/config.ts";'
);
}
);
});
});
10 changes: 9 additions & 1 deletion packages/core/src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ type TypeDefinitionFileConfiguration = Pick<
collections: Array<Pick<TransformedCollection, "name" | "typeName">>;
};

function createImportPath(directory: string, target: string) {
let importPath = path.posix.join(...path.relative(directory, target).split(path.sep));
if (!importPath.startsWith(".")) {
importPath = "./" + importPath;
}
return importPath;
}

async function createTypeDefinitionFile(
directory: string,
configuration: TypeDefinitionFileConfiguration
Expand All @@ -76,7 +84,7 @@ async function createTypeDefinitionFile(
return;
}

const importPath = path.relative(directory, configuration.path);
const importPath = createImportPath(directory, configuration.path);
let content = `import configuration from "${importPath}";
import { GetTypeByName } from "@content-collections/core";
`;
Expand Down

0 comments on commit 73dff85

Please sign in to comment.