Skip to content

Commit 9afa0c4

Browse files
authored
feat: export meta object (#162)
* feat: export meta object * Create strange-humans-enjoy.md
1 parent f8fc1d8 commit 9afa0c4

File tree

8 files changed

+83
-6
lines changed

8 files changed

+83
-6
lines changed

.changeset/strange-humans-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"jsonc-eslint-parser": minor
3+
---
4+
5+
feat: export meta object

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
1111
},
1212
"scripts": {
13-
"build": "npm run build:ts",
14-
"build:ts": "tsc --project ./tsconfig.build.json",
13+
"build": "npm run build:meta && npm run build:tsc",
14+
"build:meta": "ts-node --transpile-only ./tools/update-meta.ts",
15+
"build:tsc": "tsc --project ./tsconfig.build.json",
1516
"clean": "rimraf .nyc_output lib coverage",
1617
"lint": "eslint . --ext .js,.ts,.json",
1718
"eslint-fix": "eslint . --ext .js,.ts,.json --fix",
@@ -26,7 +27,7 @@
2627
"benchmark": "ts-node --transpile-only benchmark/index.ts",
2728
"prerelease": "npm run clean && npm run build",
2829
"release": "changeset publish",
29-
"version:ci": "changeset version"
30+
"version:ci": "env-cmd -e version-ci npm run build:meta && changeset version"
3031
},
3132
"repository": {
3233
"type": "git",
@@ -61,6 +62,7 @@
6162
"@typescript-eslint/eslint-plugin": "^5.0.0",
6263
"@typescript-eslint/parser": "^5.0.0",
6364
"benchmark": "^2.1.4",
65+
"env-cmd": "^10.1.0",
6466
"eslint": "^8.0.0",
6567
"eslint-config-prettier": "^8.0.0",
6668
"eslint-plugin-eslint-comments": "^3.2.0",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99

1010
import type * as AST from "./parser/ast";
1111
import { getVisitorKeys } from "./parser/visitor-keys";
12-
13-
export const name = "jsonc-eslint-parser";
12+
export * as meta from "./meta";
13+
export { name } from "./meta";
1414

1515
// parser
1616
export { parseForESLint };

src/meta.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "npm run build:meta"
4+
export const name = "jsonc-eslint-parser" as const;
5+
export const version = "2.2.0" as const;

tests/src/meta.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import assert from "assert";
2+
import * as parser from "../../src";
3+
import { version } from "../../package.json";
4+
const expectedMeta = {
5+
name: "jsonc-eslint-parser",
6+
version,
7+
};
8+
9+
describe("Test for meta object", () => {
10+
it("A parser should have a meta object.", () => {
11+
assert.deepStrictEqual(parser.meta, expectedMeta);
12+
});
13+
});

tools/lib/changesets-util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import getReleasePlan from "@changesets/get-release-plan";
2+
import path from "path";
3+
4+
/** Get new version string from changesets */
5+
export async function getNewVersion(): Promise<string> {
6+
const releasePlan = await getReleasePlan(path.resolve(__dirname, "../.."));
7+
8+
return releasePlan.releases.find(
9+
({ name }) => name === "jsonc-eslint-parser"
10+
)!.newVersion;
11+
}

tools/update-meta.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { ESLint } from "eslint";
4+
import { name, version } from "../package.json";
5+
import { getNewVersion } from "./lib/changesets-util";
6+
7+
const META_PATH = path.join(__dirname, "../src/meta.ts");
8+
9+
void main();
10+
11+
/** main */
12+
async function main() {
13+
if (!fs.existsSync(META_PATH)) {
14+
fs.writeFileSync(META_PATH, "", "utf8");
15+
}
16+
const eslint = new ESLint({ fix: true });
17+
const [result] = await eslint.lintText(
18+
`/*
19+
* IMPORTANT!
20+
* This file has been automatically generated,
21+
* in order to update its content execute "npm run build:meta"
22+
*/
23+
export const name = ${JSON.stringify(name)} as const;
24+
export const version = ${JSON.stringify(await getVersion())} as const;
25+
`,
26+
{ filePath: META_PATH }
27+
);
28+
fs.writeFileSync(META_PATH, result.output!);
29+
}
30+
31+
/** Get version */
32+
function getVersion() {
33+
// eslint-disable-next-line no-process-env -- ignore
34+
if (process.env.IN_VERSION_CI_SCRIPT) {
35+
return getNewVersion();
36+
}
37+
return version;
38+
}

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"*": ["typings/*"]
1919
},
2020
"declaration": true,
21-
"esModuleInterop": true
21+
"esModuleInterop": true,
22+
"resolveJsonModule": true,
23+
24+
"skipLibCheck": true
2225
},
2326
"include": [
2427
"src/**/*",

0 commit comments

Comments
 (0)