@inlang/sdk 0.4.0 → 0.5.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.
Files changed (80) hide show
  1. package/dist/adapter/solidAdapter.d.ts +2 -2
  2. package/dist/adapter/solidAdapter.d.ts.map +1 -1
  3. package/dist/adapter/solidAdapter.js +2 -2
  4. package/dist/adapter/solidAdapter.test.js +68 -78
  5. package/dist/api.d.ts +10 -6
  6. package/dist/api.d.ts.map +1 -1
  7. package/dist/api.test-d.d.ts +2 -0
  8. package/dist/api.test-d.d.ts.map +1 -0
  9. package/dist/api.test-d.js +4 -0
  10. package/dist/createMessageLintReportsQuery.d.ts +2 -2
  11. package/dist/createMessageLintReportsQuery.d.ts.map +1 -1
  12. package/dist/createMessageLintReportsQuery.js +11 -11
  13. package/dist/createMessagesQuery.js +1 -1
  14. package/dist/errors.d.ts +3 -3
  15. package/dist/errors.d.ts.map +1 -1
  16. package/dist/errors.js +6 -6
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -2
  20. package/dist/lint/message/lintMessages.d.ts +3 -7
  21. package/dist/lint/message/lintMessages.d.ts.map +1 -1
  22. package/dist/lint/message/lintMessages.test.js +38 -36
  23. package/dist/lint/message/lintSingleMessage.d.ts +3 -7
  24. package/dist/lint/message/lintSingleMessage.d.ts.map +1 -1
  25. package/dist/lint/message/lintSingleMessage.js +10 -10
  26. package/dist/lint/message/lintSingleMessage.test.js +71 -65
  27. package/dist/{openInlangProject.d.ts → loadProject.d.ts} +3 -3
  28. package/dist/loadProject.d.ts.map +1 -0
  29. package/dist/{openInlangProject.js → loadProject.js} +70 -66
  30. package/dist/loadProject.test.d.ts +2 -0
  31. package/dist/loadProject.test.d.ts.map +1 -0
  32. package/dist/{openInlangProject.test.js → loadProject.test.js} +160 -176
  33. package/dist/parseConfig.d.ts +2 -2
  34. package/dist/parseConfig.d.ts.map +1 -1
  35. package/dist/parseConfig.js +3 -3
  36. package/dist/resolve-modules/message-lint-rules/resolveMessageLintRules.js +1 -1
  37. package/dist/resolve-modules/plugins/errors.d.ts +1 -1
  38. package/dist/resolve-modules/plugins/errors.d.ts.map +1 -1
  39. package/dist/resolve-modules/plugins/resolvePlugins.js +15 -15
  40. package/dist/resolve-modules/plugins/resolvePlugins.test.js +55 -94
  41. package/dist/resolve-modules/plugins/types.d.ts +1 -1
  42. package/dist/resolve-modules/plugins/types.d.ts.map +1 -1
  43. package/dist/resolve-modules/plugins/types.test.js +10 -14
  44. package/dist/resolve-modules/resolveModules.d.ts.map +1 -1
  45. package/dist/resolve-modules/resolveModules.js +9 -9
  46. package/dist/resolve-modules/resolveModules.test.js +28 -42
  47. package/dist/resolve-modules/types.d.ts +3 -3
  48. package/dist/resolve-modules/types.d.ts.map +1 -1
  49. package/dist/versionedInterfaces.d.ts +1 -1
  50. package/dist/versionedInterfaces.d.ts.map +1 -1
  51. package/dist/versionedInterfaces.js +1 -1
  52. package/package.json +2 -2
  53. package/src/adapter/solidAdapter.test.ts +77 -87
  54. package/src/adapter/solidAdapter.ts +4 -4
  55. package/src/api.test-d.ts +8 -0
  56. package/src/api.ts +10 -6
  57. package/src/createMessageLintReportsQuery.ts +15 -20
  58. package/src/createMessagesQuery.ts +1 -1
  59. package/src/errors.ts +6 -6
  60. package/src/index.ts +4 -4
  61. package/src/lint/message/lintMessages.test.ts +38 -36
  62. package/src/lint/message/lintMessages.ts +3 -11
  63. package/src/lint/message/lintSingleMessage.test.ts +71 -65
  64. package/src/lint/message/lintSingleMessage.ts +13 -21
  65. package/src/{openInlangProject.test.ts → loadProject.test.ts} +165 -181
  66. package/src/{openInlangProject.ts → loadProject.ts} +81 -82
  67. package/src/parseConfig.ts +5 -3
  68. package/src/resolve-modules/message-lint-rules/resolveMessageLintRules.ts +1 -1
  69. package/src/resolve-modules/plugins/errors.ts +1 -1
  70. package/src/resolve-modules/plugins/resolvePlugins.test.ts +55 -94
  71. package/src/resolve-modules/plugins/resolvePlugins.ts +19 -19
  72. package/src/resolve-modules/plugins/types.test.ts +11 -15
  73. package/src/resolve-modules/plugins/types.ts +1 -1
  74. package/src/resolve-modules/resolveModules.test.ts +29 -43
  75. package/src/resolve-modules/resolveModules.ts +11 -9
  76. package/src/resolve-modules/types.ts +3 -3
  77. package/src/versionedInterfaces.ts +1 -1
  78. package/dist/openInlangProject.d.ts.map +0 -1
  79. package/dist/openInlangProject.test.d.ts +0 -2
  80. package/dist/openInlangProject.test.d.ts.map +0 -1
