Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyuanliang3 committed Feb 25, 2021
0 parents commit 71906f8
Show file tree
Hide file tree
Showing 33 changed files with 8,528 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = {
write: true,
plugin: 'autod-egg',
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
'coverage',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'autod',
'autod-egg',
'egg-bin',
'tslib',
'typescript',
],
keep: [
],
semver: [
],
test: 'scripts',
};
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*.d.ts
node_modules/
12 changes: 12 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "eslint-config-egg/typescript",
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"array-bracket-spacing": "off",
"space-before-function-paren": "off",
"arrow-parens": "off",
"comma-dangle": "off"
}
}
42 changes: 42 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 2 * * *'

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version: [8]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- name: Checkout Git Source
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm i -g npminstall && npminstall

- name: Continuous Integration
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
logs/
.DS_Store
.vscode
*.swp
*.lock
*.js
!.autod.conf.js
!yarn.lock

app/**/*.js
test/**/*.js
config/**/*.js
app/**/*.map
test/**/*.map
config/**/*.map
2 changes: 2 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
semi: true
singleQuote: true
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- 'lts/*'
before_install:
- npm i npminstall -g
install:
- npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# hackernews-async-ts

[Hacker News](https://news.ycombinator.com/) showcase using typescript && egg

## QuickStart

### Development

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

Don't tsc compile at development mode, if you had run `tsc` then you need to `npm run clean` before `npm run dev`.

### Deploy

```bash
$ npm run tsc
$ npm start
```

### Npm Scripts

- Use `npm run lint` to check code style
- Use `npm test` to run unit test
- se `npm run clean` to clean compiled js at development mode once

### Requirement

- Node.js 8.x
- Typescript 2.8+
30 changes: 30 additions & 0 deletions app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Controller } from 'egg';
import { Route, HttpGet } from 'egg-decorator-router';
import { server } from '../middleware/vite';

@Route()
export default class HomeController extends Controller {
@HttpGet('/')
public async index() {
const renderData: any = {};

if (this.config.env === 'local') {
const addressInfo: any = server?.httpServer?.address();

renderData.server = `${this.ctx.protocol}://${
addressInfo?.address === '::' ? 'localhost' : addressInfo?.address
}:${addressInfo?.port}`;
}

await this.ctx.render('index.html', renderData);
}

@HttpGet('/api')
public api() {
const { ctx } = this;

ctx.body = 'api';
}
}

// new Controller()
28 changes: 28 additions & 0 deletions app/middleware/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// import {} from 'egg';

import { EggMiddlewareFactory } from 'egg';
import { UserConfig, createServer, ViteDevServer } from 'vite';

export let server: ViteDevServer;

const middleware: EggMiddlewareFactory = (options: UserConfig) => {
return async (_ctx, next) => {
if (!server) {
server = await createServer({
...options,
configFile: false,
root: process.cwd(),
// server: { port: options.server?.port || 8088 },
});

await server.listen();

_ctx.app.on('beforeClose', () => {
server?.close();
});
}
await next();
};
};

export default middleware;
15 changes: 15 additions & 0 deletions app/service/Test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Service } from 'egg';

/**
* Test Service
*/
export default class Test extends Service {

/**
* sayHi to you
* @param name - your name
*/
public async sayHi(name: string) {
return `hi, ${name}`;
}
}
18 changes: 18 additions & 0 deletions app/view/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
</head>
<body>
<noscript>
<strong>
We're sorry but it doesn't work properly without JavaScript enabled.
Please enable it to continue.
</strong>
</noscript>
<div id="app"></div>
<script type="module" src="{{server}}/client/main.ts"></script>
</body>
</html>
14 changes: 14 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
environment:
matrix:
- nodejs_version: '8'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run test

build: off
9 changes: 9 additions & 0 deletions client/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>
<Hello name="xxxxxx"></Hello>
</div>
</template>

<script lang="ts" setup>
import Hello from './components/Hello.vue';
</script>
9 changes: 9 additions & 0 deletions client/components/Hello.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<h1>Hello {{ props.name }} !!</h1>
</template>

<script lang="ts" setup>
import { defineProps } from 'vue';
const props = defineProps({ name: String });
</script>
5 changes: 5 additions & 0 deletions client/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createApp } from 'vue';
import App from './App.vue';
import 'vite/dynamic-import-polyfill';

createApp(App).mount('#app');
30 changes: 30 additions & 0 deletions config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

// override config from framework / plugin
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1614221169780_4319';

// add your egg config in here
config.middleware = [];

// add your special config in here
const bizConfig = {
sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
};

config.view = {
defaultViewEngine: 'nunjucks',
mapping: {
'.html': 'nunjucks',
},
};

// the return config will combines to EggAppConfig
return {
...config,
...bizConfig,
};
};
21 changes: 21 additions & 0 deletions config/config.local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EggAppConfig, PowerPartial } from 'egg';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default () => {
const config: PowerPartial<EggAppConfig> = {};

config.middleware = ['vite'];

config.vite = defineConfig({
build: {
manifest: true,
rollupOptions: {
input: '/client/main.ts',
},
},
plugins: [vue()],
});

return config;
};
6 changes: 6 additions & 0 deletions config/config.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EggAppConfig, PowerPartial } from 'egg';

export default () => {
const config: PowerPartial<EggAppConfig> = {};
return config;
};
15 changes: 15 additions & 0 deletions config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { EggPlugin } from 'egg';

const plugin: EggPlugin = {
// static: true,
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
},
decoratorRouter: {
enable: true,
package: 'egg-decorator-router',
},
};

export default plugin;
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
</head>
<body>
<noscript>
<strong>
We're sorry but it doesn't work properly without JavaScript enabled.
Please enable it to continue.
</strong>
</noscript>
<div id="app"></div>
<script type="module" src="./client/main.ts"></script>
</body>
</html>
Loading

0 comments on commit 71906f8

Please sign in to comment.