Skip to content

Commit

Permalink
chore: remove ts-node (#418)
Browse files Browse the repository at this point in the history
> Remove ts-node dependency and do DB initialization via npm scripts
hook and bash script.

* Added prepare-database.sh
* Remove swc & ts-node, since swc `useDefineForClassFields` will cause
leoric create error
[ref](https://github.com/TypeStrong/ts-node/blob/fd438213ad3b3e1bcabea48627255b8be74c1c24/src/transpilers/swc.ts#L229),
which can't customize
--------------
> 删除 ts-node 依赖,通过 npm scripts hook 和 bash 来进行 DB 初始化工作

* 新增 prepare-database.sh 处理 db 初始化工作
* 删除 ts-node 及 swc,swc 新版开启 `useDefineForClassFields` 会导致 leoric
创建对象失败,且无法自定义,
[ref](https://github.com/TypeStrong/ts-node/blob/fd438213ad3b3e1bcabea48627255b8be74c1c24/src/transpilers/swc.ts#L229)一并去除
  • Loading branch information
elrrrrrrr authored Mar 16, 2023
1 parent 7ec53b1 commit 5877f71
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ npm install

```bash
# 初始化数据库
MYSQL_DATABASE=cnpmcore npm run prepare-database
MYSQL_DATABASE=cnpmcore bash ./prepare-database.sh

# 启动 Web 服务
npm run dev
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
"lint": "eslint --cache --ext .ts .",
"lint:fix": "eslint --cache --ext .ts --fix .",
"test": "npm run lint:fix && npm run test-local",
"prepare-database": "rm -rf dist && ts-node test/prepare.ts",
"test-local": "npm run prepare-database && egg-bin test",
"t": "npm run lint:fix && npm run prepare-database && egg-bin test --changed",
"cov": "npm run prepare-database && egg-bin cov",
"pretest-local": "bash ./prepare-database.sh",
"test-local": "egg-bin test",
"pret": "bash ./prepare-database.sh",
"t": "npm run lint:fix && egg-bin test --changed",
"precov": "bash ./prepare-database.sh",
"cov": "egg-bin cov",
"ci": "npm run lint && npm run cov && npm run tsc:prod",
"clean": "tsc -b --clean && rm -rf dist",
"tsc": "npm run clean && tsc -p ./tsconfig.json",
Expand Down Expand Up @@ -100,7 +102,6 @@
"validate-npm-package-name": "^3.0.0"
},
"devDependencies": {
"@swc/core": "^1.3.35",
"@types/mocha": "^10.0.1",
"@types/mysql": "^2.15.21",
"@types/semver": "^7.3.12",
Expand All @@ -111,7 +112,6 @@
"eslint-config-egg": "^12.1.0",
"git-contributor": "2",
"@cnpmjs/npm-cli-login": "^1.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"author": "killagu",
Expand Down
37 changes: 37 additions & 0 deletions prepare-database.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# read variables from environment
db_host=${MYSQL_HOST:-127.0.0.1}
db_port=${MYSQL_PORT:-3306}
db_username=${MYSQL_USER:-root}
db_password=${MYSQL_PASSWORD:-} # default to empty password
db_name=${MYSQL_DATABASE:-cnpmcore_unittest}

# preapre mysql param
param="-h $db_host -P $db_port -u $db_username"
if [ -n "$db_password" ]; then
param="$param -p$db_password"
fi

if [ "$CI" ]; then
echo "⛷️ Skipping database creation in CI environment."
else
# reset database
echo "️😈 Reset database in local"
mysql $param -e "DROP DATABASE IF EXISTS $db_name"
mysql $param -e "CREATE DATABASE $db_name CHARACTER SET utf8"
fi


# find all sql files and sort
sql_files=$(ls sql/*.sql | sort)
echo "🤖 Running the following SQL files:"

# execute sql files
for file in $sql_files; do
echo "🔖 Running $file..."
mysql $param $db_name < "$file"
done

echo "🎉 prepare database done"
mysql $param -e "USE $db_name; SHOW TABLES;"
15 changes: 0 additions & 15 deletions test/TestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,6 @@ export class TestUtil {
}
}

static async createDatabase() {
// TODO use leoric sync
const database = this.getDatabase();
const sqls = await this.getTableSqls();
// no need to create database on GitHub Action CI env
if (!process.env.CI) {
await this.query(`DROP DATABASE IF EXISTS ${database};`);
await this.query(`CREATE DATABASE IF NOT EXISTS ${database} CHARACTER SET utf8;`);
console.log('[TestUtil] CREATE DATABASE: %s', database);
}
await this.query(`USE ${database};`);
await this.query(sqls);
this.destroyConnection();
}

static async getTableNames() {
if (!this.tables) {
const database = this.getDatabase();
Expand Down
8 changes: 0 additions & 8 deletions test/prepare.ts

This file was deleted.

3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@
"exclude": [
"node_modules"
],
"ts-node": {
"swc": true
}
}

0 comments on commit 5877f71

Please sign in to comment.