@inlang/sdk 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,7 @@ import {
13
13
  ProjectSettingsInvalidError,
14
14
  PluginLoadMessagesError,
15
15
  PluginSaveMessagesError,
16
+ LoadProjectInvalidArgument,
16
17
  } from "./errors.js"
17
18
  import { createRoot, createSignal, createEffect } from "./reactivity/solid.js"
18
19
  import { createMessagesQuery } from "./createMessagesQuery.js"
@@ -21,14 +22,21 @@ import { createMessageLintReportsQuery } from "./createMessageLintReportsQuery.j
21
22
  import { ProjectSettings, Message, type NodeishFilesystemSubset } from "./versionedInterfaces.js"
22
23
  import { tryCatch, type Result } from "@inlang/result"
23
24
  import { migrateIfOutdated } from "@inlang/project-settings/migration"
25
+ import {
26
+ createNodeishFsWithAbsolutePaths,
27
+ isAbsolutePath,
28
+ } from "./createNodeishFsWithAbsolutePaths.js"
24
29
 
25
30
  const settingsCompiler = TypeCompiler.Compile(ProjectSettings)
26
31
 
27
32
  /**
28
33
  * Creates an inlang instance.
29
34
  *
30
- * - Use `_import` to pass a custom import function for testing,
35
+ * @param settingsFilePath - Absolute path to the inlang settings file.
36
+ * @param nodeishFs - Filesystem that implements the NodeishFilesystemSubset interface.
37
+ * @param _import - Use `_import` to pass a custom import function for testing,
31
38
  * and supporting legacy resolvedModules such as CJS.
39
+ * @param _capture - Use `_capture` to capture events for analytics.
32
40
  *
33
41
  */
34
42
  export const loadProject = async (args: {
@@ -37,14 +45,27 @@ export const loadProject = async (args: {
37
45
  _import?: ImportFunction
38
46
  _capture?: (id: string, props: Record<string, unknown>) => void
39
47
  }): Promise<InlangProject> => {
48
+ // -- validation --------------------------------------------------------
49
+ //! the only place where throwing is acceptable because the project
50
+ //! won't even be loaded. do not throw anywhere else. otherwise, apps
51
+ //! can't handle errors gracefully.
52
+ if (!isAbsolutePath(args.settingsFilePath)) {
53
+ throw new LoadProjectInvalidArgument(
54
+ `Expected an absolute path but received "${args.settingsFilePath}".`,
55
+ { argument: "settingsFilePath" }
56
+ )
57
+ }
58
+
59
+ // -- load project ------------------------------------------------------
40
60
  return await createRoot(async () => {
41
61
  const [initialized, markInitAsComplete, markInitAsFailed] = createAwaitable()
62
+ const nodeishFs = createNodeishFsWithAbsolutePaths(args)
42
63
 
43
64
  // -- settings ------------------------------------------------------------
44
65
 
45
66
  const [settings, _setSettings] = createSignal<ProjectSettings>()
46
67
  createEffect(() => {
47
- loadSettings({ settingsFilePath: args.settingsFilePath, nodeishFs: args.nodeishFs })
68
+ loadSettings({ settingsFilePath: args.settingsFilePath, nodeishFs })
48
69
  .then((settings) => {
49
70
  setSettings(settings)
50
71
  // rename settings to get a convenient access to the data in Posthog
@@ -58,7 +79,7 @@ export const loadProject = async (args: {
58
79
  // TODO: create FS watcher and update settings on change
59
80
 
60
81
  const writeSettingsToDisk = skipFirst((settings: ProjectSettings) =>
61
- _writeSettingsToDisk({ nodeishFs: args.nodeishFs, settings })
82
+ _writeSettingsToDisk({ nodeishFs, settings })
62
83
  )
63
84
 
64
85
  const setSettings = (settings: ProjectSettings): Result<void, ProjectSettingsInvalidError> => {
@@ -88,7 +109,7 @@ export const loadProject = async (args: {
88
109
  const _settings = settings()
89
110
  if (!_settings) return
90
111
 
91
- resolveModules({ settings: _settings, nodeishFs: args.nodeishFs, _import: args._import })
112
+ resolveModules({ settings: _settings, nodeishFs, _import: args._import })
92
113
  .then((resolvedModules) => {
93
114
  setResolvedModules(resolvedModules)
94
115
  })
@@ -1,5 +1,4 @@
1
1
  import dedent from "dedent"
2
- import { normalizePath } from "@lix-js/fs"
3
2
  import type { NodeishFilesystemSubset } from "@inlang/plugin"
4
3
  import { ModuleImportError } from "./errors.js"
5
4
 
@@ -42,7 +41,9 @@ async function $import(
42
41
  if (uri.startsWith("http")) {
43
42
  moduleAsText = await (await fetch(uri)).text()
44
43
  } else {
45
- moduleAsText = await options.readFile(normalizePath(uri), { encoding: "utf-8" })
44
+ moduleAsText = await options.readFile(uri, {
45
+ encoding: "utf-8",
46
+ })
46
47
  }
47
48
 
48
49
  const moduleWithMimeType = "data:application/javascript," + encodeURIComponent(moduleAsText)
@@ -1,4 +1,4 @@
1
- import type { NodeishFilesystem as LisaNodeishFilesystem } from "@lix-js/fs"
1
+ import type { NodeishFilesystem } from "@lix-js/fs"
2
2
  import type {
3
3
  PluginReturnedInvalidCustomApiError,
4
4
  PluginLoadMessagesFunctionAlreadyDefinedError,
@@ -18,7 +18,7 @@ import type { ProjectSettings } from "@inlang/project-settings"
18
18
  * - only uses minimally required functions to decrease the API footprint on the ecosystem.
19
19
  */
20
20
  export type NodeishFilesystemSubset = Pick<
21
- LisaNodeishFilesystem,
21
+ NodeishFilesystem,
22
22
  "readFile" | "readdir" | "mkdir" | "writeFile"
23
23
  >
24
24