Skip to content

Commit

Permalink
🔨 new prettify script.
Browse files Browse the repository at this point in the history
  • Loading branch information
loucyx committed Oct 1, 2024
1 parent dd045f3 commit 47d57aa
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ jobs:
with:
deno-version: 2.0.0-rc.5

- name: Lint
run: deno lint

- name: Prettify
run: deno run prettify

- name: Test
run: deno test --coverage

Expand Down
18 changes: 0 additions & 18 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"prettier.arrowParens": "avoid",
"prettier.bracketSameLine": true,
"prettier.bracketSpacing": true,
"prettier.embeddedLanguageFormatting": "auto",
"prettier.endOfLine": "lf",
"prettier.experimentalTernaries": true,
"prettier.htmlWhitespaceSensitivity": "strict",
"prettier.jsxSingleQuote": false,
"prettier.printWidth": 80,
"prettier.proseWrap": "always",
"prettier.quoteProps": "as-needed",
"prettier.semi": true,
"prettier.singleAttributePerLine": false,
"prettier.singleQuote": false,
"prettier.tabWidth": 4,
"prettier.trailingComma": "all",
"prettier.useTabs": true,
"prettier.vueIndentScriptAndStyle": true,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
Expand Down
6 changes: 5 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"exclude": [".git"],
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.6"
"@std/assert": "jsr:@std/assert@^1.0.6",
"prettier": "npm:prettier@^3.3.3"
},
"lint": {
"rules": {
Expand Down Expand Up @@ -122,6 +123,9 @@
]
}
},
"tasks": {
"prettify": "deno run --allow-env --allow-read --allow-sys --allow-write ./tasks/prettify.ts"
},
"workspace": [
"./@coven/compare",
"./@coven/constants",
Expand Down
101 changes: 101 additions & 0 deletions prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @import { Options } from "npm:prettier";
*/

/**
* Prettier configuration.
* @see [Prettier options](https://prettier.io/docs/en/options.html)
* @type {Options}
*/
export default {
/**
* Avoid parentheses around a sole arrow function parameter.
*/
arrowParens: "avoid",

/**
* Put the `>` of a multi-line HTML like in a new line.
*/
bracketSameLine: false,

/**
* Print spaces between brackets in object literals.
*/
bracketSpacing: true,

/**
* Control whether Prettier formats quoted code embedded in the file.
*/
embeddedLanguageFormatting: "auto",

/**
* Unix EOL.
*/
endOfLine: "lf",

/**
* Use curious ternaries, with the question mark after the condition,
* instead of on the same line as the consequent.
*/
experimentalTernaries: true,

/**
* Strict whitespace in HTML.
*/
htmlWhitespaceSensitivity: "strict",

/**
* JSX should use double quote.
*/
jsxSingleQuote: false,

/**
* Print width is ideal at 80 characters.
*/
printWidth: 80,

/**
* Wrap text to fit the print width.
*/
proseWrap: "always",

/**
* Add quotes around properties of object if needed.
*/
quoteProps: "as-needed",

/**
* Print semicolons at the ends of statements.
*/
semi: true,

/**
* Shows multiple attributes per line in HTML, Vue and JSX.
*/
singleAttributePerLine: false,

/**
* Use double quotes.
*/
singleQuote: false,

/**
* Tab width at 4 is ideal.
*/
tabWidth: 4,

/**
* Add trailing commas to make edition easier and diffing better.
*/
trailingComma: "all",

/**
* Obviously use tabs.
*/
useTabs: true,

/**
* Indent code in Vue files.
*/
vueIndentScriptAndStyle: true,
};
32 changes: 32 additions & 0 deletions tasks/prettify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { format } from "prettier";
import prettierConfig from "../prettier.config.mjs";

const extensions = [".js", ".json", ".md", ".ts"];
const decoder = new TextDecoder("utf-8");
const encoder = new TextEncoder();

const getFiles = async function* (
directory = ".",
): AsyncGenerator<[path: string, content: string]> {
for await (const entry of Deno.readDir(directory)) {
if (!entry.name.startsWith(".")) {
const path = `${directory}/${entry.name}`;

entry.isDirectory ? yield* getFiles(path)
: extensions.some(extension => entry.name.endsWith(extension)) ?
yield await Deno.readFile(path).then(content => [
path,
decoder.decode(content),
])
: undefined;
}
}
};

Array.fromAsync(getFiles()).then(files =>
files.forEach(([filepath, code]) =>
format(code, { filepath, ...prettierConfig }).then(formattedCode =>
Deno.writeFile(filepath, encoder.encode(formattedCode)),
),
),
);
10 changes: 8 additions & 2 deletions tests/@coven/terminal/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ Deno.test("Format works on string", () =>
);

Deno.test("Format works as a template tag function", () =>
assertEquals(format(1, 2)`Coven Engineering ${13}`, "Coven Engineering 13"),
assertEquals(
format(1, 2)`Coven Engineering ${13}`,
"Coven Engineering 13",
),
);

Deno.test("Nested format works", () =>
assertEquals(format(1, 2)`Hi ${format(3, 2)`witch`}!`, "Hi witch!"),
assertEquals(
format(1, 2)`Hi ${format(3, 2)`witch`}!`,
"Hi witch!",
),
);

0 comments on commit 47d57aa

Please sign in to comment.