Skip to content

Commit

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

* Create loud-elephants-attend.md

* fix
  • Loading branch information
ota-meshi committed May 9, 2023
1 parent 7c2e179 commit 6033d9c
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-elephants-attend.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-yml": minor
---

feat: export meta object
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -11,7 +11,8 @@
},
"scripts": {
"prebuild": "npm run -s clean",
"build": "npm run build:ts",
"build": "npm run build:meta && npm run build:ts",
"build:meta": "npm run ts -- ./tools/update-meta.ts",
"build:ts": "tsc --project ./tsconfig.build.json",
"clean": "rimraf .nyc_output dist coverage",
"lint": "eslint . --ext .js,.vue,.ts,.json,.md,.yml,.yaml",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Expand Up @@ -4,6 +4,7 @@ import base from "./configs/base";
import recommended from "./configs/recommended";
import standard from "./configs/standard";
import prettier from "./configs/prettier";
import * as meta from "./meta";

const configs = {
base,
Expand All @@ -18,6 +19,7 @@ const rules = ruleList.reduce((obj, r) => {
}, {} as { [key: string]: RuleModule });

export = {
meta,
configs,
rules,
};
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 "yarn build:meta"
export const name = "eslint-plugin-yml" as const;
export const version = "1.6.0" as const;
23 changes: 23 additions & 0 deletions tests/src/meta.ts
@@ -0,0 +1,23 @@
import assert from "assert";
import plugin from "../../src";
import { version } from "../../package.json";
const expectedMeta = {
name: "eslint-plugin-yml",
version,
};

describe("Test for meta object", () => {
it("A plugin should have a meta object.", () => {
assert.deepStrictEqual(plugin.meta, expectedMeta);
});

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.deepStrictEqual(processor.meta, expectedMeta);
});
}
});
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-yml")!
.newVersion;
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, "../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 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 @@ -45,7 +45,11 @@ const CONFIGS = {
};

for (const rec of ["recommended", "standard", "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";
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -19,6 +19,7 @@
// },
"esModuleInterop": true,
"outDir": "lib",
"resolveJsonModule": true,

"skipLibCheck": true
},
Expand Down

0 comments on commit 6033d9c

Please sign in to comment.