Skip to content

Commit

Permalink
feat(jekyll-md): 新增生成带front matter的markdown文件命令
Browse files Browse the repository at this point in the history
  • Loading branch information
StreakingMan committed May 15, 2022
1 parent ca606a6 commit a8b8f69
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ yarn global add streakingman-cli
- [set-git: 设置当前仓库 git 配置名称和邮箱](#set-git)
- [set-project: 快速配置项目](#set-project)
- [react-comp: 快速生成 react 组件相关文件](#react-comp)
- [jekyll-md: 生成带front matter的markdown文件](#jekyll-md)

## 使用

Expand Down Expand Up @@ -127,3 +128,37 @@ export interface MyCompProps {
className?: string
}
```

### jekyll-md

```
skm jekyll-md
? 请输入标签 标题
? 请输入分类 分类
? 请输入标签(用英文逗号分隔标签)标签1,标签2,标签3
或者
skm jekyll-md 标题 分类 标签1,标签2,标签3
```

生成如下md文件

2022-05-15-标题.md
```
---
layout: post
title: 标题
author: Max
categories: 分类
tags: 标签1 标签2 标签3
---
generated at <%- generatedAt %> by streakingman-cli@<%- version %>
```

## TODO

- [ ] 测试用例
- [ ] 使用ts重写
39 changes: 39 additions & 0 deletions actions/jekyll-md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const inquirer = require('inquirer');
const { fileGenerator } = require('../utils/file-generator');
const jekyllMD = async (title, category, tags) => {
if (!(title && category && tags)) {
const { inputTitle, inputCategory, inputTags } = await inquirer.prompt([
{
type: 'input',
name: 'inputTitle',
message: '请输入标题',
default: title || '',
},
{
type: 'input',
name: 'inputCategory',
message: '请输入分类',
default: category || '',
},
{
type: 'input',
name: 'inputTags',
message: '请输入标签(用英文逗号分隔标签)',
default: tags || '',
},
]);
title = inputTitle;
category = inputCategory;
tags = inputTags;
}

tags = tags.split(',').join(' ');
fileGenerator({
templateName: 'jekyllMarkdown',
option: { title, category, tags },
});
};

module.exports = {
jekyllMD,
};
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { checkGit } = require('./actions/check-git');
const { setGit } = require('./actions/set-git');
const { setProject } = require('./actions/set-project');
const { reactComp } = require('./actions/react-comp');
const { jekyllMD } = require('./actions/jekyll-md');
const { program } = require('commander');
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -33,6 +34,10 @@ program
.command('react-comp [compName]')
.description('快速生成react组件相关文件')
.action(reactComp);
program
.command('jekyll-md [title] [category] [tags]')
.description('生成带front matter的markdown文件')
.action(jekyllMD);

program.showHelpAfterError(`${CLINAME} -h 查看帮助`);
program.addHelpCommand(false);
Expand Down
10 changes: 10 additions & 0 deletions templates/jekyllMarkdown.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
layout: post
title: <%- title %>
author: Max
categories: <%- category %>
tags: <%- tags %>
---


generated at <%- generatedAt %> by streakingman-cli@<%- version %>
34 changes: 32 additions & 2 deletions utils/file-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ const fse = require('fs-extra');
const path = require('path');
const ejs = require('ejs');

const jekyllMarkdownName = (title) => {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const date = now.getDate();
return `${year}-${month.padStart(2, '0')}-${date.padStart(
2,
'0'
)}-${title.toLowerCase()}`;
};

const generatorTemplateFileMap = {
commitlint: '.commitlintrc.js',
editor: '.editorconfig',
Expand All @@ -16,10 +27,21 @@ const generatorTemplateFileMap = {
reactCompIndex: 'index.tsx',
reactCompStyle: 'index.module.scss',
reactCompInterface: 'interface.ts',
jekyllMarkdown: jekyllMarkdownName,
};

const fileGenerator = ({ templateName, pathName = '/', option = {} }) => {
const { prettier, stylelint, react, ts, compName, CompName } = option;
const {
prettier,
stylelint,
react,
ts,
compName,
CompName,
title,
category,
tags,
} = option;
const cwd = process.cwd();
const file = path.join(
path.join(__dirname, '../templates'),
Expand All @@ -33,11 +55,19 @@ const fileGenerator = ({ templateName, pathName = '/', option = {} }) => {
stylelint,
generatedAt: new Date().toLocaleString(),
version,
// react-comp
compName,
CompName,
// jekyllMarkdown
title,
category,
tags,
};

const filename = generatorTemplateFileMap[templateName];
let filename = generatorTemplateFileMap[templateName];
if (typeof filename === 'function') {
filename = filename(title);
}

fse.outputFileSync(
path.join(path.join(cwd, pathName), filename),
Expand Down

0 comments on commit a8b8f69

Please sign in to comment.