Skip to content

Commit b423407

Browse files
authored
Merge pull request #2088 from aeternity/fix-docs
docs: fix example generation by doing files match in js
2 parents 4109373 + 2a0e0e7 commit b423407

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
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/autorest/compiler.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ directive:
1717
1818
version: ^3.7.1
1919
use-extension:
20-
'@autorest/typescript': ^6.0.23
20+
'@autorest/typescript': ^6.0.39
2121
'@autorest/modelerfour': ^4.27.0
2222
# replace with a link to https://github.com/aeternity/aesophia_http/blob/master/config/swagger.yaml
2323
# at specific version after fixing https://github.com/aeternity/aesophia_http/issues/87

tooling/autorest/middleware.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ directive:
186186

187187
version: ^3.7.1
188188
use-extension:
189-
'@autorest/typescript': ^6.0.23
189+
'@autorest/typescript': ^6.0.39
190190
'@autorest/modelerfour': ^4.27.0
191191
input-file: middleware-openapi.yaml
192192
output-folder: ../../src/apis/middleware

tooling/autorest/node.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ directive:
150150

151151
version: ^3.7.1
152152
use-extension:
153-
'@autorest/typescript': ^6.0.23
153+
'@autorest/typescript': ^6.0.39
154154
'@autorest/modelerfour': ^4.27.0
155155
input-file: https://raw.githubusercontent.com/aeternity/aeternity/v7.3.0-rc5/apps/aehttp/priv/oas3.yaml
156156
output-folder: ../../src/apis/node

tooling/autorest/postprocessing.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ await Promise.all(
8181
throw error;
8282
}
8383

84-
// TODO: remove after solving https://github.com/Azure/autorest.typescript/issues/1955
85-
content = content.replaceAll(/ from "\.(.*)\/models";/g, ' from ".$1/models/index";');
86-
content = content.replaceAll(/ from "\.(.+)";/g, ' from ".$1.js";');
87-
8884
if (name === module) {
8985
content = content.replace(/ {2}\$host: string;/, ' readonly $host: string;');
9086

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)