@@ -1,14 +1,14 @@
1
1
  import { resolveModules } from "./resolve-modules/index.js";
2
2
  import { TypeCompiler } from "@sinclair/typebox/compiler";
3
- import { Value } from "@sinclair/typebox/value";
4
- import { ProjectFilePathNotFoundError, ProjectFileJSONSyntaxError, InvalidConfigError, PluginLoadMessagesError, PluginSaveMessagesError, } from "./errors.js";
3
+ import { ProjectSettingsFileJSONSyntaxError, ProjectSettingsFileNotFoundError, ProjectSettingsInvalidError, PluginLoadMessagesError, PluginSaveMessagesError, } from "./errors.js";
5
4
  import { createRoot, createSignal, createEffect } from "./reactivity/solid.js";
6
5
  import { createMessagesQuery } from "./createMessagesQuery.js";
7
6
  import { debounce } from "throttle-debounce";
8
7
  import { createMessageLintReportsQuery } from "./createMessageLintReportsQuery.js";
9
- import { ProjectConfig, Message } from "./versionedInterfaces.js";
8
+ import { ProjectSettings, Message } from "./versionedInterfaces.js";
10
9
  import { tryCatch } from "@inlang/result";
11
- const ConfigCompiler = TypeCompiler.Compile(ProjectConfig);
10
+ import { migrateIfOutdated } from "@inlang/project-settings/migration";
11
+ const settingsCompiler = TypeCompiler.Compile(ProjectSettings);
12
12
  /**
13
13
  * Creates an inlang instance.
14
14
  *
@@ -16,32 +16,32 @@ const ConfigCompiler = TypeCompiler.Compile(ProjectConfig);
16
16
  * and supporting legacy resolvedModules such as CJS.
17
17
  *
18
18
  */
19
- export const openInlangProject = async (args) => {
19
+ export const loadProject = async (args) => {
20
20
  return await createRoot(async () => {
21
21
  const [initialized, markInitAsComplete, markInitAsFailed] = createAwaitable();
22
- // -- config ------------------------------------------------------------
23
- const [config, _setConfig] = createSignal();
22
+ // -- settings ------------------------------------------------------------
23
+ const [settings, _setSettings] = createSignal();
24
24
  createEffect(() => {
25
- loadConfig({ projectFilePath: args.projectFilePath, nodeishFs: args.nodeishFs })
26
- .then((config) => {
27
- setConfig(config);
28
- args._capture?.("SDK used config", config);
25
+ loadSettings({ settingsFilePath: args.settingsFilePath, nodeishFs: args.nodeishFs })
26
+ .then((settings) => {
27
+ setSettings(settings);
28
+ args._capture?.("SDK used settings", settings);
29
29
  })
30
30
  .catch((err) => {
31
31
  markInitAsFailed(err);
32
32
  });
33
33
  });
34
- // TODO: create FS watcher and update config on change
35
- const writeConfigToDisk = skipFirst((config) => _writeConfigToDisk({ nodeishFs: args.nodeishFs, config }));
36
- const setConfig = (config) => {
34
+ // TODO: create FS watcher and update settings on change
35
+ const writeSettingsToDisk = skipFirst((settings) => _writeSettingsToDisk({ nodeishFs: args.nodeishFs, settings }));
36
+ const setSettings = (settings) => {
37
37
  try {
38
- const validatedConfig = validateConfig(config);
39
- _setConfig(validatedConfig);
40
- writeConfigToDisk(validatedConfig);
38
+ const validatedSettings = parseSettings(settings);
39
+ _setSettings(validatedSettings);
40
+ writeSettingsToDisk(validatedSettings);
41
41
  return { data: undefined };
42
42
  }
43
43
  catch (error) {
44
- if (error instanceof InvalidConfigError) {
44
+ if (error instanceof ProjectSettingsInvalidError) {
45
45
  return { error };
46
46
  }
47
47
  throw new Error("unhandled");
@@ -50,10 +50,10 @@ export const openInlangProject = async (args) => {
50
50
  // -- resolvedModules -----------------------------------------------------------
51
51
  const [resolvedModules, setResolvedModules] = createSignal();
52
52
  createEffect(() => {
53
- const conf = config();
54
- if (!conf)
53
+ const _settings = settings();
54
+ if (!_settings)
55
55
  return;
56
- loadModules({ config: conf, nodeishFs: args.nodeishFs, _import: args._import })
56
+ resolveModules({ settings: _settings, nodeishFs: args.nodeishFs, _import: args._import })
57
57
  .then((resolvedModules) => {
58
58
  setResolvedModules(resolvedModules);
59
59
  // TODO: handle `detectedLanguageTags`
@@ -61,11 +61,11 @@ export const openInlangProject = async (args) => {
61
61
  .catch((err) => markInitAsFailed(err));
62
62
  });
63
63
  // -- messages ----------------------------------------------------------
64
- let configValue;
65
- createEffect(() => (configValue = config())); // workaround to not run effects twice (e.g. config change + modules change) (I'm sure there exists a solid way of doing this, but I haven't found it yet)
64
+ let settingsValue;
65
+ createEffect(() => (settingsValue = settings())); // workaround to not run effects twice (e.g. settings change + modules change) (I'm sure there exists a solid way of doing this, but I haven't found it yet)
66
66
  const [messages, setMessages] = createSignal();
67
67
  createEffect(() => {
68
- const conf = config();
68
+ const conf = settings();
69
69
  if (!conf)
70
70
  return;
71
71
  const _resolvedModules = resolvedModules();
@@ -76,8 +76,8 @@ export const openInlangProject = async (args) => {
76
76
  return;
77
77
  }
78
78
  makeTrulyAsync(_resolvedModules.resolvedPluginApi.loadMessages({
79
- languageTags: configValue.languageTags,
80
- sourceLanguageTag: configValue.sourceLanguageTag,
79
+ languageTags: settingsValue.languageTags,
80
+ sourceLanguageTag: settingsValue.sourceLanguageTag,
81
81
  }))
82
82
  .then((messages) => {
83
83
  setMessages(messages);
@@ -90,29 +90,33 @@ export const openInlangProject = async (args) => {
90
90
  if (!resolvedModules())
91
91
  return [];
92
92
  return resolvedModules().messageLintRules.map((rule) => ({
93
- meta: rule.meta,
94
- module: resolvedModules()?.meta.find((m) => m.id.includes(rule.meta.id))?.module ??
93
+ id: rule.id,
94
+ displayName: rule.displayName,
95
+ description: rule.description,
96
+ module: resolvedModules()?.meta.find((m) => m.id.includes(rule.id))?.module ??
95
97
  "Unknown module. You stumbled on a bug in inlang's source code. Please open an issue.",
96
98
  // default to warning, see https://github.com/inlang/monorepo/issues/1254
97
- lintLevel: configValue.settings["project.messageLintRuleLevels"]?.[rule.meta.id] ?? "warning",
99
+ level: settingsValue["messageLintRuleLevels"]?.[rule.id] ?? "warning",
98
100
  }));
99
101
  };
100
102
  const installedPlugins = () => {
101
103
  if (!resolvedModules())
102
104
  return [];
103
105
  return resolvedModules().plugins.map((plugin) => ({
104
- meta: plugin.meta,
105
- module: resolvedModules()?.meta.find((m) => m.id.includes(plugin.meta.id))?.module ??
106
+ id: plugin.id,
107
+ displayName: plugin.displayName,
108
+ description: plugin.description,
109
+ module: resolvedModules()?.meta.find((m) => m.id.includes(plugin.id))?.module ??
106
110
  "Unknown module. You stumbled on a bug in inlang's source code. Please open an issue.",
107
111
  }));
108
112
  };
109
113
  // -- app ---------------------------------------------------------------
110
114
  const initializeError = await initialized.catch((error) => error);
111
115
  const messagesQuery = createMessagesQuery(() => messages() || []);
112
- const lintReportsQuery = createMessageLintReportsQuery(messages, config, installedMessageLintRules, resolvedModules);
116
+ const lintReportsQuery = createMessageLintReportsQuery(messages, settings, installedMessageLintRules, resolvedModules);
113
117
  const debouncedSave = skipFirst(debounce(500, async (newMessages) => {
114
118
  try {
115
- await resolvedModules().resolvedPluginApi.saveMessages({ messages: newMessages });
119
+ await resolvedModules()?.resolvedPluginApi.saveMessages({ messages: newMessages });
116
120
  }
117
121
  catch (err) {
118
122
  throw new PluginSaveMessagesError("Error in saving messages", {
@@ -138,8 +142,8 @@ export const openInlangProject = async (args) => {
138
142
  // have a query error exposed
139
143
  //...(lintErrors() ?? []),
140
144
  ]),
141
- config: createSubscribable(() => config()),
142
- setConfig,
145
+ settings: createSubscribable(() => settings()),
146
+ setSettings,
143
147
  customApi: createSubscribable(() => resolvedModules()?.resolvedPluginApi.customApi || {}),
144
148
  query: {
145
149
  messages: messagesQuery,
@@ -150,45 +154,45 @@ export const openInlangProject = async (args) => {
150
154
  };
151
155
  //const x = {} as InlangProject
152
156
  // ------------------------------------------------------------------------------------------------
153
- const loadConfig = async (args) => {
154
- const { data: configFile, error: configFileError } = await tryCatch(async () => await args.nodeishFs.readFile(args.projectFilePath, { encoding: "utf-8" }));
155
- if (configFileError)
156
- throw new ProjectFilePathNotFoundError(`Could not locate config file in (${args.projectFilePath}).`, {
157
- cause: configFileError,
157
+ const loadSettings = async (args) => {
158
+ const { data: settingsFile, error: settingsFileError } = await tryCatch(async () => await args.nodeishFs.readFile(args.settingsFilePath, { encoding: "utf-8" }));
159
+ if (settingsFileError)
160
+ throw new ProjectSettingsFileNotFoundError(`Could not locate settings file in (${args.settingsFilePath}).`, {
161
+ cause: settingsFileError,
158
162
  });
159
- const { data: parsedConfig, error: parseConfigError } = tryCatch(() => JSON.parse(configFile));
160
- if (parseConfigError)
161
- throw new ProjectFileJSONSyntaxError(`The config is not a valid JSON file.`, {
162
- cause: parseConfigError,
163
+ const json = tryCatch(() => JSON.parse(settingsFile));
164
+ if (json.error) {
165
+ throw new ProjectSettingsFileJSONSyntaxError(`The settings is not a valid JSON file.`, {
166
+ cause: json.error,
163
167
  });
164
- return validateConfig(parsedConfig);
168
+ }
169
+ return parseSettings(json.data);
165
170
  };
166
- const validateConfig = (config) => {
167
- const typeErrors = [...ConfigCompiler.Errors(config)];
168
- if (typeErrors.length > 0) {
169
- throw new InvalidConfigError(`The config is invalid according to the schema.`, {
170
- cause: typeErrors,
171
- });
171
+ const parseSettings = (settings) => {
172
+ const withMigration = migrateIfOutdated(settings);
173
+ if (settingsCompiler.Check(withMigration) === false) {
174
+ const typeErrors = [...settingsCompiler.Errors(settings)];
175
+ if (typeErrors.length > 0) {
176
+ throw new ProjectSettingsInvalidError(`The settings is invalid according to the schema.`, {
177
+ cause: typeErrors,
178
+ });
179
+ }
172
180
  }
173
- // @ts-ignore - fix after refactor
174
- return Value.Cast(ProjectConfig, config);
181
+ return withMigration;
175
182
  };
176
- const _writeConfigToDisk = async (args) => {
177
- const { data: serializedConfig, error: serializeConfigError } = tryCatch(() =>
183
+ const _writeSettingsToDisk = async (args) => {
184
+ const { data: serializedSettings, error: serializeSettingsError } = tryCatch(() =>
178
185
  // TODO: this will probably not match the original formatting
179
- JSON.stringify(args.config, undefined, 2));
180
- if (serializeConfigError)
181
- throw serializeConfigError;
182
- const { error: writeConfigError } = await tryCatch(async () => args.nodeishFs.writeFile("./project.inlang.json", serializedConfig));
183
- if (writeConfigError)
184
- throw writeConfigError;
186
+ JSON.stringify(args.settings, undefined, 2));
187
+ if (serializeSettingsError) {
188
+ throw serializeSettingsError;
189
+ }
190
+ const { error: writeSettingsError } = await tryCatch(async () => args.nodeishFs.writeFile("./project.inlang.json", serializedSettings));
191
+ if (writeSettingsError) {
192
+ throw writeSettingsError;
193
+ }
185
194
  };
186
195
  // ------------------------------------------------------------------------------------------------
187
- const loadModules = async (args) => resolveModules({
188
- config: args.config,
189
- nodeishFs: args.nodeishFs,
190
- _import: args._import,
191
- });
192
196
  const createAwaitable = () => {
193
197
  let resolve;
194
198
  let reject;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=loadProject.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadProject.test.d.ts","sourceRoot":"","sources":["../src/loadProject.test.ts"],"names":[],"mappings":""}