-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
executable file
·154 lines (126 loc) · 3.46 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node
const meow = require('meow');
const { cosmiconfigSync } = require('cosmiconfig');
const { optionSchema, i18n, useLocaleSync } = require('./src/shared');
const redent = require('redent');
const trimNewLines = require('trim-newlines');
const VTEXYCore = require('./src');
const redentBanner = require('./src/utils/redentBanner');
const pkg = require('./package.json');
// process.nextTick(function memory() {
// const used = process.memoryUsage();
// console.log(`Memory: ${Math.round((used.rss / 1024 / 1024) * 100) / 100} MB`);
// setTimeout(memory, 10000);
// });
const options = {
autoHelp: false,
flags: {
account: {
type: 'string',
alias: 'a'
},
baseDir: {
type: 'string',
alias: 'd'
},
noSSR: {
type: 'boolean'
},
locale: {
type: 'string'
},
help: {
type: 'boolean',
alias: 'h'
},
version: {
type: 'boolean',
alias: 'v'
}
}
};
const cli = meow('', options);
const showHelp = () => {
// TODO: Try to use https://www.npmjs.com/package/command-line-usage
let text = `
${i18n.__('cli.Usage')}
$ vtexy <${i18n.__('command')}> <${i18n.__('options')}>
${i18n.__('cli.Commands')}
start ${i18n.__('cli.start.description')}
init <path> ${i18n.__('cli.init.description')}
${i18n.__('cli.Options')}
--account, -a <account> ${i18n.__('cli.flags.account.description')}
--base-dir, -dir <path> ${i18n.__('cli.flags.baseDir.description')}
--no-ssr ${i18n.__('cli.flags.noSSR.description')}
--locale <en,pt> ${i18n.__('cli.flags.locale.description')}
--help, -h ${i18n.__('cli.flags.help.description')}
--version, -v ${i18n.__('cli.flags.version.description')}
`;
let help = redentBanner(text, 0);
console.log(help);
};
const showVersion = () => {
console.log(pkg.version);
};
(async () => {
if (cli.flags.locale) {
await useLocaleSync(cli.flags.locale);
process.exit(2);
}
if (cli.flags.help || process.argv[2] === undefined) {
showHelp();
process.exit(2);
}
if (cli.flags.version) {
showVersion();
process.exit(2);
}
try {
async function charger() {
const explorer = cosmiconfigSync('vtexy');
const cf = await explorer.search();
let options = {
...cli.flags,
...(cf ? cf.config : null),
...(cf ? { configPath: cf.filepath } : null)
};
if (options && options.filepath) {
options.configPath = options.filepath;
}
try {
options = await optionSchema.validateSync(
{
...(options ? options.config : null),
...(options ? options : null),
...cli.flags
},
{ stripUnknown: true }
);
return options;
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
let vtexy = VTEXYCore(charger);
let tasks = {
async init() {
await vtexy.init();
},
async start() {
await vtexy.charge();
await vtexy.start();
}
// async sync() {}
};
let run = tasks[cli.input[0]];
if (run !== undefined) {
await run();
} else {
console.log(i18n.__('errors.invalid_command'));
console.log(`Use: 'vtexy -h' to see usage commands`);
}
} catch (error) {
console.error(error);
}
})();