Skip to content

Commit ddb7993

Browse files
Script to Prune .d.ts files (with tests) (#4091)
1 parent 83affbf commit ddb7993

35 files changed

+4139
-33
lines changed

repo-scripts/prune-dts/package.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "firebase-repo-scripts-prune-dts",
3+
"version": "0.1.0",
4+
"private": true,
5+
"engines": {
6+
"node": "^8.13.0 || >=10.10.0"
7+
},
8+
"description": "A script to prune non-exported types from a d.ts.",
9+
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
10+
"scripts": {
11+
"prettier": "prettier --write '**/*.ts'",
12+
"test": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha --require ts-node/register --timeout 5000 *.test.ts"
13+
},
14+
"license": "Apache-2.0",
15+
"dependencies": {
16+
"eslint": "7.14.0",
17+
"eslint-plugin-unused-imports": "1.0.0",
18+
"prettier": "2.2.0"
19+
},
20+
"repository": {
21+
"directory": "repo-scripts/prune-dts",
22+
"type": "git",
23+
"url": "https://github.com/firebase/firebase-js-sdk.git"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/firebase/firebase-js-sdk/issues"
27+
},
28+
"nyc": {
29+
"extension": [
30+
".ts"
31+
],
32+
"reportDir": "./coverage/node"
33+
},
34+
"devDependencies": {
35+
"@types/eslint": "7.2.5",
36+
"@types/prettier": "2.1.5",
37+
"mocha": "8.2.1"
38+
}
39+
}
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import * as os from 'os';
19+
import * as fs from 'fs';
20+
import * as path from 'path';
21+
import { format, resolveConfig } from 'prettier';
22+
import { expect } from 'chai';
23+
import { describe, it } from 'mocha';
24+
25+
import { pruneDts } from './prune-dts';
26+
27+
const testCasesDir = path.resolve(__dirname, 'tests');
28+
const tmpDir = os.tmpdir();
29+
30+
const testDataFilter = /(.*).input.d.ts/;
31+
const testCaseFilterRe = /.*/;
32+
33+
async function runScript(inputFile: string): Promise<string> {
34+
const outputFile = path.resolve(tmpDir, 'output.d.ts');
35+
pruneDts(inputFile, outputFile);
36+
return outputFile;
37+
}
38+
39+
interface TestCase {
40+
name: string;
41+
inputFileName: string;
42+
outputFileName: string;
43+
}
44+
45+
function getTestCases(): TestCase[] {
46+
if (
47+
!fs.existsSync(testCasesDir) ||
48+
!fs.lstatSync(testCasesDir).isDirectory()
49+
) {
50+
throw new Error(`${testCasesDir} folder does not exist`);
51+
}
52+
53+
return fs
54+
.readdirSync(testCasesDir)
55+
.filter((fileName: string) => testDataFilter.test(fileName))
56+
.filter((fileName: string) => testCaseFilterRe.test(fileName))
57+
.map((fileName: string) => {
58+
const testCaseName = fileName.match(testDataFilter)![1];
59+
60+
const inputFileName = `${testCaseName}.input.d.ts`;
61+
const outputFileName = `${testCaseName}.output.d.ts`;
62+
63+
const name = testCaseName.replace(/-/g, ' ');
64+
return { name, inputFileName, outputFileName };
65+
});
66+
}
67+
68+
describe('Prune DTS', () => {
69+
for (const testCase of getTestCases()) {
70+
it(testCase.name, async () => {
71+
const absoluteInputFile = path.resolve(
72+
testCasesDir,
73+
testCase.inputFileName
74+
);
75+
const absoluteOutputFile = path.resolve(
76+
testCasesDir,
77+
testCase.outputFileName
78+
);
79+
80+
const tmpFile = await runScript(absoluteInputFile);
81+
const prettierConfig = await resolveConfig(absoluteInputFile);
82+
83+
const expectedDtsUnformatted = fs.readFileSync(
84+
absoluteOutputFile,
85+
'utf-8'
86+
);
87+
const expectedDts = format(expectedDtsUnformatted, {
88+
filepath: absoluteOutputFile,
89+
...prettierConfig
90+
});
91+
const actualDtsUnformatted = fs.readFileSync(tmpFile, 'utf-8');
92+
const actualDts = format(actualDtsUnformatted, {
93+
filepath: tmpFile,
94+
...prettierConfig
95+
});
96+
97+
expect(actualDts).to.equal(expectedDts);
98+
});
99+
}
100+
});

0 commit comments

Comments
 (0)