2019-10-31 15:30:26 +00:00
|
|
|
/* eslint-disable no-console */
|
2019-10-23 03:47:19 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
const { resolve } = require("path");
|
|
|
|
const { exec } = require("child_process");
|
2024-04-25 18:06:55 +00:00
|
|
|
const { dir } = require("tmp-promise");
|
2019-10-23 03:47:19 +00:00
|
|
|
const { writeFile } = require("fs-extra");
|
2019-11-16 23:01:34 +00:00
|
|
|
const toneJson = require("../../docs/tone.json");
|
2019-10-23 03:47:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all of the examples
|
|
|
|
*/
|
|
|
|
function findExamples(obj) {
|
|
|
|
let examples = [];
|
2024-04-25 18:06:55 +00:00
|
|
|
|
|
|
|
function traverse(node) {
|
|
|
|
if (node.comment && node.comment.blockTags) {
|
|
|
|
node.comment.blockTags.forEach((tag) => {
|
|
|
|
if (tag.tag === "@example") {
|
|
|
|
tag.content.forEach((example) => {
|
|
|
|
examples.push(
|
|
|
|
example.text.trim().replace(/^```ts\n|```$/g, "")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2019-10-23 03:47:19 +00:00
|
|
|
});
|
|
|
|
}
|
2024-04-25 18:06:55 +00:00
|
|
|
|
|
|
|
["children", "getSignature", "setSignature", "signatures"].forEach(
|
|
|
|
(prop) => {
|
|
|
|
if (prop in node) {
|
|
|
|
if (Array.isArray(node[prop])) {
|
|
|
|
node[prop].forEach((child) => traverse(child));
|
|
|
|
} else {
|
|
|
|
traverse(node[prop]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-10-23 03:47:19 +00:00
|
|
|
}
|
2024-04-25 18:06:55 +00:00
|
|
|
|
|
|
|
traverse(obj);
|
2019-10-23 03:47:19 +00:00
|
|
|
// filter any repeats
|
|
|
|
return [...new Set(examples)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A promise version of exec
|
|
|
|
*/
|
|
|
|
function execPromise(cmd) {
|
|
|
|
return new Promise((done, error) => {
|
2020-07-19 16:41:04 +00:00
|
|
|
exec(cmd, (_, output) => {
|
|
|
|
if (output) {
|
2020-07-18 02:17:30 +00:00
|
|
|
error(output);
|
2019-10-23 03:47:19 +00:00
|
|
|
} else {
|
2020-07-18 02:17:30 +00:00
|
|
|
done();
|
2019-10-23 03:47:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the string through the typescript compiler
|
|
|
|
*/
|
2024-04-25 18:06:55 +00:00
|
|
|
async function testExampleString(str, tmpDir, index) {
|
2020-04-17 02:24:18 +00:00
|
|
|
// str = str.replace("from \"tone\"", `from "${resolve(__dirname, "../../")}"`);
|
2020-07-18 02:10:47 +00:00
|
|
|
str = `
|
|
|
|
import * as Tone from "${resolve(__dirname, "../../")}"
|
2020-07-26 20:55:06 +00:00
|
|
|
function main(){
|
|
|
|
${str}
|
|
|
|
}
|
|
|
|
main();
|
2020-07-18 02:10:47 +00:00
|
|
|
`;
|
2024-04-25 18:06:55 +00:00
|
|
|
await writeFile(resolve(tmpDir, index + ".ts"), str);
|
2019-10-23 03:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2024-04-25 18:06:55 +00:00
|
|
|
const examples = findExamples(toneJson);
|
2019-10-23 03:47:19 +00:00
|
|
|
let passed = 0;
|
2024-04-25 18:06:55 +00:00
|
|
|
|
|
|
|
const tmp = await dir({ unsafeCleanup: true });
|
|
|
|
await Promise.all(
|
|
|
|
examples.map((e, i) => testExampleString(e, tmp.path, i))
|
|
|
|
);
|
|
|
|
|
|
|
|
await execPromise(
|
|
|
|
`tsc --noEmit --target es5 --lib dom,ES2015 ${tmp.path}/*.ts`
|
|
|
|
);
|
|
|
|
|
|
|
|
await tmp.cleanup();
|
|
|
|
|
|
|
|
console.log(`Tested ${examples.length} examples`);
|
2019-10-23 03:47:19 +00:00
|
|
|
}
|
|
|
|
main();
|