From 0791475f4cf1cc0b6d1c61ada0c117870e01e2bf Mon Sep 17 00:00:00 2001 From: "Daniel D. Beck" Date: Mon, 1 Jul 2024 16:18:46 +0200 Subject: [PATCH 1/2] Apply prettier to build script --- .prettierignore | 1 + scripts/build.ts | 36 +++++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.prettierignore b/.prettierignore index 65e93aabfd2..cfc7104a84c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,6 +10,7 @@ !/packages/compute-baseline/** !/packages/web-features/** +!/scripts/build.ts !/scripts/dist.ts !/scripts/feature-init.ts !/scripts/find-troublesome-ancestors.ts diff --git a/scripts/build.ts b/scripts/build.ts index e0c84a7f57d..d55f80fac2d 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -1,25 +1,31 @@ -import fs from 'fs'; +import fs from "fs"; -import stringify from 'fast-json-stable-stringify'; +import stringify from "fast-json-stable-stringify"; -import features from '../index.js'; -import { execSync } from 'child_process'; +import { execSync } from "child_process"; +import features from "../index.js"; -const rootDir = new URL('..', import.meta.url); -const packageDir = new URL('./packages/web-features/', rootDir); +const rootDir = new URL("..", import.meta.url); +const packageDir = new URL("./packages/web-features/", rootDir); const filesToCopy = ["LICENSE.txt", "types.ts"]; function build() { - const json = stringify(features); - // TODO: Validate the resulting JSON against a schema. - const path = new URL('index.json', packageDir); - fs.writeFileSync(path, json); - for (const file of filesToCopy) { - fs.copyFileSync(new URL(file, rootDir), new URL(file, packageDir)); - } - execSync("npm install", { cwd: "./packages/web-features", encoding: "utf-8"}); - execSync("npm run prepare", { cwd: "./packages/web-features", encoding: "utf-8"}); + const json = stringify(features); + // TODO: Validate the resulting JSON against a schema. + const path = new URL("index.json", packageDir); + fs.writeFileSync(path, json); + for (const file of filesToCopy) { + fs.copyFileSync(new URL(file, rootDir), new URL(file, packageDir)); + } + execSync("npm install", { + cwd: "./packages/web-features", + encoding: "utf-8", + }); + execSync("npm run prepare", { + cwd: "./packages/web-features", + encoding: "utf-8", + }); } build(); From 1859fb1add9edd34d183ce95d199ec7ac9468a8a Mon Sep 17 00:00:00 2001 From: "Daniel D. Beck" Date: Mon, 1 Jul 2024 17:17:33 +0200 Subject: [PATCH 2/2] Add CLI handling to `build.ts` This is a little bit of prep to facilitate generating bare JSONs as GitHub release artifacts. This also allows prettier to run on this script. --- package.json | 2 +- scripts/build.ts | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 67daac3532b..26caf5ff579 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "node": ">=18.19.0" }, "scripts": { - "build": "tsx scripts/build.ts", + "build": "tsx scripts/build.ts package", "dist": "tsx scripts/dist.ts", "schema-defs": "ts-json-schema-generator --tsconfig ./tsconfig.json --type FeatureData --path ./types.ts --id defs", "schema-defs:write": "npm run schema-defs -- --out ./schemas/defs.schema.json", diff --git a/scripts/build.ts b/scripts/build.ts index d55f80fac2d..70994b3d56a 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -1,16 +1,24 @@ -import fs from "fs"; - -import stringify from "fast-json-stable-stringify"; - import { execSync } from "child_process"; +import stringify from "fast-json-stable-stringify"; +import fs from "fs"; +import yargs from "yargs"; import features from "../index.js"; const rootDir = new URL("..", import.meta.url); -const packageDir = new URL("./packages/web-features/", rootDir); -const filesToCopy = ["LICENSE.txt", "types.ts"]; +yargs(process.argv.slice(2)) + .scriptName("build") + .command({ + command: "package", + describe: "Generate the web-features npm package", + handler: buildPackage, + }) + .parseSync(); + +function buildPackage() { + const packageDir = new URL("./packages/web-features/", rootDir); + const filesToCopy = ["LICENSE.txt", "types.ts"]; -function build() { const json = stringify(features); // TODO: Validate the resulting JSON against a schema. const path = new URL("index.json", packageDir); @@ -27,5 +35,3 @@ function build() { encoding: "utf-8", }); } - -build();