Skip to content

Commit 681a161

Browse files
committed
Add prettier
1 parent 01bf670 commit 681a161

File tree

12 files changed

+1669
-104
lines changed

12 files changed

+1669
-104
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.eslintrc.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"env": {
3+
"es2021": true,
4+
"mocha": true,
5+
"node": true
6+
},
7+
"extends": ["standard-with-typescript", "prettier"],
8+
"parserOptions": {
9+
"ecmaVersion": 2021,
10+
"project": "tsconfig.json",
11+
"sourceType": "module"
12+
},
13+
"plugins": ["@typescript-eslint"],
14+
"rules": {
15+
"@typescript-eslint/explicit-function-return-type": "off",
16+
"@typescript-eslint/naming-convention": "off",
17+
"@typescript-eslint/no-namespace": "off",
18+
"@typescript-eslint/restrict-plus-operands": "off",
19+
"@typescript-eslint/restrict-template-expressions": "off",
20+
"@typescript-eslint/strict-boolean-expressions": "off"
21+
}
22+
}

.github/workflows/build.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
41
name: Build
52

63
on:
74
push:
85
branches:
96
- '**'
10-
pull_request:
11-
branches:
12-
- '**'
137

148
jobs:
15-
169
build-check:
17-
1810
name: Build check
1911

2012
runs-on: ubuntu-latest
@@ -23,4 +15,5 @@ jobs:
2315
- uses: actions/checkout@v2
2416

2517
- run: yarn
18+
- run: yarn lint
2619
- run: yarn build

.github/workflows/release.yml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3-
41
name: Release
52

63
on:
74
release:
85
types:
9-
- created
6+
- published
107

118
jobs:
12-
13-
publish-npm:
14-
15-
name: NPM Publish
9+
publish:
10+
name: Publish
1611

1712
if: github.repository_owner == 'coajs'
1813

@@ -26,20 +21,26 @@ jobs:
2621
node-version: 12
2722
registry-url: https://registry.npmjs.org
2823

29-
- name: Build
24+
- name: Yarn
3025
run: |
3126
yarn
32-
npm --no-git-tag-version version ${{ github.event.release.tag_name }}
33-
npm run build
27+
28+
- name: Lint
29+
run: |
30+
yarn lint
31+
32+
- name: Build
33+
run: |
34+
yarn version --no-git-tag-version --new-version ${{ github.event.release.tag_name }}
35+
yarn build
3436
3537
- name: Publish
3638
env:
3739
NODE_AUTH_TOKEN: ${{ secrets.AEX_NPM_TOKEN }}
3840
run: |
39-
cd dist
40-
npm publish
41+
yarn publish dist
4142
4243
- name: Sync
4344
run: |
4445
sleep 5s
45-
npm run sync
46+
yarn sync

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 160,
3+
"semi": false,
4+
"singleQuote": true
5+
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
## 特性
1313

14-
- **简单轻量** 不超过100行代码,不依赖于其他第三方库
15-
- **自动缓存** 内置内存缓存,不需要频繁读写IO,性能友好(请在单机应用下使用)
16-
- **TypeScript** 全部使用TypeScript书写,类型约束、IDE友好
14+
- **简单轻量** 不超过 100 行代码,不依赖于其他第三方库
15+
- **自动缓存** 内置内存缓存,不需要频繁读写 IO,性能友好(请在单机应用下使用)
16+
- **TypeScript** 全部使用 TypeScript 书写,类型约束、IDE 友好
1717

1818
## 快速开始
1919

@@ -42,4 +42,4 @@ fileStorage.get('key1') // { info: 'this is value' }
4242

4343
// 超过60秒后,数据过期,返回undefined
4444
fileStorage.get('key1') // undefined
45-
```
45+
```

