Last active
June 9, 2020 12:58
-
-
Save benjie/1d879ba323e7e831e58f78307356c277 to your computer and use it in GitHub Desktop.
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 { promises: fsp } = require("fs"); | |
async function main(name) { | |
const content = await fsp.readFile(`./${name}.test.ts.snap`, "utf8"); | |
const lines = content.split("\n"); | |
const results = {}; | |
let header = ""; | |
for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) { | |
const line = lines[lineNumber]; | |
if (line.startsWith("//")) { | |
header += line + "\n"; | |
} else if (line.trim() === "") { | |
// ignore | |
} else if (line.startsWith("exports[`")) { | |
const matches = line.match( | |
/^exports\[`([a-zA-Z0-9._-]+?)\.graphql 1`\] = `/, | |
); | |
if (!matches) { | |
throw new Error( | |
`Could not process exports line ${lineNumber}: ${line}`, | |
); | |
} | |
const [_, key] = matches; | |
const finish = lines.findIndex( | |
(endLine, index) => index >= lineNumber && endLine.startsWith("`;"), | |
); | |
if (finish > lineNumber) { | |
results[key] = | |
"`\n" + lines.slice(lineNumber + 1, finish + 1).join("\n"); | |
lineNumber = finish; | |
} else { | |
throw new Error( | |
`Failed to find terminal for line ${lineNumber}: ${line}`, | |
); | |
} | |
} else { | |
throw new Error(`Failed to process line ${lineNumber}: ${line}`); | |
} | |
} | |
for (const key in results) { | |
try { | |
await fsp.mkdir(`../${name}`); | |
} catch (e) { | |
// Noop | |
} | |
try { | |
await fsp.mkdir(`../${name}/__snapshots__`); | |
} catch (e) { | |
// Noop | |
} | |
await fsp.writeFile( | |
`../${name}/__snapshots__/${key}.test.graphql.snap`, | |
`\ | |
${header} | |
exports[\`execute 1\`] = ${results[key]} | |
`, | |
); | |
} | |
} | |
main("mutations").catch((e) => { | |
console.error(e); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment