mirror of
https://github.com/enso-org/enso.git
synced 2024-11-27 15:15:54 +03:00
e7668ebc3a
Enso will now associate with two file extensions: * `.enso` — Enso source file. * If the source file belongs to a project under the Project Manager-managed directory, it will be opened. * If the source file belongs to a project located elsewhere, it will be imported into the PM-managed directory and opened; * Otherwise, opening the `.enseo` file will fail. (e.g., loose source file without any project) * `.enso-project` — Enso project bundle, i.e., `tar.gz` archive containing a compressed Enso project directory. * it will be imported under the PM-managed directory; a unique directory name shall be generated if needed. ### Important Notes On Windows, the NSIS installer is expected to handle the file associations. On macOS, the file associations are expected to be set up after the first time Enso is started, On Linux, the file associations are not supported yet.
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
/** @file Shared utility functions. */
|
|
import * as fs from 'node:fs'
|
|
import * as path from 'node:path'
|
|
import process from 'node:process'
|
|
|
|
// =================
|
|
// === Constants ===
|
|
// =================
|
|
|
|
/** Indent size for outputting JSON. */
|
|
export const INDENT_SIZE = 4
|
|
|
|
// ===================
|
|
// === Environment ===
|
|
// ===================
|
|
|
|
/**
|
|
* Get the environment variable value.
|
|
*
|
|
* @param name - The name of the environment variable.
|
|
* @returns The value of the environment variable.
|
|
* @throws {Error} If the environment variable is not set.
|
|
*/
|
|
export function requireEnv(name: string) {
|
|
return (
|
|
process.env[name] ??
|
|
(() => {
|
|
throw Error(`Missing ${name} environment variable.`)
|
|
})()
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Read the path from environment variable and resolve it.
|
|
*
|
|
* @param name - The name of the environment variable.
|
|
* @returns The resolved path.
|
|
* @throws {Error} If the environment variable is not set.
|
|
*/
|
|
export function requireEnvResolvedPath(name: string) {
|
|
return path.resolve(requireEnv(name))
|
|
}
|
|
|
|
/**
|
|
* Read the path from environment variable and resolve it. Verify that it exists.
|
|
*
|
|
* @param name - The name of the environment variable.
|
|
* @returns The resolved path.
|
|
* @throws {Error} If the environment variable is not set or path does not exist.
|
|
*/
|
|
export function requireEnvPathExist(name: string) {
|
|
const value = requireEnv(name)
|
|
if (fs.existsSync(value)) {
|
|
return value
|
|
} else {
|
|
throw Error(`File with path ${value} read from environment variable ${name} is missing.`)
|
|
}
|
|
}
|
|
|
|
// ======================
|
|
// === String Helpers ===
|
|
// ======================
|
|
|
|
/** Get the common prefix of the two strings. */
|
|
export function getCommonPrefix(a: string, b: string): string {
|
|
let i = 0
|
|
while (i < a.length && i < b.length && a[i] === b[i]) {
|
|
i++
|
|
}
|
|
return a.slice(0, i)
|
|
}
|