Skip to content

Commit

Permalink
feat: export meta object (#140)
Browse files Browse the repository at this point in the history
* feat: export meta object

* Create honest-lamps-destroy.md
  • Loading branch information
ota-meshi committed May 9, 2023
1 parent 0c5a4a2 commit 99bd0ec
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-lamps-destroy.md
@@ -0,0 +1,5 @@
---
"yaml-eslint-parser": patch
---

feat: export meta object
5 changes: 5 additions & 0 deletions .env-cmdrc
@@ -0,0 +1,5 @@
{
"version-ci": {
"IN_VERSION_CI_SCRIPT": "true"
}
}
2 changes: 2 additions & 0 deletions .github/workflows/Release.yml
Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions 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;
13 changes: 13 additions & 0 deletions 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);
});
});
10 changes: 10 additions & 0 deletions 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<string> {
const releasePlan = await getReleasePlan(path.resolve(__dirname, "../.."));

return releasePlan.releases.find(({ name }) => name === "yaml-eslint-parser")!
.newVersion;
}
38 changes: 38 additions & 0 deletions 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;
}
5 changes: 4 additions & 1 deletion tsconfig.json
Expand Up @@ -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/**/*"]
}

0 comments on commit 99bd0ec

Please sign in to comment.