Skip to content

Commit

Permalink
feat: export meta object (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed May 9, 2023
1 parent 6784ea5 commit 70c558e
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-doors-crash.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-jsonc": minor
---

feat: export meta object
3 changes: 3 additions & 0 deletions lib/configs/prettier.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import path from "path";
const base = require.resolve("./base");
const baseExtend =
Expand Down
3 changes: 3 additions & 0 deletions lib/configs/recommended-with-json.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import path from "path";
const base = require.resolve("./base");
const baseExtend =
Expand Down
3 changes: 3 additions & 0 deletions lib/configs/recommended-with-json5.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import path from "path";
const base = require.resolve("./base");
const baseExtend =
Expand Down
3 changes: 3 additions & 0 deletions lib/configs/recommended-with-jsonc.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import path from "path";
const base = require.resolve("./base");
const baseExtend =
Expand Down
3 changes: 3 additions & 0 deletions lib/index.ts
Expand Up @@ -7,6 +7,7 @@ import recommendedWithJsonc from "./configs/recommended-with-jsonc";
import recommendedWithJson5 from "./configs/recommended-with-json5";
import prettier from "./configs/prettier";
import all from "./configs/all";
import * as meta from "./meta";

// backward compatibility
import {
Expand All @@ -33,6 +34,7 @@ const rules = ruleList.reduce((obj, r) => {
}, {} as { [key: string]: RuleModule });

export default {
meta,
configs,
rules,
// as parser
Expand All @@ -43,6 +45,7 @@ export default {
getStaticJSONValue,
};
export {
meta,
configs,
rules,
// as parser
Expand Down
5 changes: 5 additions & 0 deletions lib/meta.ts
@@ -0,0 +1,5 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
export const name = "eslint-plugin-jsonc" as const;
export const version = "2.7.0" as const;
3 changes: 3 additions & 0 deletions lib/utils/rules.ts
@@ -1,3 +1,6 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import type { RuleModule } from "../types";
import arrayBracketNewline from "../rules/array-bracket-newline";
import arrayBracketSpacing from "../rules/array-bracket-spacing";
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -17,7 +17,8 @@
},
"scripts": {
"prebuild": "npm run -s clean",
"build": "npm run build:ts && npm run build:dts",
"build": "npm run build:meta && npm run build:ts && npm run build:dts",
"build:meta": "ts-node --transpile-only ./tools/update-meta.ts",
"build:ts": "tsc --project ./tsconfig.build.json",
"build:dts": "npm run build:dts-step1 && npm run build:dts-step2",
"build:dts-step1": "tsc --declaration --outDir dist-ts --project ./tsconfig.build.json",
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/meta.ts
@@ -0,0 +1,26 @@
import assert from "assert";
import plugin from "../../lib";
import { version } from "../../package.json";
const expectedMeta = {
name: "eslint-plugin-jsonc",
version,
};

describe("Test for meta object", () => {
it("A plugin should have a meta object.", () => {
assert.strictEqual(plugin.meta.name, expectedMeta.name);
assert.strictEqual(typeof plugin.meta.version, "string");
});

for (const [name, processor] of Object.entries(
// @ts-expect-error -- missing processors
plugin.processors || {}
)) {
it(`"${name}" processor should have a meta object.`, () => {
// @ts-expect-error -- missing type
assert.strictEqual(processor.meta.name, expectedMeta.name);
// @ts-expect-error -- missing type
assert.strictEqual(typeof processor.meta.version, "string");
});
}
});
20 changes: 2 additions & 18 deletions tools/lib/changesets-util.ts
@@ -1,25 +1,9 @@
import assembleReleasePlan from "@changesets/assemble-release-plan";
import readChangesets from "@changesets/read";
import { read } from "@changesets/config";
import { getPackages } from "@manypkg/get-packages";
import { readPreState } from "@changesets/pre";
import getReleasePlan from "@changesets/get-release-plan";
import path from "path";

const root = path.resolve(__dirname, "../..");

/** Get new version string from changesets */
export async function getNewVersion(): Promise<string> {
const packages = await getPackages(root);
const preState = await readPreState(root);
const config = await read(root, packages);
const changesets = await readChangesets(root);

const releasePlan = assembleReleasePlan(
changesets,
packages,
config,
preState
);
const releasePlan = await getReleasePlan(path.resolve(__dirname, "../.."));

return releasePlan.releases.find(
({ name }) => name === "eslint-plugin-jsonc"
Expand Down
2 changes: 1 addition & 1 deletion tools/render-rules.ts
Expand Up @@ -42,7 +42,7 @@ export default function renderRulesTableContent(
const link = `[${rule.meta.docs.ruleId}](${buildRulePath(
rule.meta.docs.ruleName || ""
)})`;
const replacedRules = rule.meta.docs.replacedBy || [];
const replacedRules = rule.meta.replacedBy || [];
const replacedBy = replacedRules
.map((name) => `[jsonc/${name}](${buildRulePath(name)}.md)`)
.join(", ");
Expand Down
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, "../lib/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 update"
*/
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;
}
6 changes: 5 additions & 1 deletion tools/update-rules.ts
Expand Up @@ -12,7 +12,11 @@ function camelCase(str: string) {
return str.replace(/[-/_](\w)/gu, (_, c) => (c ? c.toUpperCase() : ""));
}

let content = `
let content = `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "npm run update"
*/
import type { RuleModule } from "../types"
${rules
.map(
Expand Down
6 changes: 5 additions & 1 deletion tools/update-rulesets.ts
Expand Up @@ -58,7 +58,11 @@ const CONFIGS = {
};

for (const rec of ["json", "jsonc", "json5", "prettier"] as const) {
let content = `
let content = `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "npm run update"
*/
import path from "path"
const base = require.resolve("./base")
const baseExtend =
Expand Down
1 change: 1 addition & 0 deletions tools/update.ts
Expand Up @@ -3,3 +3,4 @@ import "./update-rulesets";
import "./update-docs";
import "./update-readme";
import "./update-docs-rules-index";
import "./update-meta";
4 changes: 3 additions & 1 deletion tsconfig.json
Expand Up @@ -18,7 +18,9 @@
"*": ["typings/*"]
},
"esModuleInterop": true,
"resolveJsonModule": true
"resolveJsonModule": true,

"skipLibCheck": true
},
"include": [
"lib/**/*",
Expand Down

0 comments on commit 70c558e

Please sign in to comment.