diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..465a96e --- /dev/null +++ b/.eslintrc @@ -0,0 +1,14 @@ +{ + "extends": "eslint-config-egg/typescript", + "parserOptions": { + // recommend to use another config file like tsconfig.eslint.json and extends tsconfig.json in it. + // because you may be need to lint test/**/*.test.ts but no need to emit to js. + // @see https://github.com/typescript-eslint/typescript-eslint/issues/890 + "project": "./tsconfig.json" + }, + "ignorePatterns": [ + "src/**/*.js", + "src/esm", + "src/cjs" + ] +} diff --git a/.gitignore b/.gitignore index 1335b4e..9c36dab 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ coverage/ dist/ node_modules/ .idea -proto +proto/ .fleet .DS_Store logs/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0dabfda --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Debug Current Test File", + "autoAttachChildProcesses": true, + "skipFiles": ["/**", "**/node_modules/**"], + "program": "${workspaceRoot}/node_modules/vitest/vitest.mjs", + "args": ["run", "${relativeFile}"], + "smartStep": true, + "console": "integratedTerminal" + } + ] +} diff --git a/package.json b/package.json index 560198e..d47c2df 100644 --- a/package.json +++ b/package.json @@ -2,17 +2,29 @@ "name": "layotto", "version": "1.0.0", "description": "Layotto Node.js SDK", - "main": "dist/index.js", + "type": "module", + "exports": { + ".": { + "import": { + "types": "./src/esm/index.d.ts", + "default": "./src/esm/index.js" + }, + "require": { + "types": "./src/cjs/index.d.ts", + "default": "./src/cjs/index.js" + } + } + }, + "types": "./src/esm/index.d.ts", + "main": "./src/cjs/index.js", "files": [ - "dist", - "proto", - "package.json", - "README.md" + "src", + "proto" ], "scripts": { "contributor": "git-contributor", "ci": "npm run lint && npm run build && npm run test:unit", - "test": "jest --runInBand --detectOpenHandles", + "test": "vitest run", "test:unit": "NODE_ENV=test npm run test 'test/unit/.*\\.test\\.ts'", "test:demo": "cd demo && npm run test:all", "lint": "tslint -p tsconfig.json", @@ -40,19 +52,16 @@ }, "homepage": "https://github.com/layotto/js-sdk#readme", "devDependencies": { - "@eggjs/tsconfig": "^1.0.0", "@types/google-protobuf": "^3.15.5", - "@types/jest": "^27.0.2", + "eslint": "^8.25.0", + "eslint-config-egg": "^12.1.0", "git-contributor": "^1.1.0", "grpc-tools": "^1.11.3", "grpc_tools_node_protoc_ts": "^5.3.2", - "jest": "^27.2.5", - "ts-jest": "^27.0.7", "ts-node": "^10.3.0", - "tslint": "^6.1.3", - "tslint-config-prettier": "^1.18.0", "type-fest": "^2.5.2", - "typescript": "^4.4.4" + "typescript": "^4.4.4", + "vitest": "^0.24.1" }, "dependencies": { "@grpc/grpc-js": "^1.4.1", diff --git a/scripts/build-grpc.sh b/scripts/build-grpc.sh index 64e04e8..faf76d1 100755 --- a/scripts/build-grpc.sh +++ b/scripts/build-grpc.sh @@ -20,11 +20,12 @@ ARCH=$(uname -m) PATH_ROOT=$(pwd) PATH_PROTO_ROOT="${PATH_ROOT}/layotto/spec/proto" PATH_PROTO_OUTPUT="${PATH_ROOT}/proto" +PATH_PROTO_OUTPUT_RUNTIME_V1="${PATH_PROTO_OUTPUT}/runtime/v1" PROTO_FILES=( -"runtime/v1/lifecycle.proto" -"runtime/v1/runtime.proto" -"runtime/v1/appcallback.proto" + "runtime/v1/lifecycle.proto" + "runtime/v1/runtime.proto" + "runtime/v1/appcallback.proto" ) generateGrpc() { @@ -48,6 +49,7 @@ generateGrpc() { --ts_out="grpc_js:$PATH_PROTO_OUTPUT" \ --grpc_out="grpc_js:$PATH_PROTO_OUTPUT" \ "$PATH_PROTO/$PATH_FILE" + cp "$PATH_PROTO/$PATH_FILE" "${PATH_PROTO_OUTPUT_RUNTIME_V1}/" } echo "" @@ -59,9 +61,12 @@ echo "" echo "Compiling gRPC files" for proto_file in ${PROTO_FILES[@]}; do - echo "generate ${proto_file}" + echo "$ generateGrpc $PATH_PROTO_ROOT ${proto_file}" generateGrpc $PATH_PROTO_ROOT "${proto_file}" done +echo "Gen commonjs package.json to ${PATH_PROTO_OUTPUT_RUNTIME_V1}/package.json" +echo "{ \"type\": \"commonjs\" }" > "${PATH_PROTO_OUTPUT_RUNTIME_V1}/package.json" + echo "" echo "DONE" diff --git a/test/unit/client/Binding.test.ts b/test/unit/client/Binding.test.ts index af325c6..8eb22f6 100644 --- a/test/unit/client/Binding.test.ts +++ b/test/unit/client/Binding.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { Client } from '../../../src'; diff --git a/test/unit/client/Client.test.ts b/test/unit/client/Client.test.ts index d9982a1..47ea1c8 100644 --- a/test/unit/client/Client.test.ts +++ b/test/unit/client/Client.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { Client } from '../../../src'; diff --git a/test/unit/client/Configuration.test.ts b/test/unit/client/Configuration.test.ts index dc7ff7c..1d52a5b 100644 --- a/test/unit/client/Configuration.test.ts +++ b/test/unit/client/Configuration.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { Client, utils } from '../../../src'; diff --git a/test/unit/client/File.test.ts b/test/unit/client/File.test.ts index 472d6e6..e1a44dc 100644 --- a/test/unit/client/File.test.ts +++ b/test/unit/client/File.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { tmpdir } from 'os'; import { existsSync, createWriteStream, createReadStream } from 'fs'; diff --git a/test/unit/client/Invoker.test.ts b/test/unit/client/Invoker.test.ts index 79f9d89..e19c234 100644 --- a/test/unit/client/Invoker.test.ts +++ b/test/unit/client/Invoker.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { Client } from '../../../src'; import { RumtimeTypes } from '../../../src'; diff --git a/test/unit/client/Lock.test.ts b/test/unit/client/Lock.test.ts index 396dcb3..2d5e9f7 100644 --- a/test/unit/client/Lock.test.ts +++ b/test/unit/client/Lock.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { Client, RumtimeTypes, utils } from '../../../src'; diff --git a/test/unit/client/PubSub.test.ts b/test/unit/client/PubSub.test.ts index 6047ab2..77331fd 100644 --- a/test/unit/client/PubSub.test.ts +++ b/test/unit/client/PubSub.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { Client } from '../../../src'; describe('client/PubSub.test.ts', () => { diff --git a/test/unit/client/Sequencer.test.ts b/test/unit/client/Sequencer.test.ts index bfb20ba..0371309 100644 --- a/test/unit/client/Sequencer.test.ts +++ b/test/unit/client/Sequencer.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { Client, RumtimeTypes } from '../../../src'; diff --git a/test/unit/client/State.test.ts b/test/unit/client/State.test.ts index 80d7f6b..cf1ee26 100644 --- a/test/unit/client/State.test.ts +++ b/test/unit/client/State.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it } from 'vitest'; import { strict as assert } from 'assert'; import { Client } from '../../../src'; import { StateOperation, StateOperationType } from '../../../src/types/State'; diff --git a/test/unit/server/PubSub.test.ts b/test/unit/server/PubSub.test.ts index 1ef43c2..763a4d4 100644 --- a/test/unit/server/PubSub.test.ts +++ b/test/unit/server/PubSub.test.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { describe, beforeAll, it, afterAll } from 'vitest'; import { strict as assert } from 'assert'; import { execSync } from 'child_process'; import { Server, Client, utils } from '../../../src'; diff --git a/tsconfig.json b/tsconfig.json index 75dee29..09bfb72 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,36 @@ { - "extends": "@eggjs/tsconfig", "compilerOptions": { - "rootDir": "./src", - "outDir": "./dist", - "module": "CommonJS", - "moduleResolution": "Node" + "useUnknownInCatchVariables": true, + "allowSyntheticDefaultImports": true, + "declaration": true, + "downlevelIteration": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "importHelpers": true, + "module": "NodeNext", + "moduleResolution": "Node", + "newLine": "LF", + "checkJs": false, + "allowJs": true, + "strict": true, + "skipLibCheck": true, + "suppressImplicitAnyIndexErrors": true, + "suppressExcessPropertyErrors": true, + "forceConsistentCasingInFileNames": true, + "target": "ES2018", + "sourceMap": true, + "esModuleInterop": true, + "stripInternal": true, + "lib": [ + "ESNext" + ], + "composite": true, + "types": [ + "node" + ], + "rootDir": "src" }, - "include": [ "src" ], - "exclude": ["node_modules", "**/test/*"] + "include": [ + "src/**/*.ts" + ] } diff --git a/tslint.json b/tslint.json deleted file mode 100644 index 5f147fb..0000000 --- a/tslint.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "rules": { - "no-console": false, - "no-shadowed-variable": false - }, - "extends": ["tslint:recommended", "tslint-config-prettier"] -} diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..288be79 --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + include: [ + 'test/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}', + ], + testTimeout: 5000, + }, +});