Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ release

public/laravel

.env
.env

tweakphp.db
tweakphp.db-wal
tweakphp.db-shm
2 changes: 1 addition & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const options = {
platform: 'node',
bundle: true,
target: 'node20',
external: ['electron'],
external: ['electron', 'better-sqlite3'],
define: {
'process.env.NODE_ENV': `"${process.argv[2] === '--dev' ? 'development' : 'production'}"`,
'process.platform': `"${process.platform}"`,
Expand Down
16 changes: 16 additions & 0 deletions migrations/001_create_code_histories_and_tab_states_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS code_histories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tab_id INTEGER NOT NULL,
code TEXT NOT NULL,
cursor_line INTEGER NOT NULL,
cursor_column INTEGER NOT NULL,
created_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS tab_states (
tab_id INTEGER PRIMARY KEY,
current_history_id INTEGER,
FOREIGN KEY (current_history_id) REFERENCES code_histories(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_code_histories_tab_id ON code_histories(tab_id);
29 changes: 19 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"version": "0.10.0",
"main": "dist/main.js",
"scripts": {
"postinstall": "electron-builder install-app-deps",
"dev": "concurrently \"vite\" \"node build.js --dev && electron .\"",
"update-clients": "node ./scripts/update-clients.js",
"build": "vite build && node build.js --prod",
Expand All @@ -26,16 +27,19 @@
"appId": "com.tweakphp.app",
"productName": "TweakPHP",
"files": [
"**/*",
"node_modules/**/*"
"**/*"
],
"extraResources": [
"build/*",
"public/*"
],
"extraFiles": [
"build/**/*",
"public/**/*"
"public/**/*",
{
"from": "migrations",
"to": "migrations"
}
],
"publish": {
"provider": "github",
Expand All @@ -49,7 +53,7 @@
"linux": {
"executableName": "tweakphp",
"description": "Tweak your PHP code easily",
"icon": "./build/icon.icns",
"icon": "./build/icon.png",
"target": [
{
"target": "AppImage",
Expand All @@ -63,7 +67,7 @@
],
"desktop": {
"Name": "TweakPHP",
"icon": "./build/icon.icns",
"icon": "./build/icon.png",
"Comment": "Tweak your PHP code easily",
"Categories": "Development"
},
Expand Down Expand Up @@ -103,6 +107,11 @@
},
"afterSign": "scripts/notarize.js"
},
"dependencies": {
"better-sqlite3": "^12.1.1",
"js-yaml": "^4.1.0",
"reka-ui": "^2.4.1"
},
"devDependencies": {
"@codingame/esbuild-import-meta-url-plugin": "~1.0.2",
"@codingame/monaco-vscode-configuration-service-override": "~7.0.7",
Expand All @@ -115,9 +124,11 @@
"@codingame/monaco-vscode-textmate-service-override": "~7.0.7",
"@codingame/monaco-vscode-theme-defaults-default-extension": "~7.0.7",
"@codingame/monaco-vscode-theme-service-override": "~7.0.7",
"@electron/rebuild": "^4.0.1",
"@headlessui/vue": "^1.7.23",
"@heroicons/vue": "^2.1.3",
"@types/adm-zip": "^0.5.7",
"@types/better-sqlite3": "^7.6.13",
"@types/express": "^5.0.0",
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.10.2",
Expand All @@ -127,6 +138,7 @@
"@types/ssh2": "^1.15.1",
"@types/ws": "^8.5.13",
"@vitejs/plugin-vue": "^5.2.1",
"@vueuse/core": "^13.4.0",
"adm-zip": "^0.5.16",
"autoprefixer": "^10.4.19",
"bufferutil": "^4.0.8",
Expand Down Expand Up @@ -166,10 +178,7 @@
"vue-router": "^4.3.0",
"vue-tippy": "^6.4.4",
"vue-tsc": "^2.0.26",
"ws": "^8.17.0"
},
"dependencies": {
"js-yaml": "^4.1.0",
"reka-ui": "^2.4.1"
"ws": "^8.17.0",
"zod": "^3.25.67"
}
}
19 changes: 19 additions & 0 deletions src/main/db/db_manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path from 'path'
import os from 'os'
import fs from 'fs'
import Database from 'better-sqlite3'

const appDataDir = path.join(os.homedir(), '.tweakphp')

if (!fs.existsSync(appDataDir)) {
fs.mkdirSync(appDataDir, { recursive: true })
}

const dbPath = path.join(appDataDir, 'tweakphp.db')

console.log(`DB PATH: ${dbPath}`)

const db = new Database(dbPath)
db.pragma('journal_mode = WAL')

export { db }
50 changes: 50 additions & 0 deletions src/main/db/migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { db } from './db_manager'
import fs from 'fs'
import path from 'path'
import { app } from 'electron'

db.prepare(
`
CREATE TABLE IF NOT EXISTS migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
run_on DATETIME DEFAULT CURRENT_TIMESTAMP
)
`
).run()

function getAppliedMigrations(): string[] {
const rows = db.prepare(`SELECT name FROM migrations ORDER BY id`).all()
return rows.map((row: any) => row.name)
}

function applyMigration(filePath: string, name: string) {
const sql = fs.readFileSync(filePath, 'utf-8')
const transaction = db.transaction(() => {
db.exec(sql)
db.prepare(`INSERT INTO migrations (name) VALUES (?)`).run(name)
})
transaction()
}

export function runMigrations() {
const migrationsDir = path.join(app.getAppPath(), 'migrations')
const migrationFiles = fs
.readdirSync(migrationsDir)
.filter(f => f.endsWith('.sql'))
.sort()
const appliedMigrations = getAppliedMigrations()

try {
for (const file of migrationFiles) {
if (!appliedMigrations.includes(file)) {
applyMigration(path.join(migrationsDir, file), file)
console.log(`Applied migration: ${file}`)
}
}
} catch (error) {
throw new Error(`Migration failed: ${error}`)
}

console.log('All migrations applied.')
}
7 changes: 7 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import * as updater from './system/updater.ts'
import * as link from './system/link.ts'
import * as tray from './system/tray.ts'

import { runMigrations } from './db/migration.ts'
import { initCodeHistory } from './tools/code-history.ts'

import url from 'url'

import { fixPath } from './utils/fix-path.ts'
import { isWindows } from './system/platform.ts'

runMigrations()

fixPath()

Object.assign(console, log.functions)
Expand Down Expand Up @@ -135,3 +140,5 @@ ipcMain.on('lsp.restart', async event => {
event.sender.send('lsp.restart.error', error?.message)
}
})

initCodeHistory()
Loading
Loading