Created
October 15, 2016 09:46
-
-
Save legend80s/240fe9d4d8d9b2468b102a04e4e89b00 to your computer and use it in GitHub Desktop.
update-route-entry.js called by init.js
This file contains hidden or 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
/* eslint-disable no-console */ | |
const fs = require('fs'); | |
function updateRouteEntry(routePath, modules) { | |
if (modules.length === 0) return; | |
fs.readFile(routePath, (err, data) => { | |
if (err) { | |
throw err; | |
} | |
const content = data.toString(); | |
const requires = modules | |
.filter(m => !content.includes(`require('./${m}').routes()`)) | |
.map(m => ` require('./${m}').routes(),`) | |
.join('\n'); | |
// console.log('requires:', requires); | |
if (requires === '') { | |
console.info('`%s` exists in %s, ignored.', modules.join(', '), routePath); | |
return; | |
} | |
const updated = content.replace(/router\.use\('\/api',/, match => `${match}\n${requires}`); | |
// console.log('updated:', updated); | |
fs.writeFile(routePath, updated, (error) => { | |
if (error) throw err; | |
console.log(`${routePath} updated!`); | |
}); | |
}); | |
} | |
module.exports = updateRouteEntry; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment