Skip to content

Commit

Permalink
publishing ready
Browse files Browse the repository at this point in the history
  • Loading branch information
uneco committed May 22, 2024
1 parent d56af4c commit 6e1cea3
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: publish to npm

on:
push:
tags:
- 'v*'

jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
registry-url: 'https://registry.npmjs.org/'

- name: Install dependencies
run: npm install

- name: Build the package
run: npm run build

- name: Publish to npm
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Done
run: echo "Package published to npm successfully."
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,12 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# lockfiles
yarn.lock
package-lock.json
.pnpm-lock.yaml
bun.lockb

# artifact
lib
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
*config.*
.github
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "vesuva",
"version": "0.0.1-notest",
"description": "dynamically reconstructs method chains as strings using proxies.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "git+github.com:kanamone/vesuva.git"
},
"type": "module",
"scripts": {
"prepare": "npm run build",
"build": "tsc -p ."
},
"author": {
"name": "uneco",
"email": "[email protected]",
"url": "https://u-ne.co"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"typescript": "^5.4.5"
}
}
65 changes: 65 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function slate <T> (): T {
return (() => {}) as T
}

export function encode (arg: unknown): string {
if (typeof arg === 'object') {
if (arg === null) {
return 'null'
}

if (Array.isArray(arg)) {
return `[${arg.map(encode).join(',')}]`
}

const keys = Object.keys(arg)
const pairs = keys.map((key) => `${key}:${encode((arg as Record<string, unknown>)[key])}`)

return `{${pairs.join(',')}}`
}

if (arg === undefined) {
return 'undefined'
}

let isVesuva = false

try {
(arg as { __VESUVA__: () => void }).__VESUVA__()
isVesuva = true
} catch (e) {}

if (isVesuva) {
return arg.toString()
}

return JSON.stringify(arg)
}

export function proxy<T extends object>(receiver: string, initialPath: string[] = []): T {
return new Proxy<T>(slate(), {
get(_target, propKey) {
const stringify = () => [receiver, ...initialPath].join('.')

const specials: Record<string, () => unknown> = {
toString: stringify,
toJSON: stringify,
__VESUVA__ () {
return true
}
}

if (typeof propKey === 'string' && propKey in specials) {
return specials[propKey]
}

return new Proxy(slate(), {
apply (_target, _thisArg, argumentsList) {
const args = argumentsList.map(encode).join(', ');
const newPath = [...initialPath, `${String(propKey)}(${args})`];
return proxy(receiver, newPath);
}
});
}
});
}
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"lib": ["ES6", "ES2015.Proxy"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"declaration": true,
"sourceMap": true,
"outDir": "./lib",
"forceConsistentCasingInFileNames": true,
"noEmit": false,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"isolatedModules": true
},
"include": ["src/index.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 6e1cea3

Please sign in to comment.