This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec.ts
98 lines (85 loc) · 2.55 KB
/
exec.ts
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
import path from 'path';
import Kefir, { Stream, Property, Observable, Emitter } from 'kefir';
import { runner } from 'hygen';
import inquirer from 'inquirer';
import execa from 'execa'; // eslint-disable-line import/default
import { Action, State, ConfiguredState, Level, LogAction } from './types';
/**
* Match the Logger interface expected by Hygen.
*/
class Logger {
constructor(private emitter: Emitter<LogAction, never>) {
this.emitter = emitter;
}
private emit(level: Level, msg: string) {
this.emitter.value({ type: 'LOG', payload: { level, msg } });
}
log(msg: string) {
this.emit('notice', msg);
}
colorful(msg: string) {
this.emit('notice', msg);
}
notice(msg: string) {
this.emit('notice', msg);
}
warn(msg: string) {
this.emit('warn', msg);
}
err(msg: string) {
this.emit('error', msg);
}
ok(msg: string) {
this.emit('ok', msg);
}
}
const selectNewProjectContext = (state: ConfiguredState) => ({
dir: state.config.dir,
name: state.config.name,
version: state.config.version,
description: state.config.description,
main: path.join(state.config.dir, 'app.js'),
author: '', // @TODO(mAAdhaTTah) get author,
license: state.config.license
});
const exec = (
action$: Stream<Action, never>,
state$: Property<State, never>
): Observable<Action, never> =>
state$
.filter(
(state: State): state is ConfiguredState => state.step === 'creating'
)
.take(1)
.flatMapFirst(state => {
const argv = ['project', 'new'];
for (const [key, value] of Object.entries(
selectNewProjectContext(state)
)) {
argv.push(`--${key}`);
argv.push(value);
}
// @TODO(mAAdhaTTah) check if dir exists and confirm overwrite
// hygen expects to handle interactivity
return Kefir.stream(emitter => {
runner(argv, {
// NOTE: This is relative to dist, where the build result is.
templates: path.join(__dirname, '..', 'templates'),
cwd: state.cwd,
logger: new Logger(emitter),
createPrompter: () => inquirer,
exec: (action, body) => {
const opts = body && body.length > 0 ? { input: body } : {};
return execa(action, { ...opts, shell: true });
},
debug: false
})
.then(result => {
emitter.value({ type: 'CREATED', payload: { result } });
})
.catch(error => {
emitter.value({ type: 'FAILED', error: true, payload: { error } });
});
});
});
export default exec;