diff --git a/.gitignore b/.gitignore index 6e36a8e8..6793298c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -bin node_modules typescript-installs +dist diff --git a/package.json b/package.json index 3854ca11..46b58b59 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "dt.json", "dtslint.json" ], - "main": "bin", - "bin": "./bin/index.js", + "main": "dist", + "bin": "./dist/bin/index.js", "contributors": [ "Andy Hanson (https://github.com/andy-ms)", "Dan Vanderkam (https://github.com/danvk)" diff --git a/src/bin/index.ts b/src/bin/index.ts new file mode 100644 index 00000000..80299cce --- /dev/null +++ b/src/bin/index.ts @@ -0,0 +1,89 @@ +#!/usr/bin/env node +import { join as joinPaths } from "path"; + +import { cleanInstalls, installAll, installNext, runTests } from "../"; + +async function main(): Promise { + const args = process.argv.slice(2); + let dirPath = process.cwd(); + let onlyTestTsNext = false; + let shouldListen = false; + + for (const arg of args) { + switch (arg) { + case "--installAll": + console.log("Cleaning old installs and installing for all TypeScript versions..."); + await cleanInstalls(); + await installAll(); + return; + + case "--version": + console.log(require("../package.json").version); + return; + + case "--onlyTestTsNext": + 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}'`); + usage(); + process.exit(1); + } + + const path = arg.indexOf("@") === 0 && arg.indexOf("/") !== -1 + // we have a scoped module, e.g. @bla/foo + // which should be converted to bla__foo + ? arg.substr(1).replace("/", "__") + : arg; + dirPath = joinPaths(dirPath, path); + } + } + + if (shouldListen) { + listen(dirPath); + // Do this *after* to ensure messages sent during installation aren't dropped. + await installAll(); + } else { + if (onlyTestTsNext) { + await installNext(); + } else { + await installAll(); + } + await runTests(dirPath, onlyTestTsNext); + } +} + +function usage(): void { + console.error("Usage: dtslint [--version] [--installAll]"); + console.error("Args:"); + console.error(" --version Print version and exit."); + 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)); + }); +} + +if (!module.parent) { + main().catch(err => { + console.error(err.stack); + process.exit(1); + }); +} diff --git a/src/index.ts b/src/index.ts index 82b71af6..de2b3063 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,89 +4,12 @@ import { readdir, readFile, stat } from "fs-extra"; import { basename, dirname, join as joinPaths } from "path"; import { checkPackageJson, checkTsconfig } from "./checks"; -import { cleanInstalls, installAll, installNext } from "./installer"; import { checkTslintJson, lint, TsVersion } from "./lint"; import { assertDefined, last, mapDefinedAsync, withoutPrefix } from "./util"; -async function main(): Promise { - const args = process.argv.slice(2); - let dirPath = process.cwd(); - let onlyTestTsNext = false; - let shouldListen = false; +export { cleanInstalls, installAll, installNext } from "./installer"; - for (const arg of args) { - switch (arg) { - case "--installAll": - console.log("Cleaning old installs and installing for all TypeScript versions..."); - await cleanInstalls(); - await installAll(); - return; - - case "--version": - console.log(require("../package.json").version); - return; - - case "--onlyTestTsNext": - 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}'`); - usage(); - process.exit(1); - } - - const path = arg.indexOf("@") === 0 && arg.indexOf("/") !== -1 - // we have a scoped module, e.g. @bla/foo - // which should be converted to bla__foo - ? arg.substr(1).replace("/", "__") - : arg; - dirPath = joinPaths(dirPath, path); - } - } - - if (shouldListen) { - listen(dirPath); - // Do this *after* to ensure messages sent during installation aren't dropped. - await installAll(); - } else { - if (onlyTestTsNext) { - await installNext(); - } else { - await installAll(); - } - await runTests(dirPath, onlyTestTsNext); - } -} - -function usage(): void { - console.error("Usage: dtslint [--version] [--installAll]"); - console.error("Args:"); - console.error(" --version Print version and exit."); - 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 { +export async function runTests(dirPath: string, onlyTestTsNext: boolean): Promise { const isOlderVersion = /^v\d+$/.test(basename(dirPath)); const indexText = await readFile(joinPaths(dirPath, "index.d.ts"), "utf-8"); @@ -194,10 +117,3 @@ function getTypeScriptVersionFromComment(text: string): TypeScriptVersion | unde } return parseTypeScriptVersionLine(line); } - -if (!module.parent) { - main().catch(err => { - console.error(err.stack); - process.exit(1); - }); -} diff --git a/test/dt-header/correct/tslint.json b/test/dt-header/correct/tslint.json index e43c1125..cea32b63 100644 --- a/test/dt-header/correct/tslint.json +++ b/test/dt-header/correct/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../../bin/rules"], + "rulesDirectory": ["../../../dist/rules"], "rules": { "dt-header": true } diff --git a/test/dt-header/wrong/tslint.json b/test/dt-header/wrong/tslint.json index e43c1125..cea32b63 100644 --- a/test/dt-header/wrong/tslint.json +++ b/test/dt-header/wrong/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../../bin/rules"], + "rulesDirectory": ["../../../dist/rules"], "rules": { "dt-header": true } diff --git a/test/expect/tslint.json b/test/expect/tslint.json index e8646206..3b9861d4 100644 --- a/test/expect/tslint.json +++ b/test/expect/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "expect": true } diff --git a/test/export-just-namespace/tslint.json b/test/export-just-namespace/tslint.json index 5c3ab252..b4635bf6 100644 --- a/test/export-just-namespace/tslint.json +++ b/test/export-just-namespace/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "export-just-namespace": true } diff --git a/test/no-any-union/tslint.json b/test/no-any-union/tslint.json index a6f28f6d..2799a1d8 100644 --- a/test/no-any-union/tslint.json +++ b/test/no-any-union/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-any-union": true } diff --git a/test/no-bad-reference/tslint.json b/test/no-bad-reference/tslint.json index 6477de50..de428a0c 100644 --- a/test/no-bad-reference/tslint.json +++ b/test/no-bad-reference/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-bad-reference": true } diff --git a/test/no-const-enum/tslint.json b/test/no-const-enum/tslint.json index f301cb1e..af4a9888 100644 --- a/test/no-const-enum/tslint.json +++ b/test/no-const-enum/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-const-enum": true } diff --git a/test/no-dead-reference/tslint.json b/test/no-dead-reference/tslint.json index 3d9bb37b..8468ead5 100644 --- a/test/no-dead-reference/tslint.json +++ b/test/no-dead-reference/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-dead-reference": true } diff --git a/test/no-import-default-of-export-equals/bad-ambient-modules/tslint.json b/test/no-import-default-of-export-equals/bad-ambient-modules/tslint.json index f4f43559..58016318 100644 --- a/test/no-import-default-of-export-equals/bad-ambient-modules/tslint.json +++ b/test/no-import-default-of-export-equals/bad-ambient-modules/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../../bin/rules"], + "rulesDirectory": ["../../../dist/rules"], "rules": { "no-import-default-of-export-equals": true } diff --git a/test/no-import-default-of-export-equals/bad-external-modules/tslint.json b/test/no-import-default-of-export-equals/bad-external-modules/tslint.json index 42c7451f..9f366bdd 100644 --- a/test/no-import-default-of-export-equals/bad-external-modules/tslint.json +++ b/test/no-import-default-of-export-equals/bad-external-modules/tslint.json @@ -1,7 +1,6 @@ { - "rulesDirectory": ["../../../bin/rules"], + "rulesDirectory": ["../../../dist/rules"], "rules": { "no-import-default-of-export-equals": true } } - \ No newline at end of file diff --git a/test/no-import-default-of-export-equals/good-ambient-modules/tslint.json b/test/no-import-default-of-export-equals/good-ambient-modules/tslint.json index f4f43559..58016318 100644 --- a/test/no-import-default-of-export-equals/good-ambient-modules/tslint.json +++ b/test/no-import-default-of-export-equals/good-ambient-modules/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../../bin/rules"], + "rulesDirectory": ["../../../dist/rules"], "rules": { "no-import-default-of-export-equals": true } diff --git a/test/no-padding/tslint.json b/test/no-padding/tslint.json index e8b7989d..5e60482b 100644 --- a/test/no-padding/tslint.json +++ b/test/no-padding/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-padding": true } diff --git a/test/no-redundant-undefined/tslint.json b/test/no-redundant-undefined/tslint.json index 0bda6a48..d24a9554 100644 --- a/test/no-redundant-undefined/tslint.json +++ b/test/no-redundant-undefined/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-redundant-undefined": true } diff --git a/test/no-relative-import-in-test/tslint.json b/test/no-relative-import-in-test/tslint.json index 66391a9a..b03fdcdb 100644 --- a/test/no-relative-import-in-test/tslint.json +++ b/test/no-relative-import-in-test/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-relative-import-in-test": true } diff --git a/test/no-single-declare-module/tslint.json b/test/no-single-declare-module/tslint.json index 9da0ac73..c6b13c3f 100644 --- a/test/no-single-declare-module/tslint.json +++ b/test/no-single-declare-module/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-single-declare-module": true } diff --git a/test/no-single-element-tuple-type/tslint.json b/test/no-single-element-tuple-type/tslint.json index 07abf62e..3b3ab378 100644 --- a/test/no-single-element-tuple-type/tslint.json +++ b/test/no-single-element-tuple-type/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-single-element-tuple-type": true } diff --git a/test/no-unnecessary-generics/tslint.json b/test/no-unnecessary-generics/tslint.json index dd1b74a5..0ad24bae 100644 --- a/test/no-unnecessary-generics/tslint.json +++ b/test/no-unnecessary-generics/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-unnecessary-generics": true } diff --git a/test/no-useless-files/tslint.json b/test/no-useless-files/tslint.json index 91da4fd1..468af240 100644 --- a/test/no-useless-files/tslint.json +++ b/test/no-useless-files/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "no-useless-files": true } diff --git a/test/prefer-declare-function/tslint.json b/test/prefer-declare-function/tslint.json index 0e67bed5..06473197 100644 --- a/test/prefer-declare-function/tslint.json +++ b/test/prefer-declare-function/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "prefer-declare-function": true } diff --git a/test/strict-export-declare-modifiers/tslint.json b/test/strict-export-declare-modifiers/tslint.json index 466c95a0..ee7a94ac 100644 --- a/test/strict-export-declare-modifiers/tslint.json +++ b/test/strict-export-declare-modifiers/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "strict-export-declare-modifiers": true } diff --git a/test/trim-file/tslint.json b/test/trim-file/tslint.json index 92f3f78e..ac69c1c4 100644 --- a/test/trim-file/tslint.json +++ b/test/trim-file/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "trim-file": true } diff --git a/test/void-return/tslint.json b/test/void-return/tslint.json index 6f223be6..ca7aa6cd 100644 --- a/test/void-return/tslint.json +++ b/test/void-return/tslint.json @@ -1,5 +1,5 @@ { - "rulesDirectory": ["../../bin/rules"], + "rulesDirectory": ["../../dist/rules"], "rules": { "void-return": true } diff --git a/tsconfig.json b/tsconfig.json index 7a4fd814..f89c9202 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "module": "commonjs", "target": "es6", "lib": ["es2017"], - "outDir": "bin", + "outDir": "dist", "sourceMap": true, "newLine": "lf",