Last active
October 31, 2018 18:35
-
-
Save rosskevin/12ed2dc43880e25b10763dc3f556a557 to your computer and use it in GitHub Desktop.
ks show with omit
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
#!/usr/bin/env node | |
const util = require('util') | |
const exec = util.promisify(require('child_process').exec) | |
const args = process.argv.slice(2) | |
const omitComponentArgsIndexes = [] | |
const omitModuleArgsIndexes = [] | |
const propagateArgs = [] | |
const options = { | |
omitComponent: [], | |
omitModule: [], | |
} | |
function debug(...args) { | |
// console.log(...args) | |
} | |
args.forEach((val, index, array) => { | |
// debug(index + ': ' + val) | |
if (val === '--omit-component') { | |
omitComponentArgsIndexes.push(index) | |
} else if (omitComponentArgsIndexes.includes(index - 1)) { | |
options.omitComponent.push(val) | |
} else if (val === '--omit-module') { | |
omitModuleArgsIndexes.push(index) | |
} else if (omitModuleArgsIndexes.includes(index - 1)) { | |
options.omitModule.push(val) | |
} else { | |
propagateArgs.push(val) | |
} | |
}) | |
async function createComponentArgLine() { | |
const { stdout, stderr } = await exec('ks component list --output json') | |
// debug('stdout:', stdout) | |
// debug('stderr:', stderr) | |
const parsed = JSON.parse(stdout) | |
// debug(JSON.stringify(parsed, null, ' ')) | |
let componentArgs = '' | |
for (const j of parsed.data) { | |
const { component } = j | |
// debug(component) | |
if (!options.omitComponent.includes(component)) { | |
let omit = false | |
for (const omitModule of options.omitModule) { | |
if (component.startsWith(`${omitModule}.`)) { | |
omit = true | |
break | |
} | |
} | |
if (!omit) { | |
componentArgs += `--component ${component} ` | |
} | |
} | |
} | |
return componentArgs | |
} | |
// top level async | |
;(async () => { | |
try { | |
let cmd | |
if (propagateArgs.includes('--component')) { | |
// --component means inclusive so skip the omits | |
cmd = `ks show ${propagateArgs.join(' ')}` | |
} else { | |
const componentArgLine = await createComponentArgLine() | |
cmd = `ks show ${propagateArgs.join(' ')} ${componentArgLine}` | |
} | |
debug(cmd) | |
const { stdout, stderr } = await exec(cmd) | |
console.log(stdout) | |
// console.log(stderr) | |
} catch (e) { | |
console.error(e) | |
process.exit(1) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment