main
|
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 9 months ago |
|
F4 Finalna Verzija
|
-
Property mode
set to
100755
|
|
File size:
895 bytes
|
| Line | |
|---|
| 1 | #!/usr/bin/env node
|
|---|
| 2 |
|
|---|
| 3 | const fs = require('fs')
|
|---|
| 4 | const path = require('path')
|
|---|
| 5 | const readline = require('readline')
|
|---|
| 6 |
|
|---|
| 7 | const flat = require('./index')
|
|---|
| 8 |
|
|---|
| 9 | const filepath = process.argv.slice(2)[0]
|
|---|
| 10 | if (filepath) {
|
|---|
| 11 | // Read from file
|
|---|
| 12 | const file = path.resolve(process.cwd(), filepath)
|
|---|
| 13 | fs.accessSync(file, fs.constants.R_OK) // allow to throw if not readable
|
|---|
| 14 | out(require(file))
|
|---|
| 15 | } else if (process.stdin.isTTY) {
|
|---|
| 16 | usage(0)
|
|---|
| 17 | } else {
|
|---|
| 18 | // Read from newline-delimited STDIN
|
|---|
| 19 | const lines = []
|
|---|
| 20 | readline.createInterface({
|
|---|
| 21 | input: process.stdin,
|
|---|
| 22 | output: process.stdout,
|
|---|
| 23 | terminal: false
|
|---|
| 24 | })
|
|---|
| 25 | .on('line', line => lines.push(line))
|
|---|
| 26 | .on('close', () => out(JSON.parse(lines.join('\n'))))
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | function out (data) {
|
|---|
| 30 | process.stdout.write(JSON.stringify(flat(data), null, 2))
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | function usage (code) {
|
|---|
| 34 | console.log(`
|
|---|
| 35 | Usage:
|
|---|
| 36 |
|
|---|
| 37 | flat foo.json
|
|---|
| 38 | cat foo.json | flat
|
|---|
| 39 | `)
|
|---|
| 40 |
|
|---|
| 41 | process.exit(code || 0)
|
|---|
| 42 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.