1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-11 18:17:48 -05:00
denoland-deno/format.ts

136 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-01-26 23:19:56 -05:00
#!/usr/bin/env deno --allow-run --allow-write
2019-01-02 09:56:17 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2019-01-26 23:19:56 -05:00
/**
* This script formats the source files in the repository.
*
* Usage: deno format.ts [--check]
*
* Options:
* --check Checks if the source files are formatted.
*/
import { args, platform, readAll, exit, run, readFile, writeFile } from "deno";
import { parse } from "./flags/mod.ts";
import { prettier, prettierPlugins } from "./prettier/prettier.ts";
2018-12-18 18:56:12 -05:00
2019-01-26 23:19:56 -05:00
const encoder = new TextEncoder();
const decoder = new TextDecoder();
2018-12-19 13:01:26 -05:00
2019-01-26 23:19:56 -05:00
// Runs commands in cross-platform way
function xrun(opts) {
return run({
...opts,
args: platform.os === "win" ? ["cmd.exe", "/c", ...opts.args] : opts.args
2018-12-19 13:01:26 -05:00
});
2019-01-26 23:19:56 -05:00
}
// Gets the source files in the repository
async function getSourceFiles() {
return decoder
.decode(
await readAll(
xrun({
args: ["git", "ls-files"],
stdout: "piped"
}).stdout
)
)
.trim()
.split(/\r?\n/);
}
/**
* Checks if the file has been formatted with prettier.
*/
async function checkFile(
filename: string,
parser: "typescript" | "markdown"
): Promise<boolean> {
const text = decoder.decode(await readFile(filename));
const formatted = prettier.check(text, {
parser,
plugins: prettierPlugins
});
if (!formatted) {
// TODO: print some diff info here to show why this failed
console.error(`${filename} ... Not formatted`);
}
return formatted;
}
/**
* Formats the given file.
*/
async function formatFile(
filename: string,
parser: "typescript" | "markdown"
): Promise<void> {
const text = decoder.decode(await readFile(filename));
const formatted = prettier.format(text, {
parser,
plugins: prettierPlugins
});
if (text !== formatted) {
console.log(`Formatting ${filename}`);
await writeFile(filename, encoder.encode(formatted));
2018-12-19 13:01:26 -05:00
}
2019-01-26 23:19:56 -05:00
}
/**
* Checks if the all files have been formatted with prettier.
*/
async function checkSourceFiles() {
const checks = [];
(await getSourceFiles()).forEach(file => {
if (/\.ts$/.test(file)) {
checks.push(checkFile(file, "typescript"));
} else if (/\.md$/.test(file)) {
checks.push(checkFile(file, "markdown"));
}
});
const results = await Promise.all(checks);
if (results.every(result => result)) {
exit(0);
} else {
2018-12-19 13:01:26 -05:00
exit(1);
}
}
2018-12-18 18:56:12 -05:00
2019-01-26 23:19:56 -05:00
/**
* Formats the all files with prettier.
*/
async function formatSourceFiles() {
const formats = [];
2018-12-19 13:01:26 -05:00
2019-01-26 23:19:56 -05:00
(await getSourceFiles()).forEach(file => {
if (/\.ts$/.test(file)) {
formats.push(formatFile(file, "typescript"));
} else if (/\.md$/.test(file)) {
formats.push(formatFile(file, "markdown"));
}
2018-12-18 18:56:12 -05:00
});
2019-01-26 23:19:56 -05:00
await Promise.all(formats);
exit(0);
}
async function main(opts) {
try {
if (opts.check) {
await checkSourceFiles();
} else {
await formatSourceFiles();
}
} catch (e) {
console.log(e);
exit(1);
}
2018-12-18 18:56:12 -05:00
}
2019-01-26 23:19:56 -05:00
main(parse(args));