tsd
Version:
Check TypeScript type definitions
97 lines (89 loc) • 3.68 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const meow_1 = __importDefault(require("meow"));
const interfaces_1 = require("./lib/interfaces");
const formatter_1 = __importDefault(require("./lib/formatter"));
const lib_1 = __importDefault(require("./lib"));
const cli = (0, meow_1.default)(`
Usage
$ tsd [path]
The given directory must contain a package.json and a typings file.
Info
--help Display help text
--version Display version info
Options
--typings -t Type definition file to test [Default: "types" property in package.json]
--files -f Glob of files to test [Default: '/path/test-d/**/*.test-d.ts' or '.tsx']
--show-diff Show type error diffs [Default: don't show]
Examples
$ tsd /path/to/project
$ tsd --files /test/some/folder/*.ts --files /test/other/folder/*.tsx
$ tsd
index.test-d.ts
✖ 10:20 Argument of type string is not assignable to parameter of type number.
`, {
flags: {
typings: {
type: 'string',
alias: 't',
},
files: {
type: 'string',
alias: 'f',
isMultiple: true,
},
showDiff: {
type: 'boolean',
},
},
});
/**
* Displays a message and exits, conditionally erroring.
*
* @param message The message to display.
* @param isError Whether or not to fail on exit.
*/
const exit = (message, { isError = true } = {}) => {
if (isError) {
console.error(message);
process.exit(1);
}
else {
console.log(message);
process.exit(0);
}
};
(() => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
try {
const cwd = cli.input.length > 0 ? cli.input[0] : process.cwd();
const { typings: typingsFile, files: testFiles, showDiff } = cli.flags;
const diagnostics = yield (0, lib_1.default)({ cwd, typingsFile, testFiles });
if (diagnostics.length > 0) {
const hasErrors = diagnostics.some(diagnostic => diagnostic.severity === 'error');
const formattedDiagnostics = (0, formatter_1.default)(diagnostics, showDiff);
exit(formattedDiagnostics, { isError: hasErrors });
}
}
catch (error) {
const potentialError = error;
if (potentialError instanceof interfaces_1.TsdError) {
exit(potentialError.message);
}
const errorMessage = (_b = (_a = potentialError === null || potentialError === void 0 ? void 0 : potentialError.stack) !== null && _a !== void 0 ? _a : potentialError === null || potentialError === void 0 ? void 0 : potentialError.message) !== null && _b !== void 0 ? _b : 'tsd unexpectedly crashed.';
exit(`Error running tsd:\n${errorMessage}`);
}
}))();
;