Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Last active November 16, 2018 15:34
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 apaleslimghost/91f1b63cd12d37c1709099bde9c1b4bd to your computer and use it in GitHub Desktop.
Save apaleslimghost/91f1b63cd12d37c1709099bde9c1b4bd to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const {f = 'Makefile', _} = require('minimist')(process.argv.slice(2))
const fs = require('fs')
const parseMakefileRules = require('@quarterto/parse-makefile-rules')
const formatBody = body => body.replace(/^/mg, '\t')
const formatRule = (rule, body) => body && `${rule}:
${formatBody(body)}
`;
const formatRules = (names, rules) =>
names.length === 1? rules[names[0]]
: /* otherwise */ names.map(
name => formatRule(name, rules[name])
).filter(Boolean).join('\n')
const makefile = fs.readFileSync(f, 'utf8')
const parsedRules = parseMakefileRules(makefile)
const rules = _.length? _
: /* otherwise */ Object.keys(parsedRules)
console.log(
formatRules(
rules,
parsedRules
) || process.exit(1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment