Skip to content

Commit

Permalink
增加hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
tedjmzhang committed Jun 3, 2024
1 parent c657ecf commit ccb5b19
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 59 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"scripts": {
"docs:dev": "vitepress dev",
"docs:build": "vitepress build",
"docs:build": "node ./src/hooks.js && vitepress build",
"docs:preview": "vitepress preview"
},
"dependencies": {
Expand All @@ -16,7 +16,7 @@
"@metalsmith/layouts": "^2.7.0",
"@metalsmith/markdown": "^1.10.0",
"@metalsmith/permalinks": "^3.0.1",
"chalk": "^5.3.0",
"chalk": "^5.3.0",
"commander": "^12.1.0",
"gogocode": "^1.0.55",
"handlebars": "^4.7.8",
Expand Down
49 changes: 0 additions & 49 deletions src/api-examples.md

This file was deleted.

1 change: 1 addition & 0 deletions src/frontend/library/commander/commander.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
const { program } = require('commander');


Expand Down
6 changes: 3 additions & 3 deletions src/frontend/library/commander/commander.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# commander.js

## 名词概念
1. 选项(options):终端传递的以`-`或者`--`开头的参数。
1. 选项(option):终端传递的以`-`或者`--`开头的参数。
2. 命令(command):终端可以调用的命令,例如git add .中的add
3. 子命令(sub-command):一个独立的文件,名称默认为`主命令文件名称-子命令名称`
4. 选项参数,命令参数:跟在选项或者命令之后的参数,<>表示必选,[]表示可选,终端没出现该选项名称或者命令名称时候,默认为undefined
Expand Down Expand Up @@ -29,7 +29,7 @@ node ./commander.js start clone placeA placeB // 会调用同级目录下面的c
```
const { program } = require('commander'); // program是一个全局的Command实例
program.options("-s --search") // 返回全局Command实例
program.option("-s --search") // 返回全局Command实例
.command('commandA") // 创建一个CommandA实例
.command('commandB") // 在CommandA实例下面创建一个新的CommandB实例
Expand All @@ -41,6 +41,6 @@ program.command('commandC') //在全局Command下面创建一个新的CommandC
[具体见官方文档](https://github.com/tj/commander.js/blob/HEAD/Readme_zh-CN.md)
- option
- command
- action 参数为命令的所有参数,外加上解析出来的options和command实例
- action 参数为命令的所有参数,外加上解析出来的option和command实例
- version
- hook
2 changes: 1 addition & 1 deletion src/frontend/library/gogocode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let ast = gogocode(string) // 生成gogoCode的ast实例

## 实例方法: 全部返回的都是gogocode实例,只不过他们的match不一样而已,返回值是一个对象,但是有each方法,返回匹配的多个值,直接调用match只会返回匹配的第一个值(改值是一个对象,对象的key是$_$name里面的name)
- find: 匹配某格式文本,并且将其命名,ast.find('const $_$key = $_$value'), 可以使用.match或者(.each(item,index => item.match)index为1是表示第一个)获取第一个匹配的,如果有多个匹配,只能.each(item => item.match)
- find: 匹配某格式文本,并且将其命名,ast.find('const $\_$key = $\_$value'), 可以使用.match或者(.each(item,index => item.match)index为1是表示第一个)获取第一个匹配的,如果有多个匹配,只能.each(item => item.match)

- replaceBy: find之后直接替换

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/library/inquirer/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# inquirer

命令行交互库
命令行交互库, 统一所有交互,然后再后面的then作为返回值处理
51 changes: 51 additions & 0 deletions src/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env node

let { program } = require("commander");
let { readFileSync, readdirSync, writeFileSync } = require("fs");
let path = require('path');

program.command('prebuild').description('发布之前执行的hook')
.action(() => {
appendJSToMD(__dirname)
})


function appendJSToMD(dirPath) {
console.log(__dirname)
let currentDirName = path.basename(dirPath)
let dirInfoList = readdirSync(dirPath, { withFileTypes: true });
// 该文件夹下面有index.js 或者dirname.js 同时该文件下面有index.md
let files = dirInfoList.filter(item => item.isFile());
let dirs = dirInfoList.filter(item => item.isDirectory());
let origin = null, destination = null;
files.forEach(item => {
if (['index.md', `${currentDirName}.md`].includes(item.name)) {
destination = item
return;
}
if (['index.js', `${currentDirName}.js`, 'index.mjs', `${currentDirName}.mjs`].includes(item.name)) {
origin = item;
return;
}
})

if (origin && destination) {
let originText = readFileSync(path.resolve(dirPath, origin.name));
writeFileSync(path.resolve(dirPath, "index-source.md"), `
\`\`\`
${originText}
\`\`\`
`)
}

dirs.forEach(item => {
appendJSToMD(path.resolve(dirPath, item.name))
})



console.log('dir', dirInfoList);
}


appendJSToMD(__dirname);
2 changes: 0 additions & 2 deletions src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ hero:
actions:
- theme: brand1
text: Markdown Examples
link: /markdown-examples
- theme: alt
text: API Examples
link: /api-examples

features:
- title: Feature A1
Expand Down
1 change: 0 additions & 1 deletion src/markdown-examples.md

This file was deleted.

0 comments on commit ccb5b19

Please sign in to comment.