Skip to content

Commit 8f73199

Browse files
alan-agius4alxhub
authored andcommitted
fix(docs-infra): update getAnswerFiles to ensure compatibility with non-POSIX file systems (angular#57970)
This update modifies the `getAnswerFiles` function to support file systems that do not adhere to POSIX standards. Prior to this change this method will always fail with the below: ``` Error: Invalid state: could not find start of answers path ``` PR Close angular#57970
1 parent ff028b8 commit 8f73199

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

adev/shared-docs/pipeline/tutorials/metadata.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,27 @@ async function getAnswerFiles(
4242
files: FileAndContentRecord,
4343
): Promise<FileAndContentRecord> {
4444
const answerFiles: FileAndContentRecord = {};
45-
const answerPrefix = 'answer/';
45+
const answerPrefix = /answer[\\/]/;
4646

4747
if (config.answerSrc) {
4848
const answersDir = join(path, config.answerSrc);
4949
const answerFilePaths = await glob('**/*', {
5050
cwd: answersDir,
5151
onlyFiles: true,
52-
absolute: true,
52+
absolute: false,
5353
});
54-
answerFilePaths.forEach((absolutePath) => {
54+
55+
for (const answerFile of answerFilePaths) {
5556
// We use the absolute file in order to read the content, but the key
5657
// needs to be a relative path within the project.
57-
const parentDir = dirname(answersDir) + '/';
58-
const pathStart = absolutePath.indexOf(parentDir);
59-
if (pathStart === -1) {
60-
throw new Error('Invalid state: could not find start of answers path');
61-
}
62-
answerFiles[absolutePath.slice(pathStart + parentDir.length)] = getFileContents(absolutePath);
63-
});
58+
answerFiles[answerFile] = getFileContents(join(answersDir, answerFile));
59+
}
6460
} else {
65-
Object.keys(files).forEach((file) => {
66-
if (file.includes(answerPrefix)) {
61+
for (const file of Object.keys(files)) {
62+
if (answerPrefix.test(file)) {
6763
answerFiles[file.replace(answerPrefix, '')] = files[file];
6864
}
69-
});
65+
}
7066
}
7167

7268
return answerFiles;

0 commit comments

Comments
 (0)