Skip to content

Commit

Permalink
chore: experiment with wasm-fmt/biome
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Nov 8, 2024
1 parent bf2e8bf commit 0243e98
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 60 deletions.
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@types/jsdom": "20.0.1",
"@typescript-eslint/eslint-plugin": "5.55.0",
"@typescript-eslint/parser": "5.55.0",
"@wasm-fmt/biome_fmt": "^0.1.12",
"async": "3.2.4",
"concurrently": "7.0.0",
"decomment": "0.9.5",
Expand Down Expand Up @@ -138,11 +139,7 @@
}
},
"lint-staged": {
"{lib,packages}/**/src/**/*.ts": [
"eslint --fix",
"prettier --write"
],
"**/*.{ts,js,md,json}": "prettier --write"
"**/*.{md,json}": "prettier --write"
},
"packageManager": "[email protected]"
}
40 changes: 40 additions & 0 deletions scripts/biome-test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import init, { format } from "@wasm-fmt/biome_fmt";
import { promises } from "node:fs";
import path, { join } from "node:path";
import { fileURLToPath } from "node:url";

import walk from "./utils/walk.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
await init();

const prettifyCode = async (dir) => {
for await (const file of walk(dir, ["node_modules"])) {
if (file.endsWith(".ts") || file.endsWith(".js")) {
if (file.endsWith("ruleset.ts")) {
continue;
}
promises.readFile(file, "utf-8").then((contents) => {
promises.writeFile(
file,
format(contents, file, {
indent_style: "space",
indent_width: 2,
line_width: 120,
line_ending: "lf",
quote_properties: "as-needed",
arrow_parentheses: "always",
semicolons: "always",
bracket_spacing: true,
bracket_same_line: false,
quote_style: "double",
trailing_comma: "all",
}),
"utf-8"
);
});
}
}
};

prettifyCode(join(__dirname, "..", "clients", "client-s3"));
16 changes: 1 addition & 15 deletions scripts/generate-clients/code-eslint-fix.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
// @ts-check
const { spawnProcess } = require("../utils/spawn-process");
const path = require("path");

const eslintFixCode = async () => {
try {
await spawnProcess(path.join(__dirname, "..", "..", "node_modules", ".bin", "esprint"), [
"check",
"--fix",
"--quiet",
]);
} catch (error) {
// esprint throws error as the clients source code does not follow 'prefer-const' rule.
// And esprint does not have a way to override rules written in .eslintrc
// We will still get linted code though.
}
// superceded by biome.
};

