Skip to content

Instantly share code, notes, and snippets.

@embarq
Created November 30, 2017 14:44
Show Gist options
  • Select an option

  • Save embarq/00b4a7b8f2a3f9457475d06bcf3bc181 to your computer and use it in GitHub Desktop.

Select an option

Save embarq/00b4a7b8f2a3f9457475d06bcf3bc181 to your computer and use it in GitHub Desktop.
/*
1. find pages with 'voj-page-outlet' mension -> get html files
2. take it's controllers -> get ts files
3. find class names -> get class name
4. find navigation nodes with class names -> get files related to navigation nodes
5. append hasFullHeightContent to navigation nodes
*/
const path = require('path');
const fs = require('fs');
const { exec } = require('child_process');
const promisify = require('util.promisify');
const { FileMatcher } = require('file-matcher');
const findInFiles = require('find-in-files');
const execAsync = promisify(exec);
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const srcDir = path.resolve(__dirname, '..', 'src');
/**
* @param {string} std
*/
function processStd(std) {
return std
.toString()
.split('\n')
.filter(x => x !== '');
}
function main() {
const command = `grep -rwl '${ srcDir }' -e '<voj-page-outlet' | sed -E 's/.html/.ts/'`;
const classNameRegExp = /(?:export\s+class\s+)(\w+)(?:\s*{)/;
execAsync(command) // getting target controllers
.then(std => processStd(std)
.map(targetFilePath => () => readFileAsync(targetFilePath)
.then(value => {
const classNameMatches = value.toString().match(classNameRegExp);
return classNameMatches != null ? classNameMatches[1] : null;
})
.then(className => {
if (className == null) { throw new Error(`Class name has no matches in "${ targetFilePath }"`) };
const options = {
path: srcDir,
recursiveSearch: true,
fileFilter: {
fileNamePattern: '**/**.ts',
content: new RegExp(`page:\\s*${ className }`, 'ig')
}
}
const matcher = new FileMatcher();
return matcher
.find(options)
.then(res => {
return {
targetFilePath,
page: className,
refs: res
};
});
})
.then(pageRef => pageRef.refs
.map((ref, index) => () => readFileAsync(ref)
.then(content => {
const classNameMatches = content.toString().match(classNameRegExp);
return classNameMatches != null ? classNameMatches[1] : null;
})
.then(className => {
if (className == null) { return };
const refs = {
ref,
page: className
}
pageRef.refs[index] = refs;
return pageRef;
})
// .then(content => content.replace(
// new RegExp(`(page:\\s*${ pageRef.page })`, 'ig'),
// (match, page) => `${ page }, hasFullHeightContent: true`
// ))
// .then(content => writeFileAsync(ref, content))
)
.reduce(
(promise, next) => promise.then(result => next().then(Array.prototype.concat.bind(result))),
Promise.resolve([])
)
)
// .then(pageRef => pageRef.refs
// .map(ref => () => readFileAsync(ref)
// .then(content => content.toString())
// .then(content => content.replace(
// new RegExp(`(page:\\s*${ pageRef.page })`, 'ig'),
// (match, page) => `${ page }, hasFullHeightContent: true`
// ))
// .then(content => writeFileAsync(ref, content))
// )
// .reduce(
// (promise, next) => promise.then(result => next().then(Array.prototype.concat.bind(result))),
// Promise.resolve([])
// )
// )
)
)
.then(result => result
.reduce(
(promise, next) => promise.then(result => next().then(Array.prototype.concat.bind(result))),
Promise.resolve([])
)
)
.then(result => result.filter(entry => entry))
.then(result => console.log(JSON.stringify(result, null, 2)))
.catch(err => console.error(err))
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment