From 87160ccc5b6d0e4c4a3e0b92a1c0ecd08362264c Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Tue, 17 Sep 2024 16:26:06 +0200 Subject: [PATCH 1/2] Add syntax & type check for Node.js example on CI Signed-off-by: Johannes Marbach --- .github/workflows/static_analysis.yml | 35 +++++++++++++++++++++++++++ examples/node/app.js | 24 +++++++----------- examples/node/package.json | 7 ++++++ examples/node/tsconfig.json | 14 +++++++++++ 4 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 examples/node/tsconfig.json diff --git a/.github/workflows/static_analysis.yml b/.github/workflows/static_analysis.yml index 128626620a9..fdccce38d4d 100644 --- a/.github/workflows/static_analysis.yml +++ b/.github/workflows/static_analysis.yml @@ -53,6 +53,41 @@ jobs: - name: Run Linter run: "yarn run lint:js" + node_example_lint: + name: "Node.js example" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + cache: "yarn" + node-version-file: package.json + + - name: Install Deps + run: "yarn install" + + - name: Build Types + run: "yarn build:types" + + - uses: actions/setup-node@v4 + with: + cache: "npm" + node-version-file: 'examples/node/package.json' + # cache-dependency-path: '**/package-lock.json' + + - name: Install Example Deps + run: "npm install" + working-directory: "examples/node" + + - name: Check Syntax + run: "node --check app.js" + working-directory: "examples/node" + + - name: Typecheck + run: "npx tsc" + working-directory: "examples/node" + workflow_lint: name: "Workflow Lint" runs-on: ubuntu-latest diff --git a/examples/node/app.js b/examples/node/app.js index 8f38c34cdbe..0a16e5a4d2c 100644 --- a/examples/node/app.js +++ b/examples/node/app.js @@ -94,20 +94,14 @@ rl.on("line", function (line) { ); } else if (line.indexOf("/file ") === 0) { var filename = line.split(" ")[1].trim(); - var stream = fs.createReadStream(filename); - matrixClient - .uploadContent({ - stream: stream, - name: filename, - }) - .then(function (url) { - var content = { - msgtype: MsgType.File, - body: filename, - url: JSON.parse(url).content_uri, - }; - matrixClient.sendMessage(viewingRoom.roomId, content); + let buffer = fs.readFileSync("./your_file_name"); + matrixClient.uploadContent(new Blob([buffer])).then(function (response) { + matrixClient.sendMessage(viewingRoom.roomId, { + msgtype: MsgType.File, + body: filename, + url: response.content_uri, }); + }); } else { matrixClient.sendTextMessage(viewingRoom.roomId, line).finally(function () { printMessages(); @@ -167,7 +161,7 @@ matrixClient.on(RoomEvent.Timeline, function (event, room, toStartOfTimeline) { if (toStartOfTimeline) { return; // don't print paginated results } - if (!viewingRoom || viewingRoom.roomId !== room.roomId) { + if (!viewingRoom || viewingRoom.roomId !== room?.roomId) { return; // not viewing a room or viewing the wrong room. } printLine(event); @@ -386,7 +380,7 @@ function print(str, formatter) { } console.log.apply(console.log, newArgs); } else { - console.log.apply(console.log, arguments); + console.log.apply(console.log, [...arguments]); } } diff --git a/examples/node/package.json b/examples/node/package.json index df42d05121b..bcc1367ff97 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -9,5 +9,12 @@ "dependencies": { "cli-color": "^1.0.0", "matrix-js-sdk": "^34.5.0" + }, + "devDependencies": { + "@types/cli-color": "^2.0.6", + "typescript": "^5.6.2" + }, + "engines": { + "node": ">=20.0.0" } } diff --git a/examples/node/tsconfig.json b/examples/node/tsconfig.json new file mode 100644 index 00000000000..78affbfd13d --- /dev/null +++ b/examples/node/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es2022", + "module": "commonjs", + "esModuleInterop": true, + "noImplicitAny": false, + "noEmit": true, + "skipLibCheck": true, + "allowJs": true, + "checkJs": true, + "strict": true + }, + "include": ["app.js"] +} From 6d0d3137c4932995a0e9a83335dbd8dc9e906396 Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Tue, 17 Sep 2024 16:52:22 +0200 Subject: [PATCH 2/2] Fix quotes --- .github/workflows/static_analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/static_analysis.yml b/.github/workflows/static_analysis.yml index fdccce38d4d..f637086d082 100644 --- a/.github/workflows/static_analysis.yml +++ b/.github/workflows/static_analysis.yml @@ -73,7 +73,7 @@ jobs: - uses: actions/setup-node@v4 with: cache: "npm" - node-version-file: 'examples/node/package.json' + node-version-file: "examples/node/package.json" # cache-dependency-path: '**/package-lock.json' - name: Install Example Deps