From 864d46c4520de26c0bf20d011c73c1d711b89ff1 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Mon, 23 Sep 2024 07:49:42 -0600 Subject: [PATCH 1/2] Move to package-extract. It doesn't add that much value at this point because we still need the set_version script, but it's one less thing to go wrong. This is a prelude to a larger set of changes to the build and release process. --- lib/compiler/passes/generate-js.js | 4 ++-- lib/peg.js | 2 +- lib/version.js | 9 +++----- package-lock.json | 17 +++++++++++++++ package.json | 9 ++++---- pnpm-lock.yaml | 12 +++++++++++ tools/header.js | 2 +- tools/set_version.js | 33 ------------------------------ tools/set_version.mjs | 23 +++++++++++++++++++++ 9 files changed, 64 insertions(+), 47 deletions(-) delete mode 100644 tools/set_version.js create mode 100644 tools/set_version.mjs diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 5f5ce348..8d145b6b 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -4,7 +4,7 @@ const asts = require("../asts"); const op = require("../opcodes"); const Stack = require("../stack"); -const VERSION = require("../../version"); +const { version } = require("../../version"); const { stringEscape, regexpClassEscape } = require("../utils"); const { SourceNode } = require("source-map-generator"); const GrammarLocation = require("../../grammar-location"); @@ -1425,7 +1425,7 @@ function generateJS(ast, options) { /** @return {(string|SourceNode)[]} */ function generateGeneratedByComment() { return [ - `// @generated by Peggy ${VERSION}.`, + `// @generated by Peggy ${version}.`, "//", "// https://peggyjs.org/", ]; diff --git a/lib/peg.js b/lib/peg.js index 0e986f59..d2a42492 100644 --- a/lib/peg.js +++ b/lib/peg.js @@ -5,7 +5,7 @@ const GrammarLocation = require("./grammar-location"); const asts = require("./compiler/asts.js"); const compiler = require("./compiler"); const parser = require("./parser"); -const VERSION = require("./version"); +const { version: VERSION } = require("./version"); const RESERVED_WORDS = [ // Reserved keywords as of ECMAScript 2015 diff --git a/lib/version.js b/lib/version.js index 82767410..2f56633f 100644 --- a/lib/version.js +++ b/lib/version.js @@ -1,9 +1,6 @@ - -// This file is generated. -// Do not edit it! Your work will be overwritten. -// -// Instead, please look at ./tools/set_version.js +// Generated by package-extract v2.1.1 +// Do not modify by hand. "use strict"; -module.exports = "4.0.3"; +exports.version = "4.0.3"; diff --git a/package-lock.json b/package-lock.json index d88651a7..538a9e53 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,6 +35,7 @@ "express": "4.21.0", "glob": "^11.0.0", "jest": "^29.7.0", + "package-extract": "2.1.1", "rimraf": "^5.0.10", "rollup": "^4.22.4", "rollup-plugin-ignore": "1.0.10", @@ -6277,6 +6278,22 @@ "node": ">=6" } }, + "node_modules/package-extract": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/package-extract/-/package-extract-2.1.1.tgz", + "integrity": "sha512-AZrNKcksaeOI8DkAs0aHaHGeXu7IAyjnSNZYO6LOwfGnr5jXgsaWxyLZUU+7pzXcegXcE2UMI4YeShJavO/Xog==", + "dev": true, + "license": "MIT", + "dependencies": { + "commander": "12.1.0" + }, + "bin": { + "package-extract": "bin/package-extract.js" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/package-json-from-dist": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/package-json-from-dist/-/package-json-from-dist-1.0.0.tgz", diff --git a/package.json b/package.json index f178ba8c..acb13363 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "clean": "rimraf -g build browser bin/*.map && mkdir browser", "parser": "node bin/peggy.js -c src/opts.mjs", "examples": "node bin/peggy.js -c docs/js/options.js docs/js/examples.peggy", - "set_version": "node ./tools/set_version", + "set_version": "node ./tools/set_version.mjs", "lint": "eslint .", "ts": "tsc --build tsconfig.json", "docs": "cd docs && npm run build", @@ -45,12 +45,12 @@ "deploy:tests": "copyfiles -f build/test-bundle.min.js docs/js/", "deploy:bench": "copyfiles -f build/benchmark-bundle.min.js docs/js/", "build": "npm run build:notidy && npm run tidy", - "build:notidy": "npm run setup && npm run make && npm run lint && npm run audit", - "setup": "npm run clean && npm run set_version", + "build:notidy": "npm run clean && npm run make && npm run lint && npm run audit", "make": "npm run parser && npm run ts && npm run rollup && npm run terser && npm run examples && npm run docs && npm run headers && npm run deploy", "audit": "npm run test && npm run benchmark", "tidy": "rimraf build", - "start": "cd docs && npm start" + "start": "cd docs && npm start", + "version": "npm run set_version && npm run parser && npm run parser && git add lib/version.js lib/parser.js" }, "devDependencies": { "@peggyjs/eslint-config": "^4.0.4", @@ -71,6 +71,7 @@ "express": "4.21.0", "glob": "^11.0.0", "jest": "^29.7.0", + "package-extract": "2.1.1", "rimraf": "^5.0.10", "rollup": "^4.22.4", "rollup-plugin-ignore": "1.0.10", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 42d96721..c5b73ff2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -72,6 +72,9 @@ importers: jest: specifier: ^29.7.0 version: 29.7.0(@types/node@22.5.5) + package-extract: + specifier: 2.1.1 + version: 2.1.1 rimraf: specifier: ^5.0.10 version: 5.0.10 @@ -2222,6 +2225,11 @@ packages: resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==} engines: {node: '>=6'} + package-extract@2.1.1: + resolution: {integrity: sha512-AZrNKcksaeOI8DkAs0aHaHGeXu7IAyjnSNZYO6LOwfGnr5jXgsaWxyLZUU+7pzXcegXcE2UMI4YeShJavO/Xog==} + engines: {node: '>=18'} + hasBin: true + package-json-from-dist@1.0.0: resolution: {integrity: sha512-dATvCeZN/8wQsGywez1mzHtTlP22H8OEfPrVMLNr4/eGa+ijtLn/6M5f0dY8UKNrC2O9UCU6SSoG3qRKnt7STw==} @@ -5409,6 +5417,10 @@ snapshots: p-try@2.2.0: {} + package-extract@2.1.1: + dependencies: + commander: 12.1.0 + package-json-from-dist@1.0.0: {} parent-module@1.0.1: diff --git a/tools/header.js b/tools/header.js index d3493546..7af51891 100644 --- a/tools/header.js +++ b/tools/header.js @@ -4,7 +4,7 @@ // Pass list of files on command line. const fs = require("fs"); -const version = require("../lib/version.js"); +const { version } = require("../lib/version.js"); const header = `\ // peggy ${version} diff --git a/tools/set_version.js b/tools/set_version.js deleted file mode 100644 index b5ee51d0..00000000 --- a/tools/set_version.js +++ /dev/null @@ -1,33 +0,0 @@ - -"use strict"; - -const fs = require("fs"); -const path = require("path"); - -const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "package.json"))); - -const file_contents = ` -// This file is generated. -// Do not edit it! Your work will be overwritten. -// -// Instead, please look at ./tools/set_version.js - -"use strict"; - -module.exports = "${pkg.version}"; -`; - -const version_file = path.join(__dirname, "..", "lib", "version.js"); -fs.writeFileSync(version_file, file_contents); - -console.log(` - Updated ${path.relative(process.cwd(), version_file)} to version ${pkg.version}`); - -const index_file = path.join(__dirname, "..", "docs", "index.html"); -const index = fs.readFileSync( - index_file, - "utf8" -); - -const updated = index.replace(/(https:\/\/unpkg.com\/peggy@)\d+\.\d+\.\d+/, `$1${pkg.version}`); -fs.writeFileSync(index_file, updated, "utf8"); -console.log(` - Updated ${path.relative(process.cwd(), index_file)} to version ${pkg.version}`); diff --git a/tools/set_version.mjs b/tools/set_version.mjs new file mode 100644 index 00000000..1010cf53 --- /dev/null +++ b/tools/set_version.mjs @@ -0,0 +1,23 @@ +import fs from "node:fs"; +import { packageExtract } from "package-extract"; + +const output = new URL("../lib/version.js", import.meta.url); + +await packageExtract({ + commonJS: true, + double: true, + semi: true, + output, +}); + +// Update links in docs. This is why package-extract isn't called directly. +const pkgFile = new URL("../package.json", import.meta.url); +const pkg = JSON.parse(fs.readFileSync(pkgFile)); +const index_file = new URL("../docs/index.html", import.meta.url); +const index = fs.readFileSync( + index_file, + "utf8" +); + +const updated = index.replace(/(https:\/\/unpkg.com\/peggy@)\d+\.\d+\.\d+/, `$1${pkg.version}`); +fs.writeFileSync(index_file, updated, "utf8"); From c7f49120193067416bb5f60b7261a0b29f6d3ab5 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Mon, 23 Sep 2024 09:38:32 -0600 Subject: [PATCH 2/2] Move regex replacement into package-extract --- lib/version.js | 2 +- package-lock.json | 16 ++++++++-------- package.json | 8 ++++---- pnpm-lock.yaml | 20 ++++++++++---------- tools/set_version.mjs | 23 ----------------------- 5 files changed, 23 insertions(+), 46 deletions(-) delete mode 100644 tools/set_version.mjs diff --git a/lib/version.js b/lib/version.js index 2f56633f..75483daa 100644 --- a/lib/version.js +++ b/lib/version.js @@ -1,4 +1,4 @@ -// Generated by package-extract v2.1.1 +// Generated by package-extract v2.2.0 // Do not modify by hand. "use strict"; diff --git a/package-lock.json b/package-lock.json index 538a9e53..ece303f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ }, "devDependencies": { "@peggyjs/eslint-config": "^4.0.4", - "@rollup/plugin-commonjs": "^26.0.1", + "@rollup/plugin-commonjs": "^26.0.2", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-multi-entry": "^6.0.1", "@rollup/plugin-node-resolve": "^15.2.4", @@ -35,7 +35,7 @@ "express": "4.21.0", "glob": "^11.0.0", "jest": "^29.7.0", - "package-extract": "2.1.1", + "package-extract": "2.2.0", "rimraf": "^5.0.10", "rollup": "^4.22.4", "rollup-plugin-ignore": "1.0.10", @@ -1537,9 +1537,9 @@ } }, "node_modules/@rollup/plugin-commonjs": { - "version": "26.0.1", - "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-26.0.1.tgz", - "integrity": "sha512-UnsKoZK6/aGIH6AdkptXhNvhaqftcjq3zZdT+LY5Ftms6JR06nADcDsYp5hTU9E2lbJUEOhdlY5J4DNTneM+jQ==", + "version": "26.0.2", + "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-26.0.2.tgz", + "integrity": "sha512-3corgoIXkETcGYMlJuCacVm/SLQsg4q5MxC80M1zU4QKYxz99Ip/TIzXiuFKgm9DJ95PDtfZ5TcFnteVAUmufg==", "dev": true, "license": "MIT", "dependencies": { @@ -6279,9 +6279,9 @@ } }, "node_modules/package-extract": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/package-extract/-/package-extract-2.1.1.tgz", - "integrity": "sha512-AZrNKcksaeOI8DkAs0aHaHGeXu7IAyjnSNZYO6LOwfGnr5jXgsaWxyLZUU+7pzXcegXcE2UMI4YeShJavO/Xog==", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/package-extract/-/package-extract-2.2.0.tgz", + "integrity": "sha512-tRVhDhmJYWLqiV7s/DS5xAhRFTXimMGvB1QVbD3w/4EQflioRv7ae+JkFZLLz+9Jv5H/SzzMQJBGk/W/uawdGA==", "dev": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index acb13363..bdac27f0 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "clean": "rimraf -g build browser bin/*.map && mkdir browser", "parser": "node bin/peggy.js -c src/opts.mjs", "examples": "node bin/peggy.js -c docs/js/options.js docs/js/examples.peggy", - "set_version": "node ./tools/set_version.mjs", + "set_version": "package-extract -o lib/version.js -dcs && package-extract -o docs/index.html -r '(?<=https:\\/\\/unpkg.com\\/peggy@)(?\\d+\\.\\d+\\.\\d+)(?=\\/browser\\/peggy\\.min\\.js)'", "lint": "eslint .", "ts": "tsc --build tsconfig.json", "docs": "cd docs && npm run build", @@ -50,11 +50,11 @@ "audit": "npm run test && npm run benchmark", "tidy": "rimraf build", "start": "cd docs && npm start", - "version": "npm run set_version && npm run parser && npm run parser && git add lib/version.js lib/parser.js" + "version": "npm run set_version && npm run parser && npm run parser && git add lib/version.js lib/parser.js docs/index.html" }, "devDependencies": { "@peggyjs/eslint-config": "^4.0.4", - "@rollup/plugin-commonjs": "^26.0.1", + "@rollup/plugin-commonjs": "^26.0.2", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-multi-entry": "^6.0.1", "@rollup/plugin-node-resolve": "^15.2.4", @@ -71,7 +71,7 @@ "express": "4.21.0", "glob": "^11.0.0", "jest": "^29.7.0", - "package-extract": "2.1.1", + "package-extract": "2.2.0", "rimraf": "^5.0.10", "rollup": "^4.22.4", "rollup-plugin-ignore": "1.0.10", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5b73ff2..c8c02663 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,8 +22,8 @@ importers: specifier: ^4.0.4 version: 4.0.4(eslint@9.11.0)(typescript@5.5.4) '@rollup/plugin-commonjs': - specifier: ^26.0.1 - version: 26.0.1(rollup@4.22.4) + specifier: ^26.0.2 + version: 26.0.2(rollup@4.22.4) '@rollup/plugin-json': specifier: ^6.1.0 version: 6.1.0(rollup@4.22.4) @@ -73,8 +73,8 @@ importers: specifier: ^29.7.0 version: 29.7.0(@types/node@22.5.5) package-extract: - specifier: 2.1.1 - version: 2.1.1 + specifier: 2.2.0 + version: 2.2.0 rimraf: specifier: ^5.0.10 version: 5.0.10 @@ -486,8 +486,8 @@ packages: engines: {node: '>=18'} hasBin: true - '@rollup/plugin-commonjs@26.0.1': - resolution: {integrity: sha512-UnsKoZK6/aGIH6AdkptXhNvhaqftcjq3zZdT+LY5Ftms6JR06nADcDsYp5hTU9E2lbJUEOhdlY5J4DNTneM+jQ==} + '@rollup/plugin-commonjs@26.0.2': + resolution: {integrity: sha512-3corgoIXkETcGYMlJuCacVm/SLQsg4q5MxC80M1zU4QKYxz99Ip/TIzXiuFKgm9DJ95PDtfZ5TcFnteVAUmufg==} engines: {node: '>=16.0.0 || 14 >= 14.17'} peerDependencies: rollup: ^2.68.0||^3.0.0||^4.0.0 @@ -2225,8 +2225,8 @@ packages: resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==} engines: {node: '>=6'} - package-extract@2.1.1: - resolution: {integrity: sha512-AZrNKcksaeOI8DkAs0aHaHGeXu7IAyjnSNZYO6LOwfGnr5jXgsaWxyLZUU+7pzXcegXcE2UMI4YeShJavO/Xog==} + package-extract@2.2.0: + resolution: {integrity: sha512-tRVhDhmJYWLqiV7s/DS5xAhRFTXimMGvB1QVbD3w/4EQflioRv7ae+JkFZLLz+9Jv5H/SzzMQJBGk/W/uawdGA==} engines: {node: '>=18'} hasBin: true @@ -3460,7 +3460,7 @@ snapshots: dependencies: playwright: 1.47.2 - '@rollup/plugin-commonjs@26.0.1(rollup@4.22.4)': + '@rollup/plugin-commonjs@26.0.2(rollup@4.22.4)': dependencies: '@rollup/pluginutils': 5.1.0(rollup@4.22.4) commondir: 1.0.1 @@ -5417,7 +5417,7 @@ snapshots: p-try@2.2.0: {} - package-extract@2.1.1: + package-extract@2.2.0: dependencies: commander: 12.1.0 diff --git a/tools/set_version.mjs b/tools/set_version.mjs deleted file mode 100644 index 1010cf53..00000000 --- a/tools/set_version.mjs +++ /dev/null @@ -1,23 +0,0 @@ -import fs from "node:fs"; -import { packageExtract } from "package-extract"; - -const output = new URL("../lib/version.js", import.meta.url); - -await packageExtract({ - commonJS: true, - double: true, - semi: true, - output, -}); - -// Update links in docs. This is why package-extract isn't called directly. -const pkgFile = new URL("../package.json", import.meta.url); -const pkg = JSON.parse(fs.readFileSync(pkgFile)); -const index_file = new URL("../docs/index.html", import.meta.url); -const index = fs.readFileSync( - index_file, - "utf8" -); - -const updated = index.replace(/(https:\/\/unpkg.com\/peggy@)\d+\.\d+\.\d+/, `$1${pkg.version}`); -fs.writeFileSync(index_file, updated, "utf8");