Skip to content

Instantly share code, notes, and snippets.

@rosskevin
Last active October 31, 2018 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosskevin/12ed2dc43880e25b10763dc3f556a557 to your computer and use it in GitHub Desktop.
Save rosskevin/12ed2dc43880e25b10763dc3f556a557 to your computer and use it in GitHub Desktop.
ks show with omit
#!/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