From 587344da627c5b23352100f2782539d574ff7408 Mon Sep 17 00:00:00 2001 From: Shyam Chen Date: Fri, 19 Apr 2024 18:48:58 +0800 Subject: [PATCH] feat(TypeScript): Add the workflow file for CI check (#1280) * feat(TypeScript): Add the workflow file for CI check * fix: Install TypeScript Execute (tsx) * test: Test failed * test: Test successful * test: on windows * Add a bug * feat: type checking * test: Test successful * feat: add main * fix: use process * feat: use es-main * Test failure * feat: streamlined configuration * Apply suggestions from code review Co-authored-by: Yudong Jin * feat: remove lock file * Test failure * Test success --------- Co-authored-by: Yudong Jin --- .github/workflows/typescript.yml | 26 +++++++++++++++++++ codes/typescript/.gitignore | 2 -- .../chapter_graph/graph_adjacency_list.ts | 5 ++-- codes/typescript/chapter_heap/my_heap.ts | 2 +- .../chapter_tree/array_binary_tree.ts | 6 ++--- codes/typescript/modules/PrintUtil.ts | 3 --- codes/typescript/package.json | 11 ++++++++ codes/typescript/tsconfig.json | 14 ++++++---- 8 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/typescript.yml create mode 100644 codes/typescript/package.json diff --git a/.github/workflows/typescript.yml b/.github/workflows/typescript.yml new file mode 100644 index 000000000..0dc3db042 --- /dev/null +++ b/.github/workflows/typescript.yml @@ -0,0 +1,26 @@ +name: TypeScript + +on: + push: + branches: ['main'] + paths: ['codes/typescript/**/*.ts'] + pull_request: + branches: ['main'] + paths: ['codes/typescript/**/*.ts'] + workflow_dispatch: + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20.x + - name: Install dependencies + run: cd codes/typescript && npm install + - name: Check TypeScript code + run: cd codes/typescript && npm run check diff --git a/codes/typescript/.gitignore b/codes/typescript/.gitignore index 201706994..d5f19d89b 100644 --- a/codes/typescript/.gitignore +++ b/codes/typescript/.gitignore @@ -1,4 +1,2 @@ node_modules -out -package.json package-lock.json diff --git a/codes/typescript/chapter_graph/graph_adjacency_list.ts b/codes/typescript/chapter_graph/graph_adjacency_list.ts index b1d079fe9..4c7692312 100644 --- a/codes/typescript/chapter_graph/graph_adjacency_list.ts +++ b/codes/typescript/chapter_graph/graph_adjacency_list.ts @@ -91,9 +91,8 @@ class GraphAdjList { } } -// need to add the package @types/node contains type definitions for Node.js, npm i --save-dev @types/node -if (require.main === module) { - /* Driver Code */ +/* Driver Code */ +if (import.meta.url.endsWith(process.argv[1])) { /* 初始化无向图 */ const v0 = new Vertex(1), v1 = new Vertex(3), diff --git a/codes/typescript/chapter_heap/my_heap.ts b/codes/typescript/chapter_heap/my_heap.ts index 188bd7f08..d9c300c98 100644 --- a/codes/typescript/chapter_heap/my_heap.ts +++ b/codes/typescript/chapter_heap/my_heap.ts @@ -122,7 +122,7 @@ class MaxHeap { } /* Driver Code */ -if (require.main === module) { +if (import.meta.url.endsWith(process.argv[1])) { /* 初始化大顶堆 */ const maxHeap = new MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]); console.log('\n输入列表并建堆后'); diff --git a/codes/typescript/chapter_tree/array_binary_tree.ts b/codes/typescript/chapter_tree/array_binary_tree.ts index 139082ee8..f77ceb499 100644 --- a/codes/typescript/chapter_tree/array_binary_tree.ts +++ b/codes/typescript/chapter_tree/array_binary_tree.ts @@ -4,8 +4,8 @@ * Author: yuan0221 (yl1452491917@gmail.com) */ -const { arrToTree } = require('../modules/TreeNode'); -const { printTree } = require('../modules/PrintUtil'); +import { arrToTree } from '../modules/TreeNode'; +import { printTree } from '../modules/PrintUtil'; type Order = 'pre' | 'in' | 'post'; @@ -148,4 +148,4 @@ console.log('中序遍历为:' + res); res = abt.postOrder(); console.log('后序遍历为:' + res); -export { }; +export {}; diff --git a/codes/typescript/modules/PrintUtil.ts b/codes/typescript/modules/PrintUtil.ts index 93dad2d1c..2245e12fc 100644 --- a/codes/typescript/modules/PrintUtil.ts +++ b/codes/typescript/modules/PrintUtil.ts @@ -79,9 +79,6 @@ function showTrunks(p: Trunk | null) { showTrunks(p.prev); process.stdout.write(p.str); - // ts-node to execute, we need to install type definitions for node - // solve: npm i --save-dev @types/node - // restart the vscode } /* 打印堆 */ diff --git a/codes/typescript/package.json b/codes/typescript/package.json new file mode 100644 index 000000000..8c33947d4 --- /dev/null +++ b/codes/typescript/package.json @@ -0,0 +1,11 @@ +{ + "private": true, + "type": "module", + "scripts": { + "check": "tsc" + }, + "devDependencies": { + "@types/node": "^20.12.7", + "typescript": "^5.4.5" + } +} diff --git a/codes/typescript/tsconfig.json b/codes/typescript/tsconfig.json index 8c057ea0d..954efb8e7 100644 --- a/codes/typescript/tsconfig.json +++ b/codes/typescript/tsconfig.json @@ -1,8 +1,12 @@ { "compilerOptions": { - "target": "ES6", - "module": "CommonJS", - "outDir": "out", - "sourceMap": true - } + "baseUrl": ".", + "module": "esnext", + "moduleResolution": "node", + "types": ["@types/node"], + "noEmit": true, + "target": "esnext", + }, + "include": ["chapter_*/*.ts"], + "exclude": ["node_modules"] }