diff --git a/bin/pharah b/bin/pharah index c240ff3..89be2d0 100644 --- a/bin/pharah +++ b/bin/pharah @@ -8,6 +8,22 @@ const req = command => path.resolve('./commands/', command) program .version(require('../package.json').version, '-v, --version') +program + .command('sync') + .description('sync the template from a url.') + .alias('s') + .action(function (cmd) { + require(req('sync')) + }) + +program + .command('exact') + .description('exact current template.') + .alias('s') + .action(function (cmd) { + require(req('exact')) + }) + program .command('init') .description('init a new project with your template.') diff --git a/commands/add.js b/commands/add.js index 34e8a37..0b32cd1 100644 --- a/commands/add.js +++ b/commands/add.js @@ -2,7 +2,6 @@ const inquirer = require('inquirer') const path = require('path') const fs = require('fs') -const showTemplateList = require('../util').showTemplateList const writeTemplateList = require('../util').writeTemplateList const checkTemplateList = require('../util').checkTemplateList diff --git a/commands/exact.js b/commands/exact.js new file mode 100644 index 0000000..60df77e --- /dev/null +++ b/commands/exact.js @@ -0,0 +1,29 @@ +const fs = require('fs') +const path = require('path') +const json = require('format-json') +const inquirer = require('inquirer') +const checkTemplateList = require('../util').checkTemplateList +const writeTemplateList = require('../util').writeTemplateList + +let templateList = checkTemplateList(path.resolve(__dirname, '../template.json')) + +module.exports = inquirer.prompt([ + { + type: 'input', + name: 'dest', + message: 'Input the path you want to exact the template.json to.', + validate(val){ + val = val.trim() + if(!val || !val.length){ + return 'Path can not be empty. You can input `pwd` in your terminal to check the path.' + } + return true + } + } +]) +.then(({ dest }) => { + const jsonPath = path.resolve(__dirname, dest, 'template.json') + + fs.openSync(jsonPath, 'w') + writeTemplateList(templateList, jsonPath) +}) \ No newline at end of file diff --git a/commands/sync.js b/commands/sync.js new file mode 100644 index 0000000..a8cc630 --- /dev/null +++ b/commands/sync.js @@ -0,0 +1,53 @@ +const download = require('download') +const path = require('path') +const inquirer = require('inquirer') +const writeTemplateList = require('../util').writeTemplateList +const checkTemplateList = require('../util').checkTemplateList + +let templateList = checkTemplateList(path.resolve(__dirname, '../template.json')) + +module.exports = inquirer.prompt([ + { + type: 'input', + name: 'url', + message: 'Url of the template.json: ', + validate(val){ + val = val.trim() + if(!val || !val.length){ + return 'Url can not be empty.' + } + return true + }, + filter(val){ + val = val.trim() + if(val.substring(0, 2) === '//') val = 'http:' + val + return val + } + }, + { + type: 'list', + name: 'mode', + message: 'Merge or Replace your current template? ', + choices: [ + 'merge', 'replace' + ] + } +]) +.then(({ url, mode }) => { + download(url, path.resolve(__dirname, '../'), { encoding: 'utf8' }) + .then(data => { + + try { + data = JSON.parse(data) + } catch (error) { + throw new Error('Template is not a json!') + } + + if(mode === 'merge'){ + templateList = Object.assign(templateList, data) + } else if(mode === 'replace'){ + templateList = data + } + writeTemplateList(templateList, path.resolve(__dirname, '../template.json')) + }) +}) \ No newline at end of file diff --git a/util.js b/util.js index b1dd85c..584d8a6 100644 --- a/util.js +++ b/util.js @@ -1,4 +1,4 @@ -const format = require('format-json') +const json = require('format-json') const fs = require('fs') const chalk = require('chalk') @@ -43,7 +43,7 @@ function showTemplateList(list){ function writeTemplateList(list, jsonPath, msg = 'Writing templates successfully!'){ if(arguments.length < 2) throw new Error('lack of params.') - fs.writeFile(jsonPath, format.plain(list), 'utf8',(err) => { + fs.writeFile(jsonPath, json.plain(list), 'utf8',(err) => { if(err){ console.log('err') } else {