-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
208 additions
and
34 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* 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"); | ||
const toneJson = require("../docs/tone.json"); | ||
const eachLimit = require("async/eachLimit"); | ||
|
||
/** | ||
* Get all of the examples | ||
*/ | ||
function findExamples(obj) { | ||
let examples = []; | ||
for (let prop in obj) { | ||
if (Array.isArray(obj[prop])) { | ||
obj[prop].forEach(child => { | ||
examples = [...examples, ...findExamples(child)]; | ||
}); | ||
} else if (prop === "comment" && obj[prop].tags) { | ||
examples = [ | ||
...examples, | ||
...obj[prop].tags.filter(tag => tag.tag === "example").map(tag => tag.text) | ||
]; | ||
} 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) => { | ||
exec(cmd, (e, output) => { | ||
if (e) { | ||
error(output); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Run the string through the typescript compiler | ||
*/ | ||
async function testExampleString(str) { | ||
str = str.replace("from \"tone\"", `from "${resolve(__dirname, "../")}"`); | ||
const { path, cleanup } = await file({ postfix: ".ts" }); | ||
// work with file here in fd | ||
await writeFile(path, str); | ||
try { | ||
await execPromise(`tsc ${path}`); | ||
} finally { | ||
cleanup(); | ||
} | ||
} | ||
|
||
const examples = findExamples(toneJson); | ||
|
||
async function main() { | ||
let passed = 0; | ||
await eachLimit(examples, 4, async example => { | ||
try { | ||
await testExampleString(example); | ||
passed++; | ||
} catch (e) { | ||
console.log(example + "\n" + e); | ||
} | ||
}); | ||
console.log(`valid examples ${passed}/${examples.length}`); | ||
} | ||
main(); | ||
|