Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/decorators #4

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions __fixtures__/decorators/src/class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { admin, creator, external, internal } from '@hyperweb/decorators';
import { BigNumber } from 'jsd-std';

export interface State {
count: BigNumber;
}

export class Counter {
private state: State;

constructor(initialState: State) {
this.state.count = initialState;
}

// Public by default (no decorator needed)
@external
@admin
public getCount(): BigNumber {
return this.state.count;
}

// Only admin and creator can increment
@internal
@admin
public increment(amount: BigNumber): void {
this.state.count = this.state.count.add(amount);
}

// Only creator can decrement
@creator
public decrement(amount: BigNumber): void {
if (this.state.count.lt(amount)) {
throw new Error('Count cannot be negative');
}
this.state.count = this.state.count.sub(amount);
}
}
12 changes: 12 additions & 0 deletions __fixtures__/decorators/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from './class';
export * from './object';
class MyClass {
@permission('debug', 'level')
@performance
async fetchData() {
// ... method implementation
}
}

export default MyClass;
export {MyClass};
30 changes: 30 additions & 0 deletions __fixtures__/decorators/src/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BigNumber } from "jsd-std";

interface State {
count: BigNumber;
}

// Core contract logic
export const start = (initialCount: BigNumber) => {
let state: State = {
count: initialCount
};

// HOW TO EVEN DO DECORATORS
return {
getCount: () => state.count,

increment: (amount: BigNumber) => {
state.count = state.count.add(amount);
return state.count;
},

decrement: (amount: BigNumber) => {
if (state.count.lt(amount)) {
throw new Error("Count cannot be negative");
}
state.count = state.count.sub(amount);
return state.count;
}
};
};
57 changes: 57 additions & 0 deletions __output__/decorators/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions __output__/decorators/bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions __output__/decorators/decorators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"../../__fixtures__/decorators/src/index.ts": [
{
"name": "permission",
"args": [
"debug",
"level"
],
"targetName": "fetchData"
},
{
"name": "performance",
"args": [],
"targetName": "fetchData"
}
],
"../../__fixtures__/decorators/src/class.ts": [
{
"name": "external",
"args": [],
"targetName": "getCount"
},
{
"name": "admin",
"args": [],
"targetName": "getCount"
},
{
"name": "internal",
"args": [],
"targetName": "increment"
},
{
"name": "admin",
"args": [],
"targetName": "increment"
},
{
"name": "creator",
"args": [],
"targetName": "decrement"
}
]
}
41 changes: 41 additions & 0 deletions packages/build/__tests__/decorators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from 'fs/promises';
import { join, resolve } from 'path';

import { createDecoratorExtractorPlugin } from '../src/decorators';
import { HyperwebBuild, HyperwebBuildOptions } from '../src/index';

const fixtureDir = resolve(join(__dirname, '/../../../__fixtures__/', 'decorators'));
const outputDir = resolve(join(__dirname, '/../../../__output__/', 'decorators'));

describe('HyperwebBuild', () => {
it('builds the fixture project successfully', async () => {
const outfile = join(outputDir, 'bundle.js');

const options: Partial<HyperwebBuildOptions> = {
entryPoints: [join(fixtureDir, 'src/index.ts')],
outfile,
external: ['otherpackage', '~somepackage'],
customPlugins: [
createDecoratorExtractorPlugin({
outputPath: join(outputDir, 'decorators.json'),
include: [/\.ts$/], // Only process TypeScript files
exclude: [/node_modules/, /\.test\.ts$/] // Skip node_modules and test files
})
]
};

await HyperwebBuild.build(options);

// Check if the output file exists
const outfileExists = await fs.access(outfile)
.then(() => true)
.catch(() => false);

expect(outfileExists).toBe(true);

// Optionally, you can read the contents of the file and perform additional checks
// const bundleContent = await fs.readFile(outfile, 'utf-8');
// expect(bundleContent).toContain('function greet');
// expect(bundleContent).toContain('function farewell');
});
});
4 changes: 4 additions & 0 deletions packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
},
"keywords": [],
"dependencies": {
"@babel/generator": "7.22.9",
"@babel/parser": "^7.22.7",
"@babel/traverse": "7.22.8",
"@babel/types": "7.22.5",
"esbuild": "^0.23.1"
}
}
Loading