This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·93 lines (81 loc) · 2.26 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const logSymbols = require('log-symbols');
const jaymock = require('@meeshkanml/jaymock');
const ora = require('ora');
const getStdin = require('get-stdin');
const execa = require('execa');
const jm = jaymock();
const cli = meow(`
Usage
$ jaymock <text> [--json]
$ cat <file> | jaymock [--json]
$ jaymock --server [--port <number>]
Options
--json, -j Print output in JSON format
--server, -s Start a development server for API mocking
--port, -p Specify a port number for the server
Examples
$ jaymock '{"firstName":"name.firstName", "lastName":"name.lastName"}'
{
"firstName": "Alvina",
"lastName": "Hodkiewicz"
}
$ jaymock '{"firstName":"name.firstName", "lastName":"name.lastName"}' --json
{"firstName":"Anthony","lastName":"Krajcik"}
$ jaymock --server --port 1337
`, {
flags: {
json: {
type: 'boolean',
alias: 'j',
default: false
},
server: {
type: 'boolean',
alias: 's',
default: false
},
port: {
type: 'number',
alias: 'p',
default: 3000
}
}
});
const input = cli.input[0];
const {json, server, port} = cli.flags;
const defaultArguments = ['run', 'dev', '--prefix', __dirname];
const serverArguments = (port === 3000) ? defaultArguments : defaultArguments.concat(['--', '--', '-p', port]);
const display = data => {
let output;
if (data.message) {
output = json ? JSON.stringify({error: data.message}) : `${logSymbols.error} ${data.message}`;
} else {
output = json ? JSON.stringify(data) : data;
}
console.log(output);
};
if (!input && !server && (process.stdin.isTTY || process.env.isTTY)) {
display(new Error('Specify a JSON template object'));
process.exit(1);
}
const loadingMessage = server ? 'Starting the development server…' : 'Generating fake data…';
const spinner = ora(loadingMessage).start();
(async () => {
if (server) {
const subprocess = execa('npm', serverArguments, {env: {FORCE_COLOR: true}});
subprocess.stdout.pipe(process.stdout);
spinner.stop();
} else {
const template = input ? input : await getStdin();
const populated = jm.populate(JSON.parse(template));
spinner.stop();
display(populated);
}
})().catch(error => {
spinner.stop();
display(error);
process.exit(1);
});