@localheroai/cli 0.0.5 → 0.0.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.
Files changed (116) hide show
  1. package/{README → README.md} +28 -10
  2. package/dist/api/auth.d.ts +2 -0
  3. package/dist/api/auth.js +28 -0
  4. package/dist/api/auth.js.map +1 -0
  5. package/dist/api/client.d.ts +3 -0
  6. package/dist/api/client.js +80 -0
  7. package/dist/api/client.js.map +1 -0
  8. package/dist/api/imports.d.ts +5 -0
  9. package/dist/api/imports.js +43 -0
  10. package/dist/api/imports.js.map +1 -0
  11. package/dist/api/projects.d.ts +2 -0
  12. package/dist/api/projects.js +42 -0
  13. package/dist/api/projects.js.map +1 -0
  14. package/dist/api/translations.d.ts +15 -0
  15. package/dist/api/translations.js +71 -0
  16. package/dist/api/translations.js.map +1 -0
  17. package/dist/cli.d.ts +2 -0
  18. package/dist/cli.js +79 -0
  19. package/dist/cli.js.map +1 -0
  20. package/dist/commands/_sync.js +22 -0
  21. package/dist/commands/_sync.js.map +1 -0
  22. package/dist/commands/_translate.js +3 -0
  23. package/dist/commands/_translate.js.map +1 -0
  24. package/dist/commands/init.d.ts +1 -0
  25. package/dist/commands/init.js +439 -0
  26. package/dist/commands/init.js.map +1 -0
  27. package/dist/commands/login.d.ts +16 -0
  28. package/dist/commands/login.js +58 -0
  29. package/dist/commands/login.js.map +1 -0
  30. package/dist/commands/pull.js +22 -0
  31. package/dist/commands/pull.js.map +1 -0
  32. package/dist/commands/push.js +56 -0
  33. package/dist/commands/push.js.map +1 -0
  34. package/dist/commands/sync.d.ts +20 -0
  35. package/dist/commands/sync.js +22 -0
  36. package/dist/commands/sync.js.map +1 -0
  37. package/dist/commands/translate.d.ts +14 -0
  38. package/dist/commands/translate.js +152 -0
  39. package/dist/commands/translate.js.map +1 -0
  40. package/dist/index.d.ts +5 -0
  41. package/dist/index.js +8 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/types/index.d.ts +75 -0
  44. package/dist/types/index.js +17 -0
  45. package/dist/types/index.js.map +1 -0
  46. package/dist/types/translate/index.js +2 -0
  47. package/dist/types/translate/index.js.map +1 -0
  48. package/dist/utils/auth.d.ts +2 -0
  49. package/dist/utils/auth.js +29 -0
  50. package/dist/utils/auth.js.map +1 -0
  51. package/dist/utils/common.js +9 -0
  52. package/dist/utils/common.js.map +1 -0
  53. package/dist/utils/config.d.ts +23 -0
  54. package/dist/utils/config.js +137 -0
  55. package/dist/utils/config.js.map +1 -0
  56. package/dist/utils/errors.js +37 -0
  57. package/dist/utils/errors.js.map +1 -0
  58. package/dist/utils/files.d.ts +32 -0
  59. package/dist/utils/files.js +355 -0
  60. package/dist/utils/files.js.map +1 -0
  61. package/dist/utils/git.d.ts +21 -0
  62. package/dist/utils/git.js +87 -0
  63. package/dist/utils/git.js.map +1 -0
  64. package/dist/utils/github.d.ts +241 -0
  65. package/dist/utils/github.js +161 -0
  66. package/dist/utils/github.js.map +1 -0
  67. package/dist/utils/import-service.d.ts +4 -0
  68. package/dist/utils/import-service.js +218 -0
  69. package/dist/utils/import-service.js.map +1 -0
  70. package/dist/utils/prompt-service.d.ts +44 -0
  71. package/dist/utils/prompt-service.js +104 -0
  72. package/dist/utils/prompt-service.js.map +1 -0
  73. package/dist/utils/sync-service.d.ts +58 -0
  74. package/dist/utils/sync-service.js +159 -0
  75. package/dist/utils/sync-service.js.map +1 -0
  76. package/dist/utils/translation-processor.js +197 -0
  77. package/dist/utils/translation-processor.js.map +1 -0
  78. package/dist/utils/translation-updater/common.d.ts +6 -0
  79. package/{src → dist}/utils/translation-updater/common.js +16 -10
  80. package/dist/utils/translation-updater/common.js.map +1 -0
  81. package/dist/utils/translation-updater/index.d.ts +5 -0
  82. package/{src → dist}/utils/translation-updater/index.js +21 -9
  83. package/dist/utils/translation-updater/index.js.map +1 -0
  84. package/dist/utils/translation-updater/json-handler.d.ts +5 -0
  85. package/dist/utils/translation-updater/json-handler.js +123 -0
  86. package/dist/utils/translation-updater/json-handler.js.map +1 -0
  87. package/dist/utils/translation-updater/yaml-handler.d.ts +5 -0
  88. package/dist/utils/translation-updater/yaml-handler.js +193 -0
  89. package/dist/utils/translation-updater/yaml-handler.js.map +1 -0
  90. package/dist/utils/translation-utils.d.ts +30 -0
  91. package/dist/utils/translation-utils.js +334 -0
  92. package/dist/utils/translation-utils.js.map +1 -0
  93. package/dist/utils/updater.js +38 -0
  94. package/dist/utils/updater.js.map +1 -0
  95. package/package.json +26 -26
  96. package/src/api/auth.js +0 -24
  97. package/src/api/client.js +0 -83
  98. package/src/api/imports.js +0 -22
  99. package/src/api/projects.js +0 -24
  100. package/src/api/translations.js +0 -58
  101. package/src/cli.js +0 -78
  102. package/src/commands/init.js +0 -485
  103. package/src/commands/login.js +0 -80
  104. package/src/commands/sync.js +0 -28
  105. package/src/commands/translate.js +0 -267
  106. package/src/utils/auth.js +0 -23
  107. package/src/utils/config.js +0 -125
  108. package/src/utils/files.js +0 -381
  109. package/src/utils/git.js +0 -72
  110. package/src/utils/github.js +0 -128
  111. package/src/utils/import-service.js +0 -128
  112. package/src/utils/prompt-service.js +0 -67
  113. package/src/utils/sync-service.js +0 -147
  114. package/src/utils/translation-updater/json-handler.js +0 -111
  115. package/src/utils/translation-updater/yaml-handler.js +0 -207
  116. package/src/utils/translation-utils.js +0 -278
