Skip to content

Commit 6abdc9d

Browse files
committed
Migrate to bun
Drop Sequelize Drop shelljs
1 parent 84b6842 commit 6abdc9d

8 files changed

Lines changed: 836 additions & 3434 deletions

File tree

.github/workflows/tests.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,11 @@ jobs:
1313
build:
1414
runs-on: ubuntu-latest
1515

16-
strategy:
17-
matrix:
18-
node-version: [18.x, 20.x, 22.x]
19-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
20-
2116
steps:
2217
- uses: actions/checkout@v4
23-
- name: Use Node.js ${{ matrix.node-version }}
24-
uses: actions/setup-node@v4
25-
with:
26-
node-version: ${{ matrix.node-version }}
27-
cache: 'npm'
28-
- run: npm install
18+
- name: Use Latest Bun Bundle
19+
uses: oven-sh/setup-bun@v2
20+
- run: bun install
2921
- run: npx vitest --project unit --coverage.enabled true --watch=false
3022
- name: 'Report Coverage'
3123
uses: davelosert/vitest-coverage-report-action@v2

bun.lockb

164 KB
Binary file not shown.

package-lock.json

Lines changed: 779 additions & 3389 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"wp-tool": "dist/bin/app.js"
99
},
1010
"scripts": {
11-
"compile:ts": "tsc -p .",
12-
"compile:alias": "tsc-alias -p tsconfig.json",
13-
"build": "run-s compile:*",
11+
"build": "bun build ./src/bin/app.ts --compile --outfile wp-tool",
1412
"test": "npm run test:unit & npm run test:e2e",
1513
"test:unit": "vitest --project unit",
1614
"test:unit:coverage": "vitest run --project unit --coverage",
@@ -29,6 +27,7 @@
2927
"commander": "^11.0.0",
3028
"crypto-random-string": "^5.0.0",
3129
"date-fns": "^2.30.0",
30+
"execa": "^9.5.2",
3231
"extract-zip": "^2.0.1",
3332
"ini": "^5.0.0",
3433
"inquirer": "^9.2.10",
@@ -37,8 +36,6 @@
3736
"php-parser": "^3.2.2",
3837
"psl": "^1.9.0",
3938
"rimraf": "^5.0.1",
40-
"sequelize": "^6.32.1",
41-
"shelljs": "^0.8.5",
4239
"tmp": "^0.2.1"
4340
},
4441
"devDependencies": {
@@ -51,9 +48,6 @@
5148
"@types/shelljs": "^0.8.12",
5249
"@types/tmp": "^0.2.3",
5350
"@vitest/coverage-v8": "^2.1.8",
54-
"npm-run-all": "^4.1.5",
55-
"ts-node": "^10.9.1",
56-
"tsc-alias": "^1.8.7",
5751
"typescript": "^5.7.3",
5852
"vitest": "^2.1.8",
5953
"vitest-mock-extended": "^2.0.2"

src/bin/app.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ program
5151
throw new Error('Using localhost or 127.0.0.1 must be installed MySQL database on host.');
5252
}
5353
}
54-
const accessible = await new DatabaseService({ host, port, username, password }).isAccessible();
54+
const accessible = await new DatabaseService({
55+
host,
56+
port,
57+
user: username,
58+
password,
59+
}).isAccessible();
5560
if (accessible) {
5661
return;
5762
}
@@ -87,7 +92,12 @@ program
8792
) {
8893
throw new Error('Using localhost or 127.0.0.1 must be installed MySQL database on host.');
8994
}
90-
const accessible = await new DatabaseService({ host, port, username, password }).isAccessible();
95+
const accessible = await new DatabaseService({
96+
host,
97+
port,
98+
user: username,
99+
password,
100+
}).isAccessible();
91101
if (accessible) {
92102
return;
93103
}

src/service/database.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1-
import { Options, QueryTypes, Sequelize } from 'sequelize';
1+
import mysql from 'mysql2/promise';
22

