Created
October 7, 2019 21:49
-
-
Save axross/86d83e64c39acbe64d602175cae9ba32 to your computer and use it in GitHub Desktop.
Migration script changes Node ts files into Deno ts files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const path = require("path"); | |
const fileNames = fs.readdirSync(path.resolve(__dirname, "./solutions")); | |
const testFiles = fileNames.filter(fileName => fileName.endsWith("_test.ts")); | |
for (const fileName of testFiles) { | |
// const fileName = testFiles[Math.floor(Math.random() * testFiles.length)]; | |
// const fileName = "search_a2d_matrix2_test.ts"; | |
try { | |
const text = fs.readFileSync( | |
path.resolve(__dirname, "./solutions", fileName), | |
"utf-8" | |
); | |
const lines = text.split("\n"); | |
const importLine = lines.findIndex(line => | |
/^import [0-9A-z]+ from "\.\/[0-9A-z]+";$/.test(line) | |
); | |
const testCaseStart = lines.findIndex(line => | |
/^ *const TEST_CASES = new Map(<.+>)?\(\[/.test(line) | |
); | |
const testCaseEnd = lines.findIndex( | |
(line, index) => index > testCaseStart && line.startsWith(" ]);") | |
); | |
const testForLoopStart = lines.findIndex(line => | |
line.startsWith(" for (const [") | |
); | |
const testForLoopEnd = lines.findIndex( | |
(line, index) => index > testForLoopStart && line.startsWith(" }") | |
); | |
if (testCaseStart !== -1) { | |
const functionName = /^import ([0-9A-z]+) from "\.\/[0-9A-z]+";$/.exec( | |
lines[importLine] | |
)[1]; | |
const testCases = JSON.parse( | |
"[" + lines.slice(testCaseStart + 1, testCaseEnd).join("") + "]" | |
); | |
let isMaybeArrayArgs = false; | |
if (testCases.some(([args, _]) => args.includes(null))) { | |
isMaybeArrayArgs = true; | |
} | |
if (testCases.some(([args, _]) => args.some(Array.isArray))) { | |
isMaybeArrayArgs = true; | |
} | |
lines.splice( | |
importLine, | |
1, | |
`import ${functionName} from "./${fileName.replace("_test.ts", ".ts")}";` | |
); | |
lines.splice(testForLoopStart, testForLoopEnd - testForLoopStart + 1); | |
lines.splice( | |
testCaseStart, | |
testCaseEnd - testCaseStart + 1, | |
...testCases.map(([args, expected]) => | |
isMaybeArrayArgs | |
? ` assertEquals(${functionName}(${JSON.stringify( | |
args | |
)}), ${JSON.stringify(expected)});` | |
: ` assert(${functionName}(${JSON.stringify( | |
args | |
)}) === ${JSON.stringify(expected)});` | |
) | |
); | |
console.log("OK", fileName); | |
// console.log(isMaybeArrayArgs); | |
// console.log(lines.join("\n")); | |
fs.writeFileSync( | |
path.resolve(__dirname, "./solutions", fileName), | |
lines.join("\n"), | |
{ encoding: "utf8", flag: "w" } | |
); | |
// break; | |
} else { | |
console.log("SKIP", fileName); | |
} | |
} catch (error) { | |
console.log("ERROR", fileName); | |
console.log(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment