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");
|
|
|
|
const { file } = require("tmp-promise");
|
|
|
|
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
|
|
|
|
2020-07-19 19:41:20 +00:00
|
|
|
const testSplit = parseInt(process.env.TEST_EXAMPLES || "0");
|
|
|
|
|
2019-10-23 03:47:19 +00:00
|
|
|
/**
|
|
|
|
* Get all of the examples
|
|
|
|
*/
|
|
|
|
function findExamples(obj) {
|
|
|
|
let examples = [];
|
2019-11-22 18:40:53 +00:00
|
|
|
for (const prop in obj) {
|
2019-10-23 03:47:19 +00:00
|
|
|
if (Array.isArray(obj[prop])) {
|
2020-07-19 16:41:04 +00:00
|
|
|
obj[prop].forEach((child) => {
|
2019-10-23 03:47:19 +00:00
|
|
|
examples = [...examples, ...findExamples(child)];
|
|
|
|
});
|
|
|
|
} else if (prop === "comment" && obj[prop].tags) {
|
|
|
|
examples = [
|
2020-04-17 02:24:18 +00:00
|
|
|
...examples,
|
2020-07-19 16:41:04 +00:00
|
|
|
...obj[prop].tags
|
2021-12-18 22:36:57 +00:00
|
|
|
.filter((tag) => tag.tag === "example")
|
|
|
|
.map((tag) => tag.text),
|
2019-10-23 03:47:19 +00:00
|
|
|
];
|
|
|
|
} else if (typeof obj[prop] === "object") {
|
|
|
|
examples = [...examples, ...findExamples(obj[prop])];
|
|
|
|
} else {
|
|
|
|
// console.log(prop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
async function testExampleString(str) {
|
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
|
|
|
`;
|
2019-10-23 03:47:19 +00:00
|
|
|
const { path, cleanup } = await file({ postfix: ".ts" });
|
|
|
|
try {
|
2020-07-18 15:36:18 +00:00
|
|
|
// work with file here in fd
|
|
|
|
await writeFile(path, str);
|
2020-07-19 16:41:04 +00:00
|
|
|
await execPromise(
|
|
|
|
`tsc --noEmit --target es5 --lib dom,ES2015 ${path}`
|
|
|
|
);
|
2019-10-23 03:47:19 +00:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2020-07-19 19:41:20 +00:00
|
|
|
let examples = findExamples(toneJson);
|
|
|
|
if (testSplit > 0) {
|
|
|
|
// split it in half and choose either the first or second half
|
|
|
|
const halfLength = Math.ceil(examples.length / 2);
|
|
|
|
const splitStart = (testSplit - 1) * halfLength;
|
2021-12-18 22:36:57 +00:00
|
|
|
const splitEnd = testSplit * halfLength;
|
2020-07-19 19:41:20 +00:00
|
|
|
examples = examples.slice(splitStart, splitEnd);
|
2021-12-18 22:36:57 +00:00
|
|
|
console.log(`testing examples ${splitStart} - ${splitEnd}`);
|
2020-07-19 19:41:20 +00:00
|
|
|
}
|
2019-10-23 03:47:19 +00:00
|
|
|
let passed = 0;
|
2020-07-19 17:42:15 +00:00
|
|
|
for (let i = 0; i < examples.length; i++) {
|
|
|
|
const example = examples[i];
|
2019-10-23 03:47:19 +00:00
|
|
|
try {
|
|
|
|
await testExampleString(example);
|
|
|
|
passed++;
|
2019-10-31 15:30:26 +00:00
|
|
|
// print a dot for each passed example
|
|
|
|
process.stdout.write(".");
|
2019-10-31 15:42:58 +00:00
|
|
|
// add a new line occasionally
|
|
|
|
if (passed % 100 === 0) {
|
|
|
|
process.stdout.write("\n");
|
|
|
|
}
|
2019-10-23 03:47:19 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(example + "\n" + e);
|
2019-11-22 20:48:22 +00:00
|
|
|
throw e;
|
2019-10-23 03:47:19 +00:00
|
|
|
}
|
2020-07-19 17:42:15 +00:00
|
|
|
}
|
2020-07-19 17:12:08 +00:00
|
|
|
console.log(`\nvalid examples ${passed}/${examples.length}`);
|
2020-07-19 16:41:04 +00:00
|
|
|
if (passed !== examples.length) {
|
|
|
|
throw new Error("didn't pass all tests");
|
|
|
|
}
|
2019-10-23 03:47:19 +00:00
|
|
|
}
|
|
|
|
main();
|