Skip to content

Commit

Permalink
Merge pull request #100 from Microsoft/listen
Browse files Browse the repository at this point in the history
Add option to run as a server
  • Loading branch information
Andy authored and sandersn committed Nov 29, 2021
1 parent 3040512 commit 97bfcbb
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/dtslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function main(): Promise<void> {
const args = process.argv.slice(2);
let dirPath = process.cwd();
let onlyTestTsNext = false;
let shouldListen = false;

for (const arg of args) {
switch (arg) {
Expand All @@ -28,6 +29,12 @@ async function main(): Promise<void> {
onlyTestTsNext = true;
break;

// Only for use by types-publisher.
// Listens for { path, onlyTestTsNext } messages and ouputs { path, status }.
case "--listen":
shouldListen = true;
break;

default:
if (arg.startsWith("--")) {
console.error(`Unknown option '${arg}'`);
Expand All @@ -45,18 +52,34 @@ async function main(): Promise<void> {
}

await installAll();
await runTests(dirPath, onlyTestTsNext);

if (shouldListen) {
listen(dirPath);
} else {
await runTests(dirPath, onlyTestTsNext);
}
}

function usage(): void {
console.error("Usage: dtslint [--version] [--noLint] [--installAll]");
console.error("Usage: dtslint [--version] [--installAll]");
console.error("Args:");
console.error(" --version Print version and exit.");
console.error(" --noLint Just run 'tsc'. (Not recommended.)");
console.error(" --installAll Cleans and installs all TypeScript versions.");
console.error(" --onlyTestTsNext Only run with `typescript@next`, not with the minimum version.");
}

function listen(dirPath: string): void {
process.on("message", (message: {}) => {
const { path, onlyTestTsNext } = message as { path: string, onlyTestTsNext: boolean };
runTests(joinPaths(dirPath, path), onlyTestTsNext)
.catch(e => e.stack)
.then(maybeError => {
process.send!({ path, status: maybeError === undefined ? "OK" : maybeError });
})
.catch(e => console.error(e.stack));
});
}

async function runTests(dirPath: string, onlyTestTsNext: boolean): Promise<void> {
const text = await readFile(joinPaths(dirPath, "index.d.ts"), "utf-8");
// If this *is* on DefinitelyTyped, types-publisher will fail if it can't parse the header.
Expand All @@ -73,7 +96,7 @@ async function runTests(dirPath: string, onlyTestTsNext: boolean): Promise<void>
await checkPackageJson(dirPath);
}
await checkTsconfig(dirPath, dt);
const err = await test(dirPath, minVersion, onlyTestTsNext);
const err = await lint(dirPath, minVersion, onlyTestTsNext);
if (err) {
throw new Error(err);
}
Expand Down Expand Up @@ -104,14 +127,6 @@ function getTypeScriptVersion(text: string): TypeScriptVersion {
return parseTypeScriptVersionLine(line);
}

async function test(
dirPath: string,
minVersion: TypeScriptVersion,
onlyTestTsNext: boolean,
): Promise<string | undefined> {
return lint(dirPath, minVersion, onlyTestTsNext);
}

if (!module.parent) {
main().catch(err => {
console.error(err.stack);
Expand Down

0 comments on commit 97bfcbb

Please sign in to comment.