diff --git a/ignore_test.ts b/ignore_test.ts index 9659d39..3a75983 100644 --- a/ignore_test.ts +++ b/ignore_test.ts @@ -1,8 +1,8 @@ // Copyright 2020 denolib maintainers. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { test, runIfMain } from "https://deno.land/std/testing/mod.ts"; import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; import { parse } from "./ignore.ts"; +const { test } = Deno; const testCases = [ { @@ -78,5 +78,3 @@ test({ } } }); - -runIfMain(import.meta); diff --git a/main.ts b/main.ts index 7bca82d..2e3d2a1 100755 --- a/main.ts +++ b/main.ts @@ -368,8 +368,8 @@ async function* getTargetFiles( /** * auto detect prettier configuration file and return config if file exist. */ -async function autoResolveConfig(): Promise { - const configFileNamesMap = { +async function autoResolveConfig(): Promise { + const configFileNamesMap: Record = { ".prettierrc.json": 1, ".prettierrc.yaml": 1, ".prettierrc.yml": 1, @@ -380,11 +380,11 @@ async function autoResolveConfig(): Promise { ".prettierrc.toml": 1 }; - const files = await Deno.readDir("."); + const files = await Deno.readdir("."); for (const f of files) { - if (f.isFile() && configFileNamesMap[f.name]) { - const c = await resolveConfig(f.name); + if (f.isFile() && configFileNamesMap[f.name!]) { + const c = await resolveConfig(f.name!); if (c) { return c; } @@ -402,7 +402,7 @@ async function autoResolveConfig(): Promise { async function resolveConfig( filepath: string ): Promise { - let config: PrettierBuildInOptions = undefined; + let config: PrettierBuildInOptions | undefined = undefined; function generateError(msg: string): Error { return new Error(`Invalid prettier configuration file: ${msg}.`); @@ -462,14 +462,14 @@ async function resolveConfig( break; } - return config; + return config!; } /** * auto detect .prettierignore and return pattern if file exist. */ async function autoResolveIgnoreFile(): Promise> { - const files = await Deno.readDir("."); + const files = await Deno.readdir("."); for (const f of files) { if (f.isFile() && f.name === ".prettierignore") { @@ -489,7 +489,7 @@ async function resolveIgnoreFile(filepath: string): Promise> { return ignore.parse(raw); } -async function main(opts): Promise { +async function main(opts: any): Promise { const { help, check, _: args } = opts; let prettierOpts: PrettierOptions = { @@ -546,7 +546,11 @@ async function main(opts): Promise { const files = getTargetFiles(args.length ? args : ["."], ignore); - const tty = Deno.isTTY(); + const tty = { + stdout: Deno.isatty(Deno.stdout.rid), + stderr: Deno.isatty(Deno.stdout.rid), + stdin: Deno.isatty(Deno.stdout.rid) + }; const shouldReadFromStdin = (!tty.stdin && (tty.stdout || tty.stderr)) || !!opts["stdin"]; diff --git a/main_test.ts b/main_test.ts index ec62b47..12b6c71 100644 --- a/main_test.ts +++ b/main_test.ts @@ -1,18 +1,17 @@ // Copyright 2020 denolib maintainers. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -import { test, runIfMain } from "https://deno.land/std/testing/mod.ts"; import { copy, emptyDir } from "https://deno.land/std/fs/mod.ts"; import { EOL, join } from "https://deno.land/std/path/mod.ts"; import { xrun } from "./util.ts"; -const { readAll, execPath } = Deno; +const { readAll, execPath, test } = Deno; const decoder = new TextDecoder(); async function run( - args: string[] + cmd: string[] ): Promise<{ stdout: string; code: number | undefined }> { - const p = xrun({ args, stdout: "piped" }); + const p = xrun({ cmd, stdout: "piped" }); const stdout = decoder.decode(await readAll(p.stdout!)); const { code } = await p.status(); @@ -397,7 +396,7 @@ test(async function testPrettierWithAutoConfig(): Promise { const cwd = join(testdata, configName); const prettierFile = join(Deno.cwd(), "main.ts"); const { stdout, stderr } = Deno.run({ - args: [ + cmd: [ execPath(), "run", "--allow-read", @@ -412,8 +411,9 @@ test(async function testPrettierWithAutoConfig(): Promise { cwd }); - const output = decoder.decode(await Deno.readAll(stdout)); - const errMsg = decoder.decode(await Deno.readAll(stderr)); + // TODO: Fix + const output = decoder.decode(await Deno.readAll(stdout as any)); + const errMsg = decoder.decode(await Deno.readAll(stderr as any)); assertEquals( errMsg @@ -463,7 +463,7 @@ test(async function testPrettierWithSpecifiedConfig(): Promise { const cwd = join(testdata, config.dir); const prettierFile = join(Deno.cwd(), "main.ts"); const { stdout, stderr } = Deno.run({ - args: [ + cmd: [ execPath(), "run", "--allow-read", @@ -478,8 +478,9 @@ test(async function testPrettierWithSpecifiedConfig(): Promise { cwd }); - const output = decoder.decode(await Deno.readAll(stdout)); - const errMsg = decoder.decode(await Deno.readAll(stderr)); + // TODO: Fix + const output = decoder.decode(await Deno.readAll(stdout as any)); + const errMsg = decoder.decode(await Deno.readAll(stderr as any)); assertEquals( errMsg @@ -498,7 +499,7 @@ test(async function testPrettierWithAutoIgnore(): Promise { const cwd = join(testdata, "ignore_file"); const prettierFile = join(Deno.cwd(), "main.ts"); const { stdout, stderr } = Deno.run({ - args: [ + cmd: [ execPath(), "run", "--allow-read", @@ -513,10 +514,10 @@ test(async function testPrettierWithAutoIgnore(): Promise { cwd }); - assertEquals(decoder.decode(await Deno.readAll(stderr)), ""); + assertEquals(decoder.decode(await Deno.readAll(stderr as any)), ""); assertEquals( - decoder.decode(await Deno.readAll(stdout)), + decoder.decode(await Deno.readAll(stdout as any)), `console.log("typescript");\nconsole.log("typescript1");\n` ); }); @@ -526,7 +527,7 @@ test(async function testPrettierWithSpecifiedIgnore(): Promise { const cwd = join(testdata, "ignore_file"); const prettierFile = join(Deno.cwd(), "main.ts"); const { stdout, stderr } = Deno.run({ - args: [ + cmd: [ execPath(), "run", "--allow-read", @@ -541,12 +542,10 @@ test(async function testPrettierWithSpecifiedIgnore(): Promise { cwd }); - assertEquals(decoder.decode(await Deno.readAll(stderr)), ""); + assertEquals(decoder.decode(await Deno.readAll(stderr as any)), ""); assertEquals( - decoder.decode(await Deno.readAll(stdout)), + decoder.decode(await Deno.readAll(stdout as any)), `console.log("javascript");\nconsole.log("javascript1");\n` ); }); - -runIfMain(import.meta); diff --git a/testdata/opts/0.ts b/testdata/opts/0.ts index 400e7ee..574f20f 100644 --- a/testdata/opts/0.ts +++ b/testdata/opts/0.ts @@ -2,5 +2,5 @@ console.log(0); console.log([ function foo() {}, function baz() {}, - a => {} + (_: any) => {} ]); diff --git a/util.ts b/util.ts index 49b96f4..70f17a2 100644 --- a/util.ts +++ b/util.ts @@ -6,6 +6,6 @@ const { build, run } = Deno; export function xrun(opts: Deno.RunOptions): Deno.Process { return run({ ...opts, - args: build.os === "win" ? ["cmd.exe", "/c", ...opts.args] : opts.args + cmd: build.os === "win" ? ["cmd.exe", "/c", ...opts.cmd] : opts.cmd }); }