module.exports = {
Expand Down
18 changes: 0 additions & 18 deletions scripts/generate-clients/code-prettify.js

This file was deleted.

19 changes: 19 additions & 0 deletions scripts/generate-clients/code-prettify.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import init from "@wasm-fmt/biome_fmt";
import fs from "node:fs";
import path from "node:path";

import { runWasmFmtBiome } from "./run-wasm-fmt-biome.mjs";

export const prettifyCode = async (dir) => {
await init();

for (const subdirectory of fs.readdirSync(dir)) {
const targets = [path.join(subdirectory, "typescript-codegen"), path.join(subdirectory, "typescript-ssdk-codegen")];

for (const target of targets) {
if (fs.existsSync(target) && fs.lstatSync(target).isDirectory()) {
runWasmFmtBiome(target);
}
}
}
};
2 changes: 1 addition & 1 deletion scripts/generate-clients/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const { emptyDirSync } = require("fs-extra");
const { generateGenericClient } = require("./code-gen");
const { copyToClients } = require("./copy-to-clients");
const { CODE_GEN_GENERIC_CLIENT_OUTPUT_DIR } = require("./code-gen-dir");
const { prettifyCode } = require("./code-prettify");
const { eslintFixCode } = require("./code-eslint-fix");

const PRIVATE_CLIENTS_DIR = path.normalize(path.join(__dirname, "..", "..", "private"));

// TODO: remove this script when generate-clients code is refactored.
(async () => {
const { prettifyCode } = await import("./code-prettify.mjs");
try {
await generateGenericClient();

Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
DEFAULT_CODE_GEN_INPUT_DIR,
TEMP_CODE_GEN_INPUT_DIR,
} = require("./code-gen-dir");
const { prettifyCode } = require("./code-prettify");
const { eslintFixCode } = require("./code-eslint-fix");
const { buildSmithyTypeScript } = require("./build-smithy-typescript");
const { SMITHY_TS_COMMIT } = require("./config");
Expand Down Expand Up @@ -75,6 +74,7 @@ const {
.help().argv;

(async () => {
const { prettifyCode } = await import("./code-prettify.mjs");
try {
if (!noSmithyCheckout) {
await buildSmithyTypeScript(repo, commit);
Expand Down
35 changes: 35 additions & 0 deletions scripts/generate-clients/run-wasm-fmt-biome.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import init, { format } from "@wasm-fmt/biome_fmt";
import { promises } from "node:fs";

import walk from "../utils/walk.js";

await init();

export const runWasmFmtBiome = async (dir) => {
for await (const file of walk(dir)) {
if (file.endsWith(".ts") || file.endsWith(".js")) {
if (file.endsWith("ruleset.ts")) {
continue;
}
promises.readFile(file, "utf-8").then((contents) => {
promises.writeFile(
file,
format(contents, file, {
indent_style: "space",
indent_width: 2,
line_width: 120,
line_ending: "lf",
quote_properties: "as-needed",
arrow_parentheses: "always",
semicolons: "always",
bracket_spacing: true,
bracket_same_line: false,
quote_style: "double",
trailing_comma: "all",
}),
"utf-8"
);
});
}
}
};
26 changes: 6 additions & 20 deletions scripts/generate-clients/single-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const { normalize, join } = require("path");
const { generateClient } = require("./code-gen");
const { codeOrdering } = require("./code-ordering");
const { copyToClients } = require("./copy-to-clients");
const { spawnProcess } = require("../utils/spawn-process");

const SDK_CLIENTS_DIR = normalize(join(__dirname, "..", "..", "clients"));

Expand Down Expand Up @@ -34,28 +33,15 @@ const { solo } = yargs(process.argv.slice(2))
require("../api-examples/get-examples");
require("../api-examples/merge-examples").merge(void 0, solo);

console.log("================ starting eslint ================", "\n", new Date().toString(), solo);
try {
await spawnProcess("npx", ["eslint", "--quiet", "--fix", `${clientFolder}/src/**/*`]);
} catch (ignored) {}
console.log("================ starting wasm-fmt biome ================", "\n", new Date().toString(), solo);

if (solo === "dynamodb") {
try {
await spawnProcess("npx", ["eslint", "--quiet", "--fix", `${libFolder}/src/**/*`]);
} catch (ignored) {}
}
const target = `${clientFolder}/src`;
const { runWasmFmtBiome } = await import("./run-wasm-fmt-biome.mjs");
runWasmFmtBiome(target);

console.log("================ starting prettier ================", "\n", new Date().toString(), solo);
await spawnProcess("npx", [
"prettier",
"--write",
"--loglevel",
"warn",
`${clientFolder}/src/**/*.{md,js,ts,json}`,
]);
await spawnProcess("npx", ["prettier", "--write", "--loglevel", "warn", `${clientFolder}/README.md`]);
if (solo === "dynamodb") {
await spawnProcess("npx", ["prettier", "--write", "--loglevel", "warn", `${libFolder}/src/**/*.{md,js,ts,json}`]);
const target = `${libFolder}/src`;
runWasmFmtBiome(target);
}

const compress = require("../endpoints-ruleset/compress");
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4016,6 +4016,11 @@
loupe "^2.3.6"
pretty-format "^29.5.0"

"@wasm-fmt/biome_fmt@^0.1.12":
version "0.1.12"
resolved "https://registry.yarnpkg.com/@wasm-fmt/biome_fmt/-/biome_fmt-0.1.12.tgz#11f239b03b19be9a755006c77183722246c8dfbc"
integrity sha512-2t7UdDh9sQ4JCXLXHfJeeqmeRcijbbgmLQ69sREWuYU7nMp8R/4F9g4bIEvJVvWmA4sNmPRvPJW/i3s5lDS11A==

"@webassemblyjs/[email protected]":
version "1.11.1"
resolved "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.11.1.tgz"
Expand Down

0 comments on commit 0243e98

Please sign in to comment.