Skip to content

Commit

Permalink
Replacing ts-node with swc-node/register (#1210)
Browse files Browse the repository at this point in the history
Closes #1170 

It seems that `ts-node` is abandoned by its maintainers, so I'm going to
replace it by `swc-node/register` the same way I did for `merge-sx`.
  • Loading branch information
RobinTail authored Oct 6, 2023
1 parent fc6a718 commit 2c9d245
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 140 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.0.0, 18.x, 20.1.0, 20.x]
node-version: [18.0.0, 18.6.0, 18.x, 20.1.0, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- name: Get yarn cache dir
Expand Down Expand Up @@ -51,6 +51,8 @@ jobs:
- name: Integration test
run: yarn test:int
- name: ESM test
# see https://github.com/RobinTail/express-zod-api/pull/1210#issuecomment-1751489201
if: matrix.node-version != '18.0.0'
run: yarn test:esm
finish:
needs: build
Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tsconfig.build.json
tsconfig.esm.json
example
.husky
jest.config.ts
jest.config.json
tests
.idea
yarn-error.log
Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
Found a vulnerability or other security issue?

Please urgently inform me privately by
[email](https://github.com/RobinTail/express-zod-api/blob/master/package.json#L133).
[email](https://github.com/RobinTail/express-zod-api/blob/master/package.json#L135).

I will try to fix it as soon as possible.
1 change: 0 additions & 1 deletion example/example.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,3 @@ tags:
description: Everything about the files processing
servers:
- url: https://example.com

7 changes: 6 additions & 1 deletion example/generate-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { writeFileSync } from "node:fs";
import { Integration } from "../src";
import { routing } from "./routing";

console.log(new Integration({ routing }).print());
writeFileSync(
"example/example.client.ts",
new Integration({ routing }).print(),
"utf-8",
);
5 changes: 4 additions & 1 deletion example/generate-open-api-schema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { writeFileSync } from "node:fs";
import { Documentation } from "../src";
import { config } from "./config";
import { routing } from "./routing";
import manifest from "../package.json";

console.log(
writeFileSync(
"example/example.swagger.yaml",
new Documentation({
routing,
config,
version: manifest.version,
title: "Example API",
serverUrl: "https://example.com",
}).getSpecAsYaml(),
"utf-8",
);
12 changes: 12 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"transform": {
"^.+\\.ts$": "@swc/jest"
},
"testEnvironment": "node",
"verbose": true,
"forceExit": true,
"collectCoverage": true,
"collectCoverageFrom": ["src/**"],
"coverageReporters": ["json-summary", "text", "html", "lcov"],
"testTimeout": 10000
}
16 changes: 0 additions & 16 deletions jest.config.ts

This file was deleted.

18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
"description": "A Typescript library to help you get an API server up and running with I/O schema validation and custom middlewares in minutes.",
"license": "MIT",
"scripts": {
"start": "ts-node example/index.ts",
"start": "yarn tsnode example/index.ts",
"build": "yarn build:compile && yarn build:tests && yarn build:assets",
"build:compile": "tsup && attw --pack",
"build:tests": "yarn build:int && yarn build:esm",
"build:int": "ts-node tools/integration-test.ts && yarn install --cwd ./tests/integration",
"build:esm": "ts-node tools/esm-test.ts && yarn install --cwd ./tests/esm",
"build:int": "yarn tsnode tools/integration-test.ts && yarn install --cwd ./tests/integration",
"build:esm": "yarn tsnode tools/esm-test.ts && yarn install --cwd ./tests/esm",
"build:assets": "yarn build:swagger && yarn build:client && yarn build:license",
"build:swagger": "ts-node example/generate-open-api-schema.ts > example/example.swagger.yaml",
"build:client": "ts-node example/generate-client.ts > example/example.client.ts && yarn prettier example/example.client.ts --write",
"build:license": "ts-node tools/license.ts > ./LICENSE",
"build:swagger": "yarn tsnode example/generate-open-api-schema.ts",
"build:client": "yarn tsnode example/generate-client.ts && yarn prettier example/example.client.ts --write",
"build:license": "yarn tsnode tools/license.ts",
"build:logo": "yarn tsnode tools/startup-logo.ts",
"test": "yarn test:types && yarn test:jest && yarn test:badge",
"test:int": "jest ./tests/integration",
"test:esm": "jest ./tests/esm",
Expand All @@ -23,6 +24,7 @@
"test:badge": "make-coverage-badge --output-path ./coverage.svg",
"lint": "eslint src example tests tools && yarn prettier *.md --check",
"mdfix": "prettier *.md --write",
"tsnode": "node -r @swc-node/register",
"precommit": "yarn build && yarn lint && yarn test && git add example/example.swagger.yaml example/example.client.ts ./LICENSE ./coverage.svg",
"prepublishOnly": "yarn build && yarn lint && yarn test",
"postversion": "git push && git push --tags",
Expand Down Expand Up @@ -81,7 +83,8 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.11.0",
"@swc/core": "^1.3.82",
"@swc-node/register": "^1.6.8",
"@swc/core": "^1.3.92",
"@swc/jest": "^0.2.29",
"@tsconfig/node18": "^18.2.1",
"@types/cors": "^2.8.14",
Expand Down Expand Up @@ -112,7 +115,6 @@
"mockdate": "^3.0.5",
"prettier": "3.0.3",
"strip-ansi": "^6.0.1",
"ts-node": "^10.9.1",
"tsd": "^0.29.0",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import jestConfig from "../jest.config";
import jestConfig from "../jest.config.json";
import { z } from "zod";
import { SchemaHandler, walkSchema } from "../src/schema-walker";

Expand Down
2 changes: 1 addition & 1 deletion tests/system/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Example", () => {
};

beforeAll(() => {
example = spawn("ts-node", ["example/index.ts"]);
example = spawn("node", ["-r", "@swc-node/register", "example/index.ts"]);
example.stdout.on("data", listener);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/system/example.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Example", () => {
};

beforeAll(() => {
example = spawn("ts-node", ["example/index.ts"]);
example = spawn("node", ["-r", "@swc-node/register", "example/index.ts"]);
example.stdout.on("data", listener);
});

Expand Down
11 changes: 5 additions & 6 deletions tools/esm-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import { getTSConfigBase } from "./tsconfig-base";

const tsconfigBase = getTSConfigBase();

// @todo revert "start" to "ts-node-esm quick-start.ts" when the ts-node issue fixed
// @link https://github.com/TypeStrong/ts-node/issues/1997
const packageJson = `
{
"name": "express-zod-api-esm-test",
"version": "1.0.0",
"scripts": {
"launch": "node --no-warnings=ExperimentalWarning --loader ts-node/esm",
"postinstall": "yarn launch fix-package.ts",
"start": "yarn launch quick-start.ts"
"tsnode": "node --loader @swc-node/register/esm",
"postinstall": "yarn tsnode fix-package.ts",
"start": "yarn tsnode quick-start.ts"
},
"type": "module",
"dependencies": {
"@tsconfig/node${tsconfigBase}": "latest",
"express-zod-api": "../../dist",
"ts-node": "10.9.1",
"@swc/core": "1.3.92",
"@swc-node/register": "1.6.8",
"typescript": "5.2.2",
"@types/node": "*"
}
Expand Down
8 changes: 5 additions & 3 deletions tools/integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ const packageJson = `
"name": "express-zod-api-integration-test",
"version": "1.0.0",
"scripts": {
"postinstall": "ts-node fix-package.ts",
"start": "ts-node quick-start.ts"
"tsnode": "node -r @swc-node/register",
"postinstall": "yarn tsnode fix-package.ts",
"start": "yarn tsnode quick-start.ts"
},
"dependencies": {
"@tsconfig/node${tsconfigBase}": "latest",
"express-zod-api": "../../dist",
"ts-node": "10.9.1",
"@swc/core": "1.3.92",
"@swc-node/register": "1.6.8",
"typescript": "5.2.2",
"@types/node": "*"
}
Expand Down
3 changes: 2 additions & 1 deletion tools/license.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { writeFileSync } from "node:fs";
import manifest from "../package.json";

const ownLicense = `
Expand All @@ -24,4 +25,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`;

console.log(ownLicense.trim());
writeFileSync("LICENSE", ownLicense.trimStart(), "utf-8");
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
"strictNullChecks": true,
"resolveJsonModule": true
},
"exclude": ["tests/integration", "tests/esm"],
"ts-node": {
"swc": false
}
"exclude": ["tests/integration", "tests/esm"]
}
Loading

0 comments on commit 2c9d245

Please sign in to comment.