lexisync 0.2.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/LICENSE +21 -0
- package/README.md +187 -0
- package/bin/lexisync.js +188 -0
- package/lib/backend.js +94 -0
- package/lib/client.js +226 -0
- package/lib/config-store.js +36 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sander Post / LexiSync
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# lexisync
|
|
2
|
+
|
|
3
|
+
Official **CLI**, **i18next backend**, and HTTP client for [LexiSync](https://lexisync-six.vercel.app) — API-first, Locize-style translation management.
|
|
4
|
+
|
|
5
|
+
[Docs](https://lexisync-six.vercel.app/docs) · [API discovery](https://lexisync-six.vercel.app/api/v1) · [OpenAPI](https://lexisync-six.vercel.app/api/v1/openapi.json)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install lexisync
|
|
11
|
+
# optional peer for the i18next backend
|
|
12
|
+
npm install i18next
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx lexisync --help
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires **Node.js ≥ 18**.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
1. Create an org + project at [lexisync-six.vercel.app](https://lexisync-six.vercel.app/onboarding/1)
|
|
24
|
+
2. Copy **project id** and secret API key (`sk_…`)
|
|
25
|
+
3. Sync source strings, translate, publish a version, load from CDN
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
export LEXISYNC_API_KEY=sk_your_secret
|
|
29
|
+
npx lexisync sync -p YOUR_PROJECT_ID -f ./locales/en.json -l en
|
|
30
|
+
npx lexisync publish -p YOUR_PROJECT_ID -v production
|
|
31
|
+
npx lexisync pull -p YOUR_PROJECT_ID -n common -l en -v production -o ./locales/en.json
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Config is stored in `~/.lexisync/config.json`. Override API host with `LEXISYNC_API_URL` (default: `https://lexisync-six.vercel.app`).
|
|
35
|
+
|
|
36
|
+
## Locize-style workflow
|
|
37
|
+
|
|
38
|
+
| Step | What |
|
|
39
|
+
|------|------|
|
|
40
|
+
| 1. Keys | Dashboard → `projectId` + `sk_…` / `pub_…` |
|
|
41
|
+
| 2. Working copy | `lexisync sync` and/or `saveMissing: true` → version **`latest`** |
|
|
42
|
+
| 3. Translate | Dashboard → project → Translate & publish |
|
|
43
|
+
| 4. Publish | `lexisync publish -v production` freezes CDN catalogs |
|
|
44
|
+
| 5. Ship | App loads `version: "production"` (immutable) or `latest` (dev) |
|
|
45
|
+
|
|
46
|
+
**CDN (no auth):**
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
GET {apiUrl}/api/cdn/{projectId}/{version}/{lng}/{namespace}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Example: `https://lexisync-six.vercel.app/api/cdn/<id>/production/en/common`
|
|
53
|
+
|
|
54
|
+
## CLI
|
|
55
|
+
|
|
56
|
+
```text
|
|
57
|
+
lexisync init [-n name] Create org (or print setup hints)
|
|
58
|
+
lexisync sync -p <id> -f <file> -l <lng> [-n namespace]
|
|
59
|
+
lexisync pull -p <id> -n <ns> -l <lng> [-v latest|production] -o <file>
|
|
60
|
+
lexisync publish -p <id> -v <version> Snapshot working copy → CDN
|
|
61
|
+
lexisync versions -p <id> List published versions
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Global flags (where supported): `--api-url`, `--api-key` / env `LEXISYNC_API_KEY`.
|
|
65
|
+
|
|
66
|
+
## i18next backend
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import i18next from 'i18next';
|
|
70
|
+
import LexiSyncBackend from 'lexisync';
|
|
71
|
+
|
|
72
|
+
await i18next.use(LexiSyncBackend).init({
|
|
73
|
+
lng: 'en',
|
|
74
|
+
ns: ['common'],
|
|
75
|
+
defaultNS: 'common',
|
|
76
|
+
backend: {
|
|
77
|
+
projectId: 'YOUR_PROJECT_ID',
|
|
78
|
+
apiKey: process.env.LEXISYNC_API_KEY, // needed for saveMissing
|
|
79
|
+
apiUrl: 'https://lexisync-six.vercel.app',
|
|
80
|
+
version: 'latest', // use "production" after publish
|
|
81
|
+
},
|
|
82
|
+
saveMissing: true, // only writes when version === "latest"
|
|
83
|
+
saveMissingTo: 'current',
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
i18next.t('hello'); // loads from CDN / working copy
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
CommonJS:
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
const LexiSyncBackend = require('lexisync');
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Also: `require('lexisync/backend')`, `require('lexisync/client')`.
|
|
96
|
+
|
|
97
|
+
## HTTP client (AI / automation)
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
const {
|
|
101
|
+
discoverApi,
|
|
102
|
+
getMe,
|
|
103
|
+
listLanguages,
|
|
104
|
+
downloadCatalog,
|
|
105
|
+
reportMissing,
|
|
106
|
+
publishVersion,
|
|
107
|
+
} = require('lexisync/client');
|
|
108
|
+
|
|
109
|
+
const apiUrl = 'https://lexisync-six.vercel.app';
|
|
110
|
+
const apiKey = process.env.LEXISYNC_API_KEY;
|
|
111
|
+
|
|
112
|
+
await discoverApi({ apiUrl });
|
|
113
|
+
await getMe({ apiUrl, apiKey });
|
|
114
|
+
await listLanguages({ apiUrl, apiKey, projectId: '…' });
|
|
115
|
+
await downloadCatalog({
|
|
116
|
+
apiUrl,
|
|
117
|
+
apiKey,
|
|
118
|
+
projectId: '…',
|
|
119
|
+
version: 'latest',
|
|
120
|
+
format: 'nested', // or "flat"
|
|
121
|
+
});
|
|
122
|
+
await reportMissing({
|
|
123
|
+
apiUrl,
|
|
124
|
+
apiKey,
|
|
125
|
+
projectId: '…',
|
|
126
|
+
namespace: 'common',
|
|
127
|
+
languageCode: 'en',
|
|
128
|
+
keys: { new_cta: 'Try now' },
|
|
129
|
+
});
|
|
130
|
+
await publishVersion({ apiUrl, apiKey, projectId: '…', version: 'production' });
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Full machine-readable map: `GET /api/v1` · OpenAPI: `GET /api/v1/openapi.json`.
|
|
134
|
+
|
|
135
|
+
## React
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
npm install lexisync-react i18next react-i18next
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
import { LexiSyncProvider, useT } from 'lexisync-react';
|
|
143
|
+
|
|
144
|
+
function App() {
|
|
145
|
+
return (
|
|
146
|
+
<LexiSyncProvider
|
|
147
|
+
projectId="…"
|
|
148
|
+
apiKey={import.meta.env.VITE_LEXISYNC_API_KEY}
|
|
149
|
+
version="latest"
|
|
150
|
+
saveMissing
|
|
151
|
+
>
|
|
152
|
+
<Page />
|
|
153
|
+
</LexiSyncProvider>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function Page() {
|
|
158
|
+
const t = useT();
|
|
159
|
+
return <h1>{t('hello')}</h1>;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
`lexisync-react` is a separate package on npm when published.
|
|
164
|
+
|
|
165
|
+
## Environment
|
|
166
|
+
|
|
167
|
+
| Variable | Description |
|
|
168
|
+
|----------|-------------|
|
|
169
|
+
| `LEXISYNC_API_URL` | API host (default production URL) |
|
|
170
|
+
| `LEXISYNC_API_KEY` | Secret key for CLI write ops |
|
|
171
|
+
|
|
172
|
+
## Security
|
|
173
|
+
|
|
174
|
+
- Use **secret** keys (`sk_…`) only server-side or in trusted CLI environments.
|
|
175
|
+
- Use **public** keys (`pub_…`) for read-only clients when possible.
|
|
176
|
+
- Never commit API keys. Prefer env vars or `~/.lexisync/config.json` (local).
|
|
177
|
+
|
|
178
|
+
## Links
|
|
179
|
+
|
|
180
|
+
- Product: https://lexisync-six.vercel.app
|
|
181
|
+
- Documentation: https://lexisync-six.vercel.app/docs
|
|
182
|
+
- API v1: https://lexisync-six.vercel.app/api/v1
|
|
183
|
+
- Source: https://github.com/sanderpost/lexisync (package under `packages/lexisync`)
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
MIT
|
package/bin/lexisync.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const { Command } = require('commander');
|
|
5
|
+
const axios = require('axios');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const { createConfig } = require('../lib/config-store');
|
|
9
|
+
const {
|
|
10
|
+
resolveApiUrl,
|
|
11
|
+
getTranslations,
|
|
12
|
+
publishVersion,
|
|
13
|
+
listVersions,
|
|
14
|
+
cdnUrl,
|
|
15
|
+
} = require('../lib/client');
|
|
16
|
+
|
|
17
|
+
const config = createConfig();
|
|
18
|
+
const program = new Command();
|
|
19
|
+
|
|
20
|
+
function apiBase() {
|
|
21
|
+
return resolveApiUrl(process.env.LEXISYNC_API_URL || config.get('apiUrl'));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
program
|
|
25
|
+
.name('lexisync')
|
|
26
|
+
.description('LexiSync CLI — sync, pull, publish versioned catalogs (Locize-style)')
|
|
27
|
+
.version('0.2.0');
|
|
28
|
+
|
|
29
|
+
program
|
|
30
|
+
.command('init')
|
|
31
|
+
.description('Create a new organization and store API keys locally')
|
|
32
|
+
.option('-n, --name <name>', 'Organization name', 'My Org')
|
|
33
|
+
.option('-u, --api-url <url>', 'LexiSync API base URL')
|
|
34
|
+
.action(async (options) => {
|
|
35
|
+
try {
|
|
36
|
+
if (options.apiUrl) config.set('apiUrl', options.apiUrl);
|
|
37
|
+
console.log(`Initializing LexiSync for: ${options.name}...`);
|
|
38
|
+
|
|
39
|
+
const response = await axios.post(`${apiBase()}/api/organizations`, { name: options.name });
|
|
40
|
+
const { id, publicApiKey, apiKey } = response.data;
|
|
41
|
+
|
|
42
|
+
config.set('apiKey', apiKey);
|
|
43
|
+
config.set('publicApiKey', publicApiKey);
|
|
44
|
+
config.set('organizationId', id);
|
|
45
|
+
|
|
46
|
+
console.log('\nSuccess.');
|
|
47
|
+
console.log(`Organization ID: ${id}`);
|
|
48
|
+
console.log(`Public key: ${publicApiKey}`);
|
|
49
|
+
console.log('\nNext: create a project in the dashboard, then:');
|
|
50
|
+
console.log(' lexisync sync -p <projectId> -f ./locales/en.json -l en');
|
|
51
|
+
console.log(' lexisync publish -p <projectId> -v production');
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error(`Error: ${err.response?.data?.error || err.message}`);
|
|
54
|
+
process.exitCode = 1;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
program
|
|
59
|
+
.command('sync')
|
|
60
|
+
.description('Upload a local JSON locale file to the working copy (latest)')
|
|
61
|
+
.option('-f, --file <path>', 'Path to local JSON file', './locales.json')
|
|
62
|
+
.option('-p, --project <id>', 'Project ID (or set in config)')
|
|
63
|
+
.option('-l, --lang <code>', 'Language code', 'en')
|
|
64
|
+
.action(async (options) => {
|
|
65
|
+
const apiKey = config.get('apiKey');
|
|
66
|
+
const projectId = options.project || config.get('projectId');
|
|
67
|
+
if (!apiKey) {
|
|
68
|
+
console.error('Error: Not authenticated. Run lexisync init or set apiKey in ~/.lexisync/config.json');
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (!projectId) {
|
|
73
|
+
console.error('Error: Pass --project <id> or set projectId in config.');
|
|
74
|
+
process.exitCode = 1;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
const filePath = path.resolve(options.file);
|
|
80
|
+
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
81
|
+
console.log(`Syncing ${filePath} → project ${projectId} (${options.lang}) [latest]...`);
|
|
82
|
+
|
|
83
|
+
const response = await axios.post(
|
|
84
|
+
`${apiBase()}/api/sync`,
|
|
85
|
+
{ projectId, languageCode: options.lang, data },
|
|
86
|
+
{ headers: { 'x-api-key': apiKey } }
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
console.log('Sync complete:', response.data);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error(`Error: ${err.response?.data?.error || err.message}`);
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
program
|
|
97
|
+
.command('pull')
|
|
98
|
+
.description('Download a catalog namespace (working copy or published version)')
|
|
99
|
+
.option('-p, --project <id>', 'Project ID')
|
|
100
|
+
.option('-n, --namespace <name>', 'Namespace', 'common')
|
|
101
|
+
.option('-l, --lang <code>', 'Language code', 'en')
|
|
102
|
+
.option('-v, --version <name>', 'Catalog version (latest | production | 1.0.0)', 'latest')
|
|
103
|
+
.option('-o, --out <path>', 'Output file', './locales.json')
|
|
104
|
+
.action(async (options) => {
|
|
105
|
+
const projectId = options.project || config.get('projectId');
|
|
106
|
+
if (!projectId) {
|
|
107
|
+
console.error('Error: Pass --project <id> or set projectId in config.');
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const resource = await getTranslations({
|
|
113
|
+
apiUrl: apiBase(),
|
|
114
|
+
projectId,
|
|
115
|
+
namespace: options.namespace,
|
|
116
|
+
language: options.lang,
|
|
117
|
+
version: options.version,
|
|
118
|
+
});
|
|
119
|
+
const outPath = path.resolve(options.out);
|
|
120
|
+
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
121
|
+
fs.writeFileSync(outPath, JSON.stringify(resource, null, 2) + '\n');
|
|
122
|
+
console.log(
|
|
123
|
+
`Wrote ${Object.keys(resource).length} keys → ${outPath} (v=${options.version})`
|
|
124
|
+
);
|
|
125
|
+
console.log(
|
|
126
|
+
'CDN URL:',
|
|
127
|
+
cdnUrl({
|
|
128
|
+
apiUrl: apiBase(),
|
|
129
|
+
projectId,
|
|
130
|
+
version: options.version,
|
|
131
|
+
language: options.lang,
|
|
132
|
+
namespace: options.namespace,
|
|
133
|
+
})
|
|
134
|
+
);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
console.error(`Error: ${err.message}`);
|
|
137
|
+
process.exitCode = 1;
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
program
|
|
142
|
+
.command('publish')
|
|
143
|
+
.description('Freeze working copy into a named version (CDN-ready catalog)')
|
|
144
|
+
.option('-p, --project <id>', 'Project ID')
|
|
145
|
+
.option('-v, --version <name>', 'Version name (e.g. production, staging, 1.2.0)', 'production')
|
|
146
|
+
.action(async (options) => {
|
|
147
|
+
const apiKey = config.get('apiKey');
|
|
148
|
+
const projectId = options.project || config.get('projectId');
|
|
149
|
+
if (!apiKey || !projectId) {
|
|
150
|
+
console.error('Error: Need apiKey + projectId (init / config / flags).');
|
|
151
|
+
process.exitCode = 1;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const result = await publishVersion({
|
|
156
|
+
apiUrl: apiBase(),
|
|
157
|
+
apiKey,
|
|
158
|
+
projectId,
|
|
159
|
+
version: options.version,
|
|
160
|
+
});
|
|
161
|
+
console.log('Published:', result);
|
|
162
|
+
} catch (err) {
|
|
163
|
+
console.error(`Error: ${err.message}`);
|
|
164
|
+
process.exitCode = 1;
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
program
|
|
169
|
+
.command('versions')
|
|
170
|
+
.description('List published catalog versions for a project')
|
|
171
|
+
.option('-p, --project <id>', 'Project ID')
|
|
172
|
+
.action(async (options) => {
|
|
173
|
+
const projectId = options.project || config.get('projectId');
|
|
174
|
+
if (!projectId) {
|
|
175
|
+
console.error('Error: Pass --project <id>');
|
|
176
|
+
process.exitCode = 1;
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
const list = await listVersions({ apiUrl: apiBase(), projectId });
|
|
181
|
+
console.log(JSON.stringify(list, null, 2));
|
|
182
|
+
} catch (err) {
|
|
183
|
+
console.error(`Error: ${err.message}`);
|
|
184
|
+
process.exitCode = 1;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
program.parse(process.argv);
|
package/lib/backend.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { getTranslations, resolveApiUrl, upsertTranslation } = require('./client');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* i18next backend plugin for LexiSync (Locize-compatible workflow).
|
|
7
|
+
*
|
|
8
|
+
* Load modes:
|
|
9
|
+
* - version "latest" (default): working copy — good for development + saveMissing
|
|
10
|
+
* - version "production" | "1.0.0" | …: published CDN catalog (immutable, cacheable)
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* import LexiSyncBackend from 'lexisync';
|
|
14
|
+
* i18next.use(LexiSyncBackend).init({
|
|
15
|
+
* backend: {
|
|
16
|
+
* projectId: '…',
|
|
17
|
+
* apiKey: 'sk_…', // write / saveMissing
|
|
18
|
+
* version: 'latest', // or 'production'
|
|
19
|
+
* apiUrl: 'https://…',
|
|
20
|
+
* },
|
|
21
|
+
* saveMissing: true,
|
|
22
|
+
* });
|
|
23
|
+
*/
|
|
24
|
+
class LexiSyncBackend {
|
|
25
|
+
constructor(services, backendOptions = {}, i18nextOptions = {}) {
|
|
26
|
+
this.services = services;
|
|
27
|
+
this.options = {
|
|
28
|
+
apiUrl: backendOptions.apiUrl,
|
|
29
|
+
projectId: backendOptions.projectId,
|
|
30
|
+
apiKey: backendOptions.apiKey,
|
|
31
|
+
version: backendOptions.version || 'latest',
|
|
32
|
+
referenceLng: backendOptions.referenceLng || backendOptions.lng,
|
|
33
|
+
...backendOptions,
|
|
34
|
+
};
|
|
35
|
+
this.i18nextOptions = i18nextOptions;
|
|
36
|
+
this.type = 'backend';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
init(services, backendOptions = {}, i18nextOptions = {}) {
|
|
40
|
+
this.services = services;
|
|
41
|
+
this.options = { ...this.options, ...backendOptions };
|
|
42
|
+
if (!this.options.version) this.options.version = 'latest';
|
|
43
|
+
this.i18nextOptions = i18nextOptions;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
read(language, namespace, callback) {
|
|
47
|
+
const { projectId, apiUrl, version } = this.options;
|
|
48
|
+
if (!projectId) {
|
|
49
|
+
callback(new Error('LexiSync backend: projectId is required'), null);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getTranslations({
|
|
54
|
+
apiUrl,
|
|
55
|
+
projectId,
|
|
56
|
+
namespace,
|
|
57
|
+
language,
|
|
58
|
+
version: version || 'latest',
|
|
59
|
+
})
|
|
60
|
+
.then((data) => callback(null, data))
|
|
61
|
+
.catch((err) => callback(err, null));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** i18next saveMissing → register new keys on the working copy (requires apiKey). */
|
|
65
|
+
create(languages, namespace, key, fallbackValue) {
|
|
66
|
+
const { projectId, apiKey, apiUrl, referenceLng, version } = this.options;
|
|
67
|
+
if (!apiKey || !projectId) return;
|
|
68
|
+
// Never write into a published version via CDN path — always latest working copy
|
|
69
|
+
if (version && version !== 'latest') return;
|
|
70
|
+
|
|
71
|
+
const lng = Array.isArray(languages) ? languages[0] : languages;
|
|
72
|
+
const languageCode = lng || referenceLng || 'en';
|
|
73
|
+
const value = typeof fallbackValue === 'string' ? fallbackValue : String(fallbackValue ?? key);
|
|
74
|
+
|
|
75
|
+
upsertTranslation({
|
|
76
|
+
apiUrl: resolveApiUrl(apiUrl),
|
|
77
|
+
apiKey,
|
|
78
|
+
projectId,
|
|
79
|
+
namespace: namespace || 'default',
|
|
80
|
+
identifier: key,
|
|
81
|
+
languageCode,
|
|
82
|
+
value,
|
|
83
|
+
}).catch((err) => {
|
|
84
|
+
if (this.services?.logger?.warn) {
|
|
85
|
+
this.services.logger.warn('LexiSync saveMissing failed', err);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
LexiSyncBackend.type = 'backend';
|
|
92
|
+
|
|
93
|
+
module.exports = LexiSyncBackend;
|
|
94
|
+
module.exports.default = LexiSyncBackend;
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_API_URL = 'https://lexisync-six.vercel.app';
|
|
4
|
+
|
|
5
|
+
function resolveApiUrl(override) {
|
|
6
|
+
const raw =
|
|
7
|
+
override ||
|
|
8
|
+
(typeof process !== 'undefined' && process.env && process.env.LEXISYNC_API_URL) ||
|
|
9
|
+
DEFAULT_API_URL;
|
|
10
|
+
return String(raw).replace(/\/$/, '');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Turn LexiSync translation rows into a flat key → string map for one namespace.
|
|
15
|
+
*/
|
|
16
|
+
function rowsToResource(rows) {
|
|
17
|
+
const out = {};
|
|
18
|
+
if (!Array.isArray(rows)) return out;
|
|
19
|
+
for (const row of rows) {
|
|
20
|
+
const id = row.key?.identifier ?? row.identifier;
|
|
21
|
+
if (id) out[id] = row.value ?? '';
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Locize-style CDN path:
|
|
28
|
+
* GET {apiUrl}/api/cdn/{projectId}/{version}/{lng}/{ns}
|
|
29
|
+
* version "latest" = working copy; other versions = published immutable catalogs.
|
|
30
|
+
*/
|
|
31
|
+
function cdnUrl({ apiUrl, projectId, version, language, namespace }) {
|
|
32
|
+
const base = resolveApiUrl(apiUrl);
|
|
33
|
+
const v = encodeURIComponent(version || 'latest');
|
|
34
|
+
const lng = encodeURIComponent(language || 'en');
|
|
35
|
+
const ns = encodeURIComponent(namespace || 'default');
|
|
36
|
+
return `${base}/api/cdn/${encodeURIComponent(projectId)}/${v}/${lng}/${ns}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function getTranslations({ apiUrl, projectId, namespace, language, version }) {
|
|
40
|
+
const ver = version || 'latest';
|
|
41
|
+
// Prefer CDN endpoint (works for latest + published)
|
|
42
|
+
const url = cdnUrl({ apiUrl, projectId, version: ver, language, namespace });
|
|
43
|
+
const res = await fetch(url);
|
|
44
|
+
if (res.ok) {
|
|
45
|
+
const data = await res.json();
|
|
46
|
+
if (data && typeof data === 'object' && !Array.isArray(data) && !data.error) {
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Fallback: legacy array API (working copy only)
|
|
52
|
+
if (ver === 'latest') {
|
|
53
|
+
const base = resolveApiUrl(apiUrl);
|
|
54
|
+
const legacy = new URL(`${base}/api/translations`);
|
|
55
|
+
legacy.searchParams.set('projectId', projectId);
|
|
56
|
+
legacy.searchParams.set('language', language);
|
|
57
|
+
if (namespace) legacy.searchParams.set('namespace', namespace);
|
|
58
|
+
const r2 = await fetch(legacy);
|
|
59
|
+
if (!r2.ok) {
|
|
60
|
+
const text = await r2.text();
|
|
61
|
+
throw new Error(text || `GET translations failed (${r2.status})`);
|
|
62
|
+
}
|
|
63
|
+
const rows = await r2.json();
|
|
64
|
+
return rowsToResource(rows);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const text = await res.text();
|
|
68
|
+
throw new Error(text || `GET catalog failed (${res.status})`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function postSync({ apiUrl, apiKey, projectId, languageCode, data }) {
|
|
72
|
+
const base = resolveApiUrl(apiUrl);
|
|
73
|
+
const res = await fetch(`${base}/api/sync`, {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
headers: {
|
|
76
|
+
'Content-Type': 'application/json',
|
|
77
|
+
'x-api-key': apiKey,
|
|
78
|
+
},
|
|
79
|
+
body: JSON.stringify({ projectId, languageCode, data }),
|
|
80
|
+
});
|
|
81
|
+
if (!res.ok) {
|
|
82
|
+
const body = await res.json().catch(() => ({}));
|
|
83
|
+
throw new Error(body.error || `Sync failed (${res.status})`);
|
|
84
|
+
}
|
|
85
|
+
return res.json();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function upsertTranslation({
|
|
89
|
+
apiUrl,
|
|
90
|
+
apiKey,
|
|
91
|
+
projectId,
|
|
92
|
+
namespace,
|
|
93
|
+
identifier,
|
|
94
|
+
languageCode,
|
|
95
|
+
value,
|
|
96
|
+
}) {
|
|
97
|
+
const base = resolveApiUrl(apiUrl);
|
|
98
|
+
const res = await fetch(`${base}/api/translations`, {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
headers: {
|
|
101
|
+
'Content-Type': 'application/json',
|
|
102
|
+
'x-api-key': apiKey,
|
|
103
|
+
},
|
|
104
|
+
body: JSON.stringify({
|
|
105
|
+
projectId,
|
|
106
|
+
namespace,
|
|
107
|
+
identifier,
|
|
108
|
+
languageCode,
|
|
109
|
+
value,
|
|
110
|
+
}),
|
|
111
|
+
});
|
|
112
|
+
if (!res.ok) {
|
|
113
|
+
const body = await res.json().catch(() => ({}));
|
|
114
|
+
throw new Error(body.error || `Upsert failed (${res.status})`);
|
|
115
|
+
}
|
|
116
|
+
return res.json();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function publishVersion({ apiUrl, apiKey, projectId, version, languages }) {
|
|
120
|
+
const base = resolveApiUrl(apiUrl);
|
|
121
|
+
const res = await fetch(`${base}/api/publish`, {
|
|
122
|
+
method: 'POST',
|
|
123
|
+
headers: {
|
|
124
|
+
'Content-Type': 'application/json',
|
|
125
|
+
'x-api-key': apiKey,
|
|
126
|
+
},
|
|
127
|
+
body: JSON.stringify({ projectId, version, languages }),
|
|
128
|
+
});
|
|
129
|
+
if (!res.ok) {
|
|
130
|
+
const body = await res.json().catch(() => ({}));
|
|
131
|
+
throw new Error(body.error || `Publish failed (${res.status})`);
|
|
132
|
+
}
|
|
133
|
+
return res.json();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function listVersions({ apiUrl, projectId }) {
|
|
137
|
+
const base = resolveApiUrl(apiUrl);
|
|
138
|
+
const url = new URL(`${base}/api/publish`);
|
|
139
|
+
url.searchParams.set('projectId', projectId);
|
|
140
|
+
const res = await fetch(url);
|
|
141
|
+
if (!res.ok) {
|
|
142
|
+
const body = await res.json().catch(() => ({}));
|
|
143
|
+
throw new Error(body.error || `List versions failed (${res.status})`);
|
|
144
|
+
}
|
|
145
|
+
return res.json();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function authHeaders(apiKey) {
|
|
149
|
+
return apiKey ? { 'x-api-key': apiKey } : {};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function v1Fetch(apiUrl, path, { apiKey, method = 'GET', body } = {}) {
|
|
153
|
+
const base = resolveApiUrl(apiUrl);
|
|
154
|
+
const res = await fetch(`${base}${path}`, {
|
|
155
|
+
method,
|
|
156
|
+
headers: {
|
|
157
|
+
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
158
|
+
...authHeaders(apiKey),
|
|
159
|
+
},
|
|
160
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
161
|
+
});
|
|
162
|
+
const data = await res.json().catch(() => ({}));
|
|
163
|
+
if (!res.ok) {
|
|
164
|
+
const msg = data?.error?.message || data?.error || `Request failed (${res.status})`;
|
|
165
|
+
throw new Error(typeof msg === 'string' ? msg : JSON.stringify(msg));
|
|
166
|
+
}
|
|
167
|
+
return data;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** GET /api/v1 — discovery map for agents */
|
|
171
|
+
async function discoverApi({ apiUrl } = {}) {
|
|
172
|
+
return v1Fetch(apiUrl, '/api/v1');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function getMe({ apiUrl, apiKey }) {
|
|
176
|
+
return v1Fetch(apiUrl, '/api/v1/me', { apiKey });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function listLanguages({ apiUrl, apiKey, projectId }) {
|
|
180
|
+
return v1Fetch(apiUrl, `/api/v1/projects/${encodeURIComponent(projectId)}/languages`, { apiKey });
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function addLanguage({ apiUrl, apiKey, projectId, code, name }) {
|
|
184
|
+
return v1Fetch(apiUrl, `/api/v1/projects/${encodeURIComponent(projectId)}/languages`, {
|
|
185
|
+
apiKey,
|
|
186
|
+
method: 'POST',
|
|
187
|
+
body: { code, name },
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function downloadCatalog({ apiUrl, apiKey, projectId, version = 'latest', format = 'nested', language, namespace }) {
|
|
192
|
+
const q = new URLSearchParams({ version, format });
|
|
193
|
+
if (language) q.set('language', language);
|
|
194
|
+
if (namespace) q.set('namespace', namespace);
|
|
195
|
+
return v1Fetch(
|
|
196
|
+
apiUrl,
|
|
197
|
+
`/api/v1/projects/${encodeURIComponent(projectId)}/download?${q}`,
|
|
198
|
+
{ apiKey }
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function reportMissing({ apiUrl, apiKey, projectId, namespace = 'common', languageCode = 'en', keys }) {
|
|
203
|
+
return v1Fetch(apiUrl, `/api/v1/projects/${encodeURIComponent(projectId)}/missing`, {
|
|
204
|
+
apiKey,
|
|
205
|
+
method: 'POST',
|
|
206
|
+
body: { namespace, languageCode, keys },
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
module.exports = {
|
|
211
|
+
DEFAULT_API_URL,
|
|
212
|
+
resolveApiUrl,
|
|
213
|
+
rowsToResource,
|
|
214
|
+
cdnUrl,
|
|
215
|
+
getTranslations,
|
|
216
|
+
postSync,
|
|
217
|
+
upsertTranslation,
|
|
218
|
+
publishVersion,
|
|
219
|
+
listVersions,
|
|
220
|
+
discoverApi,
|
|
221
|
+
getMe,
|
|
222
|
+
listLanguages,
|
|
223
|
+
addLanguage,
|
|
224
|
+
downloadCatalog,
|
|
225
|
+
reportMissing,
|
|
226
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
|
|
7
|
+
const CONFIG_DIR = path.join(os.homedir(), '.lexisync');
|
|
8
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
9
|
+
|
|
10
|
+
function readStore() {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
|
|
13
|
+
} catch {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function writeStore(data) {
|
|
19
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
20
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(data, null, 2) + '\n');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function createConfig() {
|
|
24
|
+
return {
|
|
25
|
+
get(key) {
|
|
26
|
+
return readStore()[key];
|
|
27
|
+
},
|
|
28
|
+
set(key, value) {
|
|
29
|
+
const data = readStore();
|
|
30
|
+
data[key] = value;
|
|
31
|
+
writeStore(data);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = { createConfig, CONFIG_FILE };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lexisync",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "CLI and i18next backend for LexiSync — sync locale files and load translations at runtime",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Sander Post",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/sanderpost/lexisync.git",
|
|
10
|
+
"directory": "packages/lexisync"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://lexisync-six.vercel.app/docs",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/sanderpost/lexisync/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"lexisync",
|
|
18
|
+
"i18next",
|
|
19
|
+
"localization",
|
|
20
|
+
"translation",
|
|
21
|
+
"tms",
|
|
22
|
+
"cli",
|
|
23
|
+
"i18n"
|
|
24
|
+
],
|
|
25
|
+
"type": "commonjs",
|
|
26
|
+
"main": "lib/backend.js",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./lib/backend.js",
|
|
29
|
+
"./backend": "./lib/backend.js",
|
|
30
|
+
"./client": "./lib/client.js"
|
|
31
|
+
},
|
|
32
|
+
"bin": {
|
|
33
|
+
"lexisync": "bin/lexisync.js"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"registry": "https://registry.npmjs.org/"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"bin",
|
|
41
|
+
"lib",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
],
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"prepublishOnly": "node -e \"require('./lib/backend'); require('./lib/client')\""
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"i18next": ">=22.0.0"
|
|
53
|
+
},
|
|
54
|
+
"peerDependenciesMeta": {
|
|
55
|
+
"i18next": {
|
|
56
|
+
"optional": true
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"axios": "^1.18.1",
|
|
61
|
+
"commander": "^12.1.0"
|
|
62
|
+
}
|
|
63
|
+
}
|