forked from entwicklerstube/takeoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·120 lines (90 loc) · 2.87 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
#!/usr/bin/env node
const inquirer = require('inquirer');
const updateNotifier = require('update-notifier');
const {green} = require('chalk');
const taskler = require('taskler');
const meow = require('meow');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const cli = meow(`
Usage
$ takeoff <station>
Example
$ takeoff node-module
`);
const { getStations, loadStationByName } = require('./lib/station');
const { createFilesByList } = require('./lib/wizard');
const getStationId = async stations => {
if (cli.input.length === 0) {
return ( await inquirer.prompt(stations)).stationId;
} else {
return stations[0].choices.find(({name}) => name === cli.input[0]).value;
}
};
(async () => {
try {
const stationsCollection = await getStations();
const stationsPathMap = stationsCollection.reduce( (m, station) => {
const pathStations = m[station.stationsPath] || [];
pathStations.push(station);
return {
...m,
[station.stationsPath]: pathStations
};
}, {} );
let stationChoices = [];
for( let stationsPath in stationsPathMap ) {
stationChoices.push(new inquirer.Separator(`Stations from "${stationsPath}"`));
stationChoices = stationChoices.concat(stationsPathMap[stationsPath]);
}
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} = station.run(stationProps);
console.log('🚀 Houston, lift of in 3..2..1');
const tasks = [];
if (station.preTakeoff !== undefined) {
tasks.push({
title: 'Pre Takeoff',
task: opts => station.preTakeoff(stationProps, opts)
});
}
tasks.push({
title: `Created files for station ${name}`,
task: ({emit, succeed}) => {
createFilesByList(files, emit).then(succeed);
}
});
if (station.postTakeoff !== undefined) {
tasks.push({
title: 'Post Takeoff',
task: opts => station.postTakeoff(stationProps, opts)
});
}
taskler(tasks, () => {
console.info(green('🎉 Done'));
});
} catch (err) {
console.error(err);
process.exit(1);
}
})();