Сборка в один quiz.exe для Windows
- runtime: пути и настройки для упакованного режима - index: конфиг quiz.config.json, автозапуск браузера, main() вместо top-level await - build:win: esbuild + Node SEA + postject, папка dist-win и zip - docs/windows.md и файл настроек-образец
This commit is contained in:
1 parent
7f1d26d819
commit
40c913ac95
11 files changed
+549
-104
No files matched your search
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Где лежит контент: рядом с exe или в репозитории.
|
||||
*
|
||||
* В разработке сервер запускается из `packages/server/src`, а контент лежит в
|
||||
* корне репозитория. В собранном `quiz.exe` рядом с ним стоят папки `content/`
|
||||
* и `web/` — такую папку можно скопировать на другой компьютер целиком.
|
||||
*/
|
||||
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
export interface Runtime {
|
||||
/** true — запущено как собранный exe, false — из репозитория. */
|
||||
packaged: boolean
|
||||
/** Папка, рядом с которой ищем content/, web/ и конфиг. */
|
||||
baseDir: string
|
||||
contentDir: string
|
||||
staticDir: string
|
||||
configPath: string
|
||||
/** Пример таблицы для страницы импорта. */
|
||||
templatePath: string
|
||||
}
|
||||
|
||||
function currentDir(): string {
|
||||
// В CJS-сборке (exe) есть __dirname; в ESM под tsx — import.meta.url.
|
||||
if (typeof __dirname !== 'undefined') return __dirname
|
||||
return path.dirname(fileURLToPath(import.meta.url))
|
||||
}
|
||||
|
||||
/** Собрано ли это в единый исполняемый файл. */
|
||||
export function isPackaged(): boolean {
|
||||
// process.execPath — путь к самому exe, а не к node.
|
||||
// У обычного node рядом нет папки content, у собранного — есть.
|
||||
const exeDir = path.dirname(process.execPath)
|
||||
return existsSync(path.join(exeDir, 'content'))
|
||||
}
|
||||
|
||||
export function resolveRuntime(): Runtime {
|
||||
if (isPackaged()) {
|
||||
const baseDir = path.dirname(process.execPath)
|
||||
return {
|
||||
packaged: true,
|
||||
baseDir,
|
||||
contentDir: process.env.QUIZ_CONTENT ?? path.join(baseDir, 'content'),
|
||||
staticDir: process.env.QUIZ_WEB ?? path.join(baseDir, 'web'),
|
||||
configPath: process.env.QUIZ_CONFIG ?? path.join(baseDir, 'quiz.config.json'),
|
||||
templatePath: path.join(baseDir, 'template.csv'),
|
||||
}
|
||||
}
|
||||
|
||||
const repoRoot = path.resolve(currentDir(), '../../..')
|
||||
return {
|
||||
packaged: false,
|
||||
baseDir: repoRoot,
|
||||
contentDir: process.env.QUIZ_CONTENT ?? path.join(repoRoot, 'content'),
|
||||
staticDir: process.env.QUIZ_WEB ?? path.join(repoRoot, 'packages/web/dist'),
|
||||
configPath: process.env.QUIZ_CONFIG ?? path.join(repoRoot, 'quiz.config.json'),
|
||||
templatePath: path.join(repoRoot, 'examples/template.csv'),
|
||||
}
|
||||
}
|
||||
|
||||
export interface QuizConfig {
|
||||
port?: number
|
||||
room?: string
|
||||
hostToken?: string
|
||||
pack?: string
|
||||
/** Открыть табло в браузере при старте. */
|
||||
openBrowser?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Настройки из `quiz.config.json` рядом с exe.
|
||||
*
|
||||
* Файл необязателен: без него комната и токен случайные, как раньше.
|
||||
* Переменные окружения важнее файла — так удобнее для отладки.
|
||||
*/
|
||||
export function readConfig(file: string): QuizConfig {
|
||||
if (!existsSync(file)) return {}
|
||||
|
||||
try {
|
||||
const raw: unknown = JSON.parse(readFileSync(file, 'utf8'))
|
||||
if (!raw || typeof raw !== 'object') return {}
|
||||
const data = raw as Record<string, unknown>
|
||||
|
||||
const config: QuizConfig = {}
|
||||
if (typeof data.port === 'number' && Number.isInteger(data.port)) config.port = data.port
|
||||
if (typeof data.room === 'string' && data.room.trim()) config.room = data.room.trim()
|
||||
if (typeof data.hostToken === 'string' && data.hostToken.trim()) {
|
||||
config.hostToken = data.hostToken.trim()
|
||||
}
|
||||
if (typeof data.pack === 'string' && data.pack.trim()) config.pack = data.pack.trim()
|
||||
if (typeof data.openBrowser === 'boolean') config.openBrowser = data.openBrowser
|
||||
return config
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`⚠ Не смог прочитать ${file}: ${error instanceof Error ? error.message : error}`,
|
||||
)
|
||||
return {}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user