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 12 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
7 changes: 4 additions & 3 deletions codes/typescript/chapter_graph/graph_adjacency_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Author: Justin (xiefahit@gmail.com)
*/

import esMain from 'es-main';

import { Vertex } from '../modules/Vertex';

/* 基于邻接表实现的无向图类 */
Expand Down Expand Up @@ -91,9 +93,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) {
Shyam-Chen marked this conversation as resolved.
Show resolved Hide resolved
/* Driver Code */
/* Driver Code */
if (esMain(import.meta)) {
/* 初始化无向图 */
const v0 = new Vertex(1),
v1 = new Vertex(3),
Expand Down
4 changes: 3 additions & 1 deletion codes/typescript/chapter_heap/my_heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Author: Justin (xiefahit@gmail.com)
*/

import esMain from 'es-main';
Shyam-Chen marked this conversation as resolved.
Show resolved Hide resolved

import { printHeap } from '../modules/PrintUtil';

/* 最大堆类 */
Expand Down Expand Up @@ -122,7 +124,7 @@ class MaxHeap {
}

/* Driver Code */
if (require.main === module) {
Shyam-Chen marked this conversation as resolved.
Show resolved Hide resolved
if (esMain(import.meta)) {
/* 初始化大顶堆 */
const maxHeap = new MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
console.log('\n输入列表并建堆后');
Expand Down
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 {};
3 changes: 0 additions & 3 deletions codes/typescript/modules/PrintUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/* 打印堆 */
Expand Down
144 changes: 144 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.

16 changes: 16 additions & 0 deletions codes/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"private": true,
"type": "module",
"scripts": {
"execute": "tsx",
"check": "tsc"
},
"dependencies": {
"es-main": "^1.3.0"
},
"devDependencies": {
"@types/node": "^20.12.7",
"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');
}
33 changes: 28 additions & 5 deletions codes/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
{
"compilerOptions": {
Shyam-Chen marked this conversation as resolved.
Show resolved Hide resolved
"target": "ES6",
"module": "CommonJS",
"outDir": "out",
"sourceMap": true
}
// The original author did not use strict mode.
// "noUnusedLocals": true,
// "strict": true,
// "strictNullChecks": true,

"baseUrl": ".",
"module": "esnext",
"moduleResolution": "node",
"paths": {},
"resolveJsonModule": true,
"types": ["@types/node"],

"noEmit": true,

"allowJs": true,

"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,

"jsx": "preserve",
"lib": ["ESNext", "DOM"],
"target": "esnext",

"skipLibCheck": true
},
"include": ["chapter_*/*.ts"],
"exclude": ["node_modules"]
}