Skip to content

Commit a617a47

Browse files
fix: consistently use posix paths for links generated in docs markdown
fixes #259
1 parent 807e7ab commit a617a47

File tree

8 files changed

+47
-20
lines changed

8 files changed

+47
-20
lines changed

src/commands/kit/apply.command.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ export function registerApplyCmd(program: TopLevelCommand) {
7171
// by convention, the module id looks like $platform/...
7272
const platformConfig = foundationRepo.findPlatform(platform);
7373

74-
const { kitModulePath, targetPath } = await applyKitModule(
74+
const { relativeKitModulePath, targetPath } = await applyKitModule(
7575
foundationRepo,
7676
platformConfig.id,
7777
logger,
7878
moduleId,
7979
);
8080

8181
logger.progress(
82-
`applied module ${kitModulePath} to ${
82+
`applied module ${relativeKitModulePath} to ${
8383
collie.relativePath(
8484
targetPath,
8585
)
@@ -111,7 +111,7 @@ export async function applyKitModule(
111111

112112
const factory = new CliApiFacadeFactory(logger);
113113
const tfdocs = factory.buildTerraformDocs(collie);
114-
const kitModulePath = collie.relativePath(
114+
const relativeKitModulePath = collie.relativePath(
115115
collie.resolvePath("kit", moduleId),
116116
);
117117

@@ -138,15 +138,15 @@ export async function applyKitModule(
138138
entries: [
139139
{
140140
name: "terragrunt.hcl",
141-
content: await generateTerragrunt(kitModulePath, tfdocs),
141+
content: await generateTerragrunt(relativeKitModulePath, tfdocs),
142142
},
143143
],
144144
};
145145

146146
await dir.write(platformModuleDir);
147147

148148
return {
149-
kitModulePath,
149+
relativeKitModulePath,
150150
targetPath,
151151
};
152152
}

src/commands/kit/kit-utilities.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { path } from "https://deno.land/x/[email protected]/deps.ts";
21
import {
32
Dir,
43
DirectoryGenerator,
@@ -7,6 +6,8 @@ import {
76
import { Logger } from "../../cli/Logger.ts";
87
import { TerraformDocsCliFacade } from "../../api/terraform-docs/TerraformDocsCliFacade.ts";
98
import { indent } from "../../cli/indent.ts";
9+
import { convertToPosixPath } from "../../path.ts";
10+
import * as path from "std/path";
1011

1112
export async function newKitDirectoryCreation(
1213
modulePath: string,
@@ -97,7 +98,7 @@ export async function generateTerragrunt(
9798
const isBootstrap = kitModulePath.endsWith(`${path.SEP}bootstrap`);
9899

99100
// terragrunt needs a posix style path
100-
const posixKitModulePath = kitModulePath.replaceAll("\\", "/");
101+
const posixKitModulePath = convertToPosixPath(kitModulePath);
101102

102103
const platformIncludeBlock = `include "platform" {
103104
path = find_in_parent_folders("platform.hcl")
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assertEquals } from "std/testing/assert";
22
import { ComplianceControlParser } from "./ComplianceControlParser.ts";
3+
import { isWindows } from "../os.ts";
34

45
Deno.test("parsing with POSIX paths", () => {
56
const path = "compliance/cfmm/iam/identity-lifecycle-management.md";
@@ -8,9 +9,11 @@ Deno.test("parsing with POSIX paths", () => {
89
assertEquals(result, "cfmm/iam/identity-lifecycle-management");
910
});
1011

11-
Deno.test("parsing with windows paths", () => {
12-
const path = "compliance\\cfmm\\iam\\identity-lifecycle-management.md";
13-
const result = ComplianceControlParser.toId(path);
12+
if (isWindows) {
13+
Deno.test("parsing with windows paths", () => {
14+
const path = "compliance\\cfmm\\iam\\identity-lifecycle-management.md";
15+
const result = ComplianceControlParser.toId(path);
1416

15-
assertEquals(result, "cfmm/iam/identity-lifecycle-management");
16-
});
17+
assertEquals(result, "cfmm/iam/identity-lifecycle-management");
18+
});
19+
}

src/compliance/ComplianceControlParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ModelValidator,
1010
} from "../model/schemas/ModelValidator.ts";
1111
import { ComplianceControl } from "./ComplianceControl.ts";
12+
import { convertToPosixPath } from "../path.ts";
1213

1314
// note: this is very much similar to KitModuleParser, maybe there's a common abstraction
1415
// behind that we should use - until then let's wait for rule of three
@@ -89,7 +90,7 @@ export class ComplianceControlParser {
8990
}
9091

9192
static toId(relativeControlPath: string) {
92-
const posixPath = relativeControlPath.replaceAll("\\", "/");
93+
const posixPath = convertToPosixPath(relativeControlPath);
9394

9495
const components = path.parse(posixPath);
9596

src/docs/DocumentationRepository.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as path from "std/path";
22
import { FoundationRepository } from "../model/FoundationRepository.ts";
3+
import { convertToPosixPath } from "../path.ts";
34

4-
// TODO: refactor this to consistently use resolvePath, have no public members for proper encapsulation
55
export class DocumentationRepository {
6+
// DESIGN:
67
// we use a "hidden" directory with a leading "." because terragrunt excludes hidden files and dirs
78
// when building a terragrunt-cache folder, see https://terragrunt.gruntwork.io/docs/reference/config-blocks-and-attributes/#terraform "include_in_copy"
89
// > By default, Terragrunt excludes hidden files and folders during the copy step.
@@ -15,10 +16,17 @@ export class DocumentationRepository {
1516
return this.foundation.resolvePath(this.docsRootDir, ...pathSegments);
1617
}
1718

19+
private makeLink(from: string, to: string) {
20+
// note: in generated markdown we only use POSIX style paths
21+
const relativeLink = path.relative(from, to);
22+
23+
return convertToPosixPath(relativeLink);
24+
}
25+
1826
kitModuleLink(from: string, moduleId: string) {
1927
const kitModulePath = this.resolveKitModulePath(moduleId);
2028

21-
return path.relative(from, kitModulePath);
29+
return this.makeLink(from, kitModulePath);
2230
}
2331

2432
resolveKitModulePath(moduleId: string) {
@@ -28,7 +36,7 @@ export class DocumentationRepository {
2836
controlLink(from: string, controlId: string) {
2937
const controlPath = this.resolveControlPath(controlId);
3038

31-
return path.relative(from, controlPath);
39+
return this.makeLink(from, controlPath);
3240
}
3341

3442
resolveCompliancePath(...pathSegments: string[]) {

src/foundation/FoundationDependenciesTreeBuilder.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "../kit/KitDependencyAnalyzer.ts";
99
import { buildLabeledIdPath, insert } from "../model/tree.ts";
1010
import { CollieRepository } from "../model/CollieRepository.ts";
11+
import { convertToPosixPath } from "../path.ts";
1112

1213
export interface FoundationsTree {
1314
[foundation: string]: FoundationTree;
@@ -68,9 +69,10 @@ export class FoundationDependenciesTreeBuilder {
6869
path.dirname(m.sourcePath),
6970
);
7071

71-
const id = resolvedPlatformModulePath
72-
.substring(resolvedPlatformPath.length + "/".length)
73-
.replaceAll("\\", "/"); // convert to posix style path if required
72+
const relativePlatformModulePath = resolvedPlatformModulePath
73+
.substring(resolvedPlatformPath.length + "/".length);
74+
75+
const id = convertToPosixPath(relativePlatformModulePath);
7476

7577
const label = m.sourcePath;
7678
const labeledComponents = buildLabeledIdPath(id, label);

src/kit/KitModuleParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "../model/schemas/ModelValidator.ts";
1111
import { KitModule } from "./KitModule.ts";
1212
import { ParsedKitModule } from "./ParsedKitModule.ts";
13+
import { convertToPosixPath } from "../path.ts";
1314

1415
export class KitModuleParser {
1516
constructor(
@@ -68,7 +69,7 @@ export class KitModuleParser {
6869
return;
6970
}
7071

71-
const posixRelativeModulePath = relativeModulePath.replaceAll("\\", "/");
72+
const posixRelativeModulePath = convertToPosixPath(relativeModulePath);
7273

7374
return {
7475
id: posixRelativeModulePath.substring("kit/".length),

src/path.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as path from "std/path";
2+
3+
/**
4+
* Converts a relative path with windows or POSIX path separators to a relative path with POSIX separators
5+
* https://stackoverflow.com/questions/53799385/how-can-i-convert-a-windows-path-to-posix-path-using-node-path
6+
* @param relativePath a relative path (this is important!)
7+
* @returns
8+
*/
9+
export function convertToPosixPath(relativePath: string) {
10+
return relativePath.split(path.sep).join(path.posix.sep);
11+
}

0 commit comments

Comments
 (0)