This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.ts
205 lines (166 loc) · 4.17 KB
/
gulpfile.babel.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import fs from 'fs';
import path from 'path';
import stream from 'stream';
import util from 'util';
import gulp from 'gulp';
import gulpRename from 'gulp-rename';
import gulpInsert from 'gulp-insert';
import gulpFilter from 'gulp-filter';
import gulpReplace from 'gulp-replace';
import gulpSourcemaps from 'gulp-sourcemaps';
import gulpBabel from 'gulp-babel';
import execa from 'execa';
import del from 'del';
const readFile = util.promisify(fs.readFile);
const pipeline = util.promisify(stream.pipeline);
async function exec(cmd: string, args: string[] = []) {
await execa(cmd, args, {
preferLocal: true,
stdio: 'inherit'
});
}
async function packageJson() {
return JSON.parse(await readFile('package.json', 'utf8')) as {
[p: string]: string;
};
}
async function babelrc() {
return {
...JSON.parse(await readFile('.babelrc', 'utf8')),
babelrc: false
} as {
presets: [string, {modules: boolean | string}][];
babelOpts: unknown[];
cacheDirectory?: boolean;
plugins: [string, unknown];
};
}
async function babelTarget(
src: string[],
srcOpts: unknown,
dest: string,
modules: string | boolean
) {
// Change module.
const babelOptions = await babelrc();
for (const preset of babelOptions.presets) {
if (preset[0] === '@babel/preset-env') {
preset[1].modules = modules;
}
}
if (!modules) {
babelOptions.plugins.push([
'esm-resolver',
{
source: {
extensions: [
[
['.js', '.mjs', '.jsx', '.mjsx', '.ts', '.tsx'],
'.mjs'
]
]
}
}
]);
}
// Read the package JSON.
const pkg = await packageJson();
// Filter meta data file and create replace transform.
const filterMeta = gulpFilter(['*/meta.ts'], {restore: true});
const filterMetaReplaces = [
["'@VERSION@'", JSON.stringify(pkg.version)],
["'@NAME@'", JSON.stringify(pkg.name)]
].map(([f, r]) => gulpReplace(f, r));
await pipeline(
gulp.src(src, srcOpts),
filterMeta,
...filterMetaReplaces,
filterMeta.restore,
gulpSourcemaps.init(),
gulpBabel(babelOptions as unknown),
gulpRename(path => {
if (!modules && path.extname === '.js') {
path.extname = '.mjs';
}
}),
gulpSourcemaps.write('.', {
includeContent: true,
addComment: false,
destPath: dest
}),
gulpInsert.transform((contents, file) => {
// Manually append sourcemap comment.
if (/\.m?js$/i.test(file.path)) {
const base = path.basename(file.path);
return `${contents}\n//# sourceMappingURL=${base}.map\n`;
}
return contents;
}),
gulp.dest(dest)
);
}
// clean
gulp.task('clean:logs', async () => {
await del(['npm-debug.log*', 'yarn-debug.log*', 'yarn-error.log*']);
});
gulp.task('clean:lib', async () => {
await del(['lib']);
});
gulp.task('clean:manifest', async () => {
await del(['oclif.manifest.json']);
});
gulp.task(
'clean',
gulp.parallel(['clean:logs', 'clean:lib', 'clean:manifest'])
);
// lint
gulp.task('lint:es', async () => {
await exec('eslint', ['.']);
});
gulp.task('lint', gulp.parallel(['lint:es']));
// formatting
gulp.task('format', async () => {
await exec('prettier', ['-w', '.']);
});
gulp.task('formatted', async () => {
await exec('prettier', ['-c', '.']);
});
// build
gulp.task('build:lib:dts', async () => {
await exec('tsc');
});
gulp.task('build:lib:cjs', async () => {
await babelTarget(['src/**/*.ts'], {}, 'lib', 'commonjs');
});
gulp.task('build:lib:mjs', async () => {
await babelTarget(['src/**/*.ts'], {}, 'lib', false);
});
gulp.task(
'build:lib',
gulp.parallel(['build:lib:dts', 'build:lib:cjs', 'build:lib:mjs'])
);
gulp.task('build:manifest', async () => {
await exec('oclif-dev', ['manifest']);
});
gulp.task('build:readme', async () => {
await exec('oclif-dev', ['readme']);
});
gulp.task(
'build',
gulp.parallel(['build:lib', 'build:manifest', 'build:readme'])
);
// test
gulp.task('test:node', async () => {
await exec('jasmine');
});
gulp.task('test', gulp.parallel(['test:node']));
// watch
gulp.task('watch', () => {
gulp.watch(['src/**/*', 'test/**/*'], gulp.series(['all']));
});
// all
gulp.task('all', gulp.series(['clean', 'build', 'test', 'lint', 'formatted']));
// prepack
gulp.task('prepack', gulp.series(['clean', 'build']));
// default
gulp.task('default', gulp.series(['all']));