2024-02-11 12:59:00 +11:00
|
|
|
import { onboardingPaths } from 'routes/Onboarding/paths'
|
2024-04-02 10:29:34 -04:00
|
|
|
import { BROWSER_FILE_NAME, BROWSER_PROJECT_NAME, FILE_EXT } from './constants'
|
2024-08-16 07:15:42 -04:00
|
|
|
import { isDesktop } from './isDesktop'
|
2024-08-22 13:38:53 -04:00
|
|
|
import { readAppSettingsFile } from './desktop'
|
2024-04-25 11:55:11 -07:00
|
|
|
import { readLocalStorageAppSettingsFile } from './settings/settingsUtils'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2024-08-16 07:15:42 -04:00
|
|
|
import { IS_PLAYWRIGHT_KEY } from '../../e2e/playwright/storageStates'
|
2024-08-20 22:16:44 -04:00
|
|
|
import { DeepPartial } from './types'
|
|
|
|
import { Configuration } from 'wasm-lib/kcl/bindings/Configuration'
|
2024-08-22 13:38:53 -04:00
|
|
|
import { PlatformPath } from 'path'
|
2024-02-11 12:59:00 +11:00
|
|
|
|
|
|
|
const prependRoutes =
|
|
|
|
(routesObject: Record<string, string>) => (prepend: string) => {
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries(routesObject).map(([constName, path]) => [
|
|
|
|
constName,
|
|
|
|
prepend + path,
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-17 15:10:30 -04:00
|
|
|
type OnboardingPaths = {
|
|
|
|
[K in keyof typeof onboardingPaths]: `/onboarding${(typeof onboardingPaths)[K]}`
|
|
|
|
}
|
|
|
|
|
2024-08-09 02:47:25 -04:00
|
|
|
const SETTINGS = '/settings' as const
|
|
|
|
|
2024-08-22 13:38:53 -04:00
|
|
|
export type ProjectRoute = {
|
|
|
|
projectName: string | null
|
|
|
|
projectPath: string
|
|
|
|
currentFileName: string | null
|
|
|
|
currentFilePath: string | null
|
|
|
|
}
|
|
|
|
|
2024-08-09 02:47:25 -04:00
|
|
|
export const PATHS = {
|
2024-02-11 12:59:00 +11:00
|
|
|
INDEX: '/',
|
|
|
|
HOME: '/home',
|
|
|
|
FILE: '/file',
|
2024-08-09 02:47:25 -04:00
|
|
|
SETTINGS,
|
|
|
|
SETTINGS_USER: `${SETTINGS}?tab=user` as const,
|
|
|
|
SETTINGS_PROJECT: `${SETTINGS}?tab=project` as const,
|
|
|
|
SETTINGS_KEYBINDINGS: `${SETTINGS}?tab=keybindings` as const,
|
2024-02-11 12:59:00 +11:00
|
|
|
SIGN_IN: '/signin',
|
2024-06-17 15:10:30 -04:00
|
|
|
ONBOARDING: prependRoutes(onboardingPaths)('/onboarding') as OnboardingPaths,
|
2024-04-02 10:29:34 -04:00
|
|
|
} as const
|
|
|
|
export const BROWSER_PATH = `%2F${BROWSER_PROJECT_NAME}%2F${BROWSER_FILE_NAME}${FILE_EXT}`
|
|
|
|
|
2024-04-25 11:55:11 -07:00
|
|
|
export async function getProjectMetaByRouteId(
|
|
|
|
id?: string,
|
2024-08-20 22:16:44 -04:00
|
|
|
configuration?: DeepPartial<Configuration> | Error
|
2024-04-25 11:55:11 -07:00
|
|
|
): Promise<ProjectRoute | undefined> {
|
2024-04-02 10:29:34 -04:00
|
|
|
if (!id) return undefined
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
const onDesktop = isDesktop()
|
|
|
|
const isPlaywright = localStorage.getItem(IS_PLAYWRIGHT_KEY) === 'true'
|
2024-04-02 10:29:34 -04:00
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
if (configuration === undefined || isPlaywright) {
|
|
|
|
configuration = onDesktop
|
2024-04-25 11:55:11 -07:00
|
|
|
? await readAppSettingsFile()
|
|
|
|
: readLocalStorageAppSettingsFile()
|
2024-04-02 10:29:34 -04:00
|
|
|
}
|
2024-04-25 11:55:11 -07:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(configuration)) return Promise.reject(configuration)
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
// Should not be possible but I guess logically it could be
|
|
|
|
if (configuration === undefined) {
|
|
|
|
return Promise.reject(new Error('No configuration found'))
|
|
|
|
}
|
|
|
|
|
2024-08-22 13:38:53 -04:00
|
|
|
const route = parseProjectRoute(configuration, id, window?.electron?.path)
|
2024-04-25 11:55:11 -07:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(route)) return Promise.reject(route)
|
|
|
|
|
2024-04-25 11:55:11 -07:00
|
|
|
return route
|
2024-02-11 12:59:00 +11:00
|
|
|
}
|
2024-08-22 13:38:53 -04:00
|
|
|
|
|
|
|
export async function parseProjectRoute(
|
|
|
|
configuration: DeepPartial<Configuration>,
|
|
|
|
id: string,
|
|
|
|
pathlib: PlatformPath | undefined
|
|
|
|
): Promise<ProjectRoute> {
|
|
|
|
let projectName = null
|
|
|
|
let projectPath = ''
|
|
|
|
let currentFileName = null
|
|
|
|
let currentFilePath = null
|
|
|
|
if (
|
|
|
|
pathlib &&
|
|
|
|
configuration.settings?.project?.directory &&
|
|
|
|
id.startsWith(configuration.settings.project.directory)
|
|
|
|
) {
|
|
|
|
const relativeToRoot = pathlib.relative(
|
|
|
|
configuration.settings.project.directory,
|
|
|
|
id
|
|
|
|
)
|
|
|
|
projectName = relativeToRoot.split(pathlib.sep)[0]
|
|
|
|
projectPath = pathlib.join(
|
|
|
|
configuration.settings.project.directory,
|
|
|
|
projectName
|
|
|
|
)
|
|
|
|
projectName = projectName === '' ? null : projectName
|
|
|
|
} else {
|
|
|
|
projectPath = id
|
|
|
|
if (pathlib) {
|
|
|
|
if (pathlib.extname(id) === '.kcl') {
|
|
|
|
projectPath = pathlib.dirname(id)
|
|
|
|
}
|
|
|
|
projectName = pathlib.basename(projectPath)
|
|
|
|
} else {
|
|
|
|
if (id.endsWith('.kcl')) {
|
|
|
|
projectPath = '/browser'
|
|
|
|
projectName = 'browser'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pathlib) {
|
|
|
|
if (projectPath !== id) {
|
|
|
|
currentFileName = pathlib.basename(id)
|
|
|
|
currentFilePath = id
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentFileName = 'main.kcl'
|
|
|
|
currentFilePath = id
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
projectName: projectName,
|
|
|
|
projectPath: projectPath,
|
|
|
|
currentFileName: currentFileName,
|
|
|
|
currentFilePath: currentFilePath,
|
|
|
|
}
|
|
|
|
}
|