Skip to content

Commit

Permalink
feat(helper): created copy utility module
Browse files Browse the repository at this point in the history
  • Loading branch information
robophil committed Oct 13, 2017
1 parent 1d36192 commit 4eee54c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions helper/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs-extra')
const path = require('path')
const replace = require('replace-in-file')
const cwd = process.cwd()

module.exports = (names, type, regex) => {
const src = path.resolve(__dirname, '..', `files/${type}.js`)
const dest = (type === 'model') ? `${cwd}/src/model/` : `${cwd}/src/app/`

console.log(src, dest)
return fs.ensureDir(dest).then(() => {
const copyAction = []
names.forEach((name) => {
const filename = (type === 'model') ? `${name}` : `${name}.${type}`
copyAction.push(fs.copy(src, `${dest}${filename}.js`))
})
return copyAction
}).then(copyAction => Promise.all(copyAction).then(() => {
const replaceAction = []
names.forEach(name => {
const filename = (type === 'model') ? `${name}` : `${name}.${type}`
replaceAction.push(replace({
files: `${dest}${filename}.js`,
from: regex,
to: `${name[0].toUpperCase()}${name.substr(1)}`
}))
})
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)
})
}

0 comments on commit 4eee54c

Please sign in to comment.