2020-07-26 20:37:38 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
const { JSDOM } = require("jsdom");
|
|
|
|
const { resolve } = require("path");
|
|
|
|
const { readFile, writeFile } = require("fs-extra");
|
|
|
|
const { exec } = require("child_process");
|
|
|
|
const { file } = require("tmp-promise");
|
|
|
|
const { Converter } = require("showdown");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A promise version of exec
|
|
|
|
*/
|
|
|
|
function execPromise(cmd) {
|
|
|
|
return new Promise((done, error) => {
|
|
|
|
exec(cmd, (e, output) => {
|
|
|
|
if (e) {
|
|
|
|
error(output);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testExampleString(str) {
|
|
|
|
// str = str.replace("from \"tone\"", `from "${resolve(__dirname, "../../")}"`);
|
|
|
|
str = `
|
|
|
|
import * as Tone from "${resolve(__dirname, "../../")}"
|
|
|
|
${str}
|
|
|
|
`;
|
|
|
|
const { path, cleanup } = await file({ postfix: ".ts" });
|
|
|
|
// work with file here in fd
|
|
|
|
await writeFile(path, str);
|
|
|
|
try {
|
2024-05-03 18:31:14 +00:00
|
|
|
await execPromise(
|
|
|
|
`tsc --noEmit --target es5 --lib dom,ES2015 ${path}`
|
|
|
|
);
|
2020-07-26 20:37:38 +00:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2024-05-03 18:31:14 +00:00
|
|
|
const readme = (
|
|
|
|
await readFile(resolve(__dirname, "../../README.md"))
|
|
|
|
).toString();
|
2020-07-26 20:37:38 +00:00
|
|
|
const html = new Converter().makeHtml(readme);
|
|
|
|
const dom = new JSDOM(html);
|
|
|
|
const scripts = dom.window.document.querySelectorAll("code.javascript");
|
|
|
|
for (let i = 0; i < scripts.length; i++) {
|
|
|
|
try {
|
|
|
|
await testExampleString(scripts[i].textContent);
|
|
|
|
process.stdout.write(".");
|
|
|
|
} catch (e) {
|
|
|
|
console.log("\nfailed", scripts[i].textContent);
|
|
|
|
console.log(e);
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("\n");
|
|
|
|
}
|
|
|
|
main();
|