Skip to content
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
4 changes: 1 addition & 3 deletions ignore_test.ts
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand Down Expand Up @@ -78,5 +78,3 @@ test({
}
}
});

runIfMain(import.meta);
24 changes: 14 additions & 10 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ async function* getTargetFiles(
/**
* auto detect prettier configuration file and return config if file exist.
*/
async function autoResolveConfig(): Promise<PrettierBuildInOptions> {
const configFileNamesMap = {
async function autoResolveConfig(): Promise<PrettierBuildInOptions | undefined> {
const configFileNamesMap: Record<string, 1 | undefined> = {
".prettierrc.json": 1,
".prettierrc.yaml": 1,
".prettierrc.yml": 1,
Expand All @@ -380,11 +380,11 @@ async function autoResolveConfig(): Promise<PrettierBuildInOptions> {
".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;
}
Expand All @@ -402,7 +402,7 @@ async function autoResolveConfig(): Promise<PrettierBuildInOptions> {
async function resolveConfig(
filepath: string
): Promise<PrettierBuildInOptions> {
let config: PrettierBuildInOptions = undefined;
let config: PrettierBuildInOptions | undefined = undefined;

function generateError(msg: string): Error {
return new Error(`Invalid prettier configuration file: ${msg}.`);
Expand Down Expand Up @@ -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<Set<string>> {
const files = await Deno.readDir(".");
const files = await Deno.readdir(".");

for (const f of files) {
if (f.isFile() && f.name === ".prettierignore") {
Expand All @@ -489,7 +489,7 @@ async function resolveIgnoreFile(filepath: string): Promise<Set<string>> {
return ignore.parse(raw);
}

async function main(opts): Promise<void> {
async function main(opts: any): Promise<void> {
const { help, check, _: args } = opts;

let prettierOpts: PrettierOptions = {
Expand Down Expand Up @@ -546,7 +546,11 @@ async function main(opts): Promise<void> {

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"];
Expand Down
35 changes: 17 additions & 18 deletions main_test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -397,7 +396,7 @@ test(async function testPrettierWithAutoConfig(): Promise<void> {
const cwd = join(testdata, configName);
const prettierFile = join(Deno.cwd(), "main.ts");
const { stdout, stderr } = Deno.run({
args: [
cmd: [
execPath(),
"run",
"--allow-read",
Expand All @@ -412,8 +411,9 @@ test(async function testPrettierWithAutoConfig(): Promise<void> {
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
Expand Down Expand Up @@ -463,7 +463,7 @@ test(async function testPrettierWithSpecifiedConfig(): Promise<void> {
const cwd = join(testdata, config.dir);
const prettierFile = join(Deno.cwd(), "main.ts");
const { stdout, stderr } = Deno.run({
args: [
cmd: [
execPath(),
"run",
"--allow-read",
Expand All @@ -478,8 +478,9 @@ test(async function testPrettierWithSpecifiedConfig(): Promise<void> {
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
Expand All @@ -498,7 +499,7 @@ test(async function testPrettierWithAutoIgnore(): Promise<void> {
const cwd = join(testdata, "ignore_file");
const prettierFile = join(Deno.cwd(), "main.ts");
const { stdout, stderr } = Deno.run({
args: [
cmd: [
execPath(),
"run",
"--allow-read",
Expand All @@ -513,10 +514,10 @@ test(async function testPrettierWithAutoIgnore(): Promise<void> {
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`
);
});
Expand All @@ -526,7 +527,7 @@ test(async function testPrettierWithSpecifiedIgnore(): Promise<void> {
const cwd = join(testdata, "ignore_file");
const prettierFile = join(Deno.cwd(), "main.ts");
const { stdout, stderr } = Deno.run({
args: [
cmd: [
execPath(),
"run",
"--allow-read",
Expand All @@ -541,12 +542,10 @@ test(async function testPrettierWithSpecifiedIgnore(): Promise<void> {
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);
2 changes: 1 addition & 1 deletion testdata/opts/0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ console.log(0);
console.log([
function foo() {},
function baz() {},
a => {}
(_: any) => {}
]);
2 changes: 1 addition & 1 deletion util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}