Skip to content

Commit

Permalink
Add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
adaex committed Jun 8, 2021
1 parent 01bf670 commit 681a161
Show file tree
Hide file tree
Showing 12 changed files with 1,669 additions and 104 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"env": {
"es2021": true,
"mocha": true,
"node": true
},
"extends": ["standard-with-typescript", "prettier"],
"parserOptions": {
"ecmaVersion": 2021,
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/strict-boolean-expressions": "off"
}
}
9 changes: 1 addition & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
# 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: Build

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

jobs:

build-check:

name: Build check

runs-on: ubuntu-latest
Expand All @@ -23,4 +15,5 @@ jobs:
- uses: actions/checkout@v2

- run: yarn
- run: yarn lint
- run: yarn build
29 changes: 15 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Release

on:
release:
types:
- created
- published

jobs:

publish-npm:

name: NPM Publish
publish:
name: Publish

if: github.repository_owner == 'coajs'

Expand All @@ -26,20 +21,26 @@ jobs:
node-version: 12
registry-url: https://registry.npmjs.org

- name: Build
- name: Yarn
run: |
yarn
npm --no-git-tag-version version ${{ github.event.release.tag_name }}
npm run build
- name: Lint
run: |
yarn lint
- name: Build
run: |
yarn version --no-git-tag-version --new-version ${{ github.event.release.tag_name }}
yarn build
- name: Publish
env:
NODE_AUTH_TOKEN: ${{ secrets.AEX_NPM_TOKEN }}
run: |
cd dist
npm publish
yarn publish dist
- name: Sync
run: |
sleep 5s
npm run sync
yarn sync
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 160,
"semi": false,
"singleQuote": true
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

## 特性

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

## 快速开始

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

// 超过60秒后,数据过期,返回undefined
fileStorage.get('key1') // undefined
```
```
21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@
],
"license": "MIT",
"author": "Aex",
"homepage": "https://github.com/coajs",
"homepage": "https://github.com/coajs/coa-file-storage",
"repository": {
"type": "git",
"url": "https://github.com/coajs/coa-file-storage.git"
},
"scripts": {
"dev": "tsc -w",
"build": "rm -rf dist && tsc && cp package.json *.md dist && rm -rf dist/test",
"lint": "eslint .",
"prettier": "prettier -w .",
"sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-file-storage?sync_upstream=true'",
"publish-prerelease": "yarn build && yarn version --prerelease && cp package.json *.md dist && cd dist && rm -rf test && npm publish",
"test": "NODE_PATH=run node dist/test"
},
"dependencies": {
"coa-helper": "^1.1.1"
"coa-helper": "^1.2.1"
},
"devDependencies": {
"@types/node": "^14.14.37",
"typescript": "^4.2.4"
"@types/node": "^15.12.2",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"eslint": "^7.28.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard-with-typescript": "^20.0.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"prettier": "^2.3.1",
"typescript": "^4.3.2"
}
}
}
75 changes: 35 additions & 40 deletions src/CoaFileStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,40 @@ import { _ } from 'coa-helper'
import { mkdirSync, readFileSync, writeFileSync } from 'fs'

export class CoaFileStorage {

private readonly dir: string
private readonly store = {} as { [key: string]: [number, any] }

constructor (dir: string) {
this.dir = dir
mkdirSync(dir, { recursive: true })
}

get (name: string) {
name = _.snakeCase(name)
if (!this.store[name])
this.store[name] = this.fileRead(name) || [0]
const data = this.store[name]
if (data[0] > -1 && data[0] < Date.now())
return undefined
else
return data[1]
}

set (name: string, value: any, ms: number = -1) {
name = _.snakeCase(name)
const expire = ms > 0 ? (Date.now() + ms) : -1
this.store[name] = [expire, value]
this.fileWrite(name, this.store[name])
}

private fileRead (name: string) {
try {
const string = readFileSync(`${this.dir}/${name}`).toString() || ''
return JSON.parse(string)
} catch (e) {
return undefined
}
}

private fileWrite (name: string, data: any) {
const string = JSON.stringify(data)
writeFileSync(`${this.dir}/${name}`, string)
private readonly dir: string
private readonly store: { [key: string]: [number, any] } = {}

constructor(dir: string) {
this.dir = dir
mkdirSync(dir, { recursive: true })
}

get(name: string) {
name = _.snakeCase(name)
if (!this.store[name]) this.store[name] = this.fileRead(name) || [0]
const data = this.store[name]
if (data[0] > -1 && data[0] < Date.now()) return undefined
else return data[1]
}

set(name: string, value: any, ms: number = -1) {
name = _.snakeCase(name)
const expire = ms > 0 ? Date.now() + ms : -1
this.store[name] = [expire, value]
this.fileWrite(name, this.store[name])
}

private fileRead(name: string) {
try {
const string = readFileSync(`${this.dir}/${name}`).toString() || ''
return JSON.parse(string)
} catch (e) {
return undefined
}
}

}
private fileWrite(name: string, data: any) {
const string = JSON.stringify(data)
writeFileSync(`${this.dir}/${name}`, string)
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { CoaFileStorage } from './CoaFileStorage'
export { CoaFileStorage } from './CoaFileStorage'
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
"outDir": "dist",
"declaration": true
},
"include": [
"src"
]
"include": ["src"]
}
Loading

0 comments on commit 681a161

Please sign in to comment.