@@ -1,67 +0,0 @@
1
- export function createPromptService(deps = {}) {
2
- const { inquirer = null } = deps;
3
-
4
- return {
5
- async getApiKey() {
6
- if (!inquirer) return '';
7
- return inquirer.password({
8
- message: 'API Key:',
9
- mask: '*'
10
- });
11
- },
12
-
13
- async getProjectSetup() {
14
- if (!inquirer) return {};
15
-
16
- return {
17
- projectName: '',
18
- sourceLocale: '',
19
- outputLocales: [],
20
- translationPath: '',
21
- ignorePaths: []
22
- };
23
- },
24
-
25
- async select(options) {
26
- if (!inquirer) return 'new';
27
- return inquirer.select(options);
28
- },
29
-
30
- async input(options) {
31
- if (!inquirer) return '';
32
- return inquirer.input(options);
33
- },
34
-
35
- async confirm(options) {
36
- if (!inquirer) return false;
37
- return inquirer.confirm(options);
38
- },
39
-
40
- async selectProject(projectService) {
41
- const projects = await projectService.listProjects();
42
-
43
- if (!projects || projects.length === 0) {
44
- return { choice: 'new' };
45
- }
46
-
47
- const choices = [
48
- { name: '✨ Create new project', value: 'new' },
49
- { name: '─────────────', value: 'separator', disabled: true },
50
- ...projects.map(p => ({
51
- name: p.name,
52
- value: p.id
53
- }))
54
- ];
55
-
56
- const projectChoice = await this.select({
57
- message: 'Would you like to use an existing project or create a new one?',
58
- choices
59
- });
60
-
61
- return {
62
- choice: projectChoice,
63
- project: projects.find(p => p.id === projectChoice)
64
- };
65
- }
66
- };
67
- }
@@ -1,147 +0,0 @@
1
- import chalk from 'chalk';
2
- import { configService } from './config.js';
3
- import { getUpdates } from '../api/translations.js';
4
- import { updateTranslationFile, deleteKeysFromTranslationFile } from './translation-updater/index.js';
5
- import { findTranslationFiles } from './files.js';
6
-
7
- const MAX_PAGES = 20;
8
-
9
- export const syncService = {
10
- async checkForUpdates({ verbose = false } = {}) {
11
- const config = await configService.getValidProjectConfig();
12
-
13
- if (!config.projectId) {
14
- throw new Error('Project not initialized. Please run `localhero init` first.');
15
- }
16
-
17
- const since = config.lastSyncedAt || new Date(0).toISOString();
18
-
19
- if (verbose) {
20
- console.log(chalk.blue(`Checking for updates since ${since}`));
21
- }
22
-
23
- let allFiles = [];
24
- let deletedKeys = [];
25
- let currentPage = 1;
26
- let hasMorePages = true;
27
-
28
- while (hasMorePages && currentPage <= MAX_PAGES) {
29
- const response = await getUpdates(config.projectId, { since, page: currentPage });
30
-
31
- if (response.updates?.updated_keys?.length) {
32
- allFiles = allFiles.concat(response.updates.updated_keys);
33
- }
34
-
35
- if (response.updates?.deleted_keys?.length) {
36
- deletedKeys = deletedKeys.concat(response.updates.deleted_keys);
37
- }
38
-
39
- if (response.pagination) {
40
- const { current_page, total_pages } = response.pagination;
41
- hasMorePages = current_page < total_pages;
42
- currentPage++;
43
-
44
- if (verbose && hasMorePages) {
45
- if (total_pages > MAX_PAGES) {
46
- console.log(chalk.yellow(` ⚠️ Limiting to ${MAX_PAGES} pages out of ${total_pages} total`));
47
- } else {
48
- console.log(chalk.gray(` Fetching page ${currentPage} of ${total_pages}`));
49
- }
50
- }
51
- } else {
52
- hasMorePages = false;
53
- }
54
- }
55
-
56
- if (!allFiles.length && !deletedKeys.length) {
57
- if (verbose) {
58
- console.log(chalk.green('✓ All translations are up to date'));
59
- }
60
- return { hasUpdates: false };
61
- }
62
-
63
- return {
64
- hasUpdates: true,
65
- updates: {
66
- updates: {
67
- files: allFiles,
68
- deleted_keys: deletedKeys
69
- }
70
- }
71
- };
72
- },
73
-
74
- async applyUpdates(updates, { verbose = false } = {}) {
75
- let totalUpdates = 0;
76
- let totalDeleted = 0;
77
- for (const file of updates.updates.files || []) {
78
- for (const lang of file.languages) {
79
- if (verbose) {
80
- console.log(chalk.blue(`Updating ${lang.code} translations in ${file.path}`));
81
- }
82
-
83
- const translations = {};
84
- for (const translation of lang.translations) {
85
- translations[translation.key] = translation.value;
86
- if (verbose) {
87
- const displayValue = translation.value.length > 100 ? `${translation.value.slice(0, 100)}…` : translation.value;
88
- console.log(chalk.gray(` ${translation.key} = ${displayValue}`));
89
- }
90
- }
91
-
92
- try {
93
- await updateTranslationFile(file.path, translations, lang.code);
94
- totalUpdates += Object.keys(translations).length;
95
- } catch (error) {
96
- console.error(chalk.yellow(`⚠️ Failed to update ${file.path}: ${error.message}`));
97
- }
98
- }
99
- }
100
- const deletedKeys = updates.updates.deleted_keys || [];
101
- if (deletedKeys.length > 0) {
102
- if (verbose) {
103
- console.log(chalk.blue(`\nProcessing ${deletedKeys.length} deleted keys`));
104
- }
105
- const config = await configService.getValidProjectConfig();
106
- const translationFiles = await findTranslationFiles(config, {
107
- parseContent: false,
108
- includeContent: false,
109
- extractKeys: false,
110
- verbose
111
- });
112
-
113
- if (verbose) {
114
- console.log(chalk.blue(`Found ${translationFiles.length} translation files to check for deleted keys`));
115
- }
116
- const keysToDelete = deletedKeys.map(key => key.name);
117
- for (const file of translationFiles) {
118
- try {
119
- if (verbose) {
120
- console.log(chalk.blue(`Checking for deleted keys in ${file.path} (${file.locale})`));
121
- }
122
-
123
- const deletedFromFile = await deleteKeysFromTranslationFile(file.path, keysToDelete, file.locale);
124
-
125
- if (deletedFromFile.length > 0) {
126
- totalDeleted += deletedFromFile.length;
127
-
128
- if (verbose) {
129
- console.log(chalk.green(`✓ Deleted ${deletedFromFile.length} keys from ${file.path}`));
130
- for (const key of deletedFromFile) {
131
- console.log(chalk.gray(` - ${key}`));
132
- }
133
- }
134
- } else if (verbose) {
135
- console.log(chalk.gray(` No keys to delete in ${file.path}`));
136
- }
137
- } catch (error) {
138
- console.error(chalk.yellow(`⚠️ Failed to delete keys from ${file.path}: ${error.message}`));
139
- }
140
- }
141
- }
142
-
143
- await configService.updateLastSyncedAt();
144
-
145
- return { totalUpdates, totalDeleted };
146
- }
147
- };
@@ -1,111 +0,0 @@
1
- import { promises as fs } from 'fs';
2
- import { detectJsonFormat, preserveJsonStructure } from '../files.js';
3
- import { ensureDirectoryExists } from './common.js';
4
-
5
- export async function updateJsonFile(filePath, translations, languageCode) {
6
- try {
7
- let existingContent = {};
8
- let jsonFormat = 'nested';
9
- let hasLanguageWrapper = false;
10
- const result = {
11
- updatedKeys: Object.keys(translations),
12
- created: false
13
- };
14
-
15
- try {
16
- const content = await fs.readFile(filePath, 'utf8');
17
- existingContent = JSON.parse(content);
18
- if (existingContent[languageCode] && typeof existingContent[languageCode] === 'object') {
19
- hasLanguageWrapper = true;
20
- jsonFormat = detectJsonFormat(existingContent[languageCode]);
21
- } else {
22
- jsonFormat = detectJsonFormat(existingContent);
23
- }
24
- } catch {
25
- console.warn(`Creating new JSON file: ${filePath}`);
26
- result.created = true;
27
- await ensureDirectoryExists(filePath);
28
- }
29
-
30
- let updatedContent;
31
-
32
- if (result.created) {
33
- updatedContent = {
34
- [languageCode]: preserveJsonStructure({}, translations, jsonFormat)
35
- };
36
- } else if (hasLanguageWrapper) {
37
- existingContent[languageCode] = existingContent[languageCode] || {};
38
- updatedContent = JSON.parse(JSON.stringify(existingContent));
39
- const mergedContent = preserveJsonStructure(
40
- existingContent[languageCode],
41
- translations,
42
- jsonFormat
43
- );
44
- updatedContent[languageCode] = mergedContent;
45
- } else {
46
- const existingCopy = JSON.parse(JSON.stringify(existingContent));
47
- updatedContent = preserveJsonStructure(existingCopy, translations, jsonFormat);
48
- }
49
-
50
- await fs.writeFile(filePath, JSON.stringify(updatedContent, null, 2));
51
- return result;
52
- } catch (error) {
53
- throw new Error(`Failed to update JSON file ${filePath}: ${error.message}`);
54
- }
55
- }
56
-
57
- export async function deleteKeysFromJsonFile(filePath, keysToDelete, languageCode) {
58
- try {
59
- const content = await fs.readFile(filePath, 'utf8');
60
- let jsonContent = JSON.parse(content);
61
- let hasLanguageWrapper = false;
62
- let rootContent = jsonContent;
63
- if (jsonContent[languageCode] && typeof jsonContent[languageCode] === 'object') {
64
- hasLanguageWrapper = true;
65
- rootContent = jsonContent[languageCode];
66
- }
67
-
68
- const deletedKeys = [];
69
-
70
- for (const keyPath of keysToDelete) {
71
- const keys = keyPath.split('.');
72
- const lastIndex = keys.length - 1;
73
- let current = rootContent;
74
- let parent = null;
75
- let keyInParent = '';
76
- let found = true;
77
-
78
- for (let i = 0; i < lastIndex; i++) {
79
- const key = keys[i];
80
- if (!current[key] || typeof current[key] !== 'object') {
81
- found = false;
82
- break;
83
- }
84
- parent = current;
85
- keyInParent = key;
86
- current = current[key];
87
- }
88
-
89
- if (found) {
90
- const lastKey = keys[lastIndex];
91
- if (current[lastKey] !== undefined) {
92
- delete current[lastKey];
93
- deletedKeys.push(keyPath);
94
- if (parent && Object.keys(current).length === 0) {
95
- delete parent[keyInParent];
96
- }
97
- }
98
- }
99
- }
100
- if (hasLanguageWrapper) {
101
- jsonContent[languageCode] = rootContent;
102
- } else {
103
- jsonContent = rootContent;
104
- }
105
- await fs.writeFile(filePath, JSON.stringify(jsonContent, null, 2));
106
-
107
- return deletedKeys;
108
- } catch (error) {
109
- throw new Error(`Failed to delete keys from JSON file ${filePath}: ${error.message}`);
110
- }
111
- }
@@ -1,207 +0,0 @@
1
- import { promises as fs } from 'fs';
2
- import yaml from 'yaml';
3
- import { SPECIAL_CHARS_REGEX, INTERPOLATION, fileExists, tryParseJsonArray } from './common.js';
4
-
5
- const NEEDS_QUOTES_REGEX = /[:,%{}[\]|><!&*?-]/;
6
- const LINE_WIDTH = 80;
7
-
8
- function detectYamlOptions(content) {
9
- const lines = content
10
- .split('\n')
11
- .filter(line => line.trim())
12
- .slice(0, 10);
13
-
14
- const options = {
15
- indent: 2,
16
- indentSeq: true
17
- };
18
-
19
- const indentMatch = lines.find(line => /^\s+\S/.test(line))?.match(/^(\s+)\S/);
20
- if (indentMatch) {
21
- options.indent = indentMatch[1].length;
22
- if (indentMatch[1].includes('\t')) {
23
- options.indent = 2;
24
- }
25
- }
26
-
27
- const seqMatch = lines.find(line => /^\s*-\s+\S/.test(line));
28
- if (seqMatch) {
29
- options.indentSeq = /^\s+-\s+/.test(seqMatch);
30
- }
31
-
32
- return options;
33
- }
34
-
35
- function needsQuotes(str) {
36
- if (typeof str !== 'string') return false;
37
-
38
- return (
39
- SPECIAL_CHARS_REGEX.test(str) ||
40
- str.includes(INTERPOLATION) ||
41
- NEEDS_QUOTES_REGEX.test(str) ||
42
- (str.includes(' ') && /[:"']/g.test(str))
43
- );
44
- }
45
-
46
- function shouldForceQuotes(str) {
47
- if (typeof str !== 'string') return false;
48
-
49
- // Special case: strings containing quotes but no interpolation
50
- // don't need outer quotes
51
- if (str.includes('"') && !str.includes(INTERPOLATION)) {
52
- return false;
53
- }
54
-
55
- return needsQuotes(str);
56
- }
57
-
58
- function processArrayItems(array, yamlDoc) {
59
- return array.map(item => {
60
- const itemNode = yamlDoc.createNode(item);
61
- if (shouldForceQuotes(item)) {
62
- itemNode.type = 'QUOTE_DOUBLE';
63
- }
64
- return itemNode;
65
- });
66
- }
67
-
68
- async function createYamlDocument(filePath) {
69
- const exists = await fileExists(filePath);
70
- if (!exists) {
71
- console.warn(`Creating new file: ${filePath}`);
72
- const doc = new yaml.Document();
73
- doc.contents = doc.createNode({});
74
- return { doc, created: true, options: { indent: 2, indentSeq: true, lineWidth: LINE_WIDTH } };
75
- }
76
-
77
- const content = await fs.readFile(filePath, 'utf8');
78
- const options = detectYamlOptions(content);
79
- const doc = yaml.parseDocument(content);
80
- doc.options.lineWidth = LINE_WIDTH;
81
- return {
82
- doc,
83
- created: false,
84
- options
85
- };
86
- }
87
-
88
- async function updateYamlTranslations(yamlDoc, translations, languageCode) {
89
- if (!yamlDoc.contents) {
90
- yamlDoc.contents = yamlDoc.createNode({});
91
- }
92
-
93
- const rootNode = yamlDoc.contents;
94
- if (!rootNode.has(languageCode)) {
95
- rootNode.set(languageCode, yamlDoc.createNode({}));
96
- }
97
-
98
- const langNode = rootNode.get(languageCode);
99
-
100
- for (const [keyPath, newValue] of Object.entries(translations)) {
101
- const keys = keyPath.split('.');
102
- let current = langNode;
103
-
104
- for (let i = 0; i < keys.length - 1; i++) {
105
- const key = keys[i];
106
- if (!current.has(key)) {
107
- current.set(key, yamlDoc.createNode({}));
108
- }
109
- current = current.get(key);
110
- }
111
-
112
- const lastKey = keys[keys.length - 1];
113
-
114
- if (Array.isArray(newValue)) {
115
- const arrayNode = new yaml.YAMLSeq();
116
- processArrayItems(newValue, yamlDoc).forEach(item => arrayNode.add(item));
117
- current.set(lastKey, arrayNode);
118
- continue;
119
- }
120
-
121
- const array = tryParseJsonArray(newValue);
122
- if (array) {
123
- const arrayNode = new yaml.YAMLSeq();
124
- processArrayItems(array, yamlDoc).forEach(item => arrayNode.add(item));
125
- current.set(lastKey, arrayNode);
126
- continue;
127
- }
128
-
129
- if (typeof newValue === 'string' && newValue.includes('\n')) {
130
- const scalar = new yaml.Scalar(newValue);
131
- scalar.type = 'BLOCK_LITERAL';
132
- current.set(lastKey, scalar);
133
- continue;
134
- }
135
-
136
- const node = yamlDoc.createNode(newValue);
137
- if (needsQuotes(newValue)) {
138
- node.type = 'QUOTE_DOUBLE';
139
- }
140
- current.set(lastKey, node);
141
- }
142
- }
143
-
144
- export async function updateYamlFile(filePath, translations, languageCode) {
145
- const { doc: yamlDoc, created, options } = await createYamlDocument(filePath);
146
-
147
- await updateYamlTranslations(yamlDoc, translations, languageCode);
148
-
149
- yamlDoc.options.indent = options.indent;
150
- yamlDoc.options.indentSeq = options.indentSeq;
151
- yamlDoc.options.lineWidth = LINE_WIDTH;
152
-
153
- await fs.writeFile(filePath, yamlDoc.toString());
154
- return {
155
- updatedKeys: Object.keys(translations),
156
- created
157
- };
158
- }
159
-
160
- export async function deleteKeysFromYamlFile(filePath, keysToDelete, languageCode) {
161
- try {
162
- const content = await fs.readFile(filePath, 'utf8');
163
- const yamlDoc = yaml.parseDocument(content);
164
- if (!yamlDoc.contents || !yamlDoc.contents.has(languageCode)) {
165
- return [];
166
- }
167
-
168
- const langNode = yamlDoc.contents.get(languageCode);
169
- const deletedKeys = [];
170
-
171
- for (const keyPath of keysToDelete) {
172
- const keys = keyPath.split('.');
173
- const lastIndex = keys.length - 1;
174
- let current = langNode;
175
- let parent = null;
176
- let keyInParent = '';
177
- let found = true;
178
-
179
- for (let i = 0; i < lastIndex; i++) {
180
- const key = keys[i];
181
- if (!current.has(key)) {
182
- found = false;
183
- break;
184
- }
185
- parent = current;
186
- keyInParent = key;
187
- current = current.get(key);
188
- }
189
-
190
- if (found) {
191
- const lastKey = keys[lastIndex];
192
- if (current.has(lastKey)) {
193
- current.delete(lastKey);
194
- deletedKeys.push(keyPath);
195
- if (parent && current.items.length === 0) {
196
- parent.delete(keyInParent);
197
- }
198
- }
199
- }
200
- }
201
-
202
- await fs.writeFile(filePath, yamlDoc.toString());
203
- return deletedKeys;
204
- } catch (error) {
205
- throw new Error(`Failed to delete keys from YAML file ${filePath}: ${error.message}`);
206
- }
207
- }