Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add undist script #2474

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"build:extended": "tsx scripts/build.ts extended-json",
"build": "tsx scripts/build.ts package",
"dist": "tsx scripts/dist.ts",
"undist": "tsx scripts/undist.ts",
"feature-init": "tsx scripts/feature-init.ts",
"format": "npx prettier --write .",
"schema:write": "npm run schema -- --out ./schemas/data.schema.json",
Expand Down
78 changes: 78 additions & 0 deletions scripts/undist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import yargs from "yargs";
import { fileURLToPath } from "node:url";
import fs from "node:fs";
import path from "node:path";
import YAML from "yaml";

const argv = yargs(process.argv.slice(2))
.scriptName("undist")
.usage("$0 path", "Generate .yml from .yml.dist", (yargs) =>
yargs.positional("path", {
type: "string",
describe: "Path to feature.yml.dist file",
demandOption: true,
}),
)
.option("add", {
alias: "a",
type: "array",
describe: "BCD keys to add to the feature.yml file",
})
.option("sort", {
alias: "s",
type: "boolean",
default: true,
describe: "Sort the compat_features array",
}).argv;

// Support `path/to/feature.yml.dist`, `path/to/feature.yml`, or `path/to/feature`
function getPaths(featurePath: string): { yml: string; dist: string } {
const { name, ext, dir } = path.parse(featurePath);
const id = ext === ".dist" ? path.basename(name) : `${name}.yml`;

return {
yml: path.join(dir, id),
dist: path.join(dir, `${id}.dist`),
};
}

async function main() {
const { path, sort, add = [] } = argv;

const { yml, dist } = getPaths(path);

// Load and parse the .yml file
const ymlContents = fs.readFileSync(yml, { encoding: "utf-8" });
const ymlData = YAML.parseDocument(ymlContents);

// Assert 'compat_features' doesn't already exist
if (ymlData && ymlData.has("compat_features")) {
throw new Error(
`The key 'compat_features' is already present in the file ${yml}`,
);
}

// Load and parse the .dist.yml file
const distContents = fs.readFileSync(dist, { encoding: "utf-8" });
const distData = YAML.parse(distContents);

const compatFeatures = distData.compat_features;
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
if (!compatFeatures) {
throw new Error(`No 'compat_features' found in ${dist}`);
}
if (add.length > 0) {
compatFeatures.push(...add);
}

if (sort) {
compatFeatures.sort();
}
ymlData.set("compat_features", compatFeatures);

fs.writeFileSync(yml, ymlData.toString({ lineWidth: 0 }));
console.log(`Added keys to ${yml}`);
}

if (process.argv[1] === fileURLToPath(import.meta.url)) {
await main();
}
Loading