Skip to content

Commit

Permalink
chore: refact
Browse files Browse the repository at this point in the history
chore: app init, not impl
  • Loading branch information
npmstudy authored and sangshilong committed Oct 27, 2023
1 parent 6b8ee72 commit 9f846dd
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @tomrpc/mount

## 1.0.0

### Major Changes

- init
31 changes: 31 additions & 0 deletions packages/app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# @tomrpc/app


tom.json

```
{
name:'hi'
base: import.meta.url
static: 'public'
core: true
view: true
jwt: true
port: 3000
mount: './fn'
lifeCyle: {
}
}
```


```js
import app from '@tomrpc/app';

const rpc = createServer(cfg);

rpc.start();

```
37 changes: 37 additions & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@tomrpc/app",
"version": "1.0.0",
"description": "My Awesome lib",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"author": "npmstudy <[email protected]>",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsup src -- --dts-resolve",
"build:fast": "tsup src",
"dev": "tsup src --watch",
"test": "vitest run",
"test:watch": "vitest watch",
"coverage": "vitest run --coverage"
},
"keywords": [
"awesome-keywords"
],
"devDependencies": {
"@vitest/coverage-v8": "^0.34.2",
"happy-dom": "^6.0.4"
},
"dependencies": {
"@tomrpc/import-dir": "^1.2.4",
"debug": "^4.3.4",
"desm": "^1.3.0",
"flat": "^6.0.1"
}
}
9 changes: 9 additions & 0 deletions packages/app/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from 'vitest';

import mount from '../mount';

describe('lib', () => {
it('should render lib', () => {
// expect(lib()).toBe('lib');
});
});
25 changes: 25 additions & 0 deletions packages/app/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import debug from 'debug';

import mount from './mount';
import { isFunction } from './utils';

const log = debug('@tomrpc/mount');

// {a:{a:1, b:2}}
// {a.b:2, a.a:1}
// a.b => /a/b + function

export default async (rpc, dir) => {
const files = await mount(rpc.base, dir);

log(files);

Object.keys(files).forEach((key) => {
if (isFunction(files[key])) {
log('add funtion key=' + key);
rpc.fn(key, files[key]);
} else {
console.dir(`key=${key} is not a function`);
}
});
};
20 changes: 20 additions & 0 deletions packages/app/src/mount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import importDir, { join } from '@tomrpc/import-dir';
import debug from 'debug';
import { flatten } from 'flat';

const log = debug('@tomrpc/mount');

// {a:{a:1, b:2}}
// {a.b:2, a.a:1}
// a.b => /a/b + function

export default async (base, dir) => {
const files = await importDir(join(base, dir), {
recurse: true,
extensions: ['.ts', '.js', '.json'],
});

log(files);

return flatten(files);
};
6 changes: 6 additions & 0 deletions packages/app/src/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mount from './mount';

(async () => {
const files = await mount(import.meta.url, './f');
console.dir(files);
})();
18 changes: 18 additions & 0 deletions packages/app/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const toString = Object.prototype.toString;

export function isFunction(fn) {
if (!fn) {
return false;
}
const string = toString.call(fn);
return (
string === '[object Function]' ||
(typeof fn === 'function' && string !== '[object RegExp]') ||
(typeof window !== 'undefined' &&
// IE8 and below
(fn === window.setTimeout ||
fn === window.alert ||
fn === window.confirm ||
fn === window.prompt))
);
}
8 changes: 8 additions & 0 deletions packages/app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": [
"vitest/globals"
]
}
}
12 changes: 12 additions & 0 deletions packages/app/tsup.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'tsup';

export default defineConfig((options) => ({
entry: 'src/index.ts',
sourcemap: !options.watch,
minify: !options.watch,
dts: true,
format: ['esm'],
loader: {
'.js': 'jsx',
},
}));
10 changes: 10 additions & 0 deletions packages/app/vitest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="vitest" />

import { defineConfig } from 'vite';

export default defineConfig({
test: {
globals: true,
environment: 'happy-dom',
},
});
2 changes: 1 addition & 1 deletion packages/mount/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publishConfig": {
"access": "public"
},
"author": "npmstudy <npmstudy@126.com>",
"author": "npmstudy <npmstudy@qq.com>",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down

0 comments on commit 9f846dd

Please sign in to comment.