forked from GoogleChrome/developer.chrome.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.js
More file actions
65 lines (55 loc) · 1.92 KB
/
types.js
File metadata and controls
65 lines (55 loc) · 1.92 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
/**
* @fileoverview Fetches and preprocesses types from Chrome sources.
*/
const dtsParse = require('./lib/dts-parse.js');
const tmp = require('tmp');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const chromeTypesTag = 'latest';
/**
* @param {string} targetDir
*/
async function fetchAndPrepare(targetDir) {
const packageName = `chrome-types@${chromeTypesTag}`;
/** @type {childProcess.ExecFileSyncOptions} */
const options = {cwd: targetDir, stdio: 'inherit'};
childProcess.execFileSync('npm', ['install', packageName], options);
const packageDir = path.join(targetDir, 'node_modules/chrome-types');
/** @type {{version: string}} */
const packageJson = JSON.parse(
fs.readFileSync(path.join(packageDir, 'package.json'), 'utf-8')
);
console.warn('fetched', packageName, targetDir, packageJson.version);
return path.join(packageDir, '_all.d.ts');
}
/**
* @param {string} targetFile
*/
async function parse(targetFile) {
const defs = await dtsParse({sources: [targetFile]});
const outputFile = path.join(__dirname, '../data/chrome-types.json');
fs.writeFileSync(outputFile, JSON.stringify(defs, undefined, 2));
}
async function run() {
const t = tmp.dirSync();
try {
if (process.argv.length === 2) {
// Normal fetch, get from npm.
const targetFile = await fetchAndPrepare(t.name);
await parse(targetFile);
} else if (process.argv.length === 3) {
// Supports reading an arbitrary types file. Copy to our working folder first.
const file = process.argv[2];
const targetFile = path.join(t.name, '_all.d.ts');
fs.copyFileSync(file, targetFile);
console.warn('working on', path.join(t.name, '_all.d.ts'));
await parse(path.join(t.name, '_all.d.ts'));
} else {
throw new Error('invalid usage: ./types.js <path_to_dts>');
}
} finally {
fs.rmSync(t.name, {recursive: true});
}
}
run();