From 99bd0ec03a015dce6dfbcf9972d2c7c92efad4ad Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Tue, 9 May 2023 09:51:27 +0900 Subject: [PATCH] feat: export meta object (#140) * feat: export meta object * Create honest-lamps-destroy.md --- .changeset/honest-lamps-destroy.md | 5 ++++ .env-cmdrc | 5 ++++ .github/workflows/Release.yml | 2 ++ package.json | 8 +++++-- src/index.ts | 4 ++-- src/meta.ts | 5 ++++ tests/src/meta.ts | 13 ++++++++++ tools/lib/changesets-util.ts | 10 ++++++++ tools/update-meta.ts | 38 ++++++++++++++++++++++++++++++ tsconfig.json | 5 +++- 10 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 .changeset/honest-lamps-destroy.md create mode 100644 .env-cmdrc create mode 100644 src/meta.ts create mode 100644 tests/src/meta.ts create mode 100644 tools/lib/changesets-util.ts create mode 100644 tools/update-meta.ts diff --git a/.changeset/honest-lamps-destroy.md b/.changeset/honest-lamps-destroy.md new file mode 100644 index 0000000..9f54da4 --- /dev/null +++ b/.changeset/honest-lamps-destroy.md @@ -0,0 +1,5 @@ +--- +"yaml-eslint-parser": patch +--- + +feat: export meta object diff --git a/.env-cmdrc b/.env-cmdrc new file mode 100644 index 0000000..db5ffa1 --- /dev/null +++ b/.env-cmdrc @@ -0,0 +1,5 @@ +{ + "version-ci": { + "IN_VERSION_CI_SCRIPT": "true" + } +} diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index e4b4dbb..ed2e5d1 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -26,6 +26,8 @@ jobs: id: changesets uses: changesets/action@v1 with: + # this expects you to have a npm script called version that runs some logic and then calls `changeset version`. + version: yarn version:ci # This expects you to have a script called release which does a build for your packages and calls changeset publish publish: npm run release commit: "chore: release yaml-eslint-parser" diff --git a/package.json b/package.json index 5a89875..471d834 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ }, "scripts": { "prebuild": "npm run -s clean", - "build": "tsc --project ./tsconfig.build.json", + "build": "npm run build:meta && npm run build:tsc", + "build:meta": "ts-node --transpile-only ./tools/update-meta.ts", + "build:tsc": "tsc --project ./tsconfig.build.json", "clean": "rimraf .nyc_output lib coverage", "lint": "eslint . --ext .js,.ts,.json", "eslint-fix": "npm run lint -- --fix", @@ -22,7 +24,8 @@ "update-fixtures": "ts-node ./tools/update-fixtures.ts", "benchmark": "ts-node --transpile-only benchmark/index.ts", "prerelease": "npm run clean && npm run build", - "release": "changeset publish" + "release": "changeset publish", + "version:ci": "env-cmd -e version-ci npm run build:meta && changeset version" }, "repository": { "type": "git", @@ -59,6 +62,7 @@ "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "benchmark": "^2.1.4", + "env-cmd": "^10.1.0", "eslint": "^8.0.0", "eslint-config-prettier": "^8.0.0", "eslint-plugin-eslint-comments": "^3.2.0", diff --git a/src/index.ts b/src/index.ts index a2be698..114ea79 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,11 +4,11 @@ import { traverseNodes } from "./traverse"; import { getStaticYAMLValue } from "./utils"; import { KEYS } from "./visitor-keys"; import { ParseError } from "./errors"; +export * as meta from "./meta"; +export { name } from "./meta"; export { AST, ParseError }; -export const name = "yaml-eslint-parser"; - // parser export { parseForESLint }; // Keys diff --git a/src/meta.ts b/src/meta.ts new file mode 100644 index 0000000..7c187b3 --- /dev/null +++ b/src/meta.ts @@ -0,0 +1,5 @@ +// IMPORTANT! +// This file has been automatically generated, +// in order to update its content execute "npm run build:meta" +export const name = "yaml-eslint-parser" as const; +export const version = "1.2.1" as const; diff --git a/tests/src/meta.ts b/tests/src/meta.ts new file mode 100644 index 0000000..5bae2df --- /dev/null +++ b/tests/src/meta.ts @@ -0,0 +1,13 @@ +import assert from "assert"; +import * as parser from "../../src"; +import { version } from "../../package.json"; +const expectedMeta = { + name: "yaml-eslint-parser", + version, +}; + +describe("Test for meta object", () => { + it("A parser should have a meta object.", () => { + assert.deepStrictEqual(parser.meta, expectedMeta); + }); +}); diff --git a/tools/lib/changesets-util.ts b/tools/lib/changesets-util.ts new file mode 100644 index 0000000..d9f9da1 --- /dev/null +++ b/tools/lib/changesets-util.ts @@ -0,0 +1,10 @@ +import getReleasePlan from "@changesets/get-release-plan"; +import path from "path"; + +/** Get new version string from changesets */ +export async function getNewVersion(): Promise { + const releasePlan = await getReleasePlan(path.resolve(__dirname, "../..")); + + return releasePlan.releases.find(({ name }) => name === "yaml-eslint-parser")! + .newVersion; +} diff --git a/tools/update-meta.ts b/tools/update-meta.ts new file mode 100644 index 0000000..98667a2 --- /dev/null +++ b/tools/update-meta.ts @@ -0,0 +1,38 @@ +import fs from "fs"; +import path from "path"; +import { ESLint } from "eslint"; +import { name, version } from "../package.json"; +import { getNewVersion } from "./lib/changesets-util"; + +const META_PATH = path.join(__dirname, "../src/meta.ts"); + +void main(); + +/** main */ +async function main() { + if (!fs.existsSync(META_PATH)) { + fs.writeFileSync(META_PATH, "", "utf8"); + } + const eslint = new ESLint({ fix: true }); + const [result] = await eslint.lintText( + `/* + * IMPORTANT! + * This file has been automatically generated, + * in order to update its content execute "npm run build:meta" + */ +export const name = ${JSON.stringify(name)} as const; +export const version = ${JSON.stringify(await getVersion())} as const; +`, + { filePath: META_PATH } + ); + fs.writeFileSync(META_PATH, result.output!); +} + +/** Get version */ +function getVersion() { + // eslint-disable-next-line no-process-env -- ignore + if (process.env.IN_VERSION_CI_SCRIPT) { + return getNewVersion(); + } + return version; +} diff --git a/tsconfig.json b/tsconfig.json index 292bdcf..567eb7d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,10 @@ "noFallthroughCasesInSwitch": true, "noUnusedLocals": true, "noUnusedParameters": true, - "esModuleInterop": true + "esModuleInterop": true, + "resolveJsonModule": true, + + "skipLibCheck": true }, "include": ["src/**/*.ts", "tests/**/*.ts", "tools/**/*.ts", "benchmark/**/*"] }