Skip to content

Commit

Permalink
chore: add sync mako docs script
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Oct 14, 2024
1 parent 26595ea commit 9cad8f5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ Specify the plugins to use.
};
}) => void;
load?: (filePath: string) => Promise<{ content: string, type: 'css'|'js'|'jsx'|'ts'|'tsx' }>;
loadInclude?: (filePath: string) => boolean;
resolveId?: (id: string, importer: string, { isEntry: bool }) => Promise<{ id: string, external: bool }>;
}
```

Expand Down Expand Up @@ -819,3 +821,4 @@ e.g. If you want to ignore the `foo` directory under root directory, you can set
- Default: `true`

Whether to write the build result to disk when mode is development.

2 changes: 2 additions & 0 deletions docs/zh-CN/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ e.g.
};
}) => void;
load?: (filePath: string) => Promise<{ content: string, type: 'css'|'js'|'jsx'|'ts'|'tsx' }>;
loadInclude?: (filePath: string) => boolean;
resolveId?: (id: string, importer: string, { isEntry: bool }) => Promise<{ id: string, external: bool }>;
}
```

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"scripts": {
"build": "esno scripts/build.ts",
"dev": "nodemon --exec \"npm run build\" --watch . -e \"ejs md ts html\" -i dist -i node_modules",
"deploy": "esno scripts/deploy.ts"
"deploy": "esno scripts/deploy.ts",
"sync:docs": "esno scripts/sync-mako-docs.ts"
},
"devDependencies": {
"@types/ejs": "^3.1.5",
Expand Down
19 changes: 19 additions & 0 deletions plans/plan-sync-mako-docs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

在 scripts 下新增一个 script,文件名为 sync-mako-docs.ts。

1、通过 ts 脚本实现 config.md 和 config.zh-CN.md 的同步。参考其他 scripts 脚本,用 `import 'zx/globals';` 引入 zx 库简化脚本编写
2、同步 mako 仓库下的 docs 下的所有文件到 makojs.dev 仓库下的对应文件里。
需要同步的文件如下:
- docs/config.md (同步到 docs/docs/config.md)
- docs/config.zh-CN.md (同步到 docs/zh-CN/docs/config.md)
3、同步时需保留当前仓库下 md 文档页头 yaml 头信息,如:
```yaml
---
title: Mako - Config
translated_at: '2024-08-25T16:11:18.282Z'
---
```
4、所有逻辑写在一个脚本 ts 文件里,不要拆文件。
5、在 package.json 里新增一个 script,key 为 sync:docs,执行 `npm run sync:docs` 时,会调用 sync-mako-docs.ts 脚本。
6、makojs.dev 仓库路径为当前 cwd。
7、mako 仓库路径从 cwd 下的 .env.json 里获取,key 为 makoRepoPath 。
52 changes: 52 additions & 0 deletions scripts/sync-mako-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'zx/globals';
import * as fs from 'fs';
import * as path from 'path';

// Read .env.json to get mako repo path
const envConfig = JSON.parse(fs.readFileSync('.env.json', 'utf-8'));
const makoRepoPath = envConfig.makoRepoPath;

// Define file mappings
const fileMappings = [
{ src: 'docs/config.md', dest: 'docs/docs/config.md' },
{ src: 'docs/config.zh-CN.md', dest: 'docs/zh-CN/docs/config.md' },
];

async function syncFile(srcPath: string, destPath: string) {
const srcFullPath = path.join(makoRepoPath, srcPath);
const destFullPath = path.join(process.cwd(), destPath);

// Read source file
const srcContent = await fs.promises.readFile(srcFullPath, 'utf-8');

// Read destination file to preserve YAML front matter
let destContent = '';
try {
destContent = await fs.promises.readFile(destFullPath, 'utf-8');
} catch (error) {
console.log(`Destination file ${destPath} not found. Creating new file.`);
}

// Extract YAML front matter from destination file
const yamlMatch = destContent.match(/^---\n([\s\S]*?)\n---/);
const yamlFrontMatter = yamlMatch ? yamlMatch[0] : '';

// Combine YAML front matter with source content
const newContent = yamlFrontMatter ? `${yamlFrontMatter}\n\n${srcContent}` : srcContent;

// Write the combined content to the destination file
await fs.promises.writeFile(destFullPath, newContent, 'utf-8');
console.log(`Synced ${srcPath} to ${destPath}`);
}

async function main() {
for (const mapping of fileMappings) {
await syncFile(mapping.src, mapping.dest);
}
console.log('Sync completed successfully!');
}

main().catch(error => {
console.error('An error occurred:', error);
process.exit(1);
});

0 comments on commit 9cad8f5

Please sign in to comment.