Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a command line interface to nextstep-plist
- Loading branch information
Showing
1 changed file
with
52 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,54 @@ | ||
#!/usr/bin/env node | ||
let parse = require("./code/parse") | ||
let stringify = require("./code/stringify") | ||
|
||
module.exports = { | ||
parse: require('./code/parse'), | ||
stringify: require('./code/stringify') | ||
parse, | ||
stringify, | ||
} | ||
|
||
async function getStdin() { | ||
return new Promise((honour, betray) => { | ||
let result = "" | ||
|
||
process.stdin.on("data", function(chunk) { | ||
result += chunk | ||
}) | ||
|
||
process.stdin.on("end", () => honour(result)) | ||
|
||
process.stdin.on("error", betray) | ||
}) | ||
} | ||
|
||
let help = ` | ||
commands: | ||
to-json — take stdin as a plist and output JSON | ||
to-plist — take stdin as JSON and output plist | ||
` | ||
|
||
;(async function monkey() { | ||
if (!module.parent) { | ||
let {EOL} = require("os") | ||
switch (process.argv[2]) { | ||
case "to-json": { | ||
let input = await getStdin() | ||
let object = parse(input) | ||
let output = JSON.stringify(object, null, "\t") | ||
return process.stdout.write(output + EOL) | ||
} | ||
case "to-plist": { | ||
let input = await getStdin() | ||
let object = JSON.parse(input) | ||
let output = stringify(object) | ||
return process.stdout.write(output + EOL) | ||
} | ||
default: { | ||
process.stdout.write(help) | ||
return process.exit(1) | ||
} | ||
} | ||
} | ||
})().catch(error => { | ||
console.error(process.env.debug ? error : error.message) | ||
}) |