running examples through typescript checker

This commit is contained in:
Yotam Mann 2020-07-19 10:50:44 -07:00
parent 554a560737
commit 8a82a5f794
2 changed files with 74 additions and 0 deletions

View file

@ -42,6 +42,11 @@ jobs:
- npm run docs
- npm run test:examples
env : TEST_EXAMPLES=1
- stage: test
script:
- npm run build
- npm run test:html
env : TEST_HTML=1
- stage: deploy
os: linux
script: npm run build

69
test/scripts/test_html.js Normal file
View file

@ -0,0 +1,69 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-var-requires */
const { JSDOM } = require("jsdom");
const glob = require("glob");
const { resolve } = require("path");
const { readFile, writeFile } = require("fs-extra");
const { exec } = require("child_process");
const { file } = require("tmp-promise");
/**
* 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, "../../")}"
let ui: any;
let drawer: any;
let meter: any;
let piano: any;
let fft: any;
let waveform: any;
let document: any;
let p5: any;
${str}
`;
const { path, cleanup } = await file({ postfix: ".ts" });
// work with file here in fd
await writeFile(path, str);
try {
await execPromise(`tsc --noEmit --target es5 --lib dom,ES2015 ${path}`);
} finally {
cleanup();
}
}
const htmlFiles = glob.sync(resolve(__dirname, "../../examples/*.html"));
async function main() {
for (let i = 0; i < htmlFiles.length; i++) {
const path = htmlFiles[i];
const fileAsString = (await readFile(path)).toString();
const dom = new JSDOM(fileAsString);
const scriptTag = dom.window.document.querySelector("body script");
if (scriptTag) {
try {
await testExampleString(scriptTag.textContent);
console.log("passed", path);
} catch (e) {
console.log("failed", path);
console.log(e);
throw new Error(e);
}
}
}
}
main();