diff --git a/.bin/idoc.js b/.bin/idoc.js index 34cc34de..7aeb4e24 100755 --- a/.bin/idoc.js +++ b/.bin/idoc.js @@ -15,11 +15,12 @@ commander commander .option("-C, --Create ", "Select Directory Makefile.") .option("-v", "App version information.") - .option('-i, init','init a documentation.') + .option('-i, init','Init a documentation.') .option('-b, build','Markdown produces static pages document.') .option('-w, watch','Listener "md" file is automatically generated pages.') .option('-s, server','Open local static html server.') .option('-c, clean','Clear the generate static files.') + .option('-t, theme','Choose a theme.') .option('-d, deploy','Publish to a gh-pages branch on GitHub.') commander @@ -33,6 +34,8 @@ commander log(' $ idoc server'); log(' $ idoc clean'); log(' $ idoc deploy'); + log(' $ idoc theme'); + log(' $ idoc -t ~/git/idoc-theme-slate/'); // 图片文字 http://patorjk.com/software/taag/#p=testall&f=Graffiti&t=idoc diff --git a/index.js b/index.js index f69def4b..44638bbd 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ var watch = require('./lib/watch'); var file = require('./lib/file'); var clean = require('./lib/clean'); var deploy = require('./lib/deploy'); +var theme = require('./lib/theme'); var color = require('colorful'); var path = require('path'); var server = require('ssr'); @@ -44,5 +45,8 @@ module.exports = function(commander){ if(commander.deploy){ return deploy(commander); } + if(commander.theme){ + return theme.runTask(commander); + } commander.outputHelp(); } \ No newline at end of file diff --git a/lib/build.js b/lib/build.js index 27a91071..c2c0c703 100644 --- a/lib/build.js +++ b/lib/build.js @@ -19,8 +19,18 @@ function build(commander,changeFile){ // 载入配置文件 var pkg = require(path.resolve('package.json')); - // 模板目录 - template = path.dirname(__dirname) + '/theme/' + pkg.idoc.theme; + + // 指定模板目录 + if(commander.theme && commander.args.length > 0) template = commander.args[0]; + else{ + // 模板目录 + template = path.dirname(__dirname) + '/theme/' + pkg.idoc.theme; + if(!file.isDir(template)){ + template = process.cwd()+ '/' +pkg.idoc.theme; + } + } + // 皮肤目录不存在判断 + if(!file.isDir(template)) return log(color.red('\nTheme directory does not exist.!\n') ) var name = pkg.name; var idocmd = pkg.idoc.md; diff --git a/lib/file.js b/lib/file.js index ace524fd..f801f4b4 100644 --- a/lib/file.js +++ b/lib/file.js @@ -130,13 +130,14 @@ function isDir(_path){ //获取路径下面的所有dir function currentDir(_path){ - if(exists(_path)) + if(!exists(_path)) return []; return _.filter(fs.readdirSync(_path),function(dirname){ return isDir(_path+dirname); }) } //获取路径下面的所有file function currentFile(_path){ + if(!exists(_path)) return []; return _.filter(fs.readdirSync(_path),function(dirname){ return isFile(_path+dirname); }) diff --git a/lib/init.js b/lib/init.js index 84848aa5..90963a66 100644 --- a/lib/init.js +++ b/lib/init.js @@ -7,6 +7,7 @@ var _ = require('underscore'); var color = require('colorful'); var copy = require('./copy'); var file = require('./file'); +var theme = require('./theme'); var menu_json = require('./menu_json'); var log = console.log; @@ -61,9 +62,7 @@ function runTask(commander){ type: "list", name: "idoctheme", message: "Choose a theme.", - choices: function(){ - return file.currentDir(template + '/theme/'); - } + choices: theme.themeList() },{ message: 'Author', name: 'author', diff --git a/lib/theme.js b/lib/theme.js index c114b632..335d4b4f 100644 --- a/lib/theme.js +++ b/lib/theme.js @@ -1,3 +1,57 @@ /** * 选择主题命令 - */ \ No newline at end of file + */ +var inquirer = require('inquirer'); +var file = require('./file'); +var path = require('path'); +var build = require('./build'); +var server = require('ssr'); + +var template = path.dirname(__dirname); + +var theme = module.exports = { + runTask:runTask, + themeList:themeList +} + +function runTask(commander){ + + var pkg = require(path.resolve('package.json')); + + if(commander.theme && commander.args.length > 0){ + build(commander); + server(); + }else{ + + inquirer.prompt([{ + type: "list", + name: "theme", + message: " Choose a theme.", + choices: themeList() + }],function(answers){ + pkg.idoc.theme = answers.theme; + file.write(process.cwd() + '/package.json',JSON.stringify(pkg, null, 4)); + build(commander); + }) + + } + +} + +/** + * [themeList 获取主题列表] + * @return {[type]} [array] + */ +function themeList(){ + var _def = file.currentDir(template + '/theme/'); + + var _m = file.currentDir(process.cwd() + '/') + + for (var i = 0; i < _m.length; i++) { + if(/^idoc\-theme./.test(_m[i])){ + _def.push(_m[i]) + } + }; + + return _def +} \ No newline at end of file