Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TypeScript): Add the workflow file for CI check #1280

Merged
merged 18 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/typescript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Install dependencies
run: cd codes/typescript && npm install
- name: Execute TypeScript code
run: deno run -A codes/typescript/test_all.ts
- name: Check TypeScript code
run: cd codes/typescript && npm run check
3 changes: 0 additions & 3 deletions codes/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
node_modules
out
package.json
package-lock.json
79 changes: 38 additions & 41 deletions codes/typescript/chapter_graph/graph_adjacency_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,50 +91,47 @@ 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) {
Shyam-Chen marked this conversation as resolved.
Show resolved Hide resolved
/* Driver Code */
/* 初始化无向图 */
const v0 = new Vertex(1),
v1 = new Vertex(3),
v2 = new Vertex(2),
v3 = new Vertex(5),
v4 = new Vertex(4);
const edges = [
[v0, v1],
[v1, v2],
[v2, v3],
[v0, v3],
[v2, v4],
[v3, v4],
];
const graph = new GraphAdjList(edges);
console.log('\n初始化后,图为');
graph.print();
/* Driver Code */
/* 初始化无向图 */
const v0 = new Vertex(1),
v1 = new Vertex(3),
v2 = new Vertex(2),
v3 = new Vertex(5),
v4 = new Vertex(4);
const edges = [
[v0, v1],
[v1, v2],
[v2, v3],
[v0, v3],
[v2, v4],
[v3, v4],
];
const graph = new GraphAdjList(edges);
console.log('\n初始化后,图为');
graph.print();

/* 添加边 */
// 顶点 1, 2 即 v0, v2
graph.addEdge(v0, v2);
console.log('\n添加边 1-2 后,图为');
graph.print();
/* 添加边 */
// 顶点 1, 2 即 v0, v2
graph.addEdge(v0, v2);
console.log('\n添加边 1-2 后,图为');
graph.print();

/* 删除边 */
// 顶点 1, 3 即 v0, v1
graph.removeEdge(v0, v1);
console.log('\n删除边 1-3 后,图为');
graph.print();
/* 删除边 */
// 顶点 1, 3 即 v0, v1
graph.removeEdge(v0, v1);
console.log('\n删除边 1-3 后,图为');
graph.print();

/* 添加顶点 */
const v5 = new Vertex(6);
graph.addVertex(v5);
console.log('\n添加顶点 6 后,图为');
graph.print();
/* 添加顶点 */
const v5 = new Vertex(6);
graph.addVertex(v5);
console.log('\n添加顶点 6 后,图为');
graph.print();

/* 删除顶点 */
// 顶点 3 即 v1
graph.removeVertex(v1);
console.log('\n删除顶点 3 后,图为');
graph.print();
}
/* 删除顶点 */
// 顶点 3 即 v1
graph.removeVertex(v1);
console.log('\n删除顶点 3 后,图为');
graph.print();

export { GraphAdjList };
4 changes: 2 additions & 2 deletions codes/typescript/chapter_heap/my_heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class MaxHeap {
}

/* Driver Code */
if (require.main === module) {
Shyam-Chen marked this conversation as resolved.
Show resolved Hide resolved

/* 初始化大顶堆 */
const maxHeap = new MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
console.log('\n输入列表并建堆后');
Expand Down Expand Up @@ -150,6 +150,6 @@ if (require.main === module) {
/* 判断堆是否为空 */
const isEmpty = maxHeap.isEmpty();
console.log(`\n堆是否为空 ${isEmpty}`);
}


export { MaxHeap };
6 changes: 3 additions & 3 deletions codes/typescript/chapter_tree/array_binary_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -148,4 +148,4 @@ console.log('中序遍历为:' + res);
res = abt.postOrder();
console.log('后序遍历为:' + res);

export { };
export {};
5 changes: 1 addition & 4 deletions codes/typescript/modules/PrintUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ 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
console.log(p.str);
}

/* 打印堆 */
Expand Down
120 changes: 120 additions & 0 deletions codes/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions codes/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"private": true,
"type": "module",
"scripts": {
"execute": "tsx",
"check": "tsc"
},
"devDependencies": {
"tsx": "^4.7.2",
"typescript": "^5.4.5"
}
}
57 changes: 57 additions & 0 deletions codes/typescript/test_all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expandGlob, type WalkEntry } from 'jsr:@std/fs';
Shyam-Chen marked this conversation as resolved.
Show resolved Hide resolved
import { relative } from 'jsr:@std/path';

const entries = [] as WalkEntry[];

for await (const entry of expandGlob('codes/typescript/chapter_*/*.ts')) {
entries.push(entry);
}

const processes = [] as {
status: Promise<Deno.CommandStatus>;
stderr: ReadableStream<Uint8Array>;
}[];

for (const file of entries) {
const execute = new Deno.Command('npm', {
args: ['run', 'execute', relative(import.meta.dirname!, file.path)],
cwd: import.meta.dirname,
stdin: 'piped',
stdout: 'piped',
stderr: 'piped',
});

const process = execute.spawn();
processes.push({ status: process.status, stderr: process.stderr });
}

const results = await Promise.all(
processes.map(async (item) => {
const status = await item.status;
return { status, stderr: item.stderr };
})
);

const errors = [] as ReadableStream<Uint8Array>[];

for (const result of results) {
if (!result.status.success) {
errors.push(result.stderr);
}
}

console.log(`Tested ${entries.length} files`);
console.log(`Found exception in ${errors.length} files`);

if (errors.length) {
console.log();

for (const error of errors) {
const reader = error.getReader();
const { value } = await reader.read();
const decoder = new TextDecoder();
console.error(decoder.decode(value));
}

throw new Error('Test failed');
}
Loading