1 | #!/usr/bin/env node
|
2 | import minimist = require('minimist');
|
3 | import stdin = require('get-stdin');
|
4 | import {json2ts, json2tsMulti} from './';
|
5 | import {fromJS, OrderedSet} from 'immutable';
|
6 | import {join, parse, ParsedPath} from "path";
|
7 | import {existsSync, readFile, readFileSync} from "fs";
|
8 | const argv = minimist(process.argv.slice(2));
|
9 |
|
10 |
|
11 | const inputs = OrderedSet<string>(argv._);
|
12 |
|
13 |
|
14 | const defaults = {
|
15 | stdin: false,
|
16 | namespace: false,
|
17 | flow: false
|
18 | };
|
19 |
|
20 |
|
21 | const options = {
|
22 | ...defaults,
|
23 | ...argv
|
24 | };
|
25 |
|
26 | if (options.stdin) {
|
27 | stdin().then((str: string) => {
|
28 | if (str === '') {
|
29 | console.error('no input provided');
|
30 | } else {
|
31 | try {
|
32 | JSON.parse(str);
|
33 | console.log(json2ts(str, options));
|
34 | } catch (e) {
|
35 | console.error('Invalid JSON');
|
36 | console.error(e.message);
|
37 | }
|
38 | }
|
39 | })
|
40 | .catch(err => {
|
41 | console.error(err);
|
42 | })
|
43 | } else {
|
44 | if (inputs.size === 0) {
|
45 | console.error('Oops! You provided no inputs');
|
46 | console.log(`
|
47 | You can pipe JSON to this program with the --stdin flag:
|
48 |
|
49 | curl http://example.com/some-json | json-ts --stdin
|
50 |
|
51 | Or, provide path names:
|
52 |
|
53 | json-ts path/to/my-file.json
|
54 | `);
|
55 | } else {
|
56 | const queue = inputs
|
57 | .map(input => {
|
58 | return {
|
59 | input,
|
60 | parsed: parse(input),
|
61 | };
|
62 | })
|
63 | .map(incoming => {
|
64 | return {
|
65 | incoming,
|
66 | resolved: resolveInput(incoming, process.cwd())
|
67 | }
|
68 | });
|
69 |
|
70 | const withErrors = queue.filter(x => x.resolved.errors.length > 0);
|
71 | const withoutErrors = queue.filter(x => x.resolved.errors.length === 0);
|
72 | if (withErrors.size) {
|
73 | console.log('Sorry, there were errors with your input.');
|
74 | withErrors.forEach(function (item) {
|
75 | console.log('');
|
76 | console.log(` ${item.incoming.input}:`);
|
77 | console.log(' ', item.resolved.errors[0].error.message);
|
78 | })
|
79 | } else {
|
80 | const strings = withoutErrors.map(item => {
|
81 | return item.resolved.content;
|
82 | });
|
83 | console.log(json2tsMulti((strings as any), options));
|
84 | }
|
85 | }
|
86 | }
|
87 |
|
88 | interface IIncomingInput {
|
89 | input: string,
|
90 | parsed: ParsedPath,
|
91 | }
|
92 | interface InputError {
|
93 | kind: string,
|
94 | error: Error
|
95 | }
|
96 | interface IResolvedInput {
|
97 | errors: InputError[],
|
98 | content?: string
|
99 | }
|
100 |
|
101 | function resolveInput(incoming: IIncomingInput, cwd): IResolvedInput {
|
102 | const absolute = join(cwd, incoming.parsed.dir, incoming.parsed.base);
|
103 | if (!existsSync(absolute)) {
|
104 | return {
|
105 | errors: [{
|
106 | kind: 'FileNotFound',
|
107 | error: new Error(`File not found`)
|
108 | }]
|
109 | }
|
110 | }
|
111 | const data = readFileSync(absolute, 'utf8');
|
112 | try {
|
113 | JSON.parse(data);
|
114 | return {
|
115 | errors: [],
|
116 | content: data
|
117 | }
|
118 | } catch (e) {
|
119 | return {
|
120 | errors: [{
|
121 | kind: 'InvalidJson',
|
122 | error: e
|
123 | }]
|
124 | }
|
125 | }
|
126 | }
|
127 |
|
128 |
|
129 |
|