forked from WhiteMinds/LiveAutoRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f66318
commit 753145a
Showing
28 changed files
with
507 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import envPaths from 'env-paths' | ||
|
||
export const appName = process.env.AppName ?? 'live-auto-record' | ||
|
||
export const paths = envPaths(appName, { suffix: '' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import path from 'path' | ||
import { paths } from './env' | ||
import { readJSONFileSync, writeJSONFileSync } from './utils' | ||
|
||
export interface Settings { | ||
notExitOnAllWindowsClosed: boolean | ||
noticeOnRecordStart: boolean | ||
} | ||
|
||
const settingsConfigPath = path.join(paths.config, 'settings.json') | ||
|
||
const settings = readJSONFileSync<Settings>(settingsConfigPath, { | ||
notExitOnAllWindowsClosed: true, | ||
noticeOnRecordStart: true, | ||
}) | ||
|
||
export function getSettings(): Settings { | ||
return settings | ||
} | ||
|
||
export function setSettings(newSettings: Settings): Settings { | ||
writeJSONFileSync(settingsConfigPath, newSettings) | ||
return newSettings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
export async function readJSONFile<T = unknown>( | ||
filePath: string, | ||
defaultValue: T | ||
): Promise<T> { | ||
if (!fs.existsSync(filePath)) return defaultValue | ||
|
||
const buffer = await fs.promises.readFile(filePath) | ||
return JSON.parse(buffer.toString('utf8')) as T | ||
} | ||
|
||
export function readJSONFileSync<T = unknown>( | ||
filePath: string, | ||
defaultValue: T | ||
): T { | ||
if (!fs.existsSync(filePath)) return defaultValue | ||
|
||
const buffer = fs.readFileSync(filePath) | ||
return JSON.parse(buffer.toString('utf8')) as T | ||
} | ||
|
||
export async function writeJSONFile<T = unknown>( | ||
filePath: string, | ||
json: T | ||
): Promise<void> { | ||
fs.mkdirSync(path.dirname(filePath), { recursive: true }) | ||
await fs.promises.writeFile(filePath, JSON.stringify(json)) | ||
} | ||
|
||
export function writeJSONFileSync<T = unknown>( | ||
filePath: string, | ||
json: T | ||
): void { | ||
fs.mkdirSync(path.dirname(filePath), { recursive: true }) | ||
fs.writeFileSync(filePath, JSON.stringify(json)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import envPaths from 'env-paths' | ||
|
||
// TODO: 后面得改改 | ||
// TODO: 后面得改改,或许应该在 startServer 时注入 | ||
export const appName = process.env.AppName ?? 'live-auto-record' | ||
|
||
export const paths = envPaths(appName, { suffix: '' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Router } from 'express' | ||
import { Settings } from '@autorecord/shared' | ||
import { API } from './api_types' | ||
import { ServerOpts } from '../types' | ||
import { scheduleBroadcastMessage } from './event' | ||
|
||
export function createRouter(serverOpts: ServerOpts) { | ||
const router = Router() | ||
|
||
async function getSettings( | ||
args: API.getSettings.Args | ||
): Promise<API.getSettings.Resp> { | ||
return serverOpts.getSettings() | ||
} | ||
|
||
async function setSettings( | ||
args: API.setSettings.Args | ||
): Promise<API.setSettings.Resp> { | ||
const newSettings: Settings = args | ||
return serverOpts.setSettings(newSettings) | ||
} | ||
|
||
router | ||
.route('/settings') | ||
.get(async (req, res) => { | ||
res.json({ payload: await getSettings({}) }) | ||
}) | ||
.put(async (req, res) => { | ||
// TODO: 这里先不做 schema 校验,以后再加 | ||
const args = req.body as API.setSettings.Args | ||
const newSettings = await setSettings(args) | ||
|
||
res.json({ payload: newSettings }) | ||
|
||
scheduleBroadcastMessage({ | ||
event: 'settings_change', | ||
settings: newSettings, | ||
}) | ||
}) | ||
|
||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Settings } from '@autorecord/shared' | ||
|
||
export interface ServerOpts { | ||
getSettings: () => Promise<Settings> | ||
setSettings: (newSettings: Settings) => Promise<Settings> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@autorecord/shared", | ||
"version": "0.0.1", | ||
"description": "internal utils shared across @autorecord packages", | ||
"main": "./lib/index.js", | ||
"private": true, | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc -w" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"repository": "https://github.com/WhiteMinds/LiveAutoRecord", | ||
"author": "WhiteMind", | ||
"license": "LGPL", | ||
"devDependencies": { | ||
"typescript": "^4.8.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Settings { | ||
notExitOnAllWindowsClosed: boolean | ||
noticeOnRecordStart: boolean | ||
} |
Oops, something went wrong.