Skip to content

Commit

Permalink
feat(generate): added generate types for other components
Browse files Browse the repository at this point in the history
  • Loading branch information
robophil committed Oct 13, 2017
1 parent 4eee54c commit 2d9c7d8
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 29 deletions.
13 changes: 12 additions & 1 deletion bin/cmd/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ module.exports = (cmd, names, options) => {
case 'model':
require('./types/model')(names, options)
break

case 'requester':
require('./types/requester')(names, options)
break
case 'responder':
require('./types/responder')(names, options)
break
case 'publisher':
require('./types/publisher')(names, options)
break
case 'subscriber':
require('./types/subscriber')(names, options)
break
default:
break
}
Expand Down
39 changes: 11 additions & 28 deletions bin/cmd/types/model.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
const fs = require('fs-extra')
const path = require('path')
const replace = require('replace-in-file')
const cwd = process.cwd()

const dest = `${cwd}/src/models/`
const src = path.resolve(__dirname, '..', '..', '..', 'files/model.js')
const copy = require('../../../helper/copy')

module.exports = (names, options) => {
fs.ensureDir(dest).then(() => {
const copyAction = []
names.forEach((name) => {
copyAction.push(fs.copy(src, `${dest}${name}.js`))
})
return copyAction
}).then(copyAction => Promise.all(copyAction).then(() => {
const replaceAction = []
const type = 'model'
const regex = /serviceName/g
copy(names, type, regex).then(() => {
console.log('\n')
names.forEach(name => {
replaceAction.push(replace({
files: `${dest}${name}.js`,
from: /model_name/g,
to: name
}))
})
return replaceAction
}).catch(err => new Error(err))
.then(replaceAction => Promise.all(replaceAction).then(() => {
})).catch(err => new Error(err)))
.catch(err => {
if (err instanceof Error) throw err
else throw new Error(err)
console.info(`New ${type} has been created as src/model/${name}.js`)
})
console.log('\n')
}).catch(err => {
if (err instanceof Error) throw err
else throw new Error(err)
})
}
16 changes: 16 additions & 0 deletions bin/cmd/types/publisher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const copy = require('../../../helper/copy')

module.exports = (names, options) => {
const type = 'publisher'
const regex = /serviceName/g
copy(names, type, regex).then(() => {
console.log('\n')
names.forEach(name => {
console.info(`New ${type} has been created as src/app/${name}.${type}.js`)
})
console.log('\n')
}).catch(err => {
if (err instanceof Error) throw err
else throw new Error(err)
})
}
16 changes: 16 additions & 0 deletions bin/cmd/types/requester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const copy = require('../../../helper/copy')

module.exports = (names, options) => {
const type = 'requester'
const regex = /serviceName/g
copy(names, type, regex).then(() => {
console.log('\n')
names.forEach(name => {
console.info(`New ${type} has been created as src/app/${name}.${type}.js`)
})
console.log('\n')
}).catch(err => {
if (err instanceof Error) throw err
else throw new Error(err)
})
}
16 changes: 16 additions & 0 deletions bin/cmd/types/responder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const copy = require('../../../helper/copy')

module.exports = (names, options) => {
const type = 'responder'
const regex = /serviceName/g
copy(names, type, regex).then(() => {
console.log('\n')
names.forEach(name => {
console.info(`New ${type} has been created as src/app/${name}.${type}.js`)
})
console.log('\n')
}).catch(err => {
if (err instanceof Error) throw err
else throw new Error(err)
})
}
16 changes: 16 additions & 0 deletions bin/cmd/types/subscriber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const copy = require('../../../helper/copy')

module.exports = (names, options) => {
const type = 'subscriber'
const regex = /serviceName/g
copy(names, type, regex).then(() => {
console.log('\n')
names.forEach(name => {
console.info(`New ${type} has been created as src/app/${name}.${type}.js`)
})
console.log('\n')
}).catch(err => {
if (err instanceof Error) throw err
else throw new Error(err)
})
}

0 comments on commit 2d9c7d8

Please sign in to comment.