2023-03-15 06:42:14 +03:00
|
|
|
/** @file Shared utility functions. */
|
|
|
|
import * as fs from 'node:fs'
|
|
|
|
import * as path from 'node:path'
|
2023-03-16 16:09:31 +03:00
|
|
|
import process from 'node:process'
|
2022-08-10 04:41:44 +03:00
|
|
|
|
Project Sharing (#6077)
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.
2023-04-06 16:26:37 +03:00
|
|
|
// =================
|
|
|
|
// === Constants ===
|
|
|
|
// =================
|
|
|
|
|
|
|
|
/** Indent size for outputting JSON. */
|
|
|
|
export const INDENT_SIZE = 4
|
|
|
|
|
|
|
|
// ===================
|
|
|
|
// === Environment ===
|
|
|
|
// ===================
|
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
/** Get the environment variable value.
|
2023-03-15 06:42:14 +03:00
|
|
|
* @param name - The name of the environment variable.
|
|
|
|
* @returns The value of the environment variable.
|
2023-05-19 22:55:29 +03:00
|
|
|
* @throws {Error} If the environment variable is not set. */
|
2023-03-15 06:42:14 +03:00
|
|
|
export function requireEnv(name: string) {
|
2022-08-10 04:41:44 +03:00
|
|
|
return (
|
|
|
|
process.env[name] ??
|
|
|
|
(() => {
|
|
|
|
throw Error(`Missing ${name} environment variable.`)
|
|
|
|
})()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
/** Read the path from environment variable and resolve it.
|
2023-03-15 06:42:14 +03:00
|
|
|
* @param name - The name of the environment variable.
|
|
|
|
* @returns The resolved path.
|
2023-05-19 22:55:29 +03:00
|
|
|
* @throws {Error} If the environment variable is not set. */
|
2023-03-15 06:42:14 +03:00
|
|
|
export function requireEnvResolvedPath(name: string) {
|
|
|
|
return path.resolve(requireEnv(name))
|
2022-08-10 04:41:44 +03:00
|
|
|
}
|
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
/** Read the path from environment variable and resolve it. Verify that it exists.
|
2023-03-15 06:42:14 +03:00
|
|
|
* @param name - The name of the environment variable.
|
|
|
|
* @returns The resolved path.
|
2023-05-19 22:55:29 +03:00
|
|
|
* @throws {Error} If the environment variable is not set or path does not exist. */
|
2023-03-15 06:42:14 +03:00
|
|
|
export function requireEnvPathExist(name: string) {
|
|
|
|
const value = requireEnv(name)
|
2023-03-20 12:35:16 +03:00
|
|
|
if (fs.existsSync(value)) {
|
|
|
|
return value
|
|
|
|
} else {
|
|
|
|
throw Error(`File with path ${value} read from environment variable ${name} is missing.`)
|
|
|
|
}
|
2022-08-10 04:41:44 +03:00
|
|
|
}
|
Project Sharing (#6077)
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.
2023-04-06 16:26:37 +03:00
|
|
|
|
|
|
|
// ======================
|
|
|
|
// === 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)
|
|
|
|
}
|