Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Programmatic apis #174

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bin
node_modules
typescript-installs
dist
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"dt.json",
"dtslint.json"
],
"main": "bin",
"bin": "./bin/index.js",
"main": "dist",
"bin": "./dist/bin/index.js",
"contributors": [
"Andy Hanson <[email protected]> (https://github.com/andy-ms)",
"Dan Vanderkam <[email protected]> (https://github.com/danvk)"
Expand Down
89 changes: 89 additions & 0 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env node
import { join as joinPaths } from "path";

import { cleanInstalls, installAll, installNext, runTests } from "../";

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) {
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);
});
}
88 changes: 2 additions & 86 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<void> {
export async function runTests(dirPath: string, onlyTestTsNext: boolean): Promise<void> {
const isOlderVersion = /^v\d+$/.test(basename(dirPath));

const indexText = await readFile(joinPaths(dirPath, "index.d.ts"), "utf-8");
Expand Down Expand Up @@ -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);
});
}
2 changes: 1 addition & 1 deletion test/dt-header/correct/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../../bin/rules"],
"rulesDirectory": ["../../../dist/rules"],
"rules": {
"dt-header": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/dt-header/wrong/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../../bin/rules"],
"rulesDirectory": ["../../../dist/rules"],
"rules": {
"dt-header": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/expect/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"expect": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/export-just-namespace/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"export-just-namespace": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-any-union/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-any-union": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-bad-reference/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-bad-reference": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-const-enum/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-const-enum": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-dead-reference/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-dead-reference": true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../../bin/rules"],
"rulesDirectory": ["../../../dist/rules"],
"rules": {
"no-import-default-of-export-equals": true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"rulesDirectory": ["../../../bin/rules"],
"rulesDirectory": ["../../../dist/rules"],
"rules": {
"no-import-default-of-export-equals": true
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../../bin/rules"],
"rulesDirectory": ["../../../dist/rules"],
"rules": {
"no-import-default-of-export-equals": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-padding/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-padding": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-redundant-undefined/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-redundant-undefined": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-relative-import-in-test/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-relative-import-in-test": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-single-declare-module/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-single-declare-module": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-single-element-tuple-type/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-single-element-tuple-type": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-unnecessary-generics/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-unnecessary-generics": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/no-useless-files/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"no-useless-files": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/prefer-declare-function/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"prefer-declare-function": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/strict-export-declare-modifiers/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"strict-export-declare-modifiers": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/trim-file/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"trim-file": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/void-return/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rulesDirectory": ["../../bin/rules"],
"rulesDirectory": ["../../dist/rules"],
"rules": {
"void-return": true
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "commonjs",
"target": "es6",
"lib": ["es2017"],
"outDir": "bin",
"outDir": "dist",
"sourceMap": true,
"newLine": "lf",

Expand Down