|
1 | 1 | import path from 'path';
|
2 |
| -import fs from 'fs'; |
| 2 | +import fs from 'fs/promises'; |
3 | 3 |
|
4 | 4 | function splitCodeIntoBlocks(_text) {
|
5 | 5 | const content = [];
|
@@ -34,25 +34,32 @@ function splitCodeIntoBlocks(_text) {
|
34 | 34 | return content;
|
35 | 35 | }
|
36 | 36 |
|
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('_')); |
40 | 41 |
|
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'); |
51 | 46 |
|
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'); |
54 | 57 |
|
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