This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.functions.ts
154 lines (138 loc) · 4.6 KB
/
gulpfile.functions.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { exec, execSync } from 'child_process';
import * as fs from 'fs';
import * as gulp from 'gulp';
import { series, src, dest } from 'gulp';
const DEFAULT_COMMAND_TIMEOUT_SECONDS = 1800;
export interface RunOptions {
commandTimeoutSeconds?: number;
cwd?: string;
env?: { [key: string]: string };
}
function print(value: string) {
if (value) {
process.stdout.write(value);
}
}
function printError(value: string) {
if (value) {
process.stderr.write(value);
}
}
export function doRun(command, options: RunOptions = {}): () => Promise<void> {
return async () => {
print(`doRun: command '${command}' with options ${JSON.stringify(options)}`);
const env = { ...process.env, ...(options.env || {}) };
return new Promise((resolve, reject) => {
const process = exec(
command,
{
...options,
timeout: (options.commandTimeoutSeconds || DEFAULT_COMMAND_TIMEOUT_SECONDS) * 1000,
killSignal: 'SIGKILL',
maxBuffer: 100 * 1000 * 1000,
env
},
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
}
);
process.stdout.on('data', (data) => {
print(data);
});
process.stderr.on('data', (data) => {
printError(data.toString());
});
});
};
}
function tslint({ cwd, fix = false }) {
const fixParam = fix ? '--fix --force' : '';
const prettierConfigPath = cwd === '.' ? '' : '../';
const prettier = `node_modules/.bin/prettier ${
fix ? '--write' : '--check'
} --config ${prettierConfigPath}.prettierrc --ignore-path ${prettierConfigPath}.prettierignore '**/*.{json,ts,html,scss}'`;
return doRun(`node_modules/.bin/tslint -p tsconfig.json -c tslint.json ${fixParam} && ${prettier}`, {
cwd,
commandTimeoutSeconds: 300
});
}
function eslint({ cwd, fix = false }: { cwd: string; fix?: boolean }) {
const path = cwd === '.' ? '*.ts' : '.';
const ext = '--ext .ts';
const ignorePath = './.eslintignore';
const fixParam = fix ? '--fix' : '';
const prettierConfigPath = cwd === '.' ? '' : '../';
const prettier = `node_modules/.bin/prettier ${
fix ? '--write' : '--check'
} --config ${prettierConfigPath}.prettierrc --ignore-path ${prettierConfigPath}.prettierignore '**/*.{json,ts,html,scss}'`;
return cwd === 'web'
? doRun(`node_modules/.bin/ng lint ${fixParam} && ${prettier}`, { cwd })
: doRun(`npx eslint ${fixParam} ${path} ${ext} --ignore-path '${ignorePath}' && ${prettier}`, { cwd });
}
export function lint(cwd, fix = false) {
if (fs.existsSync(`${cwd}/tsconfig.json`) && fs.existsSync(`${cwd}/tslint.json`)) {
return tslint({ cwd, fix });
}
return eslint({ cwd, fix });
}
export function mkdir(subdir, runOptions) {
execSync(`mkdir -p ${subdir}`, runOptions);
}
export function cp(from: string, to: string, runOptions) {
execSync(`cp -r ${from} ${to}`, runOptions);
}
export function copy(source: string, destination: string, runOptions) {
return src(source, runOptions).pipe(dest(destination, runOptions));
}
export function task(options: { name: string; desc?: string; fct: any; alias?: string }) {
const { name, desc, fct, alias } = options;
fct.description = desc || '';
gulp.task(name, fct);
if (alias) {
gulp.task(alias, fct);
}
}
export function mochaRunner({
testStage,
cwd,
baseDir = 'tests',
coverage = true
}: {
testStage: string;
cwd: string;
baseDir?: string;
coverage?: boolean;
}): () => Promise<void> {
const pattern = baseDir === 'tests' ? `${baseDir}/${testStage}/*.spec.?s?(x)` : `${baseDir}/**/*.spec.?s?(x)`;
const mocha = `node_modules/.bin/mocha '${pattern}' --exit${testStage === 'architecture' ? ' --timeout 10000' : ''}`;
const nyc = `${coverage ? 'node_modules/.bin/nyc' : ''}`;
const command = `NODE_PATH=./ NODE_ENV=test ${nyc} ${mocha}`;
return doRun(command, {
cwd,
commandTimeoutSeconds: 5 * 60
});
}
export function generateTestTask(module: string, testStage: string, baseDir?: string) {
task({
name: `${module}:exec-${testStage}-test`,
fct:
module === 'web'
? doRun('node_modules/.bin/ng test --watch=false --browsers ChromeHeadless', { cwd: module })
: mochaRunner({
testStage,
cwd: module,
baseDir,
coverage: testStage !== 'architecture'
}),
desc: `Runs all ${testStage} tests on ${module} (without npm install beforehand)`
});
task({
name: `${module}:${testStage}-test`,
fct: series(`${module}:install`, `${module}:exec-${testStage}-test`),
desc: `Runs all ${testStage} tests on ${module}`
});
}