chore: migrate workspace packages to ESM

- Add "type": "module" to all workspace package.json files
- Replace __dirname with import.meta.url-based resolveCliPath() helper
- Convert stream-json/stream-chain CJS named imports to default + destructure
  for ESM compatibility
- Use createRequire() for @node-rs/jieba native module loading in ESM context

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
diguaandCursor authored and digua committed 2026-05-26 23:19:47 +08:00
1 parent 83151fd8e7
commit 0ba9ae04a6
22 files changed
+92 -40

No files matched your search

+4 -3
View File
@@ -24,6 +24,7 @@ import {
executeReadonlySql,
} from '@openchatlab/core'
import { getVersion } from './version'
import { resolveCliPath } from './paths'
const program = new Command()
@@ -291,7 +292,7 @@ program
let webRoot: string | undefined
if (options.web !== undefined) {
const webDir = typeof options.web === 'string' ? options.web : path.resolve(__dirname, '../dist-web')
const webDir = typeof options.web === 'string' ? options.web : resolveCliPath('dist-web')
if (fs.existsSync(webDir)) {
webRoot = path.resolve(webDir)
} else {
@@ -349,7 +350,7 @@ program
.action(async (options) => {
const { startHttpServer } = await import('./http')
const port = parseInt(options.port, 10)
const webDir = path.resolve(__dirname, '../dist-web')
const webDir = resolveCliPath('dist-web')
let webRoot: string | undefined
if (fs.existsSync(webDir)) {
@@ -434,7 +435,7 @@ function openBrowser(url: string): void {
*/
function resolveNativeBinding(): string | undefined {
if (process.versions.electron) return undefined
const nativePath = path.resolve(__dirname, '../native/better_sqlite3.node')
const nativePath = resolveCliPath('native/better_sqlite3.node')
if (fs.existsSync(nativePath)) return nativePath
return undefined
}
+2 -2
View File
@@ -6,7 +6,6 @@
*/
import * as fs from 'fs'
import * as path from 'path'
import * as crypto from 'crypto'
import type { FastifyInstance } from 'fastify'
import { loadConfig, writeConfigField, MigrationRunner, ALL_MIGRATIONS } from '@openchatlab/config'
@@ -28,6 +27,7 @@ import { registerAiRoutes } from './routes/ai'
import { registerPreferencesRoutes } from './routes/preferences'
import { initServerAiLogger, closeServerAiLogger } from '../ai/logger'
import { initSync, cleanupSync } from '../sync'
import { resolveCliPath } from '../paths'
let server: FastifyInstance | null = null
let dbManager: DatabaseManager | null = null
@@ -43,7 +43,7 @@ export interface HttpServerOptions {
function resolveNativeBinding(): string | undefined {
if (process.versions.electron) return undefined
const nativePath = path.resolve(__dirname, '../../native/better_sqlite3.node')
const nativePath = resolveCliPath('native/better_sqlite3.node')
if (fs.existsSync(nativePath)) return nativePath
return undefined
}
+2 -2
View File
@@ -3,9 +3,9 @@
*/
import * as fs from 'fs'
import * as path from 'path'
import type { TimeFilter } from '@openchatlab/shared-types'
import type { DatabaseManager } from '@openchatlab/node-runtime'
import { resolveCliPath } from '../../../paths'
export function parseTimeFilter(query: Record<string, string | undefined>): TimeFilter | undefined {
const { startTs, endTs, memberId } = query
@@ -19,7 +19,7 @@ export function parseTimeFilter(query: Record<string, string | undefined>): Time
export function resolveNativeBinding(): string | undefined {
if (process.versions.electron) return undefined
const nativePath = path.resolve(__dirname, '../../../../native/better_sqlite3.node')
const nativePath = resolveCliPath('native/better_sqlite3.node')
if (fs.existsSync(nativePath)) return nativePath
return undefined
}
+2 -2
View File
@@ -5,7 +5,6 @@
*/
import fs from 'fs'
import path from 'path'
import { loadConfig } from '@openchatlab/config'
import {
NodePathProvider,
@@ -15,10 +14,11 @@ import {
} from '@openchatlab/node-runtime'
import { startMcpServer } from 'chatlab-mcp'
import { getVersion } from './version'
import { resolveCliPath } from './paths'
function resolveNativeBinding(): string | undefined {
if (process.versions.electron) return undefined
const nativePath = path.resolve(__dirname, '../native/better_sqlite3.node')
const nativePath = resolveCliPath('native/better_sqlite3.node')
if (fs.existsSync(nativePath)) return nativePath
return undefined
}
+8
View File
@@ -0,0 +1,8 @@
import path from 'path'
import { fileURLToPath } from 'url'
export function resolveCliPath(...segments: string[]): string {
const moduleDir = path.dirname(fileURLToPath(import.meta.url))
const packageRoot = path.basename(moduleDir) === 'dist' ? path.dirname(moduleDir) : path.resolve(moduleDir, '..')
return path.resolve(packageRoot, ...segments)
}
+2 -1
View File
@@ -17,6 +17,7 @@ import type { DatabaseManager } from '@openchatlab/node-runtime'
import { openBetterSqliteDatabase } from '@openchatlab/node-runtime'
import { parseFile } from '../import/chatlab-reader'
import { importData } from '../import/importer'
import { resolveCliPath } from '../paths'
function getTempFilePath(ext: string): string {
const id = crypto.randomBytes(8).toString('hex')
@@ -52,7 +53,7 @@ export class NodeFetcher implements HttpFetcher {
function resolveNativeBinding(): string | undefined {
if (process.versions.electron) return undefined
const nativePath = path.resolve(__dirname, '../../native/better_sqlite3.node')
const nativePath = resolveCliPath('native/better_sqlite3.node')
if (fs.existsSync(nativePath)) return nativePath
return undefined
}
+2 -2
View File
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { resolveCliPath } from './paths'
let cached: string | undefined
@@ -10,7 +10,7 @@ let cached: string | undefined
export function getVersion(): string {
if (cached) return cached
try {
const pkgPath = resolve(__dirname, '../package.json')
const pkgPath = resolveCliPath('package.json')
cached = JSON.parse(readFileSync(pkgPath, 'utf-8')).version
} catch {
cached = '0.0.0-dev'