-
Notifications
You must be signed in to change notification settings - Fork 10
/
downlevel.js
49 lines (39 loc) · 2.05 KB
/
downlevel.js
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
const childProcess = require("child_process");
const fs = require("fs");
const main = () => {
const tempFolder = `temp/downlevel`;
childProcess.execSync(`rm -rf ${tempFolder}`, { stdio: "inherit" });
fs.mkdirSync(`${tempFolder}`, { recursive: true });
childProcess.execSync(`cp -r dist ${tempFolder}/0`, { stdio: "inherit" });
for (let i = 1; i < 10; i++) {
const [prev, next] = [i - 1, i].map(n => `${tempFolder}/${n}`);
console.log(`downleveling into ${next}`);
childProcess.execSync(`downlevel-dts ${prev} ${next} --to=3.4`, { stdio: "inherit" });
const [prevResult, nextResult] = [prev, next].map(folder =>
fs.readFileSync(`${folder}/generated/interface.d.ts`).toString()
);
if (prevResult === nextResult) {
if (i <= 2) {
throw new Error(
`downleveling stabilised too quickly, maybe https://github.com/sandersn/downlevel-dts/issues/50 has been fixed?`
);
}
const target = `ts34/dist`;
console.log(`downleveling stabilised after ${i} iterations, copying to ${target}.`);
childProcess.execSync(`rm -rf ${target}`, { stdio: "inherit" });
fs.mkdirSync("ts34", { recursive: true });
childProcess.execSync(`cp -r ${next} ${target}`, { stdio: "inherit" });
console.log(`Replacing push implementation incompatible with typescript < 4`);
childProcess.execSync(`mv ${target}/push.ts34.d.ts ${target}/push.d.ts`, { stdio: "inherit" });
console.log(`Running old version of typescript on output`);
childProcess.execSync(`npx -p [email protected] tsc ts34/dist/index.d.ts --noEmit`, { stdio: "inherit" });
console.log(`Removing temporary files`);
childProcess.execSync(`rm -rf ${tempFolder}`, { stdio: "inherit" });
console.log("Done.");
return;
}
console.log({ prev, next }, "had different outputs, running again");
}
throw new Error(`Downleveling failed!`);
};
main();