Skip to content

Commit

Permalink
feat(build): add cjs and types (#4)
Browse files Browse the repository at this point in the history
* feat: add cjs and types

* feat: add main.cjs
* feat: add main.d.ts
* feat: update package.json exports

* feat: add build

* fix(build): use node for imports

* chore(build): remove console log

* fix(types): add call signature

* fix: ignore tgz
  • Loading branch information
kettei-sproutty authored Aug 17, 2024
1 parent 97a0364 commit 7d8eed6
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@
node_modules

# Vitest
coverage
coverage

# Misc
.DS_Store

# Build
/package
*.tgz
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
},
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"github.copilot.enable": {
"*": true,
"plaintext": false,
"markdown": false,
"scminput": false
}
}
3 changes: 3 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"organizeImports": {
"enabled": true
},
"files": {
"ignore": ["src/main.d.ts", "tests/**/*.js"]
},
"linter": {
"enabled": true,
"rules": {
Expand Down
14 changes: 14 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { execSync } from "node:child_process";
import fs from "node:fs";

const pkg = fs.readFileSync("package.json");
const packageJSON = JSON.parse(pkg);

packageJSON.scripts = undefined;
packageJSON.devDependencies = undefined;

fs.writeFileSync("package.json", JSON.stringify(packageJSON, undefined, "\t"));

execSync("pnpm pack");

fs.writeFileSync("package.json", pkg);
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
"version": "0.1.0",
"description": "",
"license": "(MIT OR Apache-2.0)",
"main": "src/main.mjs",
"keywords": [],
"files": ["src", "package.json"],
"exports": {
".": {
"default": "./src/main.mjs",
"import": "./src/main.mjs",
"require": "./src/main.cjs",
"node": "./src/main.mjs",
"types": "./src/main.d.ts"
}
},
"scripts": {
"build": "node build.mjs",
"format": "biome format --write",
"test": "vitest"
},
"keywords": [],
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@types/node": "^22.4.0",
Expand Down
18 changes: 18 additions & 0 deletions src/main.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const util = require("node:util");

const handler = {
get(target, property) {
const properties = target?.()?.properties || [];
properties.push(property);
return new Proxy(() => ({ properties }), handler);
},

apply(target, _thisArg, args) {
const properties = target?.()?.properties;
return util.styleText(properties, args.join(" "));
},
};

const nodeSyles = new Proxy(() => {}, handler);

module.exports = nodeSyles;
15 changes: 15 additions & 0 deletions src/main.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { styleText } from "node:util";

type OmitArray<T> = T extends (infer U)[] ? U : T;

type Params = OmitArray<Parameters<typeof styleText>[0]>;

type NodeStylesProxy = {
[key in Params]: NodeStylesProxy & ((...args: any[]) => string);
};

declare const nodeStyles: NodeStylesProxy;

nodeStyles.bgBlack("hello");

export default nodeStyles;

0 comments on commit 7d8eed6

Please sign in to comment.