package.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@
1111
],
1212
"license": "MIT",
1313
"author": "Aex",
14-
"homepage": "https://github.com/coajs",
14+
"homepage": "https://github.com/coajs/coa-file-storage",
1515
"repository": {
1616
"type": "git",
1717
"url": "https://github.com/coajs/coa-file-storage.git"
1818
},
1919
"scripts": {
2020
"dev": "tsc -w",
2121
"build": "rm -rf dist && tsc && cp package.json *.md dist && rm -rf dist/test",
22+
"lint": "eslint .",
23+
"prettier": "prettier -w .",
2224
"sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-file-storage?sync_upstream=true'",
23-
"publish-prerelease": "yarn build && yarn version --prerelease && cp package.json *.md dist && cd dist && rm -rf test && npm publish",
2425
"test": "NODE_PATH=run node dist/test"
2526
},
2627
"dependencies": {
27-
"coa-helper": "^1.1.1"
28+
"coa-helper": "^1.2.1"
2829
},
2930
"devDependencies": {
30-
"@types/node": "^14.14.37",
31-
"typescript": "^4.2.4"
31+
"@types/node": "^15.12.2",
32+
"@typescript-eslint/eslint-plugin": "^4.26.1",
33+
"eslint": "^7.28.0",
34+
"eslint-config-prettier": "^8.3.0",
35+
"eslint-config-standard-with-typescript": "^20.0.0",
36+
"eslint-plugin-import": "^2.23.4",
37+
"eslint-plugin-node": "^11.1.0",
38+
"eslint-plugin-promise": "^5.1.0",
39+
"prettier": "^2.3.1",
40+
"typescript": "^4.3.2"
3241
}
33-
}
42+
}

src/CoaFileStorage.ts

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,40 @@ import { _ } from 'coa-helper'
22
import { mkdirSync, readFileSync, writeFileSync } from 'fs'
33

44
export class CoaFileStorage {
5-
6-
private readonly dir: string
7-
private readonly store = {} as { [key: string]: [number, any] }
8-
9-
constructor (dir: string) {
10-
this.dir = dir
11-
mkdirSync(dir, { recursive: true })
12-
}
13-
14-
get (name: string) {
15-
name = _.snakeCase(name)
16-
if (!this.store[name])
17-
this.store[name] = this.fileRead(name) || [0]
18-
const data = this.store[name]
19-
if (data[0] > -1 && data[0] < Date.now())
20-
return undefined
21-
else
22-
return data[1]
23-
}
24-
25-
set (name: string, value: any, ms: number = -1) {
26-
name = _.snakeCase(name)
27-
const expire = ms > 0 ? (Date.now() + ms) : -1
28-
this.store[name] = [expire, value]
29-
this.fileWrite(name, this.store[name])
30-
}
31-
32-
private fileRead (name: string) {
33-
try {
34-
const string = readFileSync(`${this.dir}/${name}`).toString() || ''
35-
return JSON.parse(string)
36-
} catch (e) {
37-
return undefined
38-
}
39-
}
40-
41-
private fileWrite (name: string, data: any) {
42-
const string = JSON.stringify(data)
43-
writeFileSync(`${this.dir}/${name}`, string)
5+
private readonly dir: string
6+
private readonly store: { [key: string]: [number, any] } = {}
7+
8+
constructor(dir: string) {
9+
this.dir = dir
10+
mkdirSync(dir, { recursive: true })
11+
}
12+
13+
get(name: string) {
14+
name = _.snakeCase(name)
15+
if (!this.store[name]) this.store[name] = this.fileRead(name) || [0]
16+
const data = this.store[name]
17+
if (data[0] > -1 && data[0] < Date.now()) return undefined
18+
else return data[1]
19+
}
20+
21+
set(name: string, value: any, ms: number = -1) {
22+
name = _.snakeCase(name)
23+
const expire = ms > 0 ? Date.now() + ms : -1
24+
this.store[name] = [expire, value]
25+
this.fileWrite(name, this.store[name])
26+
}
27+
28+
private fileRead(name: string) {
29+
try {
30+
const string = readFileSync(`${this.dir}/${name}`).toString() || ''
31+
return JSON.parse(string)
32+
} catch (e) {
33+
return undefined
4434
}
35+
}
4536

46-
}
37+
private fileWrite(name: string, data: any) {
38+
const string = JSON.stringify(data)
39+
writeFileSync(`${this.dir}/${name}`, string)
40+
}
41+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { CoaFileStorage } from './CoaFileStorage'
1+
export { CoaFileStorage } from './CoaFileStorage'

0 commit comments

Comments
 (0)