Skip to content

Commit 905c00d

Browse files
authored
Merge pull request #4 from DoneDeal0/fix-node22-imports
fix: use mjs format
2 parents 23295ae + 7e6e65d commit 905c00d

File tree

9 files changed

+74
-54
lines changed

9 files changed

+74
-54
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<img width="1003" height="377" alt="banner" src="https://res.cloudinary.com/dmjisqsyo/image/upload/v1754334056/banner_cjwgin.png" />
1+
<img width="1003" height="377" alt="banner" src="https://res.cloudinary.com/dmjisqsyo/image/upload/v1755025208/codfather_corrected_af9prv.png" />
22

33

44
[![CI](https://github.com/DoneDeal0/codefather/actions/workflows/ci.yml/badge.svg)](https://github.com/DoneDeal0/codefather/actions/workflows/ci.yml)
@@ -72,7 +72,7 @@ npm install @donedeal0/codefather --save-dev
7272
- If a `.github/CODEOWNERS` file is present, it will be used to generate the config.
7373
- Accepts two optional flags:
7474
- `json`: generates a json config file instead of a `ts` one.
75-
- `overwrite`: overwrite an existing codefather config.
75+
- `overwrite`: overwrites an existing codefather config.
7676
- example: `npm run codefather-init json overwrite`
7777
- `codefather-github`: similar to `codefather`, but designed to run in a GitHub Action environment
7878

@@ -87,8 +87,8 @@ You can either add a script shortcut in your `package.json`:
8787
Or directly run the commands with `npx`:
8888

8989
```bash
90-
npx codefather
9190
npx codefather-init
91+
npx codefather
9292
```
9393

9494
## CONFIG
@@ -188,6 +188,18 @@ git config user.username # return DonCorleone
188188

189189
In a Github Action, `codefather` will use Github's API, so you don't have to worry about the git config.
190190

191+
## How to Write Rules
192+
193+
- Match all files in a folder (recursively): `src/myfolder/`
194+
- Match a specific file: `src/myfolder/file.ts`
195+
- Match files by extension in a folder (glob): `src/folder/*.css`
196+
- Match files by extension in a folder (regex): `/^src\/folder\/.*\.css$/`
197+
- Match any file in any subfolder: `src/**`
198+
- Match dotfiles: `.env`
199+
- Use `*` for single-level matches, `**` for recursive matches
200+
201+
ℹ️ *More examples are available in the test files. Codefather's matching patterns follow classic file matcher rules, like GitHub CODEOWNERS.*
202+
191203
<hr/>
192204

193205
# GITHUB ACTION

cli/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env node
22
import { runCheck } from "./run-check/index.js";
3-
export * from "@shared/models";
43

54
runCheck();

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
{
22
"name": "@donedeal0/codefather",
3-
"version": "1.0.0",
3+
"version": "1.0.7",
44
"description": "Codefather protects your codebase by controlling who can change what. Set authorization levels, lock down files, and enforce your rules—offline via CLI or online with GitHub Actions.",
55
"license": "ISC",
66
"author": "DoneDeal0",
77
"files": [
88
"dist"
99
],
10-
"main": "dist/index.cjs",
11-
"module": "dist/index.js",
10+
"type": "module",
1211
"types": "dist/index.d.ts",
13-
"publishConfig": {
14-
"access": "public"
15-
},
16-
"exports": {
17-
".": {
18-
"types": "./dist/index.d.ts",
19-
"import": "./dist/index.js",
20-
"require": "./dist/index.cjs"
21-
}
12+
"bin": {
13+
"codefather": "./dist/index.mjs",
14+
"codefather-github": "./dist/scripts/github.mjs",
15+
"codefather-init": "./dist/scripts/init.mjs"
2216
},
2317
"repository": {
2418
"type": "git",
@@ -30,9 +24,11 @@
3024
"funding": {
3125
"type": "github",
3226
"url": "https://github.com/sponsors/DoneDeal0"
27+
},
28+
"publishConfig": {
29+
"access": "public"
3330
},
3431
"readme": "./README.md",
35-
"type": "module",
3632
"declaration": true,
3733
"keywords": [
3834
"codeowners",
@@ -74,11 +70,6 @@
7470
"godfather",
7571
"authorization"
7672
],
77-
"bin": {
78-
"codefather": "./dist/index.js",
79-
"codefather-github": "./dist/scripts/github.js",
80-
"codefather-init": "./dist/scripts/init.js"
81-
},
8273
"scripts": {
8374
"build": "tsup",
8475
"codefather-github": "npm run build && node dist/scripts/github.js",
@@ -94,7 +85,7 @@
9485
"dependencies": {
9586
"@actions/github": "^6.0.1",
9687
"@octokit/rest": "^22.0.0",
97-
"tsx": "^4.20.3"
88+
"esbuild": "^0.25.8"
9889
},
9990
"devDependencies": {
10091
"@commitlint/cli": "^19.8.1",
@@ -114,7 +105,7 @@
114105
"swc-loader": "^0.2.6",
115106
"ts-node": "^10.9.2",
116107
"tsup": "^8.5.0",
117-
"typescript-eslint": "^8.38.0",
118-
"typescript": "^5.8.3"
108+
"typescript": "^5.8.3",
109+
"typescript-eslint": "^8.38.0"
119110
}
120111
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function importDataBuffer(dataUrl: string) {
2+
return import(dataUrl);
3+
}

shared/loader/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { transform } from "esbuild";
12
import fs from "fs";
23
import path from "path";
3-
import { pathToFileURL } from "url";
44
import { getRandomMessage } from "@shared/messages";
55
import { MessageType, type CodefatherConfig } from "@shared/models";
66
import { safeJSONParse } from "@shared/parser";
7+
import { importDataBuffer } from "./import-data-buffer";
78

89
export async function loadConfig(): Promise<CodefatherConfig> {
910
try {
@@ -13,9 +14,13 @@ export async function loadConfig(): Promise<CodefatherConfig> {
1314
const jsonPath = path.resolve(root, "codefather.json");
1415

1516
if (fs.existsSync(tsPath)) {
16-
const { register } = await import("tsx/esm/api");
17-
register();
18-
const config = await import(pathToFileURL(tsPath).href);
17+
const tsCode = fs.readFileSync(tsPath, "utf-8");
18+
const { code } = await transform(tsCode, {
19+
loader: "ts",
20+
format: "esm",
21+
});
22+
const dataUrl = `data:text/javascript;base64,${Buffer.from(code).toString("base64")}`;
23+
const config = await importDataBuffer(dataUrl);
1924
// a typescript file import may have several 'default' levels depending on the environment
2025
return config?.default?.default || config?.default || config;
2126
}

shared/loader/loader.test.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
import { writeFileSync, unlinkSync } from "fs";
1+
import { writeFileSync, unlinkSync, existsSync } from "fs";
22
import { resolve } from "path";
33
import { loadConfig } from ".";
44

5+
jest.mock("./import-data-buffer", () => ({
6+
importDataBuffer: jest.fn(async () => ({
7+
default: { rules: [{ match: ["src/**"], goodfellas: ["sonny"] }] },
8+
})),
9+
}));
10+
511
const tsConfigPath = resolve(process.cwd(), "codefather.ts");
612
const jsonConfigPath = resolve(process.cwd(), "codefather.json");
713

814
describe("loadConfig", () => {
15+
afterEach(() => {
16+
if (existsSync(tsConfigPath)) unlinkSync(tsConfigPath);
17+
if (existsSync(jsonConfigPath)) unlinkSync(jsonConfigPath);
18+
});
919
test("returns config when codefather.ts exists", async () => {
1020
writeFileSync(
1121
tsConfigPath,
@@ -14,7 +24,6 @@ describe("loadConfig", () => {
1424
};`
1525
);
1626
const result = await loadConfig();
17-
unlinkSync(tsConfigPath);
1827
expect(result?.rules?.[0]?.goodfellas).toEqual(["sonny"]);
1928
});
2029
test("returns config when codefather.json exists", async () => {
@@ -25,30 +34,20 @@ describe("loadConfig", () => {
2534
})
2635
);
2736
const result = await loadConfig();
28-
unlinkSync(jsonConfigPath);
2937
expect(result?.rules?.[0]?.goodfellas).toEqual(["sonny"]);
3038
});
3139
test("throws an error if codefather.json is not properly formatted", async () => {
3240
writeFileSync(
3341
jsonConfigPath,
3442
`{ rules: { match: ["src/**"] goodfellas: ["sonny"] }] }`
3543
);
36-
try {
37-
await loadConfig();
38-
} catch (err) {
39-
unlinkSync(jsonConfigPath);
40-
expect(err instanceof Error ? err.message : err).toBe(
41-
"Your JSON file is invalid. You gotta respect the rules if you want my help."
42-
);
43-
}
44+
await expect(loadConfig()).rejects.toThrow(
45+
"Your codefather.json file is invalid. You gotta respect the rules if you want my help."
46+
);
4447
});
4548
test("throws an error when no codefather.(ts|json) exists", async () => {
46-
try {
47-
await loadConfig();
48-
} catch (err) {
49-
expect(err instanceof Error ? err.message : err).toBe(
50-
"𐄂 The codefather.ts file doesn't exist. Maybe someone whacked it?"
51-
);
52-
}
49+
await expect(loadConfig()).rejects.toThrow(
50+
"𐄂 The codefather.ts file doesn't exist. Maybe someone whacked it?"
51+
);
5352
});
5453
});

shared/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function safeJSONParse<T>(json: string): T {
44
// eslint-disable-next-line @typescript-eslint/no-unused-vars
55
} catch (_) {
66
throw new Error(
7-
"Your JSON file is invalid. You gotta respect the rules if you want my help."
7+
"Your codefather.json file is invalid. You gotta respect the rules if you want my help."
88
);
99
}
1010
}

tsup.config.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ export default defineConfig([
77
"scripts/init": "scripts/init/index.ts",
88
"scripts/github": "scripts/github/index.ts",
99
},
10-
format: ["cjs", "esm"],
1110
dts: {
12-
entry: ["cli/index.ts"],
11+
entry: ["shared/models/index.ts"],
1312
resolve: true,
1413
},
15-
splitting: true,
14+
format: ["esm"],
15+
splitting: false,
1616
clean: true,
1717
treeshake: true,
1818
shims: true,
1919
minify: true,
2020
platform: "node",
21-
name: "MAIN",
22-
external: ["tsx"],
21+
name: "CLI",
22+
external: ["esbuild"],
23+
outExtension: () => ({ js: ".mjs" }),
2324
},
2425
]);

0 commit comments

Comments
 (0)