Skip to content

Commit

Permalink
refactor: rewrite and separate into multiple packages
Browse files Browse the repository at this point in the history
  • Loading branch information
varHarrie authored Sep 8, 2021
1 parent d33de3e commit e5bf7c5
Show file tree
Hide file tree
Showing 106 changed files with 34,206 additions and 1 deletion.
43 changes: 43 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"env": {
"node": true
},
"parser": "@typescript-eslint/parser",
"extends": [
"airbnb-base",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"prettier"
],
"rules": {
"consistent-return": "off",
"no-await-in-loop": "off",
"no-bitwise": "off",
"no-console": "off",
"no-param-reassign": "off",
"no-restricted-globals": "off",
"no-restricted-syntax": "off",
"no-return-assign": "off",
"no-shadow": "off",
"no-use-before-define": "off",
"import/extensions": "off",
"import/export": "off",
"import/namespace": "off",
"import/no-unresolved": "off",
"import/prefer-default-export": "off",
"@typescript-eslint/no-shadow": ["error"],
"@typescript-eslint/no-inferrable-types": "off"
},
"overrides": [
{
"files": ["**/*.js", "**/scripts/*.ts"],
"rules": {
"import/no-extraneous-dependencies": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-var-requires": "off"
}
}
]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
.idea
node_modules
lib
*.tgz
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"printWidth": 180,
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"semi": true,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-present, varHarrie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
99 changes: 98 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,100 @@
<p align="center">
<a href="https://varharrie.github.io/mokia/" target="_blank" rel="noopener noreferrer">
<img width="180" src="https://raw.githubusercontent.com/varharrie/mokia/master/packages/docs/docs/.vuepress/public/logo.png" alt="logo">
</a>
</p>

<p align="center">
<a href="https://github.com/varharrie/mokia/blob/master/LICENSE">
<img src="https://img.shields.io/npm/l/mokia.svg" alt="License">
</a>
<a href="https://www.npmjs.com/package/mokia">
<img src="https://img.shields.io/npm/v/mokia.svg" alt="Version">
</a>
<a href="https://npmcharts.com/compare/mokia?minimal=true">
<img src="https://img.shields.io/npm/dm/mokia.svg" alt="Downloads">
</a>
<a href="https://github.com/varHarrie/mokia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc">
<img src="https://img.shields.io/github/issues/varharrie/mokia.svg" alt="Issues">
</a>
</p>

# Mokia

A data mocking library.
🐒 An out of the box mock API server to help quickly create back-end prototype and data simulations.

Documentation: [中文](https://varharrie.github.io/mokia/)

## Basic Usage

Create entry file (e.g. index.js):

```javascript
// index.js
module.exports = {
port: 3000,
'GET /users'() {
return this.list(() => ({ id: this.uuid(), name: this.fullName() }));
},
'GET /users/:id'(req) {
return { id: req.params.id, name: this.fullName() };
},
};
```

Start local http server:

```bash
npx mokia index.js --watch
```

Open browser and go to http://localhost:3000/users, you will get the response.

## Advanced Usage

TypeScript Support and Class-style mock schema:

```typescript
// index.ts

import mokia from 'mokia';

class User {
@mokia.uuid()
id: string;

@mokia.fullName()
name: string;

constructor(id?: string) {
if (id) this.id = id;
}
}

class Article {
@mokia.uuid()
id: string;

@mokia.generate(User)
author: User;

@mokia.passage()
content: string;

constructor(id?: string) {
if (id) this.id = id;
}
}

export default mokia.defineConfig({
port: 3000,
'GET /users': mokia.list(User),
'GET /users/:id': (req) => new User(req.params.id),
'GET /articles': mokia.list(Article),
'GET /articles/:id': (req) => new Article(req.params.id),
});
```

### License

[MIT](./LICENSE)
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] };
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"private": true,
"name": "mokia-root",
"version": "0.4.0",
"engines": {
"node": ">=12.0"
},
"workspaces": [
"packages/producer",
"packages/decorator",
"packages/server",
"packages/cli",
"packages/mokia",
"packages/docs"
],
"scripts": {
"prepare": "husky install",
"bootstrap": "yarn install && yarn workspaces info",
"lint": "eslint packages/*/src/** --ext .ts",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "node scripts/release.js"
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@types/node": "^14.17.9",
"@typescript-eslint/eslint-plugin": "^4.29.2",
"@typescript-eslint/parser": "^4.29.2",
"chalk": "^4.1.2",
"conventional-changelog-cli": "^2.1.1",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.0",
"execa": "^5.1.1",
"husky": "^7.0.1",
"ora": "^5.4.1",
"prettier": "^2.3.2",
"prompts": "^2.4.1",
"semver": "^7.3.5",
"ts-node": "^10.2.1",
"typescript": "^4.4.2"
}
}
21 changes: 21 additions & 0 deletions packages/cli/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-present, varHarrie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @mokia/cli

> CLI for mokia.
See the website [mokia](https://varharrie.github.io/mokia/) for more information.
3 changes: 3 additions & 0 deletions packages/cli/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib');
47 changes: 47 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@mokia/cli",
"version": "0.4.0",
"description": "CLI for mokia.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"bin": {
"mokia": "bin/index.js"
},
"files": [
"bin",
"lib"
],
"scripts": {
"test": "echo \"No test specified\" && exit 0",
"clean": "rimraf lib",
"build": "npm run clean && tsc -p tsconfig.build.json"
},
"homepage": "https://github.com/varHarrie/mokia#readme",
"author": "varharrie <varharrie@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/varHarrie/mokia.git"
},
"bugs": {
"url": "https://github.com/varHarrie/mokia/issues"
},
"dependencies": {
"@mokia/producer": "0.4.0",
"@mokia/server": "0.4.0",
"@types/debug": "^4.1.5",
"@types/node": "^14.14.37",
"chalk": "^2.4.2",
"chokidar": "^3.5.1",
"debug": "^4.3.1",
"meow": "^9.0.0",
"ora": "^5.4.0",
"pirates": "^4.0.1",
"tree-kill": "^1.2.2",
"ts-node": "^9.1.1",
"typescript": "^4.4.2"
},
"devDependencies": {
"rimraf": "^3.0.2"
}
}
41 changes: 41 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import meow from 'meow';
import run from './run';

const cli = meow(
`
Usage
$ mokia <entry-file> [options]
Options
--version Print version
--help Print help
--host, -h Set host, default to "localhost"
--port, -p Set port, default to 8080
--prefix Set route prefix
--proxy Set Proxy target
--silent, -s Hide request logs
--watch, -w Watch input files
--debug Enable debug mode
Example
$ mokia index.js
$ mokia example/index.ts --port 3000
`,
{
flags: {
host: { type: 'string', alias: 'h' },
port: { type: 'string', alias: 'p' },
prefix: { type: 'string' },
proxy: { type: 'string' },
silent: { type: 'boolean', alias: 's' },
watch: { type: 'boolean', alias: 'w' },
debug: { type: 'boolean' },
},
},
);

if (cli.input[0]) {
run(cli.input[0], cli.flags);
} else {
cli.showHelp();
}
Loading

0 comments on commit e5bf7c5

Please sign in to comment.