From cc092672b605b36c8b2feea8175ec7ed1890c9e5 Mon Sep 17 00:00:00 2001 From: mmkal Date: Sat, 5 Dec 2020 14:08:30 -0500 Subject: [PATCH] downlevel in a loop, until stabilised Workaround for https://github.com/sandersn/downlevel-dts/issues/50 --- downlevel.js | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 downlevel.js diff --git a/downlevel.js b/downlevel.js new file mode 100644 index 00000000..3d2e232f --- /dev/null +++ b/downlevel.js @@ -0,0 +1,42 @@ +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 typescript@3.8 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(); diff --git a/package.json b/package.json index b5f74169..3a7ce92f 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "clean": "del-cli 'dist' 'ts34' 'src/generated' 'test/generated'", "codegen": "ts-node codegen/generate-schema && ts-node codegen/generate-client && ts-node codegen/generate-tests", "compile": "tsc -p tsconfig.lib.json", - "postcompile": "downlevel-dts dist ts34/dist --to=3.4 && mv ts34/dist/push.ts34.d.ts ts34/dist/push.d.ts", + "postcompile": "node downlevel", "coverage": "yarn test --coverage", "coveralls": "yarn coverage --coverageReporters=text-lcov | coveralls", "lint": "eslint --max-warnings 0 --ext .ts,.js,.md .",