Skip to content

Commit b43d2aa

Browse files
committed
feat(config): add unwrapSingleRootDir option
1 parent 1fc9b94 commit b43d2aa

4 files changed

Lines changed: 22 additions & 6 deletions

File tree

src/config-schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const DefaultsSchema = z
2121
maxFiles: z.number().min(1).optional(),
2222
allowHosts: z.array(z.string().min(1)).min(1),
2323
toc: z.union([z.boolean(), TocFormatSchema]).optional(),
24+
unwrapSingleRootDir: z.boolean().optional(),
2425
})
2526
.strict();
2627

src/config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface DocsCacheDefaults {
2525
maxFiles?: number;
2626
allowHosts: string[];
2727
toc?: boolean | TocFormat;
28+
unwrapSingleRootDir?: boolean;
2829
}
2930

3031
export interface DocsCacheSource {
@@ -84,6 +85,7 @@ export const DEFAULT_CONFIG: DocsCacheConfig = {
8485
maxBytes: 200000000,
8586
allowHosts: ["github.com", "gitlab.com"],
8687
toc: true,
88+
unwrapSingleRootDir: false,
8789
},
8890
sources: [],
8991
};
@@ -299,6 +301,13 @@ export const validateConfig = (input: unknown): DocsCacheConfig => {
299301
defaultsInput.toc !== undefined
300302
? (defaultsInput.toc as boolean | TocFormat)
301303
: defaultValues.toc,
304+
unwrapSingleRootDir:
305+
defaultsInput.unwrapSingleRootDir !== undefined
306+
? assertBoolean(
307+
defaultsInput.unwrapSingleRootDir,
308+
"defaults.unwrapSingleRootDir",
309+
)
310+
: defaultValues.unwrapSingleRootDir,
302311
};
303312
} else if (targetModeOverride !== undefined) {
304313
defaults = {
@@ -431,7 +440,8 @@ export const resolveSources = (
431440
maxFiles: source.maxFiles ?? defaults.maxFiles,
432441
integrity: source.integrity,
433442
toc: source.toc ?? defaults.toc,
434-
unwrapSingleRootDir: source.unwrapSingleRootDir,
443+
unwrapSingleRootDir:
444+
source.unwrapSingleRootDir ?? defaults.unwrapSingleRootDir,
435445
}));
436446
};
437447

src/materialize.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ export const materializeSource = async (params: MaterializeParams) => {
204204
resolved.unwrapSingleRootDir,
205205
);
206206
const targetDirs = new Set<string>();
207-
for (const { relativePath, normalized } of entries) {
207+
for (const { normalized } of entries) {
208208
const rootPath = unwrapPrefix
209209
? normalized.replace(unwrapPrefix, "")
210-
: relativePath;
211-
targetDirs.add(path.dirname(rootPath));
210+
: normalized;
211+
targetDirs.add(path.posix.dirname(rootPath));
212212
}
213213
await Promise.all(
214214
Array.from(targetDirs, (dir) =>

src/sync.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,14 @@ const normalizeRulesValue = (
133133
};
134134

135135
const computeRulesHash = (source: DocsCacheResolvedSource) => {
136-
const payload = Object.fromEntries(
137-
RULES_HASH_KEYS.map((key) => [key, normalizeRulesValue(key, source[key])]),
136+
const entries = RULES_HASH_KEYS.map((key) => [
137+
key,
138+
normalizeRulesValue(key, source[key]),
139+
]) as Array<[string, unknown]>;
140+
entries.sort(([left]: [string, unknown], [right]: [string, unknown]) =>
141+
left.localeCompare(right),
138142
);
143+
const payload = Object.fromEntries(entries);
139144
const hash = createHash("sha256");
140145
hash.update(JSON.stringify(payload));
141146
return hash.digest("hex");

0 commit comments

Comments
 (0)