Skip to content

Commit c8a717a

Browse files
committed
chore(docs): fix example generation by doing files match in js
In Ubuntu: ``` sh -c "echo [^b]*" ``` does the same as ``` sh -c "echo b*" ```
1 parent d551b1b commit c8a717a

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"build:api:middleware": "node tooling/autorest/middleware-prepare.js && autorest tooling/autorest/middleware.yaml && node tooling/autorest/postprocessing.js middleware",
3333
"build:generate": "tsx tooling/generate-schema.ts",
3434
"build": "run-p build:api:* build:assets build:generate && run-p build:dist build:es build:types",
35-
"docs:examples": "node tooling/docs/examples-to-md.js examples/node/[^_]*.js",
35+
"docs:examples": "node tooling/docs/examples-to-md.js examples/node",
3636
"docs:api": "typedoc",
3737
"commitlint": "commitlint --from develop",
3838
"lint": "run-p lint:*",

tooling/docs/examples-to-md.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import fs from 'fs';
2+
import fs from 'fs/promises';
33

44
function splitCodeIntoBlocks(_text) {
55
const content = [];
@@ -34,25 +34,32 @@ function splitCodeIntoBlocks(_text) {
3434
return content;
3535
}
3636

37-
process.argv.slice(2).forEach((fileName) => {
38-
const inputFilePath = path.resolve(process.cwd(), fileName);
39-
const text = fs.readFileSync(inputFilePath).toString();
37+
const directory = process.argv[2];
38+
const files = (await fs.readdir(directory))
39+
.filter((file) => file.endsWith('.js'))
40+
.filter((file) => !file.startsWith('_'));
4041

41-
const textMd = splitCodeIntoBlocks(text)
42-
.map(({ type, content }) => ({
43-
type,
44-
content: type === 'code' ? content.replace(/^\n+|\n+$/g, '') : content.replace(/^ /, ''),
45-
}))
46-
.filter(({ type, content }) => type !== 'code' || content)
47-
.filter(({ content }) => !content.includes('License'))
48-
.filter(({ content }) => !content.includes('#!/'))
49-
.map(({ type, content }) => (type === 'code' ? `\`\`\`js\n${content}\n\`\`\`` : content))
50-
.join('\n');
42+
await Promise.all(
43+
files.map(async (fileName) => {
44+
const inputFilePath = path.resolve(process.cwd(), directory, fileName);
45+
const text = await fs.readFile(inputFilePath, 'utf8');
5146

52-
const fileParsedPath = path.parse(path.resolve(process.cwd(), 'docs', fileName));
53-
fs.mkdirSync(fileParsedPath.dir, { recursive: true });
47+
const textMd = splitCodeIntoBlocks(text)
48+
.map(({ type, content }) => ({
49+
type,
50+
content: type === 'code' ? content.replace(/^\n+|\n+$/g, '') : content.replace(/^ /, ''),
51+
}))
52+
.filter(({ type, content }) => type !== 'code' || content)
53+
.filter(({ content }) => !content.includes('License'))
54+
.filter(({ content }) => !content.includes('#!/'))
55+
.map(({ type, content }) => (type === 'code' ? `\`\`\`js\n${content}\n\`\`\`` : content))
56+
.join('\n');
5457

55-
const outputFilePath = path.format({ ...fileParsedPath, base: undefined, ext: '.md' });
56-
fs.writeFileSync(outputFilePath, Buffer.from(textMd));
57-
console.log(`${inputFilePath} -> ${outputFilePath}`);
58-
});
58+
const fileParsedPath = path.parse(path.resolve(process.cwd(), 'docs', fileName));
59+
await fs.mkdir(fileParsedPath.dir, { recursive: true });
60+
61+
const outputFilePath = path.format({ ...fileParsedPath, base: undefined, ext: '.md' });
62+
await fs.writeFile(outputFilePath, Buffer.from(textMd));
63+
console.log(`${inputFilePath} -> ${outputFilePath}`);
64+
}),
65+
);

0 commit comments

Comments
 (0)