Last active
August 6, 2017 03:16
-
-
Save langdonx/05aa4d4980e254a9ae5921055d8c1aba to your computer and use it in GitHub Desktop.
modify modules and html
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
const fs = require('fs'); | |
// addComponentToModule('./src/library/library.module.js', 'Module', '', '', 'new-component', 'newComponent', 'NewComponent'); | |
// addComponentToModule('./src/docs/docs.module.js', 'Demo', 'demos/', '-demo', 'new-component', 'newComponent', 'NewComponent'); | |
// addComponentToDemoHtml('new-component', 'newComponent', 'NewComponent'); | |
module.exports.addComponentToModule = function addComponentToModule(filename, variableSuffix, importPrefix, importSuffix, componentName, camelName, capitalName) { | |
// read the file | |
let content = fs.readFileSync(filename, 'utf8'); | |
// TODO use this for safety https://github.com/sindresorhus/detect-newline | |
const eol = '\r\n'; | |
// find the import statements | |
const importMatches = content.match(new RegExp(`(import [A-z]+${variableSuffix} from '.*';${eol})`, 'gm')); | |
const lastImport = importMatches[importMatches.length - 1]; | |
// append our import after the last one | |
const newImport = `import ${capitalName}${variableSuffix} from './${importPrefix}${componentName}/${componentName}${importSuffix}.module';${eol}`; | |
content = content.replace(lastImport, `${lastImport}${newImport}`); | |
// extract module name from lastImport | |
const moduleNameMatches = lastImport.match(new RegExp(`import (.*)${variableSuffix}`)); | |
const lastDependencyWithRegExp = ` ${moduleNameMatches[1]}${variableSuffix},?${eol}`; | |
const newDependency = ` ${capitalName}${variableSuffix},${eol}`; | |
// since we're not enforcing comma-dangle:always, this is trickier than it needs to be | |
content = content.replace(new RegExp(lastDependencyWithRegExp), `${lastDependencyWithRegExp.replace('?', '')}${newDependency}`); | |
fs.writeFileSync(filename, content, 'utf8'); | |
return true; | |
}; | |
module.exports.addComponentToDemoHtml = function addComponentToDemoHtml(componentName, camelName, capitalName) { | |
// read the file | |
const filename = './src/docs/docs.html'; | |
let content = fs.readFileSync(filename, 'utf8'); | |
// TODO use this for safety https://github.com/sindresorhus/detect-newline | |
const eol = '\r\n'; | |
// find the last instance of </demo> and add our stuff after it | |
const demoCloser = `</demo>${eol}`; | |
const position = content.lastIndexOf(demoCloser) + demoCloser.length; | |
const titleName = capitalName.replace(/([A-Z])/g, ' $1').trim(); | |
const newDemo = ` <h1>${titleName}</h1> | |
<demo> | |
<${componentName}-demo></${componentName}-demo> | |
</demo>${eol}`; | |
content = `${content.slice(0, position)}${newDemo}${content.slice(position)}`; | |
fs.writeFileSync(filename, content, 'utf8'); | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment