@orxataguy/tyr 1.0.3 → 1.0.4
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/package.json +1 -1
- package/src/core/sys/config.ts +52 -27
package/package.json
CHANGED
package/src/core/sys/config.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import yaml from 'js-yaml';
|
|
3
2
|
import { homedir, platform } from 'os';
|
|
4
|
-
import { existsSync, cpSync, rmSync } from 'fs';
|
|
3
|
+
import { existsSync, cpSync, rmSync, mkdirSync, readdirSync } from 'fs';
|
|
5
4
|
import { execSync } from 'child_process';
|
|
6
5
|
import type { TyrContext } from '../Kernel';
|
|
7
6
|
|
|
@@ -13,7 +12,40 @@ function removeDirRecursive(dirPath: string): void {
|
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
function clearDirExceptLogs(dirPath: string): void {
|
|
16
|
+
if (!existsSync(dirPath)) return;
|
|
17
|
+
for (const entry of readdirSync(dirPath, { withFileTypes: true })) {
|
|
18
|
+
if (entry.name === 'logs') continue;
|
|
19
|
+
const fullPath = path.join(dirPath, entry.name);
|
|
20
|
+
if (entry.isDirectory()) {
|
|
21
|
+
removeDirRecursive(fullPath);
|
|
22
|
+
} else {
|
|
23
|
+
rmSync(fullPath, { force: true });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function copyDirContents(srcDir: string, destDir: string): void {
|
|
29
|
+
for (const entry of readdirSync(srcDir, { withFileTypes: true })) {
|
|
30
|
+
cpSync(
|
|
31
|
+
path.join(srcDir, entry.name),
|
|
32
|
+
path.join(destDir, entry.name),
|
|
33
|
+
{ recursive: true, force: true },
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function backupUserRoot(userRoot: string, backupPath: string): void {
|
|
39
|
+
mkdirSync(backupPath, { recursive: true });
|
|
40
|
+
for (const entry of readdirSync(userRoot, { withFileTypes: true })) {
|
|
41
|
+
if (entry.name === 'logs') continue;
|
|
42
|
+
cpSync(
|
|
43
|
+
path.join(userRoot, entry.name),
|
|
44
|
+
path.join(backupPath, entry.name),
|
|
45
|
+
{ recursive: true },
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
17
49
|
|
|
18
50
|
function detectShellRcFile(homeDir: string): string | null {
|
|
19
51
|
const shell = process.env.SHELL || '';
|
|
@@ -27,8 +59,6 @@ function detectShellRcFile(homeDir: string): string | null {
|
|
|
27
59
|
return fallbacks.find(p => existsSync(p)) ?? path.join(homeDir, '.bashrc');
|
|
28
60
|
}
|
|
29
61
|
|
|
30
|
-
// ─── File templates ───────────────────────────────────────────────────────────
|
|
31
|
-
|
|
32
62
|
const ENV_TEMPLATE = `# ~/.tyr/.env
|
|
33
63
|
# Variables de entorno para Tyr. Este archivo nunca debe subirse a git.
|
|
34
64
|
#
|
|
@@ -77,8 +107,6 @@ const PS_PLUGINS_TEMPLATE = `# ~/.tyr/plugins.ps1
|
|
|
77
107
|
# Import-Module PSReadLine
|
|
78
108
|
`;
|
|
79
109
|
|
|
80
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
81
|
-
|
|
82
110
|
function makeTimestamp(): string {
|
|
83
111
|
const now = new Date();
|
|
84
112
|
const pad = (n: number) => String(n).padStart(2, '0');
|
|
@@ -113,8 +141,6 @@ async function configureWindowsShell(tyrFs: any, logger: any, aliasesPath: strin
|
|
|
113
141
|
logger.info('Reinicia PowerShell para aplicar los cambios.');
|
|
114
142
|
}
|
|
115
143
|
|
|
116
|
-
// ─── Main ─────────────────────────────────────────────────────────────────────
|
|
117
|
-
|
|
118
144
|
export default function config({ logger, fs: tyrFs, frameworkRoot, shell }: TyrContext) {
|
|
119
145
|
return async (args: string[]) => {
|
|
120
146
|
const homeDir = homedir();
|
|
@@ -132,37 +158,40 @@ export default function config({ logger, fs: tyrFs, frameworkRoot, shell }: TyrC
|
|
|
132
158
|
return;
|
|
133
159
|
}
|
|
134
160
|
|
|
135
|
-
|
|
136
|
-
let backupPath: string | null = null;
|
|
161
|
+
let backupPath: string | null = null;
|
|
137
162
|
if (existsSync(userRoot)) {
|
|
138
163
|
backupPath = `${userRoot}.bak.${makeTimestamp()}`;
|
|
139
|
-
|
|
140
|
-
removeDirRecursive(userRoot);
|
|
164
|
+
backupUserRoot(userRoot, backupPath);
|
|
141
165
|
logger.warn(`Configuración anterior respaldada en: ${backupPath}`);
|
|
142
166
|
}
|
|
143
167
|
|
|
144
|
-
|
|
145
|
-
let repoHasContent = false;
|
|
168
|
+
let repoHasContent = false;
|
|
146
169
|
if (repoUrl) {
|
|
170
|
+
const tempDir = `${userRoot}.setup-temp`;
|
|
171
|
+
if (existsSync(tempDir)) {
|
|
172
|
+
try { removeDirRecursive(tempDir); } catch {}
|
|
173
|
+
}
|
|
174
|
+
|
|
147
175
|
logger.info(`\nClonando repositorio: ${repoUrl}`);
|
|
148
176
|
try {
|
|
149
|
-
await shell.exec(`git clone "${repoUrl}" "${
|
|
177
|
+
await shell.exec(`git clone "${repoUrl}" "${tempDir}"`);
|
|
150
178
|
} catch (e) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
cpSync(backupPath, userRoot, { recursive: true });
|
|
154
|
-
removeDirRecursive(backupPath);
|
|
155
|
-
logger.warn('Error al clonar. Configuración anterior restaurada.');
|
|
179
|
+
if (existsSync(tempDir)) {
|
|
180
|
+
try { removeDirRecursive(tempDir); } catch {}
|
|
156
181
|
}
|
|
157
182
|
throw e;
|
|
158
183
|
}
|
|
159
|
-
|
|
184
|
+
|
|
185
|
+
repoHasContent = existsSync(path.join(tempDir, 'map.yml'));
|
|
160
186
|
logger.success(repoHasContent
|
|
161
187
|
? 'Repositorio clonado con configuración existente.'
|
|
162
188
|
: 'Repositorio vacío — iniciando configuración por defecto...');
|
|
189
|
+
|
|
190
|
+
clearDirExceptLogs(userRoot);
|
|
191
|
+
copyDirContents(tempDir, userRoot);
|
|
192
|
+
try { removeDirRecursive(tempDir); } catch {}
|
|
163
193
|
}
|
|
164
194
|
|
|
165
|
-
// ── 3. Initialize if needed (no repo, or repo was empty) ──────────────
|
|
166
195
|
if (!repoHasContent) {
|
|
167
196
|
logger.info('\nInicializando ~/.tyr...\n');
|
|
168
197
|
|
|
@@ -181,19 +210,16 @@ export default function config({ logger, fs: tyrFs, frameworkRoot, shell }: TyrC
|
|
|
181
210
|
logger.success(`Archivo creado: ${pluginsPath}`);
|
|
182
211
|
}
|
|
183
212
|
|
|
184
|
-
// Write map.yml
|
|
185
213
|
const mapPath = path.join(userRoot, 'map.yml');
|
|
186
214
|
await tyrFs.write(mapPath, 'commands: {}\n');
|
|
187
215
|
logger.success(`Archivo creado: ${mapPath}`);
|
|
188
216
|
|
|
189
|
-
// Write .env template
|
|
190
217
|
const envPath = path.join(userRoot, '.env');
|
|
191
218
|
if (!tyrFs.exists(envPath)) {
|
|
192
219
|
await tyrFs.write(envPath, ENV_TEMPLATE);
|
|
193
220
|
logger.success(`Archivo creado: ${envPath}`);
|
|
194
221
|
}
|
|
195
222
|
|
|
196
|
-
// If linked to a repo, commit and push
|
|
197
223
|
if (repoUrl) {
|
|
198
224
|
logger.info('\nSubiendo configuración inicial al repositorio...');
|
|
199
225
|
shell.cd(userRoot);
|
|
@@ -208,7 +234,6 @@ export default function config({ logger, fs: tyrFs, frameworkRoot, shell }: TyrC
|
|
|
208
234
|
}
|
|
209
235
|
}
|
|
210
236
|
|
|
211
|
-
// ── 4. Configure shell (always) ────────────────────────────────────────
|
|
212
237
|
const aliasesPath = path.join(userRoot, `aliases${ext}`);
|
|
213
238
|
const pluginsPath = path.join(userRoot, `plugins${ext}`);
|
|
214
239
|
|