-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.mjs
653 lines (585 loc) · 18.3 KB
/
gulpfile.mjs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// All other imports are lazy so that single tasks start up fast.
import {
existsSync,
promises,
readdirSync,
readFileSync,
writeFileSync,
} from 'fs';
import gulp from 'gulp';
import {basename, dirname, join, resolve} from 'path';
import {gzipSync} from 'zlib';
const UTF8 = 'utf-8';
const TEST_MODULES = ['', 'ui-react'];
const ALL_MODULES = [...TEST_MODULES];
const ALL_DEFINITIONS = [...ALL_MODULES];
const DIST_DIR = 'dist';
const DOCS_DIR = 'docs';
const TMP_DIR = 'tmp';
const LINT_BLOCKS = /```[jt]sx?( [^\n]+)?(\n.*?)```/gms;
const TYPES_DOC_CODE_BLOCKS = /\/\/\/\s*(\S*)(.*?)(?=(\s*\/\/)|(\n\n)|(\n$))/gs;
const TYPES_DOC_BLOCKS = /(\/\*\*.*?\*\/)\s*\/\/\/\s*(\S*)/gs;
const getGlobalName = (module) =>
'TinyTick' +
(module == ''
? ''
: basename(module)
.split('-')
.map((part) => part[0].toUpperCase() + part.slice(1).toLowerCase())
.join(''));
const getPrettierConfig = async () => ({
...JSON.parse(await promises.readFile('.prettierrc', UTF8)),
parser: 'typescript',
});
const allOf = async (array, cb) => await Promise.all(array.map(cb));
const testModules = async (cb) => await allOf(TEST_MODULES, cb);
const allModules = async (cb) => await allOf(ALL_MODULES, cb);
const allDefinitions = async (cb) => await allOf(ALL_DEFINITIONS, cb);
const clearDir = async (dir = DIST_DIR) => {
try {
await removeDir(dir);
} catch {}
await makeDir(dir);
};
const makeDir = async (dir) => {
try {
await promises.mkdir(dir);
} catch {}
};
const ensureDir = async (fileOrDirectory) => {
await promises.mkdir(dirname(fileOrDirectory), {recursive: true});
return fileOrDirectory;
};
const removeDir = async (dir) => {
await promises.rm(dir, {recursive: true});
};
const forEachDeepFile = (dir, callback, extension = '') =>
forEachDirAndFile(
dir,
(dir) => forEachDeepFile(dir, callback, extension),
(file) => callback(file),
extension,
);
const forEachDirAndFile = (dir, dirCallback, fileCallback, extension = '') =>
readdirSync(dir, {withFileTypes: true}).forEach((entry) => {
const path = resolve(join(dir, entry.name));
if (entry.isDirectory()) {
dirCallback?.(path);
} else if (path.endsWith(extension)) {
fileCallback?.(path);
}
});
const gzipFile = async (fileName) =>
await promises.writeFile(
`${fileName}.gz`,
gzipSync(await promises.readFile(fileName, UTF8), {level: 9}),
);
const copyPackageFiles = async (forProd = false) => {
const mins = forProd ? [null, 'min'] : [null];
const modules = forProd ? ALL_MODULES : TEST_MODULES;
const json = JSON.parse(await promises.readFile('package.json', UTF8));
delete json.private;
delete json.scripts;
delete json.devEngines;
delete json.devDependencies;
json.main = './index.js';
json.types = './@types/index.d.ts';
json.typesVersions = {'*': {}};
json.exports = {};
mins.forEach((min) => {
modules.forEach((module) => {
const path = [min, module].filter((part) => part).join('/');
const typesPath = ['.', '@types', module, 'index.d.']
.filter((part) => part)
.join('/');
const codePath = (path ? '/' : '') + path;
json.typesVersions['*'][path ? path : '.'] = [typesPath + 'ts'];
json.exports['.' + codePath] = {
default: {
types: typesPath + 'ts',
default: '.' + codePath + '/index.js',
},
};
});
});
await promises.writeFile(
join(DIST_DIR, 'package.json'),
JSON.stringify(json, undefined, 2),
UTF8,
);
await promises.copyFile('LICENSE', join(DIST_DIR, 'LICENSE'));
await promises.copyFile('readme.md', join(DIST_DIR, 'readme.md'));
await promises.copyFile('releases.md', join(DIST_DIR, 'releases.md'));
};
let labelBlocks;
const getLabelBlocks = async () => {
if (labelBlocks == null) {
labelBlocks = new Map();
await allModules(async (module) => {
[
...(
await promises.readFile(`src/@types/${module}/docs.js`, UTF8)
).matchAll(TYPES_DOC_BLOCKS),
].forEach(([_, block, label]) => {
if (labelBlocks.has(label)) {
throw new Error(`Duplicate label '${label}' in ${module}`);
}
labelBlocks.set(label, block);
});
});
}
return labelBlocks;
};
const copyDefinition = async (dir, module) => {
const labelBlocks = await getLabelBlocks();
// Add easier-to-read with-schemas blocks
const codeBlocks = new Map();
[
...(
await promises.readFile(`src/@types/${module}/index.d.ts`, UTF8)
).matchAll(TYPES_DOC_CODE_BLOCKS),
].forEach(([_, label, code]) => {
const prefix = code.match(/^\n\s*/m)?.[0];
if (prefix) {
codeBlocks.set(
label,
code
.replace(/export type \S+ =\s/, '')
.replace(/export function /, '')
.replaceAll(prefix, prefix + ' * '),
);
}
});
const fileRewrite = (block) =>
block.replace(TYPES_DOC_CODE_BLOCKS, (_, label, code) => {
if (labelBlocks.has(label)) {
let block = labelBlocks.get(label);
return block + code;
}
throw `Missing docs label ${label} in ${module}`;
});
const definitionFile = await ensureDir(`${dir}/@types/${module}/index.d.ts`);
await promises.writeFile(
definitionFile,
fileRewrite(
await promises.readFile(`src/@types/${module}/index.d.ts`, UTF8),
),
UTF8,
);
};
const copyDefinitions = async (dir) => {
await allDefinitions((module) => copyDefinition(dir, module));
};
const execute = async (cmd) => {
const {exec} = await import('child_process');
const {promisify} = await import('util');
try {
await promisify(exec)(cmd);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
throw e.stdout;
}
};
const lintCheckFiles = async (dir) => {
const {default: prettier} = await import('prettier');
const prettierConfig = await getPrettierConfig();
const filePaths = [];
['.ts', '.tsx', '.js', '.d.ts'].forEach((extension) =>
forEachDeepFile(dir, (filePath) => filePaths.push(filePath), extension),
);
await allOf(filePaths, async (filePath) => {
const code = await promises.readFile(filePath, UTF8);
if (
!(await prettier.check(code, {...prettierConfig, filepath: filePath}))
) {
writeFileSync(
filePath,
await prettier.format(
code,
{...prettierConfig, filepath: filePath},
UTF8,
),
);
}
});
const {
default: {ESLint},
} = await import('eslint');
const esLint = new ESLint({});
const results = await esLint.lintFiles([dir]);
if (
results.filter((result) => result.errorCount > 0 || result.warningCount > 0)
.length > 0
) {
const formatter = await esLint.loadFormatter();
const errors = await formatter.format(results);
throw errors;
}
};
const lintCheckDocs = async (dir) => {
const {
default: {ESLint},
} = await import('eslint');
const esLint = new ESLint({
overrideConfig: {
rules: {
'no-console': 0,
'react/prop-types': 0,
'react-hooks/rules-of-hooks': 0,
'@typescript-eslint/no-unused-expressions': 0,
'max-len': [
2,
{code: 80, ignorePattern: '^(\\s+\\* )?((im|ex)ports?|// ->)\\W.*'},
],
},
},
});
const {default: prettier} = await import('prettier');
const prettierConfig = await getPrettierConfig();
const docConfig = {...prettierConfig, printWidth: 75};
const filePaths = [];
['.js', '.d.ts'].forEach((extension) =>
forEachDeepFile(dir, (filePath) => filePaths.push(filePath), extension),
);
await allOf(filePaths, async (filePath) => {
const code = await promises.readFile(filePath, UTF8);
await allOf(
[...(code.matchAll(LINT_BLOCKS) ?? [])],
async ([_, hint, docBlock]) => {
if (hint?.trim() == 'override') {
return; // can't lint orphaned TS methods
}
const code = docBlock.replace(/\n +\* ?/g, '\n').trimStart();
let pretty = code;
if (!(await prettier.check(code, docConfig))) {
pretty = await prettier.format(code, docConfig);
writeFileSync(
filePath,
readFileSync(filePath, UTF8).replace(
docBlock,
'\n' +
pretty
.trim()
.split('\n')
.map((line) => (line == '' ? ' *' : ' * ' + line))
.join('\n') +
'\n * ',
),
UTF8,
);
}
const results = await esLint.lintText(pretty);
if (
results.filter(
(result) => result.errorCount > 0 || result.warningCount > 0,
).length > 0
) {
const formatter = await esLint.loadFormatter();
const errors = await formatter.format(results);
throw `${filePath} does not lint:\n${pretty}\n\n${errors}`;
}
},
);
});
};
const spellCheck = async (dir, deep = false) =>
await execute(`cspell "${dir}/*${deep ? '*' : ''}"`);
const getTsOptions = async (dir) => {
const {default: tsc} = await import('typescript');
return tsc.parseJsonSourceFileConfigFileContent(
tsc.readJsonConfigFile(`${dir}/tsconfig.json`, tsc.sys.readFile),
tsc.sys,
dir,
);
};
const tsCheck = async (dir) => {
const path = await import('path');
const {default: tsc} = await import('typescript');
const {analyzeTsConfig} = await import('ts-unused-exports');
const {fileNames, options} = await getTsOptions(dir);
const results = tsc
.getPreEmitDiagnostics(
tsc.createProgram(
fileNames.filter(
(fileName) => !fileName.startsWith('test/unit/core/types'),
),
options,
),
)
.filter((result) => !result.file?.fileName.includes('/node_modules/'));
if (results.length > 0) {
const resultText = results
.map((result) => {
const {file, messageText, start} = result;
const {line, character} = file.getLineAndCharacterOfPosition(start);
return `${file.fileName}:${line}:${character}\n${JSON.stringify(
messageText,
)}`;
})
.join('\n\n');
throw resultText;
}
const unusedResults = Object.entries(
analyzeTsConfig(`${path.resolve(dir)}/tsconfig.json`, [
'--excludeDeclarationFiles',
'--excludePathsFromReport=' +
'server.mjs;jest/reporter.js;jest/environment.js;build.ts;' +
TEST_MODULES.map((module) => `${module}.ts`).join(';'),
]).unusedExports,
)
.map(
([file, exps]) =>
`${file}: ${exps.map((exp) => exp.exportName).join(', ')}`,
)
.join('\n');
if (unusedResults != '') {
throw `Unused exports for ${dir} in: \n${unusedResults}`;
}
};
const compileModule = async (module, dir = DIST_DIR, min = false) => {
const {default: esbuild} = await import('rollup-plugin-esbuild');
const {rollup} = await import('rollup');
const {default: replace} = await import('@rollup/plugin-replace');
const {default: prettierPlugin} = await import('rollup-plugin-prettier');
const {default: shebang} = await import('rollup-plugin-preserve-shebang');
const {default: image} = await import('@rollup/plugin-image');
const {default: terser} = await import('@rollup/plugin-terser');
let inputFile = `src/${module}/index.ts`;
if (!existsSync(inputFile)) {
inputFile += 'x';
}
const inputConfig = {
external: ['react', 'react/jsx-runtime'],
input: inputFile,
plugins: [
esbuild({
target: 'esnext',
legalComments: 'inline',
jsx: 'automatic',
}),
replace({
'/*!': '\n/*',
delimiters: ['', ''],
preventAssignment: true,
}),
shebang(),
image(),
min
? [terser({toplevel: true, compress: {unsafe: true, passes: 3}})]
: prettierPlugin(await getPrettierConfig()),
],
onwarn: (warning, warn) => {
if (warning.code !== 'MISSING_NODE_BUILTINS') {
warn(warning);
}
},
};
const moduleDir = dirname(await ensureDir(dir + '/' + module + '/-'));
const index = 'index.js';
const outputConfig = {
dir: moduleDir,
entryFileNames: index,
format: 'esm',
interop: 'default',
name: getGlobalName(module),
};
await (await rollup(inputConfig)).write(outputConfig);
if (min) {
await gzipFile(join(moduleDir, index));
}
};
// coverageMode = 0: none; 1: screen; 2: json; 3: html
const test = async (
dirs,
{coverageMode, countAsserts, puppeteer, serialTests} = {},
) => {
const {default: jest} = await import('jest');
await makeDir(TMP_DIR);
const {
results: {success},
} = await jest.runCLI(
{
roots: dirs,
setupFilesAfterEnv: ['./test/jest/setup'],
...(puppeteer
? {
setupFilesAfterEnv: ['expect-puppeteer'],
preset: 'jest-puppeteer',
detectOpenHandles: true,
maxWorkers: 2,
}
: {testEnvironment: './test/jest/environment'}),
...(coverageMode > 0
? {
collectCoverage: true,
coverageProvider: 'babel',
collectCoverageFrom: [
`${DIST_DIR}/index.js`,
`${DIST_DIR}/ui-react/index.js`,
// Other modules cannot be fully exercised in isolation.
],
coverageReporters: ['text-summary']
.concat(coverageMode > 1 ? ['json-summary'] : [])
.concat(coverageMode > 2 ? ['lcov'] : []),
coverageDirectory: 'tmp',
}
: {}),
...(countAsserts
? {
testEnvironment: './test/jest/environment',
reporters: ['default', './test/jest/reporter'],
runInBand: true,
}
: {}),
...(serialTests ? {runInBand: true} : {}),
},
[''],
);
if (!success) {
await removeDir(TMP_DIR);
throw 'Test failed';
}
if (coverageMode == 2) {
await promises.writeFile(
'coverage.json',
JSON.stringify({
...(countAsserts
? JSON.parse(await promises.readFile('./tmp/counts.json'))
: {}),
...JSON.parse(await promises.readFile('./tmp/coverage-summary.json'))
.total,
}),
UTF8,
);
}
if (coverageMode < 3) {
await removeDir(TMP_DIR);
}
};
const compileModulesForProd = async () => {
await clearDir(DIST_DIR);
await copyPackageFiles(true);
await copyDefinitions(DIST_DIR);
await allModules(async (module) => {
await compileModule(module, `${DIST_DIR}/`);
await compileModule(module, `${DIST_DIR}/min`, true);
});
};
const compileDocsAndAssets = async (api = true, pages = true) => {
const {default: esbuild} = await import('esbuild');
await makeDir(TMP_DIR);
await esbuild.build({
entryPoints: ['site/build.ts'],
external: ['tinydocs', 'react', '@prettier/sync'],
target: 'esnext',
bundle: true,
outfile: './tmp/build.js',
format: 'esm',
platform: 'node',
});
// eslint-disable-next-line import/no-unresolved
const {build} = await import('./tmp/build.js');
await build(esbuild, DOCS_DIR, api, pages);
await removeDir(TMP_DIR);
};
const npmInstall = async () => {
const {exec} = await import('child_process');
const {promisify} = await import('util');
return await promisify(exec)('npm install --legacy-peer-deps');
};
const npmPublish = async () => {
const {exec} = await import('child_process');
const {promisify} = await import('util');
return await promisify(exec)('npm publish');
};
const {parallel, series} = gulp;
// --
export const preparePackage = copyPackageFiles;
export const compileForTest = async () => {
await clearDir(DIST_DIR);
await copyPackageFiles();
await copyDefinitions(DIST_DIR);
await testModules(async (module) => {
await compileModule(module, DIST_DIR);
});
};
export const lintFiles = async () => {
await lintCheckFiles('src');
await lintCheckFiles('test');
await lintCheckFiles('site');
};
export const lintDocs = async () => await lintCheckDocs('src');
export const lint = series(lintDocs, lintFiles);
export const spell = async () => {
await spellCheck('.');
await spellCheck('src', true);
await spellCheck('test', true);
await spellCheck('site', true);
};
export const ts = async () => {
await tsCheck('src');
await tsCheck('test');
await tsCheck('site');
};
export const compileForProd = async () => await compileModulesForProd();
export const compileForProdFast = async () => await compileModulesForProd(true);
export const testUnit = async () => {
await test(['test/unit'], {coverageMode: 1, serialTests: true});
};
export const testUnitFast = async () => {
await test(['test/unit/core'], {coverageMode: 1});
};
export const testUnitCountAsserts = async () => {
await test(['test/unit'], {coverageMode: 2, countAsserts: true});
};
export const testUnitSaveCoverage = async () => {
await test(['test/unit/core'], {coverageMode: 3});
};
export const compileAndTestUnit = series(compileForTest, testUnit);
export const compileAndTestUnitFast = series(compileForTest, testUnitFast);
export const compileAndTestUnitSaveCoverage = series(
compileForTest,
testUnitSaveCoverage,
);
export const testPerf = async () => {
// await test(['test/perf'], {serialTests: true});
};
export const compileAndTestPerf = series(compileForTest, testPerf);
export const compileDocsPagesOnly = async () =>
await compileDocsAndAssets(false);
export const compileDocsAssetsOnly = async () =>
await compileDocsAndAssets(false, false);
export const compileDocs = async () => await compileDocsAndAssets();
export const compileForProdAndDocs = series(compileForProd, compileDocs);
export const testE2e = async () => {
// await test(['test/e2e'], {puppeteer: true});
};
export const compileAndTestE2e = series(compileForProdAndDocs, testE2e);
export const testProd = async () => {
await execute('attw --pack dist --format table-flipped --profile esm-only');
await test(['test/prod']);
};
export const compileAndTestProd = series(compileForProdAndDocs, testProd);
export const serveDocs = async () => {
const {createTestServer} = await import('./test/server.mjs');
createTestServer(DOCS_DIR, '8081');
};
export const preCommit = series(
parallel(lint, spell, ts),
compileForTest,
testUnit,
compileForProd,
);
export const prePublishPackage = series(
npmInstall,
compileForTest,
parallel(lint, spell, ts),
testUnitCountAsserts,
testPerf,
compileForProd,
testProd,
compileDocs,
testE2e,
);
export const publishPackage = series(prePublishPackage, npmPublish);