Skip to content

Commit

Permalink
Fix and update a whole bunch of things
Browse files Browse the repository at this point in the history
- Remove tslint in favour of eslint
    - Lint
- Update modules
- Replace gulp-typescript with a utility that manually runs tsc
- Fix #34 (this was cuz a consumer was using strict and we weren't — derp)
    - Fix a bunch of other issues caught by strict mode
- Fix tests:
    - Scry Cards search by set
    - Scry on errors should retry
  • Loading branch information
Chiri Iridia Vulpes committed Dec 21, 2020
1 parent f20888f commit e735940
Show file tree
Hide file tree
Showing 14 changed files with 1,961 additions and 1,415 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
out
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-nocheck
var tsnode = require("ts-node").register();
tsnode.enabled(true);
const eslintrc = "./.eslintrc.ts";
delete require.cache[require.resolve(eslintrc)];
module.exports = require(eslintrc);
tsnode.enabled(false);
41 changes: 41 additions & 0 deletions .eslintrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ParserOptions } from "@typescript-eslint/parser";
import { ESLint } from "eslint";

module.exports = {
root: true,
parser: "@typescript-eslint/parser",
parserOptions: {
tsconfigRootDir: __dirname,
project: ["./tsconfig.json", "src/tsconfig.json"],
} as ParserOptions,
plugins: [
"@typescript-eslint",
],
extends: [
// use all recommended rules as base
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
],
rules: {
// eslint
"comma-dangle": ["warn", "always-multiline"],
"quotes": ["warn", "double", { avoidEscape: true }],
"no-constant-condition": ["warn", { checkLoops: false }],
"no-cond-assign": ["off"],

// typescript-eslint
"@typescript-eslint/no-explicit-any": ["off"],
"@typescript-eslint/await-thenable": ["off"],
"@typescript-eslint/restrict-template-expressions": ["off"],
"@typescript-eslint/no-non-null-assertion": ["off"],
"@typescript-eslint/no-namespace": ["off"],
"@typescript-eslint/explicit-module-boundary-types": ["off"],
"@typescript-eslint/no-unsafe-member-access": ["off"],
"@typescript-eslint/ban-types": ["off"],
"@typescript-eslint/no-empty-interface": ["off"],
"@typescript-eslint/no-unsafe-assignment": ["off"],
"@typescript-eslint/no-unsafe-return": ["off"],

},
} as ESLint.Options;
168 changes: 143 additions & 25 deletions gulpfile.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,155 @@
import * as del from "del";
import { dest, series, src, task, watch } from "gulp";
import * as mocha from "gulp-mocha";
import * as plumber from "gulp-plumber";
import * as typescript from "gulp-typescript";
import * as merge from "merge2";
/* eslint-disable no-control-regex */
import chalk from "chalk";
import { exec } from "child_process";
import del from "del";
import { series, src, task, watch } from "gulp";
import mocha from "gulp-mocha";
import * as path from "path";

task("build", series(clean, compile, test));
task("build", series(
clean,
compile,
test));

task("watch", series("build", (cb) => {
watch("./src/**/*.ts", series("build"));
cb();
}));
task("watch", series("build", done =>
watch("./src/**/*.ts", series("build"))
&& done()));

async function clean () {
await del("out");
async function compile () {
await new TypescriptWatch("src", "out").once();
}

let project: typescript.Project;
function compile () {
project = project || typescript.createProject("./src/tsconfig.json");

const result = project.src()
.pipe(plumber())
.pipe(project());

return merge([
result.js.pipe(dest("out")),
result.dts.pipe(dest("out")),
]);
async function clean () {
await del("out");
}

function test () {
return src("out/tests/Main.js", { read: false })
.pipe(mocha({ reporter: "even-more-min" }))
.on("error", () => process.exitCode = 1);
}

export default class TypescriptWatch {
private onDataHandler: (data: string) => any;
private onCompleteHandler: (done: () => any) => any;
private initialized: (() => void) | true | undefined;
private readonly inDir: string;
private readonly outDir: string;
private declaration: string | undefined;

public constructor (dir: string, outDir: string) {
this.inDir = path.resolve(dir);
this.outDir = path.resolve(outDir);
}

public onData (handler: (data: string) => boolean | undefined | void) {
this.onDataHandler = handler;
return this;
}

public onComplete (handler: (done: () => any) => any) {
this.onCompleteHandler = handler;
return this;
}

public setDeclaration (dir: string) {
this.declaration = path.resolve(dir);
return this;
}

public async once () {
const ocwd = process.cwd();
process.chdir(this.inDir);
const declaration = this.declaration ? `--declaration --declarationDir ${this.declaration}` : "";
const task = exec(`npx tsc --outDir ${this.outDir} --pretty ${declaration}`);
process.chdir(ocwd);

task.stderr!.on("data", data => process.stderr.write(data));

task.stdout!.on("data", (data: Buffer) => {
if (this.onDataHandler && this.onDataHandler(data.toString()) === false)
return;

process.stdout.write(handleTscOut(0, data, `${path.relative(ocwd, this.inDir).replace(/\\/g, "/")}/`));
});

return new Promise<void>((resolve, reject) => task.on("close", code => {
if (!code) resolve();
else reject(code);
}));
}

public watch () {
const ocwd = process.cwd();
process.chdir(this.inDir);
const declaration = this.declaration ? `--declaration --declarationDir ${this.declaration}` : "";
const task = exec(`npx tsc --outDir ${this.outDir} --pretty --watch ${declaration}`);
process.chdir(ocwd);

task.stderr!.on("data", data => process.stderr.write(data));

let start: number;
task.stdout!.on("data", (data: Buffer) => {
if (this.onDataHandler && this.onDataHandler(data.toString()) === false)
return;

if (/\bincremental compilation|in watch mode\b/.test(data.toString()))
start = Date.now();

process.stdout.write(handleTscOut(start, data, `${path.relative(ocwd, this.inDir).replace(/\\/g, "/")}/`));

if (/Watching for file changes./.test(data.toString()) && this.initialized) {
if (typeof this.initialized === "function") {
this.initialized();
this.initialized = true;

} else {
this.onCompleteHandler(() => { return; });
}
}
});

return this;
}

public async waitForInitial () {
return new Promise<void>(resolve => {
if (this.initialized === true) return resolve();

this.initialized = resolve;
});
}
}

function handleTscOut (startTime: number, data: string | Buffer, prefix?: string) {
data = data.toString()
.replace("\u001bc", "")
.replace(/. Watching for file changes.\r\n/, ` after ${getTimeString(startTime)}`)
.replace(/(incremental compilation...|in watch mode...)\r\n/g, "$1")
.replace(/( TS\d{4}: [^\r\n]*?\r\n)\r\n/g, "$1")
.replace(/(~+[^\r\n]*?\r\n)\r\n\r\n/g, "$1")
.replace(/(?=\[30;47m(\d+| +)\u001b\[0m)/g, "\t");

if (prefix) {
data = data.replace(/(\u001b\[96m.*?\u001b\[0m:\u001b\[93m)/g, chalk.cyan(`${prefix}$1`));
}

return data;
}

function getTimeString (start: number) {
const time = Date.now() - start;
let timeString;

if (time >= 1000) {
timeString = `${(time / 1000).toFixed(2).replace(/0+$/, "")} s`;

} else if (time >= 100) {
timeString = `${(time / 100).toFixed(0)} ms`;

} else {
timeString = `${time} μs`;
}

return chalk.magenta(timeString);
}
Loading

0 comments on commit e735940

Please sign in to comment.