-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |