Skip to content

Instantly share code, notes, and snippets.

@legend80s
Created October 15, 2016 09:46
Show Gist options
  • Save legend80s/240fe9d4d8d9b2468b102a04e4e89b00 to your computer and use it in GitHub Desktop.
Save legend80s/240fe9d4d8d9b2468b102a04e4e89b00 to your computer and use it in GitHub Desktop.
update-route-entry.js called by init.js
/* 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