/
attach.ts
47 lines (44 loc) · 1.16 KB
/
attach.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
import { options, resolver, on, add } from 'exits';
import { ADD_TYPES } from './add';
import manager from './manager';
import chalk from 'chalk';
import logger from '~/utils/logger';
export default function attach(): void {
if (manager.isAttached()) return;
// Attachment must go first (other potential errors must be catched)
manager.attach();
options({
spawned: {
signals: 'none',
wait: 'all'
},
resolver(type, arg) {
try {
if (type === 'signal') {
logger.debug('Received a termination signal: exiting with code 1');
return resolver('exit', 1);
}
return resolver(type, arg);
} catch (err) {
return resolver('exit', 1);
}
}
});
// Add hooks
on('triggered', () => {
if (manager.isPending()) {
logger.info('\n' + chalk.yellow.bold('-') + ' Waiting on termination');
}
});
add(ADD_TYPES.START_LOG, () => {
if (manager.isPending()) {
logger.info(chalk.yellow.bold('-') + ' Preparing exit');
}
});
on('done', () => {
if (manager.isPending()) {
logger.info(chalk.green.bold('✓') + ' Done');
manager.flush();
}
});
}