-
Notifications
You must be signed in to change notification settings - Fork 7
/
deploy.ts
119 lines (108 loc) · 4.04 KB
/
deploy.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { make } from 'makit';
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import * as chokidar from 'chokidar';
import { pushFactory } from '../plugin/push/index';
import { RecipeImpl, pickConfig } from '../utils/recipe-factory';
import { MakeProcess } from '../utils/make-process';
const chalk = require('chalk');
import stdout from '../utils/stdout';
import { MakeRestrictor } from '../utils/make-restrictor';
const ignored = /^\./;
const devConfig = resolve(`${process.cwd()}/dev.config.js`);
const config = existsSync(devConfig) ? require(devConfig) : {};
const push = pushFactory({
onEnd: (totalCount, successCount, failCount) => {
const str = `✈ deploy end [ total ${totalCount}, success ${successCount}, fail ${failCount} ]`;
const info = chalk`{greenBright.bold ${str}}`;
stdout.bottom('\n' + info);
},
onProcess: ({path, to}) => {
const info: string[] = [chalk`{greenBright.bold ✈ deploying ...}`];
info.push(chalk['gray']('└┬ ' + to));
info.push(chalk['gray'](' └─ ' + path.replace(process.cwd() + '/', '')));
stdout.bottom(info.join('\n'));
},
...config
});
interface DeployRecipeOption{
to: string
}
const proc = new MakeProcess({estimatedTime: 60});
const restrictor = new MakeRestrictor(proc);
export const deploy: RecipeImpl<DeployRecipeOption, undefined> = ({dep, to}, config, done) => {
push(dep, to, dep, done);
};
export function watchDeploy(outputFolder: string, deployConf: any) {
// const conf = pickConfig(configs, path, '**/', '**');
return ctx => {
console.log('inside watchdeploy');
chokidar
.watch(outputFolder, { ignored })
.on('add', path => {
const { to } = pickConfig(deployConf, path, '**/');
if (to) {
restrictor.whenIdle(() => {
push(path, to, path);
});
}
})
.on('change', path => {
const { to } = pickConfig(deployConf, path, '**/');
if (to) {
restrictor.whenIdle(() => {
push(path, to, path);
});
}
})
.on('error', error => console.error('Error while watching', error));
};
}
export interface WatchToBuildOption{
src: string
}
export interface WatchToBuildConfig{
file: string
onAdd?: string | string[]
onChange: string | string[]
}
export const watchToBuild = ({src}: WatchToBuildOption, configs: WatchToBuildConfig[]) => {
return async (ctx) => {
// 既然要发布, 就提前检查 receiver
if (!config.receiver) {
throw new Error('dev.config.js - receiver is required!');
}
// 先编译一把
proc.start();
await make('build');
proc.end();
chokidar
.watch(src, { ignored, ignoreInitial: true })
.on('add', async (path) => {
const conf = pickConfig(configs, path, '**/', '**');
if (conf.onAdd !== undefined) {
const addRules = typeof conf.onAdd === 'string' ? [conf.onAdd] : conf.onAdd;
restrictor.make(...addRules);
// proc.start();
// for(let i = 0; i < addRules.length; i++ ) {
// await make(addRules[i]);
// }
// proc.end();
}
})
.on('change', async path => {
console.log('rebuilding', path);
const conf = pickConfig(configs, path, '**/', '**');
if (conf.onChange !== undefined) {
const changeRules = typeof conf.onChange === 'string' ? [conf.onChange] : conf.onChange;
restrictor.make(...changeRules);
// proc.start();
// for(let i = 0; i < changeRules.length; i++ ) {
// await make(changeRules[i]);
// }
// proc.end();
}
})
.on('error', error => console.error('Error while watching', error));
}
};