Permalink
Browse files

文章生成及路由渲染命令

npm run article -- --new 'fileName'  |  new article
npm run article -- -n 'fileName'     |  --new alias
npm run article -- --render          |  render router
npm run article -- -r                |  --render alias
npm run article -- --help            |  help
npm run article -- -h                |  --help alias
  • Loading branch information...
1 parent 41fab22 commit bcc54e6713ecf1f6febe22011e7d20d934f0ccbd @leenty committed Jan 10, 2017
Showing with 102 additions and 34 deletions.
  1. +47 −22 build/create-router.js
  2. +50 −10 build/new-article.js
  3. +1 −0 package.json
  4. +1 −1 src/articleList.json
  5. +2 −0 src/articlesPreRender.js
  6. +1 −1 src/articlesRoutes.js
@@ -1,10 +1,12 @@
-// node build/new-article.js --new 'vue2-7'
-
var path = require('path')
var fs = require("fs")
const relativePath = '../src/md/articles/'
+const replaceArticleName = function (articleName) {
+ return articleName.replace(' ', '_').replace('-', '_')
+}
+
const getArticleList = function () {
let prm = new Promise((resolve, reject) => {
fs.readdir(path.join(__dirname, relativePath), function(err, files){
@@ -30,30 +32,11 @@ const getArticleInfo = function (name) {
return Promise.all(prmArr)
}
-const createArticleList = function (infoArr) {
- let prm = new Promise((resolve, reject) => {
- let routesStr = '['
- infoArr.forEach((v, k) => {
- let articleName = v.fileName.replace(' ', '_').replace('-', '_')
- routesStr += `${k ? ', ' : ''}{`
- + `"name": "${articleName}", `
- + `"title": "${v.title}"`
- + `}`
- })
- routesStr += ']'
- fs.writeFile(path.join(__dirname, `../src/articleList.json`), routesStr, err => {
- err && reject(err)
- resolve('\n文章列表生成完毕!')
- })
- })
- return prm
-}
-
const createRoutes = function (infoArr) {
let prm = new Promise((resolve, reject) => {
let routesStr = 'const articlesRouter = ['
infoArr.forEach((v, k) => {
- let articleName = v.fileName.replace(' ', '_').replace('-', '_')
+ let articleName = replaceArticleName(v.fileName)
routesStr += `${k ? ', ' : ''}{`
+ `name: '${articleName}', `
+ `path: '/${v.date}/${articleName}', `
@@ -74,11 +57,53 @@ const generateRouter = function (infoArr) {
let prmArr = []
prmArr.push(createRoutes(infoArr))
prmArr.push(createArticleList(infoArr))
+ prmArr.push(createPreRender(infoArr))
return Promise.all(prmArr)
}
+/*****************************/
+
+const createArticleList = function (infoArr) {
+ let prm = new Promise((resolve, reject) => {
+ let routesStr = '['
+ infoArr.forEach((v, k) => {
+ let articleName = replaceArticleName(v.fileName)
+ routesStr += `${k ? ', ' : ''}{`
+ + `"name": "${articleName}", `
+ + `"title": "${v.title}"`
+ + `}`
+ })
+ routesStr += ']'
+ fs.writeFile(path.join(__dirname, `../src/articleList.json`), routesStr, err => {
+ err && reject(err)
+ resolve('\n文章列表生成完毕!')
+ })
+ })
+ return prm
+}
+
+/*******************************/
+
+const createPreRender = function (infoArr) {
+ let prm = new Promise((resolve, reject) => {
+ let preRenderArr = 'const preRenderArr = ['
+ infoArr.forEach((v, k) => {
+ let articleName = replaceArticleName(v.fileName)
+ preRenderArr += `${k ? ', ' : ''}'/${v.date}/${articleName}'`
+ })
+ preRenderArr += `]\n export default preRenderArr`
+ fs.writeFile(path.join(__dirname, `../src/articlesPreRender.js`), preRenderArr, err => {
+ err && reject(err)
+ resolve('\n预渲染路由生成完毕!')
+ })
+ })
+ return prm
+}
+
exports.createArticleList = createArticleList
+exports.createPreRender = createPreRender
+
exports.createRouter = function () {
getArticleList()
.then(getArticleInfo, console.log)
View
@@ -1,4 +1,4 @@
-// node build/new-article.js --new 'vue2-7'
+// node build/new-article.js --new 'fileName'
var path = require('path')
var fs = require("fs")
@@ -26,7 +26,7 @@ Date.prototype.format = function(fmt) {
return fmt
}
-var argv = require('minimist')(process.argv.slice(2))
+var arg = require('minimist')(process.argv.slice(2))
/***********************/
const setAritcleInfo = function (name) {
@@ -40,17 +40,57 @@ const setAritcleInfo = function (name) {
}
const createArticle = function (name) {
- const articlePath = path.join(__dirname, `${relativePath}${name}.md`)
- fs.writeFile(articlePath, setAritcleInfo(name), err => {
- err && console.log(err)
- console.log(`新建文章${name}\n路径: ${articlePath}`)
+ let prm = new Promise((resolve, reject) => {
+ if (!name || name == 0) {
+ reject(
+ `错误的文件名 ${name}` +
+ `\n请使用:` +
+ `\n npm run article -- --new 'fileName'` +
+ `创建文章`
+ )
+ } else {
+ const articlePath = path.join(__dirname, `${relativePath}${name}.md`)
+ fs.writeFile(articlePath, setAritcleInfo(name), err => {
+ err && reject(err)
+ resolve(`新建文章${name}\n路径: ${articlePath}`)
+ })
+ }
})
+ return prm
}
-;(function (arg) {
- createArticle(arg.n ? arg.n : arg.new)
- router.createRouter()
-})(argv)
+const checkArg = function () {
+ if (arg.n || arg.new) {
+ return {
+ type: 'new',
+ arg: arg.new ? arg.new : arg.n
+ }
+ }
+ if (arg.r || arg.render) {
+ return {
+ type: 'render',
+ arg: arg.render ? arg.render : arg.r
+ }
+ }
+ console.log(`npm run article -- --new 'fileName' | new article\n`
+ + `npm run article -- -n 'fileName' | --new alias\n`
+ + `npm run article -- --render | render router\n`
+ + `npm run article -- -r | --render alias\n`
+ + `npm run article -- --help | help\n`
+ + `npm run article -- -h | --help alias\n`);
+ return false
+}
+
+;(function () {
+ let argObj = checkArg()
+ argObj &&
+ (argObj.type === 'new'
+ ? createArticle(argObj.arg).then(done => {
+ console.log(done)
+ return router.createRouter()
+ }).catch(console.log)
+ : router.createRouter())
+})()
// var temp = require('../src/articles.json')
// console.log(temp, typeof temp)
View
@@ -7,6 +7,7 @@
"scripts": {
"deploy": "node build/deploy.js",
"dev": "node build/dev-server.js",
+ "article": "node build/new-article.js",
"dist": "node build/dist-server.js",
"build": "node build/build.js",
"unit": "karma start test/unit/karma.conf.js --single-run",
@@ -1 +1 @@
-[{"name": "vue2_1", "title": "vue2.0一起在懵逼的海洋里越陷越深(一)"}, {"name": "vue2_2", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_3", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_4", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_5", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_6", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_7", "title": "vue2-7"}]
+[{"name": "vue1", "title": "vue1"}, {"name": "vue2_1", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_2", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_3", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_4", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_5", "title": "vue2.0一起在懵逼的海洋里越陷越深("}, {"name": "vue2_6", "title": "vue2.0一起在懵逼的海洋里越陷越深(六)"}]
@@ -0,0 +1,2 @@
+const preRenderArr = ['/2017/01/08/vue1', '/2016/10/21/vue2_1', '/2016/11/20/vue2_2', '/2016/11/27/vue2_3', '/2016/12/04/vue2_4', '/2016/12/11/vue2_5', '/2016/12/18/vue2_6']
+ export default preRenderArr
@@ -1,2 +1,2 @@
-const articlesRouter = [{name: 'vue2_1', path: '/2016/10/21/vue2_1', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(一)'}, component: require('./md/articles/vue2-1.md')}, {name: 'vue2_2', path: '/2016/11/20/vue2_2', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(二)'}, component: require('./md/articles/vue2-2.md')}, {name: 'vue2_3', path: '/2016/11/27/vue2_3', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(三)'}, component: require('./md/articles/vue2-3.md')}, {name: 'vue2_4', path: '/2016/12/04/vue2_4', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(四)'}, component: require('./md/articles/vue2-4.md')}, {name: 'vue2_5', path: '/2016/12/11/vue2_5', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(五)'}, component: require('./md/articles/vue2-5.md')}, {name: 'vue2_6', path: '/2016/12/18/vue2_6', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(六)'}, component: require('./md/articles/vue2-6.md')}, {name: 'vue2_7', path: '/2017/01/07/vue2_7', meta: {title: 'vue2-7'}, component: require('./md/articles/vue2-7.md')}]
+const articlesRouter = [{name: 'vue1', path: '/2017/01/08/vue1', meta: {title: 'vue1'}, component: require('./md/articles/vue1.md')}, {name: 'vue2_1', path: '/2016/10/21/vue2_1', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(一)'}, component: require('./md/articles/vue2-1.md')}, {name: 'vue2_2', path: '/2016/11/20/vue2_2', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(二)'}, component: require('./md/articles/vue2-2.md')}, {name: 'vue2_3', path: '/2016/11/27/vue2_3', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(三)'}, component: require('./md/articles/vue2-3.md')}, {name: 'vue2_4', path: '/2016/12/04/vue2_4', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(四)'}, component: require('./md/articles/vue2-4.md')}, {name: 'vue2_5', path: '/2016/12/11/vue2_5', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(五)'}, component: require('./md/articles/vue2-5.md')}, {name: 'vue2_6', path: '/2016/12/18/vue2_6', meta: {title: 'vue2.0一起在懵逼的海洋里越陷越深(六)'}, component: require('./md/articles/vue2-6.md')}]
export default articlesRouter

0 comments on commit bcc54e6

Please sign in to comment.