11/**
22 * 批量添加和修改front matter ,需要配置 ./config.yml 文件。
33 */
4- const fs = require ( 'fs' ) ; // 文件模块
5- const path = require ( 'path' ) ; // 路径模块
6- const matter = require ( 'gray-matter' ) ; // front matter解析器 https://github.com/jonschlinkert/gray-matter
4+ const fs = require ( 'fs' ) // 文件模块
5+ const path = require ( 'path' ) // 路径模块
6+ const matter = require ( 'gray-matter' ) // front matter解析器 https://github.com/jonschlinkert/gray-matter
77const jsonToYaml = require ( 'json2yaml' )
88const yamlToJs = require ( 'yamljs' )
99const inquirer = require ( 'inquirer' ) // 命令行操作
1010const chalk = require ( 'chalk' ) // 命令行打印美化
11- const readFileList = require ( './modules/readFileList' ) ;
12- const { type, repairDate} = require ( './modules/fn' ) ;
11+ const readFileList = require ( './modules/readFileList' )
12+ const { type, repairDate } = require ( './modules/fn' )
1313const log = console . log
1414
1515const configPath = path . join ( __dirname , 'config.yml' ) // 配置文件的路径
1616
17- main ( ) ;
17+ main ( )
1818
1919/**
2020 * 主体函数
2121 */
2222async function main ( ) {
23+ const promptList = [
24+ {
25+ type : 'confirm' ,
26+ message : chalk . yellow ( '批量操作frontmatter有修改数据的风险,确定要继续吗?' ) ,
27+ name : 'edit'
28+ }
29+ ]
30+ let edit = true
2331
24- const promptList = [ {
25- type : "confirm" ,
26- message : chalk . yellow ( '批量操作frontmatter有修改数据的风险,确定要继续吗?' ) ,
27- name : "edit" ,
28- } ] ;
29- let edit = true ;
30-
31- await inquirer . prompt ( promptList ) . then ( answers => {
32+ await inquirer . prompt ( promptList ) . then ( ( answers ) => {
3233 edit = answers . edit
3334 } )
3435
35- if ( ! edit ) { // 退出操作
36+ if ( ! edit ) {
37+ // 退出操作
3638 return
3739 }
38-
40+
3941 const config = yamlToJs . load ( configPath ) // 解析配置文件的数据转为js对象
4042
4143 if ( type ( config . path ) !== 'array' ) {
@@ -48,27 +50,26 @@ async function main() {
4850 return
4951 }
5052
51- const filePath = path . join ( __dirname , '..' , ...config . path ) ; // 要批量修改的文件路径
52- const files = readFileList ( filePath ) ; // 读取所有md文件数据
53+ const filePath = path . join ( __dirname , '..' , ...config . path ) // 要批量修改的文件路径
54+ const files = readFileList ( filePath ) // 读取所有md文件数据
5355
54- files . forEach ( file => {
55- let dataStr = fs . readFileSync ( file . filePath , 'utf8' ) ; // 读取每个md文件的内容
56+ files . forEach ( ( file ) => {
57+ let dataStr = fs . readFileSync ( file . filePath , 'utf8' ) // 读取每个md文件的内容
5658 const fileMatterObj = matter ( dataStr ) // 解析md文件的front Matter。 fileMatterObj => {content:'剔除frontmatter后的文件内容字符串', data:{<frontmatter对象>}, ...}
57- let matterData = fileMatterObj . data ; // 得到md文件的front Matter
58-
59+ let matterData = fileMatterObj . data // 得到md文件的front Matter
60+
5961 let mark = false
6062 // 删除操作
6163 if ( config . delete ) {
62- if ( type ( config . delete ) !== 'array' ) {
64+ if ( type ( config . delete ) !== 'array' ) {
6365 log ( chalk . yellow ( '未能完成删除操作,delete字段的值应该是一个数组!' ) )
6466 } else {
65- config . delete . forEach ( item => {
67+ config . delete . forEach ( ( item ) => {
6668 if ( matterData [ item ] ) {
6769 delete matterData [ item ]
6870 mark = true
6971 }
7072 } )
71-
7273 }
7374 }
7475
@@ -77,16 +78,21 @@ async function main() {
7778 Object . assign ( matterData , config . data ) // 将配置数据合并到front Matter对象
7879 mark = true
7980 }
80-
81+
8182 // 有操作时才继续
8283 if ( mark ) {
83- if ( matterData . date && type ( matterData . date ) === 'date' ) {
84+ if ( matterData . date && type ( matterData . date ) === 'date' ) {
8485 matterData . date = repairDate ( matterData . date ) // 修复时间格式
8586 }
86- const newData = jsonToYaml . stringify ( matterData ) . replace ( / \n \s { 2 } / g, "\n" ) . replace ( / " / g, "" ) + '---\r\n' + fileMatterObj . content ;
87- fs . writeFileSync ( file . filePath , newData ) ; // 写入
87+ const newData =
88+ jsonToYaml
89+ . stringify ( matterData )
90+ . replace ( / \n \s { 2 } / g, '\n' )
91+ . replace ( / " / g, '' ) +
92+ '---\r\n' +
93+ fileMatterObj . content
94+ fs . writeFileSync ( file . filePath , newData ) // 写入
8895 log ( chalk . green ( `update frontmatter:${ file . filePath } ` ) )
8996 }
90-
9197 } )
9298}
0 commit comments