@localheroai/cli 0.0.2 → 0.0.3

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.
@@ -1,154 +0,0 @@
1
- import { promises as fs } from 'fs';
2
- import path from 'path';
3
- import yaml from 'yaml';
4
-
5
- function getExistingQuoteStyles(content) {
6
- const styles = new Map();
7
-
8
- // Pre-split lines and get non-empty, non-comment lines
9
- const lines = content.match(/[^\n]+/g) || [];
10
- let currentPath = new Array(10); // Pre-allocate array with reasonable size
11
- let pathLength = 0;
12
-
13
- // Regex patterns - compile once
14
- const indentRegex = /^\s*/;
15
- const keyValueRegex = /^([^:]+):\s*(.*)$/;
16
- const doubleQuoteRegex = /^"(.*)"$/;
17
- const singleQuoteRegex = /^'(.*)'$/;
18
-
19
- for (let i = 0; i < lines.length; i++) {
20
- const line = lines[i];
21
- if (!line || line.trim().startsWith('#')) continue;
22
-
23
- // Calculate indent level
24
- const indent = line.match(indentRegex)[0].length;
25
- const level = indent >> 1; // Divide by 2 using bit shift
26
-
27
- // Adjust current path
28
- pathLength = level;
29
-
30
- // Extract key and value
31
- const match = line.trim().match(keyValueRegex);
32
- if (match) {
33
- const key = match[1].trim();
34
- const value = match[2];
35
-
36
- currentPath[level] = key;
37
-
38
- // Only process if there's a value
39
- if (value) {
40
- // Build path string only when needed
41
- const fullPath = currentPath.slice(0, pathLength + 1).join('.');
42
-
43
- // Check quote style
44
- const valueTrimed = value.trim();
45
- const hasDoubleQuotes = doubleQuoteRegex.test(valueTrimed);
46
- const hasSingleQuotes = !hasDoubleQuotes && singleQuoteRegex.test(valueTrimed);
47
-
48
- if (hasDoubleQuotes || hasSingleQuotes || valueTrimed) {
49
- styles.set(fullPath, {
50
- quoted: hasDoubleQuotes || hasSingleQuotes,
51
- quoteType: hasDoubleQuotes ? '"' : (hasSingleQuotes ? "'" : ''),
52
- originalValue: valueTrimed
53
- });
54
- }
55
- }
56
- }
57
- }
58
-
59
- return styles;
60
- }
61
-
62
- // Cache for repeated string operations
63
- const SPECIAL_CHARS_REGEX = /[:@#,\[\]{}?|>&*!\n]/;
64
- const INTERPOLATION = '%{';
65
- const INDENT_CACHE = new Map();
66
-
67
- function getIndent(level) {
68
- let indent = INDENT_CACHE.get(level);
69
- if (!indent) {
70
- indent = ' '.repeat(level);
71
- INDENT_CACHE.set(level, indent);
72
- }
73
- return indent;
74
- }
75
-
76
- function stringifyYaml(obj, indent = 0, parentPath = '', result = []) {
77
- const indentStr = getIndent(indent);
78
-
79
- for (const [key, value] of Object.entries(obj)) {
80
- const currentPath = parentPath ? `${parentPath}.${key}` : key;
81
-
82
- if (value && typeof value === 'object') {
83
- result.push(`${indentStr}${key}:`);
84
- stringifyYaml(value, indent + 2, currentPath, result);
85
- } else {
86
- let formattedValue = value;
87
-
88
- if (typeof value === 'string') {
89
- const existingStyle = existingStyles.get(currentPath);
90
-
91
- if (existingStyle?.quoted) {
92
- formattedValue = `${existingStyle.quoteType}${value}${existingStyle.quoteType}`;
93
- } else if (existingStyle?.originalValue === value) {
94
- formattedValue = existingStyle.originalValue;
95
- } else if (value.includes(INTERPOLATION) || SPECIAL_CHARS_REGEX.test(value)) {
96
- formattedValue = `"${value}"`;
97
- }
98
- }
99
-
100
- result.push(`${indentStr}${key}: ${formattedValue}`);
101
- }
102
- }
103
-
104
- return result;
105
- }
106
-
107
- // Store styles globally to avoid passing as parameter
108
- let existingStyles;
109
-
110
- export async function updateTranslationFile(filePath, translations, languageCode) {
111
- try {
112
- if (path.extname(filePath).slice(1) === 'json') {
113
- // Handle JSON (existing code)
114
- return;
115
- }
116
-
117
- let existingContent = '';
118
-
119
- try {
120
- existingContent = await fs.readFile(filePath, 'utf8');
121
- existingStyles = getExistingQuoteStyles(existingContent);
122
- } catch (error) {
123
- console.warn(`Creating new file: ${filePath}`);
124
- existingStyles = new Map();
125
- }
126
-
127
- // Parse existing content
128
- const yamlContent = yaml.parse(existingContent) || {};
129
- yamlContent[languageCode] = yamlContent[languageCode] || {};
130
-
131
- // Update translations efficiently
132
- for (const [keyPath, newValue] of Object.entries(translations)) {
133
- const keys = keyPath.split('.');
134
- let current = yamlContent[languageCode];
135
- const lastIndex = keys.length - 1;
136
-
137
- for (let i = 0; i < lastIndex; i++) {
138
- const key = keys[i];
139
- current[key] = current[key] || {};
140
- current = current[key];
141
- }
142
-
143
- current[keys[lastIndex]] = newValue;
144
- }
145
-
146
- const content = stringifyYaml(yamlContent);
147
-
148
- await fs.writeFile(filePath, content.join('\n'));
149
- return Object.keys(translations);
150
-
151
- } catch (error) {
152
- throw new Error(`Failed to update translation file ${filePath}: ${error.message}`);
153
- }
154
- }