@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.
- package/dist/adapter/solidAdapter.d.ts +2 -2
- package/dist/adapter/solidAdapter.d.ts.map +1 -1
- package/dist/adapter/solidAdapter.js +2 -2
- package/dist/adapter/solidAdapter.test.js +68 -78
- package/dist/api.d.ts +10 -6
- package/dist/api.d.ts.map +1 -1
- package/dist/api.test-d.d.ts +2 -0
- package/dist/api.test-d.d.ts.map +1 -0
- package/dist/api.test-d.js +4 -0
- package/dist/createMessageLintReportsQuery.d.ts +2 -2
- package/dist/createMessageLintReportsQuery.d.ts.map +1 -1
- package/dist/createMessageLintReportsQuery.js +11 -11
- package/dist/createMessagesQuery.js +1 -1
- package/dist/errors.d.ts +3 -3
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +6 -6
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/lint/message/lintMessages.d.ts +3 -7
- package/dist/lint/message/lintMessages.d.ts.map +1 -1
- package/dist/lint/message/lintMessages.test.js +38 -36
- package/dist/lint/message/lintSingleMessage.d.ts +3 -7
- package/dist/lint/message/lintSingleMessage.d.ts.map +1 -1
- package/dist/lint/message/lintSingleMessage.js +10 -10
- package/dist/lint/message/lintSingleMessage.test.js +71 -65
- package/dist/{openInlangProject.d.ts → loadProject.d.ts} +3 -3
- package/dist/loadProject.d.ts.map +1 -0
- package/dist/{openInlangProject.js → loadProject.js} +70 -66
- package/dist/loadProject.test.d.ts +2 -0
- package/dist/loadProject.test.d.ts.map +1 -0
- package/dist/{openInlangProject.test.js → loadProject.test.js} +160 -176
- package/dist/parseConfig.d.ts +2 -2
- package/dist/parseConfig.d.ts.map +1 -1
- package/dist/parseConfig.js +3 -3
- package/dist/resolve-modules/message-lint-rules/resolveMessageLintRules.js +1 -1
- package/dist/resolve-modules/plugins/errors.d.ts +1 -1
- package/dist/resolve-modules/plugins/errors.d.ts.map +1 -1
- package/dist/resolve-modules/plugins/resolvePlugins.js +15 -15
- package/dist/resolve-modules/plugins/resolvePlugins.test.js +55 -94
- package/dist/resolve-modules/plugins/types.d.ts +1 -1
- package/dist/resolve-modules/plugins/types.d.ts.map +1 -1
- package/dist/resolve-modules/plugins/types.test.js +10 -14
- package/dist/resolve-modules/resolveModules.d.ts.map +1 -1
- package/dist/resolve-modules/resolveModules.js +9 -9
- package/dist/resolve-modules/resolveModules.test.js +28 -42
- package/dist/resolve-modules/types.d.ts +3 -3
- package/dist/resolve-modules/types.d.ts.map +1 -1
- package/dist/versionedInterfaces.d.ts +1 -1
- package/dist/versionedInterfaces.d.ts.map +1 -1
- package/dist/versionedInterfaces.js +1 -1
- package/package.json +2 -2
- package/src/adapter/solidAdapter.test.ts +77 -87
- package/src/adapter/solidAdapter.ts +4 -4
- package/src/api.test-d.ts +8 -0
- package/src/api.ts +10 -6
- package/src/createMessageLintReportsQuery.ts +15 -20
- package/src/createMessagesQuery.ts +1 -1
- package/src/errors.ts +6 -6
- package/src/index.ts +4 -4
- package/src/lint/message/lintMessages.test.ts +38 -36
- package/src/lint/message/lintMessages.ts +3 -11
- package/src/lint/message/lintSingleMessage.test.ts +71 -65
- package/src/lint/message/lintSingleMessage.ts +13 -21
- package/src/{openInlangProject.test.ts → loadProject.test.ts} +165 -181
- package/src/{openInlangProject.ts → loadProject.ts} +81 -82
- package/src/parseConfig.ts +5 -3
- package/src/resolve-modules/message-lint-rules/resolveMessageLintRules.ts +1 -1
- package/src/resolve-modules/plugins/errors.ts +1 -1
- package/src/resolve-modules/plugins/resolvePlugins.test.ts +55 -94
- package/src/resolve-modules/plugins/resolvePlugins.ts +19 -19
- package/src/resolve-modules/plugins/types.test.ts +11 -15
- package/src/resolve-modules/plugins/types.ts +1 -1
- package/src/resolve-modules/resolveModules.test.ts +29 -43
- package/src/resolve-modules/resolveModules.ts +11 -9
- package/src/resolve-modules/types.ts +3 -3
- package/src/versionedInterfaces.ts +1 -1
- package/dist/openInlangProject.d.ts.map +0 -1
- package/dist/openInlangProject.test.d.ts +0 -2
- 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 {
|
|
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 {
|
|
8
|
+
import { ProjectSettings, Message } from "./versionedInterfaces.js";
|
|
10
9
|
import { tryCatch } from "@inlang/result";
|
|
11
|
-
|
|
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
|
|
19
|
+
export const loadProject = async (args) => {
|
|
20
20
|
return await createRoot(async () => {
|
|
21
21
|
const [initialized, markInitAsComplete, markInitAsFailed] = createAwaitable();
|
|
22
|
-
// --
|
|
23
|
-
const [
|
|
22
|
+
// -- settings ------------------------------------------------------------
|
|
23
|
+
const [settings, _setSettings] = createSignal();
|
|
24
24
|
createEffect(() => {
|
|
25
|
-
|
|
26
|
-
.then((
|
|
27
|
-
|
|
28
|
-
args._capture?.("SDK used
|
|
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
|
|
35
|
-
const
|
|
36
|
-
const
|
|
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
|
|
39
|
-
|
|
40
|
-
|
|
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
|
|
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
|
|
54
|
-
if (!
|
|
53
|
+
const _settings = settings();
|
|
54
|
+
if (!_settings)
|
|
55
55
|
return;
|
|
56
|
-
|
|
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
|
|
65
|
-
createEffect(() => (
|
|
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 =
|
|
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:
|
|
80
|
-
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
|
-
|
|
94
|
-
|
|
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
|
-
|
|
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
|
-
|
|
105
|
-
|
|
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,
|
|
116
|
+
const lintReportsQuery = createMessageLintReportsQuery(messages, settings, installedMessageLintRules, resolvedModules);
|
|
113
117
|
const debouncedSave = skipFirst(debounce(500, async (newMessages) => {
|
|
114
118
|
try {
|
|
115
|
-
await resolvedModules()
|
|
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
|
-
|
|
142
|
-
|
|
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
|
|
154
|
-
const { data:
|
|
155
|
-
if (
|
|
156
|
-
throw new
|
|
157
|
-
cause:
|
|
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
|
|
160
|
-
if (
|
|
161
|
-
throw new
|
|
162
|
-
cause:
|
|
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
|
-
|
|
168
|
+
}
|
|
169
|
+
return parseSettings(json.data);
|
|
165
170
|
};
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
if (
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
174
|
-
return Value.Cast(ProjectConfig, config);
|
|
181
|
+
return withMigration;
|
|
175
182
|
};
|
|
176
|
-
const
|
|
177
|
-
const { data:
|
|
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.
|
|
180
|
-
if (
|
|
181
|
-
throw
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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 @@
|
|
|
1
|
+
{"version":3,"file":"loadProject.test.d.ts","sourceRoot":"","sources":["../src/loadProject.test.ts"],"names":[],"mappings":""}
|