@inlang/sdk 0.35.5 → 0.35.7
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/api.d.ts +2 -13
- package/dist/api.d.ts.map +1 -1
- package/dist/createNewProject.test.js +1 -3
- package/dist/loadProject.d.ts.map +1 -1
- package/dist/loadProject.js +67 -32
- package/dist/loadProject.test.js +6 -2
- package/dist/persistence/batchedIO.d.ts +11 -0
- package/dist/persistence/batchedIO.d.ts.map +1 -0
- package/dist/persistence/batchedIO.js +49 -0
- package/dist/persistence/batchedIO.test.d.ts +2 -0
- package/dist/persistence/batchedIO.test.d.ts.map +1 -0
- package/dist/persistence/batchedIO.test.js +56 -0
- package/dist/persistence/filelock/acquireFileLock.d.ts.map +1 -1
- package/dist/persistence/filelock/acquireFileLock.js +3 -1
- package/dist/persistence/filelock/releaseLock.d.ts.map +1 -1
- package/dist/persistence/filelock/releaseLock.js +2 -1
- package/dist/persistence/store.d.ts +107 -0
- package/dist/persistence/store.d.ts.map +1 -0
- package/dist/persistence/store.js +99 -0
- package/dist/persistence/store.test.d.ts +2 -0
- package/dist/persistence/store.test.d.ts.map +1 -0
- package/dist/persistence/store.test.js +79 -0
- package/dist/persistence/storeApi.d.ts +22 -0
- package/dist/persistence/storeApi.d.ts.map +1 -0
- package/dist/persistence/storeApi.js +1 -0
- package/dist/reactivity/solid.test.js +1 -6
- package/dist/resolve-modules/plugins/resolvePlugins.d.ts.map +1 -1
- package/dist/resolve-modules/plugins/resolvePlugins.js +3 -10
- package/dist/test-utilities/sleep.d.ts +4 -0
- package/dist/test-utilities/sleep.d.ts.map +1 -0
- package/dist/test-utilities/sleep.js +9 -0
- package/dist/v2/helper.d.ts +131 -0
- package/dist/v2/helper.d.ts.map +1 -0
- package/dist/v2/helper.js +75 -0
- package/dist/v2/helper.test.d.ts +2 -0
- package/dist/v2/helper.test.d.ts.map +1 -0
- package/dist/v2/{createMessageBundle.test.js → helper.test.js} +1 -1
- package/dist/v2/index.d.ts +2 -0
- package/dist/v2/index.d.ts.map +1 -1
- package/dist/v2/index.js +2 -1
- package/dist/v2/mocks/index.d.ts +3 -0
- package/dist/v2/mocks/index.d.ts.map +1 -0
- package/dist/v2/mocks/index.js +2 -0
- package/dist/v2/mocks/multipleMatcher/bundle.d.ts +3 -0
- package/dist/v2/mocks/multipleMatcher/bundle.d.ts.map +1 -0
- package/dist/v2/mocks/multipleMatcher/bundle.js +194 -0
- package/dist/v2/mocks/multipleMatcher/bundle.test.d.ts +2 -0
- package/dist/v2/mocks/multipleMatcher/bundle.test.d.ts.map +1 -0
- package/dist/v2/mocks/multipleMatcher/bundle.test.js +10 -0
- package/dist/v2/mocks/plural/bundle.d.ts +1 -1
- package/dist/v2/mocks/plural/bundle.d.ts.map +1 -1
- package/dist/v2/mocks/plural/bundle.js +1 -1
- package/dist/v2/mocks/plural/bundle.test.js +6 -6
- package/dist/v2/shim.d.ts +12 -0
- package/dist/v2/shim.d.ts.map +1 -0
- package/dist/v2/shim.js +151 -0
- package/dist/v2/shim.test.d.ts +2 -0
- package/dist/v2/shim.test.d.ts.map +1 -0
- package/dist/v2/shim.test.js +49 -0
- package/dist/v2/stubQueryApi.d.ts +9 -0
- package/dist/v2/stubQueryApi.d.ts.map +1 -0
- package/dist/v2/stubQueryApi.js +38 -0
- package/dist/v2/types.d.ts +110 -0
- package/dist/v2/types.d.ts.map +1 -1
- package/dist/v2/types.js +9 -0
- package/package.json +6 -5
- package/src/api.ts +2 -13
- package/src/createNewProject.test.ts +1 -4
- package/src/loadProject.test.ts +6 -2
- package/src/loadProject.ts +86 -45
- package/src/persistence/batchedIO.test.ts +63 -0
- package/src/persistence/batchedIO.ts +64 -0
- package/src/persistence/filelock/acquireFileLock.ts +5 -2
- package/src/persistence/filelock/releaseLock.ts +2 -1
- package/src/persistence/store.test.ts +102 -0
- package/src/persistence/store.ts +119 -0
- package/src/persistence/storeApi.ts +19 -0
- package/src/reactivity/solid.test.ts +1 -8
- package/src/resolve-modules/plugins/resolvePlugins.ts +4 -13
- package/src/test-utilities/sleep.ts +11 -0
- package/src/v2/{createMessageBundle.test.ts → helper.test.ts} +1 -1
- package/src/v2/helper.ts +98 -0
- package/src/v2/index.ts +2 -0
- package/src/v2/mocks/index.ts +2 -0
- package/src/v2/mocks/multipleMatcher/bundle.test.ts +11 -0
- package/src/v2/mocks/multipleMatcher/bundle.ts +196 -0
- package/src/v2/mocks/plural/bundle.test.ts +6 -6
- package/src/v2/mocks/plural/bundle.ts +1 -1
- package/src/v2/shim.test.ts +56 -0
- package/src/v2/shim.ts +173 -0
- package/src/v2/stubQueryApi.ts +43 -0
- package/src/v2/types.ts +17 -0
- package/dist/persistence/plugin.d.ts +0 -31
- package/dist/persistence/plugin.d.ts.map +0 -1
- package/dist/persistence/plugin.js +0 -42
- package/dist/persistence/plugin.test.d.ts +0 -2
- package/dist/persistence/plugin.test.d.ts.map +0 -1
- package/dist/persistence/plugin.test.js +0 -49
- package/dist/v2/createMessageBundle.d.ts +0 -25
- package/dist/v2/createMessageBundle.d.ts.map +0 -1
- package/dist/v2/createMessageBundle.js +0 -36
- package/dist/v2/createMessageBundle.test.d.ts +0 -2
- package/dist/v2/createMessageBundle.test.d.ts.map +0 -1
- package/src/persistence/plugin.test.ts +0 -60
- package/src/persistence/plugin.ts +0 -56
- package/src/v2/createMessageBundle.ts +0 -43
package/src/v2/helper.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LanguageTag,
|
|
3
|
+
MessageBundle,
|
|
4
|
+
MessageBundleWithSlots,
|
|
5
|
+
Message,
|
|
6
|
+
MessageSlot,
|
|
7
|
+
Text,
|
|
8
|
+
} from "./types.js"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* create v2 MessageBundle
|
|
12
|
+
* @example createMessageBundle({
|
|
13
|
+
* id: "greeting",
|
|
14
|
+
* messages: [
|
|
15
|
+
* createMessage({locale: "en", text: "Hello world!"})
|
|
16
|
+
* createMessage({locale: "de", text: "Hallo Welt!"})
|
|
17
|
+
* ]
|
|
18
|
+
* })
|
|
19
|
+
*/
|
|
20
|
+
export function createMessageBundle(args: {
|
|
21
|
+
id: string
|
|
22
|
+
messages: Message[]
|
|
23
|
+
alias?: MessageBundle["alias"]
|
|
24
|
+
}): MessageBundle {
|
|
25
|
+
return {
|
|
26
|
+
id: args.id,
|
|
27
|
+
alias: args.alias ?? {},
|
|
28
|
+
messages: args.messages,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* create v2 Messsage AST with text-only pattern
|
|
34
|
+
* @example createMessage({locale: "en", text: "Hello world"})
|
|
35
|
+
*/
|
|
36
|
+
export function createMessage(args: {
|
|
37
|
+
locale: LanguageTag
|
|
38
|
+
text: string
|
|
39
|
+
match?: Array<string>
|
|
40
|
+
}): Message {
|
|
41
|
+
return {
|
|
42
|
+
locale: args.locale,
|
|
43
|
+
declarations: [],
|
|
44
|
+
selectors: [],
|
|
45
|
+
variants: [{ match: args.match ? args.match : [], pattern: [toTextElement(args.text ?? "")] }],
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function toTextElement(text: string): Text {
|
|
50
|
+
return {
|
|
51
|
+
type: "text",
|
|
52
|
+
value: text,
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ****************************
|
|
57
|
+
// WIP experimental persistence
|
|
58
|
+
// ****************************
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* create MessageSlot for a locale (only used for persistence)
|
|
62
|
+
*/
|
|
63
|
+
export function createMessageSlot(locale: LanguageTag): MessageSlot {
|
|
64
|
+
return {
|
|
65
|
+
locale,
|
|
66
|
+
slot: true,
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* return structuredClone with message slots for all locales not yet present
|
|
72
|
+
*/
|
|
73
|
+
export function addSlots(messageBundle: MessageBundle, locales: string[]): MessageBundleWithSlots {
|
|
74
|
+
const bundle = structuredClone(messageBundle) as MessageBundleWithSlots
|
|
75
|
+
bundle.messages = locales.map((locale) => {
|
|
76
|
+
return bundle.messages.find((message) => message.locale === locale) ?? createMessageSlot(locale)
|
|
77
|
+
})
|
|
78
|
+
return bundle
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* remove empty message slots without first creating a structured clone
|
|
83
|
+
*/
|
|
84
|
+
export function removeSlots(messageBundle: MessageBundleWithSlots) {
|
|
85
|
+
messageBundle.messages = messageBundle.messages.filter((message) => !("slot" in message))
|
|
86
|
+
return messageBundle as MessageBundle
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Add newlines between bundles and messages to avoid merge conflicts
|
|
91
|
+
*/
|
|
92
|
+
export function injectJSONNewlines(json: string): string {
|
|
93
|
+
return json
|
|
94
|
+
.replace(/\{"id":"/g, '\n\n\n\n{"id":"')
|
|
95
|
+
.replace(/"messages":\[\{"locale":"/g, '"messages":[\n\n\n\n{"locale":"')
|
|
96
|
+
.replace(/\}\]\}\]\},\{"locale":"/g, '}]}]},\n\n\n\n{"locale":"')
|
|
97
|
+
.replace(/"slot":true\},\{"locale":/g, '"slot":true},\n\n\n\n{"locale":')
|
|
98
|
+
}
|
package/src/v2/index.ts
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest"
|
|
2
|
+
import { multipleMatcherBundle } from "./bundle.js"
|
|
3
|
+
import { MessageBundle } from "../../types.js"
|
|
4
|
+
import { Value } from "@sinclair/typebox/value"
|
|
5
|
+
|
|
6
|
+
describe("mock plural messageBundle", () => {
|
|
7
|
+
it("is valid", () => {
|
|
8
|
+
const messageBundle: unknown = multipleMatcherBundle
|
|
9
|
+
expect(Value.Check(MessageBundle, messageBundle)).toBe(true)
|
|
10
|
+
})
|
|
11
|
+
})
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { MessageBundle } from "../../types.js"
|
|
2
|
+
|
|
3
|
+
export const multipleMatcherBundle: MessageBundle = {
|
|
4
|
+
id: "mock-bundle-human-id",
|
|
5
|
+
alias: {
|
|
6
|
+
default: "mock-bundle-alias",
|
|
7
|
+
},
|
|
8
|
+
messages: [
|
|
9
|
+
{
|
|
10
|
+
locale: "en",
|
|
11
|
+
declarations: [],
|
|
12
|
+
selectors: [
|
|
13
|
+
{
|
|
14
|
+
type: "expression",
|
|
15
|
+
arg: {
|
|
16
|
+
type: "variable",
|
|
17
|
+
name: "count",
|
|
18
|
+
},
|
|
19
|
+
annotation: { type: "function", name: "plural", options: [] },
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
variants: [
|
|
23
|
+
{
|
|
24
|
+
match: ["one"],
|
|
25
|
+
pattern: [
|
|
26
|
+
{
|
|
27
|
+
type: "text",
|
|
28
|
+
value: "Show ",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
type: "expression",
|
|
32
|
+
arg: {
|
|
33
|
+
type: "variable",
|
|
34
|
+
name: "count",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
type: "text",
|
|
39
|
+
value: " message.",
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
match: ["many"],
|
|
45
|
+
pattern: [
|
|
46
|
+
{
|
|
47
|
+
type: "text",
|
|
48
|
+
value: "Show ",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: "expression",
|
|
52
|
+
arg: {
|
|
53
|
+
type: "variable",
|
|
54
|
+
name: "count",
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
type: "text",
|
|
59
|
+
value: " messages.",
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
match: ["*"],
|
|
65
|
+
pattern: [
|
|
66
|
+
{
|
|
67
|
+
type: "text",
|
|
68
|
+
value: "Show ",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
type: "expression",
|
|
72
|
+
arg: {
|
|
73
|
+
type: "variable",
|
|
74
|
+
name: "count",
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
locale: "de",
|
|
83
|
+
declarations: [],
|
|
84
|
+
selectors: [
|
|
85
|
+
{
|
|
86
|
+
type: "expression",
|
|
87
|
+
arg: {
|
|
88
|
+
type: "variable",
|
|
89
|
+
name: "count",
|
|
90
|
+
},
|
|
91
|
+
annotation: { type: "function", name: "plural", options: [] },
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
type: "expression",
|
|
95
|
+
arg: {
|
|
96
|
+
type: "variable",
|
|
97
|
+
name: "formal",
|
|
98
|
+
},
|
|
99
|
+
annotation: { type: "function", name: "bool", options: [] },
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
variants: [
|
|
103
|
+
{
|
|
104
|
+
match: ["one", "formal"],
|
|
105
|
+
pattern: [
|
|
106
|
+
{
|
|
107
|
+
type: "text",
|
|
108
|
+
value: "Zeigen Sie bitte Ihre Nachricht.",
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
match: ["one", "informal"],
|
|
114
|
+
pattern: [
|
|
115
|
+
{
|
|
116
|
+
type: "text",
|
|
117
|
+
value: "Zeigen Deine Nachricht.",
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
match: ["many", "formal"],
|
|
123
|
+
pattern: [
|
|
124
|
+
{
|
|
125
|
+
type: "text",
|
|
126
|
+
value: "Zeigen Sie bitte Ihre ",
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
type: "expression",
|
|
130
|
+
arg: {
|
|
131
|
+
type: "variable",
|
|
132
|
+
name: "count",
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
type: "text",
|
|
137
|
+
value: " Nachrichten.",
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
match: ["many", "informal"],
|
|
143
|
+
pattern: [
|
|
144
|
+
{
|
|
145
|
+
type: "text",
|
|
146
|
+
value: "Zeigen Deine ",
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
type: "expression",
|
|
150
|
+
arg: {
|
|
151
|
+
type: "variable",
|
|
152
|
+
name: "count",
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
type: "text",
|
|
157
|
+
value: " Nachrichten.",
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
match: ["many", "*"],
|
|
163
|
+
pattern: [
|
|
164
|
+
{
|
|
165
|
+
type: "expression",
|
|
166
|
+
arg: {
|
|
167
|
+
type: "variable",
|
|
168
|
+
name: "count",
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
type: "text",
|
|
173
|
+
value: " Nachrichten zeigen.",
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
match: ["*"],
|
|
179
|
+
pattern: [
|
|
180
|
+
{
|
|
181
|
+
type: "text",
|
|
182
|
+
value: "Zeige ",
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
type: "expression",
|
|
186
|
+
arg: {
|
|
187
|
+
type: "variable",
|
|
188
|
+
name: "count",
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
2
|
|
|
3
3
|
import { describe, it, expect } from "vitest"
|
|
4
|
-
import {
|
|
4
|
+
import { pluralBundle } from "./bundle.js"
|
|
5
5
|
import { MessageBundle } from "../../types.js"
|
|
6
6
|
import { Value } from "@sinclair/typebox/value"
|
|
7
7
|
|
|
8
8
|
describe("mock plural messageBundle", () => {
|
|
9
9
|
it("is valid", () => {
|
|
10
|
-
const messageBundle: unknown =
|
|
10
|
+
const messageBundle: unknown = pluralBundle
|
|
11
11
|
expect(Value.Check(MessageBundle, messageBundle)).toBe(true)
|
|
12
12
|
|
|
13
|
-
expect(
|
|
14
|
-
expect(
|
|
15
|
-
expect(
|
|
16
|
-
expect(
|
|
13
|
+
expect(pluralBundle.messages.length).toBe(2)
|
|
14
|
+
expect(pluralBundle.messages[0]!.declarations.length).toBe(1)
|
|
15
|
+
expect(pluralBundle.messages[0]!.selectors.length).toBe(1)
|
|
16
|
+
expect(pluralBundle.messages[0]!.variants.length).toBe(3)
|
|
17
17
|
})
|
|
18
18
|
})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { test, expect } from "vitest"
|
|
2
|
+
import * as V2 from "./types.js"
|
|
3
|
+
import * as V1 from "@inlang/message"
|
|
4
|
+
import { createMessageBundle, createMessage } from "./helper.js"
|
|
5
|
+
import { toV1Message, fromV1Message } from "./shim.js"
|
|
6
|
+
import { Value } from "@sinclair/typebox/value"
|
|
7
|
+
|
|
8
|
+
const bundle = createMessageBundle({
|
|
9
|
+
id: "hello_world",
|
|
10
|
+
messages: [
|
|
11
|
+
createMessage({ locale: "en", text: "Hello World!" }),
|
|
12
|
+
createMessage({ locale: "de", text: "Hallo Welt!" }),
|
|
13
|
+
],
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test("toV1Message and fromV1Message", () => {
|
|
17
|
+
expect(Value.Check(V2.MessageBundle, bundle)).toBe(true)
|
|
18
|
+
|
|
19
|
+
const v1Message: unknown = toV1Message(bundle)
|
|
20
|
+
expect(Value.Check(V1.Message, v1Message)).toBe(true)
|
|
21
|
+
|
|
22
|
+
expect(v1Message).toEqual({
|
|
23
|
+
id: "hello_world",
|
|
24
|
+
alias: {},
|
|
25
|
+
variants: [
|
|
26
|
+
{
|
|
27
|
+
languageTag: "en",
|
|
28
|
+
match: [],
|
|
29
|
+
pattern: [
|
|
30
|
+
{
|
|
31
|
+
type: "Text",
|
|
32
|
+
value: "Hello World!",
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
languageTag: "de",
|
|
38
|
+
match: [],
|
|
39
|
+
pattern: [
|
|
40
|
+
{
|
|
41
|
+
type: "Text",
|
|
42
|
+
value: "Hallo Welt!",
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
selectors: [],
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const v2MessageBundle: unknown = fromV1Message(v1Message as V1.Message)
|
|
51
|
+
expect(Value.Check(V2.MessageBundle, v2MessageBundle)).toBe(true)
|
|
52
|
+
|
|
53
|
+
expect(v2MessageBundle).toEqual(bundle)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test.todo("with variable references", () => {})
|
package/src/v2/shim.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert between v1 Message and v2 MessageBundle formats.
|
|
3
|
+
* Code adapted from https://github.com/opral/monorepo/pull/2655 legacy.ts
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as V2 from "./types.js"
|
|
7
|
+
import * as V1 from "@inlang/message"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @throws If the message cannot be represented in the v1 format
|
|
11
|
+
*/
|
|
12
|
+
export function toV1Message(bundle: V2.MessageBundle): V1.Message {
|
|
13
|
+
const variants: V1.Variant[] = []
|
|
14
|
+
const selectorNames = new Set<string>()
|
|
15
|
+
|
|
16
|
+
for (const message of bundle.messages) {
|
|
17
|
+
// collect all selector names
|
|
18
|
+
for (const selector of message.selectors.map(toV1Expression)) {
|
|
19
|
+
selectorNames.add(selector.name)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// collect all variants
|
|
23
|
+
for (const variant of message.variants) {
|
|
24
|
+
variants.push({
|
|
25
|
+
languageTag: message.locale,
|
|
26
|
+
match: variant.match,
|
|
27
|
+
pattern: toV1Pattern(variant.pattern),
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const selectors: V1.Expression[] = [...selectorNames].map((name) => ({
|
|
33
|
+
type: "VariableReference",
|
|
34
|
+
name,
|
|
35
|
+
}))
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
id: bundle.id,
|
|
39
|
+
alias: bundle.alias,
|
|
40
|
+
variants,
|
|
41
|
+
selectors,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @throws If the pattern cannot be represented in the v1 format
|
|
47
|
+
*/
|
|
48
|
+
function toV1Pattern(pattern: V2.Pattern): V1.Pattern {
|
|
49
|
+
return pattern.map((element) => {
|
|
50
|
+
switch (element.type) {
|
|
51
|
+
case "text": {
|
|
52
|
+
return {
|
|
53
|
+
type: "Text",
|
|
54
|
+
value: element.value,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
case "expression": {
|
|
59
|
+
return toV1Expression(element)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
default: {
|
|
63
|
+
throw new Error(`Unsupported pattern element type`)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function toV1Expression(expression: V2.Expression): V1.Expression {
|
|
70
|
+
if (expression.annotation !== undefined)
|
|
71
|
+
throw new Error("Cannot convert an expression with an annotation to the v1 format")
|
|
72
|
+
|
|
73
|
+
if (expression.arg.type !== "variable") {
|
|
74
|
+
throw new Error("Can only convert variable references to the v1 format")
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
type: "VariableReference",
|
|
79
|
+
name: expression.arg.name,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function fromV1Message(v1Message: V1.Message): V2.MessageBundle {
|
|
84
|
+
const languages = dedup(v1Message.variants.map((variant) => variant.languageTag))
|
|
85
|
+
|
|
86
|
+
const messages: V2.Message[] = languages.map((language): V2.Message => {
|
|
87
|
+
//All variants that will be part of this message
|
|
88
|
+
const v1Variants = v1Message.variants.filter((variant) => variant.languageTag === language)
|
|
89
|
+
|
|
90
|
+
//find all selector names
|
|
91
|
+
const selectorNames = new Set<string>()
|
|
92
|
+
for (const v1Selector of v1Message.selectors) {
|
|
93
|
+
selectorNames.add(v1Selector.name)
|
|
94
|
+
}
|
|
95
|
+
const selectors: V2.Expression[] = [...selectorNames].map((name) => ({
|
|
96
|
+
type: "expression",
|
|
97
|
+
annotation: undefined,
|
|
98
|
+
arg: {
|
|
99
|
+
type: "variable",
|
|
100
|
+
name: name,
|
|
101
|
+
},
|
|
102
|
+
}))
|
|
103
|
+
|
|
104
|
+
//The set of variables that need to be defined - Certainly includes the selectors
|
|
105
|
+
const variableNames = new Set<string>(selectorNames)
|
|
106
|
+
const variants: V2.Variant[] = []
|
|
107
|
+
for (const v1Variant of v1Variants) {
|
|
108
|
+
for (const element of v1Variant.pattern) {
|
|
109
|
+
if (element.type === "VariableReference") {
|
|
110
|
+
variableNames.add(element.name)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
variants.push({
|
|
115
|
+
match: v1Variant.match,
|
|
116
|
+
pattern: fromV1Pattern(v1Variant.pattern),
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//Create an input declaration for each variable and selector we need
|
|
121
|
+
const declarations: V2.Declaration[] = [...variableNames].map((name) => ({
|
|
122
|
+
type: "input",
|
|
123
|
+
name,
|
|
124
|
+
value: {
|
|
125
|
+
type: "expression",
|
|
126
|
+
annotation: undefined,
|
|
127
|
+
arg: {
|
|
128
|
+
type: "variable",
|
|
129
|
+
name,
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
}))
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
locale: language,
|
|
136
|
+
declarations,
|
|
137
|
+
selectors,
|
|
138
|
+
variants,
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
id: v1Message.id,
|
|
144
|
+
alias: v1Message.alias,
|
|
145
|
+
messages,
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function fromV1Pattern(pattern: V1.Pattern): V2.Pattern {
|
|
150
|
+
return pattern.map((element) => {
|
|
151
|
+
switch (element.type) {
|
|
152
|
+
case "Text": {
|
|
153
|
+
return {
|
|
154
|
+
type: "text",
|
|
155
|
+
value: element.value,
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
case "VariableReference":
|
|
159
|
+
return {
|
|
160
|
+
type: "expression",
|
|
161
|
+
arg: {
|
|
162
|
+
type: "variable",
|
|
163
|
+
name: element.name,
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Dedups an array by converting it to a set and back
|
|
172
|
+
*/
|
|
173
|
+
const dedup = <T extends Array<unknown>>(arr: T): T => [...new Set(arr)] as T
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { MessageQueryApi, MessageLintReportsQueryApi } from "../api.js"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* noop implementation of the message query api for use with experimental.persistence = true.
|
|
5
|
+
* NOTE: If we implemented v2 shims for the old api we could use existing tests and make apps
|
|
6
|
+
* backwards compatible.
|
|
7
|
+
*/
|
|
8
|
+
export const stubMessagesQuery: MessageQueryApi = {
|
|
9
|
+
create: () => false,
|
|
10
|
+
// @ts-expect-error
|
|
11
|
+
get: subscribable(() => undefined),
|
|
12
|
+
// @ts-expect-error
|
|
13
|
+
getByDefaultAlias: subscribable(() => undefined),
|
|
14
|
+
// @ts-expect-error
|
|
15
|
+
includedMessageIds: subscribable(() => []),
|
|
16
|
+
// @ts-expect-error
|
|
17
|
+
getAll: subscribable(() => []),
|
|
18
|
+
update: () => false,
|
|
19
|
+
upsert: () => {},
|
|
20
|
+
delete: () => false,
|
|
21
|
+
setDelegate: () => {},
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const stubMessageLintReportsQuery: MessageLintReportsQueryApi = {
|
|
25
|
+
// @ts-expect-error
|
|
26
|
+
get: subscribable(() => []),
|
|
27
|
+
// @ts-expect-error
|
|
28
|
+
getAll: settleable(subscribable(() => [])),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
32
|
+
function subscribable(fn: Function) {
|
|
33
|
+
return Object.assign(fn, {
|
|
34
|
+
subscribe: () => {},
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
39
|
+
function settleable(fn: Function) {
|
|
40
|
+
return Object.assign(fn, {
|
|
41
|
+
settled: async () => [],
|
|
42
|
+
})
|
|
43
|
+
}
|
package/src/v2/types.ts
CHANGED
|
@@ -140,3 +140,20 @@ export const MessageBundle = Type.Object({
|
|
|
140
140
|
alias: Type.Record(Type.String(), Type.String()),
|
|
141
141
|
messages: Type.Array(Message),
|
|
142
142
|
})
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A MessageSlot is a placeholder for a message with a locale.
|
|
146
|
+
* This is useful to avoid merge conflicts when translations are added.
|
|
147
|
+
*/
|
|
148
|
+
export type MessageSlot = Static<typeof MessageSlot>
|
|
149
|
+
export const MessageSlot = Type.Object({
|
|
150
|
+
locale: LanguageTag,
|
|
151
|
+
slot: Type.Literal(true),
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
export type MessageBundleWithSlots = Static<typeof MessageBundleWithSlots>
|
|
155
|
+
export const MessageBundleWithSlots = Type.Object({
|
|
156
|
+
id: Type.String(),
|
|
157
|
+
alias: Type.Record(Type.String(), Type.String()),
|
|
158
|
+
messages: Type.Array(Type.Union([Message, MessageSlot])),
|
|
159
|
+
})
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { ProjectSettings, Message } from "@inlang/sdk";
|
|
2
|
-
import { type NodeishFilesystem } from "@lix-js/fs";
|
|
3
|
-
export declare const pluginId = "plugin.sdk.persistence";
|
|
4
|
-
export declare function loadMessages(args: {
|
|
5
|
-
settings: ProjectSettings;
|
|
6
|
-
nodeishFs: NodeishFilesystem;
|
|
7
|
-
}): Promise<{
|
|
8
|
-
id: string;
|
|
9
|
-
alias: Record<string, string>;
|
|
10
|
-
selectors: {
|
|
11
|
-
type: "VariableReference";
|
|
12
|
-
name: string;
|
|
13
|
-
}[];
|
|
14
|
-
variants: {
|
|
15
|
-
languageTag: string;
|
|
16
|
-
match: string[];
|
|
17
|
-
pattern: ({
|
|
18
|
-
type: "Text";
|
|
19
|
-
value: string;
|
|
20
|
-
} | {
|
|
21
|
-
type: "VariableReference";
|
|
22
|
-
name: string;
|
|
23
|
-
})[];
|
|
24
|
-
}[];
|
|
25
|
-
}[]>;
|
|
26
|
-
export declare function saveMessages(args: {
|
|
27
|
-
settings: ProjectSettings;
|
|
28
|
-
nodeishFs: NodeishFilesystem;
|
|
29
|
-
messages: Message[];
|
|
30
|
-
}): Promise<void>;
|
|
31
|
-
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/persistence/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAC3D,OAAO,EAAc,KAAK,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAM/D,eAAO,MAAM,QAAQ,2BAA2B,CAAA;AAEhD,wBAAsB,YAAY,CAAC,IAAI,EAAE;IACxC,QAAQ,EAAE,eAAe,CAAA;IACzB,SAAS,EAAE,iBAAiB,CAAA;CAC5B;;;;;;;;;;;;;;;;;;KAeA;AAED,wBAAsB,YAAY,CAAC,IAAI,EAAE;IACxC,QAAQ,EAAE,eAAe,CAAA;IACzB,SAAS,EAAE,iBAAiB,CAAA;IAC5B,QAAQ,EAAE,OAAO,EAAE,CAAA;CACnB,iBAcA"}
|