33
export default class DatabaseService {
4-
sequelize: Sequelize;
5-
constructor(options?: Options) {
6-
this.sequelize = new Sequelize(options);
4+
options?: mysql.ConnectionOptions;
5+
constructor(options?: mysql.ConnectionOptions) {
6+
this.options = options;
7+
}
8+
private async createConnection() {
9+
return await mysql.createConnection(
10+
this.options || { host: 'localhost', port: 3306, user: 'root', password: '' }
11+
);
712
}
813
async isAccessible() {
914
try {
10-
await this.sequelize.authenticate();
15+
const connection = await this.createConnection();
16+
connection.destroy();
1117
return true;
1218
} catch (e) {
1319
console.error('Failed To Connect MySQL Server.', { cause: e });
1420
return false;
15-
} finally {
16-
await this.sequelize.close();
1721
}
1822
}
1923
async dropUser(identity: string) {
20-
await this.sequelize.query(`DROP USER IF EXISTS ${identity};`, { type: QueryTypes.RAW });
24+
const connection = await this.createConnection();
25+
await connection.execute(`DROP USER IF EXISTS ${identity};`);
26+
connection.destroy();
2127
}
2228
async createDatabase(database: string) {
23-
await this.sequelize.query(`CREATE DATABASE IF NOT EXISTS ${database};`);
29+
const connection = await this.createConnection();
30+
await connection.execute(`CREATE DATABASE IF NOT EXISTS ${database};`);
31+
connection.destroy();
2432
}
2533
async createUserAndGrantPermission(identity: string, password: string, database: string) {
26-
await this.sequelize.query(`
34+
const connection = await this.createConnection();
35+
await connection.execute(`
2736
CREATE USER '${identity}'@'%' IDENTIFIED WITH mysql_native_password BY '${password}';
2837
GRANT ALL PRIVILEGES ON ${database}.* TO '${identity}'@'%';
2938
FLUSH PRIVILEGES;`);
39+
connection.destroy();
3040
}
3141
}

src/service/installation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default class InstallationService {
8686
// finish directory with permission
8787
setWpInstalledPathPermission(wpDir, identity);
8888

89-
const databaseService = new DatabaseService({ host, port, username, password });
89+
const databaseService = new DatabaseService({ host, port, user: username, password });
9090

9191
if (wpConfigDbUser !== undefined) {
9292
await databaseService.dropUser(wpConfigDbUser);

src/util/unix.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
1-
import shell from 'shelljs';
1+
import { execaCommandSync } from 'execa';
22

33
import { getPHPErrorLogPath } from './path';
44

55
import PHPVersionNotFoundError from '@/error/PHPVersionNotFoundError';
66
import RestartServiceError from '@/error/RestartServiceError';
77

88
export function isPHPInstalled() {
9-
return shell.which('php');
9+
return execaCommandSync('which php', { reject: false }).exitCode === 0;
1010
}
1111

1212
export function isMySQLInstalled() {
13-
return shell.which('mysqld');
13+
return execaCommandSync('which mysqld', { reject: false }).exitCode === 0;
1414
}
1515

1616
export function restartNginxService() {
17-
if (shell.exec(`systemctl restart nginx`).code !== 0) {
17+
if (execaCommandSync('systemctl restart nginx', { reject: false }).exitCode !== 0) {
1818
throw new RestartServiceError('nginx');
1919
}
2020
}
2121

2222
export function restartPHPFPMService(phpVer: string) {
23-
if (shell.exec(`systemctl restart php${phpVer}-fpm`).code !== 0) {
23+
if (execaCommandSync(`systemctl restart php${phpVer}-fpm`, { reject: false }).exitCode !== 0) {
2424
throw new RestartServiceError('php-fpm');
2525
}
2626
}
2727

2828
export function createPHPErrorLogFile(phpVer: string, identity: string) {
2929
const path = getPHPErrorLogPath(phpVer, identity);
30-
shell.touch(path);
31-
shell.exec(`chown ${identity}:${identity} ${path}`);
30+
execaCommandSync(`touch ${path}`);
31+
execaCommandSync(`chown ${identity}:${identity} ${path}`);
3232
}
3333

3434
export function setWpInstalledPathPermission(wpInstalledPath: string, identity: string) {
35-
shell.exec(`chown ${identity}:${identity} -R ${wpInstalledPath}`);
36-
shell.exec(`find ${wpInstalledPath} -type d -exec chmod 755 {} \;`);
37-
shell.exec(`find ${wpInstalledPath} -type f -exec chmod 644 {} \;`);
35+
execaCommandSync(`chown ${identity}:${identity} -R ${wpInstalledPath}`);
36+
execaCommandSync(`find ${wpInstalledPath} -type d -exec chmod 755 {} \;`);
37+
execaCommandSync(`find ${wpInstalledPath} -type f -exec chmod 644 {} \;`);
3838
}
3939

4040
export function createUnixUser(identity: string) {
41-
if (shell.grep(identity, '/etc/passwd').code === 0) {
41+
if (execaCommandSync(`grep ${identity} /etc/passwd`, { reject: false }).exitCode === 0) {
4242
return;
4343
}
4444
if (
45-
shell.exec(`adduser --system --no-create-home --group --disabled-login ${identity}`).code !== 0
45+
execaCommandSync(`adduser --system --no-create-home --group --disabled-login ${identity}`, {
46+
reject: false,
47+
}).exitCode !== 0
4648
) {
4749
throw new Error(`Failed to create the user named ${identity}`);
4850
}
4951
}
5052

53+
export function getPHPVersionOutput() {
54+
return execaCommandSync('php -v', { reject: false }).stdout || '';
55+
}
56+
5157
export function getCurrentPHPVersion() {
52-
const matches = shell.exec('php -v').stdout.match(/^PHP ([0-9]\.[0-9])/);
58+
const matches = getPHPVersionOutput().match(/^PHP ([0-9]\.[0-9])/);
5359
if (matches === null) {
5460
throw new PHPVersionNotFoundError('Cannot find php version using `php -v`');
5561
}

0 commit comments

Comments
 (0)