diff --git a/package.json b/package.json index b0460b8869ab5..749aa5a4e9c5a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": "yarn@1.22.22" } diff --git a/scripts/biome-test.mjs b/scripts/biome-test.mjs new file mode 100644 index 0000000000000..dac7a0a2d4a07 --- /dev/null +++ b/scripts/biome-test.mjs @@ -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")); diff --git a/scripts/generate-clients/code-eslint-fix.js b/scripts/generate-clients/code-eslint-fix.js index 5aaa8933d53c0..08dd82908f207 100644 --- a/scripts/generate-clients/code-eslint-fix.js +++ b/scripts/generate-clients/code-eslint-fix.js @@ -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 = { diff --git a/scripts/generate-clients/code-prettify.js b/scripts/generate-clients/code-prettify.js deleted file mode 100644 index 610442509d0ff..0000000000000 --- a/scripts/generate-clients/code-prettify.js +++ /dev/null @@ -1,18 +0,0 @@ -// @ts-check -const { spawnProcess } = require("../utils/spawn-process"); -const path = require("path"); - -const prettifyCode = async (dir) => { - await spawnProcess(path.join(__dirname, "..", "..", "node_modules", ".bin", "pprettier"), [ - "--write", - `${dir}/*/typescript-codegen/**/*.{ts,js,md,json}`, - ]); - await spawnProcess(path.join(__dirname, "..", "..", "node_modules", ".bin", "pprettier"), [ - "--write", - `${dir}/*/typescript-ssdk-codegen/**/*.{ts,js,md,json}`, - ]); -}; - -module.exports = { - prettifyCode, -}; diff --git a/scripts/generate-clients/code-prettify.mjs b/scripts/generate-clients/code-prettify.mjs new file mode 100644 index 0000000000000..772174aec19dd --- /dev/null +++ b/scripts/generate-clients/code-prettify.mjs @@ -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); + } + } + } +}; diff --git a/scripts/generate-clients/generic.js b/scripts/generate-clients/generic.js index d6a7dd7cd3513..dff92217258ec 100644 --- a/scripts/generate-clients/generic.js +++ b/scripts/generate-clients/generic.js @@ -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(); diff --git a/scripts/generate-clients/index.js b/scripts/generate-clients/index.js index 564e84f8cf195..93c2b53241b68 100644 --- a/scripts/generate-clients/index.js +++ b/scripts/generate-clients/index.js @@ -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"); @@ -75,6 +74,7 @@ const { .help().argv; (async () => { + const { prettifyCode } = await import("./code-prettify.mjs"); try { if (!noSmithyCheckout) { await buildSmithyTypeScript(repo, commit); diff --git a/scripts/generate-clients/run-wasm-fmt-biome.mjs b/scripts/generate-clients/run-wasm-fmt-biome.mjs new file mode 100644 index 0000000000000..6ea3d44b4555b --- /dev/null +++ b/scripts/generate-clients/run-wasm-fmt-biome.mjs @@ -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" + ); + }); + } + } +}; diff --git a/scripts/generate-clients/single-service.js b/scripts/generate-clients/single-service.js index e4f82a48ba320..4636b45a4b7ad 100644 --- a/scripts/generate-clients/single-service.js +++ b/scripts/generate-clients/single-service.js @@ -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")); @@ -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"); diff --git a/yarn.lock b/yarn.lock index 5c0b0a0276059..234efaa846470 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/ast@1.11.1": version "1.11.1" resolved "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.11.1.tgz"