mirror of
https://github.com/denoland/deno.git
synced 2024-11-30 16:40:57 -05:00
Port prettier (#156)
This commit is contained in:
parent
37e3239fc0
commit
b792fe8c72
10 changed files with 30853 additions and 26 deletions
|
@ -10,6 +10,7 @@ jobs:
|
|||
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
|
||||
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
|
||||
- script: deno test.ts --allow-run --allow-net --allow-write
|
||||
- script: deno format.ts --allow-run --allow-write --check
|
||||
|
||||
- job: 'Mac'
|
||||
pool:
|
||||
|
@ -18,6 +19,7 @@ jobs:
|
|||
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
|
||||
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
|
||||
- script: deno test.ts --allow-run --allow-net --allow-write
|
||||
- script: deno format.ts --allow-run --allow-write --check
|
||||
|
||||
- job: 'Windows'
|
||||
pool:
|
||||
|
@ -26,3 +28,4 @@ jobs:
|
|||
- powershell: iwr https://deno.land/x/install/install.ps1 -out install.ps1; .\install.ps1 $(DENO_VERSION)
|
||||
- script: echo '##vso[task.prependpath]C:\Users\VssAdministrator\.deno\bin\'
|
||||
- script: 'C:\Users\VssAdministrator\.deno\bin\deno.exe test.ts --allow-run --allow-net --allow-write'
|
||||
- script: 'C:\Users\VssAdministrator\.deno\bin\deno.exe format.ts --allow-run --allow-write --check'
|
||||
|
|
0
flags/tests/parse.ts
Normal file → Executable file
0
flags/tests/parse.ts
Normal file → Executable file
145
format.ts
145
format.ts
|
@ -1,36 +1,135 @@
|
|||
#!/usr/bin/env deno --allow-run
|
||||
#!/usr/bin/env deno --allow-run --allow-write
|
||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
/**
|
||||
* 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";
|
||||
|
||||
import { readAll, exit, run } from "deno";
|
||||
const encoder = new TextEncoder();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
async function checkVersion() {
|
||||
const prettierVersion = run({
|
||||
args: ["bash", "-c", "prettier --version"],
|
||||
stdout: "piped"
|
||||
// Runs commands in cross-platform way
|
||||
function xrun(opts) {
|
||||
return run({
|
||||
...opts,
|
||||
args: platform.os === "win" ? ["cmd.exe", "/c", ...opts.args] : opts.args
|
||||
});
|
||||
const b = await readAll(prettierVersion.stdout);
|
||||
const s = await prettierVersion.status();
|
||||
if (s.code != 0) {
|
||||
console.log("error calling prettier --version error");
|
||||
exit(s.code);
|
||||
}
|
||||
|
||||
// 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`);
|
||||
}
|
||||
const version = new TextDecoder().decode(b).trim();
|
||||
const requiredVersion = "1.15";
|
||||
if (!version.startsWith(requiredVersion)) {
|
||||
console.log(`Required prettier version: ${requiredVersion}`);
|
||||
console.log(`Installed prettier version: ${version}`);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await checkVersion();
|
||||
/**
|
||||
* Formats the all files with prettier.
|
||||
*/
|
||||
async function formatSourceFiles() {
|
||||
const formats = [];
|
||||
|
||||
const prettier = run({
|
||||
args: ["bash", "-c", "prettier --write '**/*.ts' '**/*.md'"]
|
||||
(await getSourceFiles()).forEach(file => {
|
||||
if (/\.ts$/.test(file)) {
|
||||
formats.push(formatFile(file, "typescript"));
|
||||
} else if (/\.md$/.test(file)) {
|
||||
formats.push(formatFile(file, "markdown"));
|
||||
}
|
||||
});
|
||||
const s = await prettier.status();
|
||||
exit(s.code);
|
||||
|
||||
await Promise.all(formats);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
main();
|
||||
async function main(opts) {
|
||||
try {
|
||||
if (opts.check) {
|
||||
await checkSourceFiles();
|
||||
} else {
|
||||
await formatSourceFiles();
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main(parse(args));
|
||||
|
|
|
@ -24,4 +24,4 @@ export interface ParsedPath {
|
|||
name: string;
|
||||
}
|
||||
|
||||
export type FormatInputPathObject = Partial<ParsedPath>
|
||||
export type FormatInputPathObject = Partial<ParsedPath>;
|
||||
|
|
|
@ -980,7 +980,7 @@ export const win32 = {
|
|||
} else {
|
||||
ret.name = path.slice(startPart, startDot);
|
||||
ret.base = path.slice(startPart, end);
|
||||
ret.ext = path.slice(startDot, end);
|
||||
ret.ext = path.slice(startDot, end);
|
||||
}
|
||||
|
||||
// If the directory is the root, use the entire root as the `dir` including
|
||||
|
|
11
prettier/parser_markdown.js
Normal file
11
prettier/parser_markdown.js
Normal file
File diff suppressed because one or more lines are too long
11
prettier/parser_typescript.js
Normal file
11
prettier/parser_typescript.js
Normal file
File diff suppressed because one or more lines are too long
9
prettier/prettier.ts
Normal file
9
prettier/prettier.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
import "./standalone.js";
|
||||
import "./parser_typescript.js";
|
||||
import "./parser_markdown.js";
|
||||
|
||||
// TODO: provide decent type declarions for these
|
||||
const { prettier, prettierPlugins } = window as any;
|
||||
|
||||
export { prettier, prettierPlugins };
|
30688
prettier/standalone.js
Normal file
30688
prettier/standalone.js
Normal file
File diff suppressed because one or more lines are too long
|
@ -35,7 +35,13 @@ test(function testingAssertFail() {
|
|||
let didThrow = false;
|
||||
|
||||
assert.throws(assert.fail, Error, "Failed assertion.");
|
||||
assert.throws(() => { assert.fail("foo"); }, Error, "Failed assertion: foo");
|
||||
assert.throws(
|
||||
() => {
|
||||
assert.fail("foo");
|
||||
},
|
||||
Error,
|
||||
"Failed assertion: foo"
|
||||
);
|
||||
});
|
||||
|
||||
test(function testingAssertEqualActualUncoercable() {
|
||||
|
|
Loading…
Reference in a new issue