Skip to content

Commit

Permalink
增加同步远程json,以及抽取json到本地目录的命令
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaZhengRefn committed Feb 16, 2018
1 parent 9690bb0 commit 270b401
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
16 changes: 16 additions & 0 deletions bin/pharah
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
1 change: 0 additions & 1 deletion commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions commands/exact.js
Original file line number Diff line number Diff line change
@@ -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)
})
53 changes: 53 additions & 0 deletions commands/sync.js
Original file line number Diff line number Diff line change
@@ -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'))
})
})
4 changes: 2 additions & 2 deletions util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const format = require('format-json')
const json = require('format-json')
const fs = require('fs')
const chalk = require('chalk')

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 270b401

Please sign in to comment.