Repository navigation
Expand file tree
/
Copy pathworkbox-types.js
More file actions
89 lines (77 loc) · 2.62 KB
/
Copy pathworkbox-types.js
File metadata and controls
89 lines (77 loc) · 2.62 KB
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
/**
* @fileoverview Fetches and preprocesses types from Workbox.
*/
const dtsParse = require('./lib/dts-parse.js');
const tmp = require('tmp');
const fs = require('fs');
const mvdir = require('mvdir');
const path = require('path');
const childProcess = require('child_process');
// Just contains SW packages for now.
const workboxPackages = [
'workbox-background-sync',
'workbox-broadcast-update',
'workbox-build',
'workbox-cacheable-response',
'workbox-core',
'workbox-expiration',
'workbox-google-analytics',
'workbox-navigation-preload',
'workbox-precaching',
'workbox-range-requests',
'workbox-recipes',
'workbox-routing',
'workbox-strategies',
'workbox-streams',
'workbox-webpack-plugin',
'workbox-window',
];
/**
* @param {string[]} packages
* @param {string} targetDir
*/
async function fetchAndPrepare(packages, targetDir) {
/** @type {childProcess.ExecFileSyncOptions} */
const options = {cwd: targetDir, stdio: 'inherit'};
childProcess.execFileSync('npm', ['install', ...packages], options);
const versionData = Object.fromEntries(
packages.map(name => {
const p = path.join(targetDir, 'node_modules', name, 'package.json');
/** @type {{version: string}} */
const packageJson = JSON.parse(fs.readFileSync(p, 'utf-8'));
return [name, packageJson.version];
})
);
console.warn('fetched', versionData);
}
async function run() {
const t = tmp.dirSync();
try {
// webpack is a peerDependency of workbox-webpack-plugin, and needs to be
// manually installed.
childProcess.execFileSync('npm', ['install', 'webpack'], {
cwd: t.name,
stdio: 'inherit',
});
await fetchAndPrepare(workboxPackages, t.name);
const sources = [];
for (const packageName of workboxPackages) {
const packagePath = path.join(t.name, 'node_modules', packageName);
const packageJsonPath = path.join(packagePath, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const typesPathParsed = path.parse(packageJson.types || 'index.d.ts');
const isNotTopLevel = !!typesPathParsed.dir;
if (isNotTopLevel) {
const typesOutputPath = path.join(packagePath, typesPathParsed.dir);
await mvdir(typesOutputPath, packagePath, {overwrite: true});
}
sources.push(path.join(packagePath, 'index.d.ts'));
}
const defs = await dtsParse({sources, mode: 'workbox'});
const outputFile = path.join(__dirname, '../data/workbox-types.json');
fs.writeFileSync(outputFile, JSON.stringify(defs, undefined, 2));
} finally {
fs.rmSync(t.name, {recursive: true});
}
}
run();