Skip to content

Commit

Permalink
downlevel in a loop, until stabilised
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Dec 5, 2020
1 parent 504c4d8 commit cc09267
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions downlevel.js
Original file line number Diff line number Diff line change
@@ -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 [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();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down

0 comments on commit cc09267

Please sign in to comment.