-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper): created copy utility module
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |