-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·78 lines (56 loc) · 2.07 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
#!/usr/bin/env node
const inquirer = require('inquirer');
const { getStations, loadStationByName } = require('./lib/station');
const { createFilesByList } = require('./lib/wizard');
const getStationId = async stations => {
return ( await inquirer.prompt(stations)).stationId;
};
(async () => {
try {
const stationsCollection = await getStations();
const stationsPathMap = stationsCollection.reduce( (m, station) => {
const pathStations = m[station.stationsPath] || [];
pathStations.push(station);
return Object.assign({}, m, {
[station.stationsPath]: pathStations
});
}, {} );
let stationChoices = [];
for( let stationsPath in stationsPathMap ) {
stationChoices.push(new inquirer.Separator(`Stations from "${stationsPath}"`));
stationChoices = stationChoices.concat(stationsPathMap[stationsPath]);
}
if (stationChoices.length === 0) {
console.log('No takeoff stations found.'); // eslint-disable-line no-console
process.exit(1);
}
const chooseBetweenAvailableStations = [{
type: 'list',
name: 'stationId',
message: 'What do you want to create?',
choices: stationChoices
}];
const stationId = await getStationId(chooseBetweenAvailableStations);
const { name, stationsPath } = stationsCollection.find(({ value }) => value === stationId);
const station = await loadStationByName({ name, stationsPath });
const requiredStationProps = [];
station.requiredProps && station.requiredProps.forEach(prop => {
if (typeof prop === 'string') {
requiredStationProps.push({
type: 'input',
name: prop,
message: prop
});
} else {
requiredStationProps.push(prop);
}
});
const stationProps = await inquirer.prompt(requiredStationProps);
const { files } = await station.run(stationProps);
await createFilesByList(files);
console.log('Done'); // eslint-disable-line no-console
} catch (err) {
console.error(err); // eslint-disable-line no-console
process.exit(1);
}
})();