fix: Jan should have access to it resource folders

This commit is contained in:
Louis 2024-08-15 21:20:21 +07:00
parent e12d50108b
commit 265ebad7cf
6 changed files with 27 additions and 28 deletions

View File

@ -77,8 +77,8 @@ export class App implements Processor {
port: args?.port,
isCorsEnabled: args?.isCorsEnabled,
isVerboseEnabled: args?.isVerboseEnabled,
schemaPath: join(await appResourcePath(), 'docs', 'openapi', 'jan.yaml'),
baseDir: join(await appResourcePath(), 'docs', 'openapi'),
schemaPath: join(appResourcePath(), 'docs', 'openapi', 'jan.yaml'),
baseDir: join(appResourcePath(), 'docs', 'openapi'),
prefix: args?.prefix,
})
}

View File

@ -42,7 +42,7 @@ export class Extension implements Processor {
* @returns An array of paths to the base extensions.
*/
async baseExtensions() {
const baseExtensionPath = join(await appResourcePath(), 'pre-install')
const baseExtensionPath = join(appResourcePath(), 'pre-install')
return readdirSync(baseExtensionPath)
.filter((file) => extname(file) === '.tgz')
.map((file) => join(baseExtensionPath, file))

View File

@ -11,34 +11,37 @@ export function normalizeFilePath(path: string): string {
return path.replace(/^(file:[\\/]+)([^:\s]+)$/, '$2')
}
export async function appResourcePath(): Promise<string> {
let electron: any = undefined
export function appResourcePath() {
try {
const moduleName = 'electron'
electron = await import(moduleName)
const electron = require('electron')
// electron
if (electron && electron.protocol) {
let appPath = join(electron.app.getAppPath(), '..', 'app.asar.unpacked')
if (!electron.app.isPackaged) {
// for development mode
appPath = join(electron.app.getAppPath())
}
return appPath
}
} catch (err) {
console.error('Electron is not available')
}
// electron
if (electron && electron.protocol) {
let appPath = join(electron.app.getAppPath(), '..', 'app.asar.unpacked')
if (!electron.app.isPackaged) {
// for development mode
appPath = join(electron.app.getAppPath())
}
return appPath
}
// server
return join(global.core.appPath(), '../../..')
}
export function validatePath(path: string) {
const janDataFolderPath = getJanDataFolderPath()
const appDataFolderPath = getJanDataFolderPath()
const resourcePath = appResourcePath()
const applicationSupportPath = global.core?.appPath() ?? resourcePath
const absolutePath = resolve(__dirname, path)
if (!absolutePath.startsWith(janDataFolderPath)) {
if (
![appDataFolderPath, resourcePath, applicationSupportPath].some((whiteListedPath) =>
absolutePath.startsWith(whiteListedPath)
)
) {
throw new Error(`Invalid path: ${absolutePath}`)
}
}

View File

@ -11,11 +11,7 @@ import {
lstatSync,
} from 'fs'
import Store from 'electron-store'
import {
getJanExtensionsPath,
getJanDataFolderPath,
appResourcePath,
} from '@janhq/core/node'
import { getJanDataFolderPath, appResourcePath } from '@janhq/core/node'
/**
* Migrates the extensions & themes.
@ -43,9 +39,9 @@ async function migrateThemes() {
if (!existsSync(join(getJanDataFolderPath(), 'themes')))
mkdirSync(join(getJanDataFolderPath(), 'themes'), { recursive: true })
const themes = readdirSync(join(await appResourcePath(), 'themes'))
const themes = readdirSync(join(appResourcePath(), 'themes'))
for (const theme of themes) {
const themePath = join(await appResourcePath(), 'themes', theme)
const themePath = join(appResourcePath(), 'themes', theme)
if (existsSync(themePath) && !lstatSync(themePath).isDirectory()) {
continue
}

View File

@ -2,7 +2,6 @@ import { PropsWithChildren } from 'react'
import { Metadata } from 'next'
import 'katex/dist/katex.min.css'
import '@/styles/main.scss'
export const metadata: Metadata = {

View File

@ -5,6 +5,7 @@
@import 'tailwindcss/utilities';
@import '@janhq/joi/dist/main.css';
@import 'katex/dist/katex.min.css';
@import './base/global.scss';