@pipelab/core-node 1.0.0-beta.2 → 1.0.0-beta.21

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 (45) hide show
  1. package/.oxfmtrc.json +1 -1
  2. package/CHANGELOG.md +164 -0
  3. package/dist/config-Bi0ORcTK.mjs +2 -0
  4. package/dist/config-CFgGRD9U.mjs +265 -0
  5. package/dist/config-CFgGRD9U.mjs.map +1 -0
  6. package/dist/index.d.mts +74 -53
  7. package/dist/index.d.mts.map +1 -1
  8. package/dist/index.mjs +1705 -576
  9. package/dist/index.mjs.map +1 -1
  10. package/package.json +9 -5
  11. package/scratch/simulate-updates.ts +120 -0
  12. package/src/config.test.ts +232 -0
  13. package/src/config.ts +111 -50
  14. package/src/context.ts +117 -5
  15. package/src/handler-func.ts +2 -77
  16. package/src/handlers/build-history.ts +60 -90
  17. package/src/handlers/config.ts +265 -55
  18. package/src/handlers/engine.ts +32 -35
  19. package/src/handlers/history.ts +0 -40
  20. package/src/handlers/index.ts +4 -0
  21. package/src/handlers/migration.ts +621 -0
  22. package/src/handlers/plugins.ts +305 -0
  23. package/src/handlers/system.ts +7 -2
  24. package/src/index.ts +9 -2
  25. package/src/plugins-registry.ts +280 -38
  26. package/src/presets/c3toSteam.ts +13 -7
  27. package/src/presets/demo.ts +9 -9
  28. package/src/presets/list.ts +0 -6
  29. package/src/presets/moreToCome.ts +3 -2
  30. package/src/presets/newProject.ts +3 -2
  31. package/src/presets/test-c3-offline.ts +15 -6
  32. package/src/presets/test-c3-unzip.ts +19 -8
  33. package/src/runner.ts +2 -8
  34. package/src/server.ts +45 -4
  35. package/src/types/runner.ts +2 -30
  36. package/src/utils/fs-extras.ts +6 -24
  37. package/src/utils/github.test.ts +211 -0
  38. package/src/utils/github.ts +90 -10
  39. package/src/utils/remote.test.ts +209 -0
  40. package/src/utils/remote.ts +325 -87
  41. package/src/utils/storage.ts +2 -1
  42. package/src/utils.ts +20 -24
  43. package/src/migrations.ts +0 -72
  44. package/src/presets/if.ts +0 -69
  45. package/src/presets/loop.ts +0 -65
package/.oxfmtrc.json CHANGED
@@ -1,3 +1,3 @@
1
1
  {
2
- "ignorePatterns": ["dist", "out", "node_modules", "bin"]
2
+ "ignorePatterns": ["dist", "out", "node_modules", "bin", "**/*.d.ts"]
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,169 @@
1
1
  # @pipelab/core-node
2
2
 
3
+ ## 1.0.0-beta.21
4
+
5
+ ### Patch Changes
6
+
7
+ - sd
8
+ - Updated dependencies
9
+ - @pipelab/constants@1.0.0-beta.19
10
+ - @pipelab/shared@1.0.0-beta.17
11
+
12
+ ## 1.0.0-beta.20
13
+
14
+ ### Patch Changes
15
+
16
+ - df
17
+ - Updated dependencies
18
+ - @pipelab/constants@1.0.0-beta.18
19
+ - @pipelab/shared@1.0.0-beta.16
20
+
21
+ ## 1.0.0-beta.19
22
+
23
+ ### Patch Changes
24
+
25
+ - sdsd
26
+ - Updated dependencies
27
+ - @pipelab/constants@1.0.0-beta.17
28
+ - @pipelab/shared@1.0.0-beta.15
29
+
30
+ ## 1.0.0-beta.18
31
+
32
+ ### Patch Changes
33
+
34
+ - sd
35
+ - Updated dependencies
36
+ - @pipelab/constants@1.0.0-beta.16
37
+ - @pipelab/shared@1.0.0-beta.14
38
+
39
+ ## 1.0.0-beta.17
40
+
41
+ ### Patch Changes
42
+
43
+ - sd
44
+ - Updated dependencies
45
+ - @pipelab/constants@1.0.0-beta.15
46
+ - @pipelab/shared@1.0.0-beta.13
47
+
48
+ ## 1.0.0-beta.16
49
+
50
+ ### Patch Changes
51
+
52
+ - qs
53
+ - Updated dependencies
54
+ - @pipelab/constants@1.0.0-beta.14
55
+ - @pipelab/shared@1.0.0-beta.12
56
+
57
+ ## 1.0.0-beta.15
58
+
59
+ ### Patch Changes
60
+
61
+ - sd
62
+ - Updated dependencies
63
+ - @pipelab/constants@1.0.0-beta.13
64
+ - @pipelab/shared@1.0.0-beta.11
65
+
66
+ ## 1.0.0-beta.14
67
+
68
+ ### Patch Changes
69
+
70
+ - s
71
+ - Updated dependencies
72
+ - @pipelab/constants@1.0.0-beta.12
73
+ - @pipelab/shared@1.0.0-beta.10
74
+
75
+ ## 1.0.0-beta.13
76
+
77
+ ### Patch Changes
78
+
79
+ - fg
80
+ - Updated dependencies
81
+ - @pipelab/constants@1.0.0-beta.11
82
+ - @pipelab/shared@1.0.0-beta.9
83
+
84
+ ## 1.0.0-beta.12
85
+
86
+ ### Patch Changes
87
+
88
+ - changes
89
+ - Updated dependencies
90
+ - @pipelab/constants@1.0.0-beta.10
91
+ - @pipelab/shared@1.0.0-beta.8
92
+
93
+ ## 1.0.0-beta.11
94
+
95
+ ### Patch Changes
96
+
97
+ - bump
98
+ - Updated dependencies
99
+ - @pipelab/constants@1.0.0-beta.9
100
+ - @pipelab/shared@1.0.0-beta.7
101
+
102
+ ## 1.0.0-beta.10
103
+
104
+ ### Patch Changes
105
+
106
+ - fixes
107
+ - Updated dependencies
108
+ - @pipelab/constants@1.0.0-beta.8
109
+ - @pipelab/shared@1.0.0-beta.6
110
+
111
+ ## 1.0.0-beta.9
112
+
113
+ ### Patch Changes
114
+
115
+ - Updated dependencies
116
+ - @pipelab/constants@1.0.0-beta.7
117
+
118
+ ## 1.0.0-beta.8
119
+
120
+ ### Patch Changes
121
+
122
+ - fix package installation
123
+
124
+ ## 1.0.0-beta.7
125
+
126
+ ### Patch Changes
127
+
128
+ - bump
129
+ - Updated dependencies
130
+ - @pipelab/constants@1.0.0-beta.6
131
+ - @pipelab/shared@1.0.0-beta.5
132
+
133
+ ## 1.0.0-beta.6
134
+
135
+ ### Patch Changes
136
+
137
+ - beta tag
138
+ - Updated dependencies
139
+ - @pipelab/constants@1.0.0-beta.5
140
+ - @pipelab/shared@1.0.0-beta.4
141
+
142
+ ## 1.0.0-beta.5
143
+
144
+ ### Patch Changes
145
+
146
+ - bump
147
+ - Updated dependencies
148
+ - @pipelab/constants@1.0.0-beta.4
149
+ - @pipelab/shared@1.0.0-beta.3
150
+
151
+ ## 1.0.0-beta.4
152
+
153
+ ### Patch Changes
154
+
155
+ - Updated dependencies
156
+ - @pipelab/constants@1.0.0-beta.3
157
+
158
+ ## 1.0.0-beta.3
159
+
160
+ ### Patch Changes
161
+
162
+ - update
163
+ - Updated dependencies
164
+ - @pipelab/constants@1.0.0-beta.2
165
+ - @pipelab/shared@1.0.0-beta.2
166
+
3
167
  ## 1.0.0-beta.2
4
168
 
5
169
  ### Patch Changes
@@ -0,0 +1,2 @@
1
+ import { c as setupSettingsConfigFile } from "./config-CFgGRD9U.mjs";
2
+ export { setupSettingsConfigFile };
@@ -0,0 +1,265 @@
1
+ import path, { dirname, join } from "node:path";
2
+ import { createWriteStream } from "node:fs";
3
+ import fs$1, { mkdir, readdir, stat, writeFile } from "node:fs/promises";
4
+ import { appSettingsMigrator, connectionsMigrator, fileRepoMigrations, savedFileMigrator, useLogger } from "@pipelab/shared";
5
+ import { execa } from "execa";
6
+ import tar from "tar";
7
+ import yauzl from "yauzl";
8
+ import archiver from "archiver";
9
+ import { pipeline } from "node:stream/promises";
10
+ //#region src/utils/fs-extras.ts
11
+ /**
12
+ * Ensures a directory exists and a file is created with default content if missing.
13
+ */
14
+ const ensure = async (filesPath, defaultContent = "{}") => {
15
+ await mkdir(dirname(filesPath), { recursive: true });
16
+ try {
17
+ if ((await stat(filesPath)).size === 0) await writeFile(filesPath, defaultContent);
18
+ } catch {
19
+ await writeFile(filesPath, defaultContent);
20
+ }
21
+ };
22
+ /**
23
+ * Extracts a .tar.gz archive.
24
+ */
25
+ async function extractTarGz(archivePath, destinationDir) {
26
+ await mkdir(destinationDir, { recursive: true });
27
+ await tar.x({
28
+ file: archivePath,
29
+ cwd: destinationDir
30
+ });
31
+ }
32
+ /**
33
+ * Extracts a .zip archive.
34
+ */
35
+ async function extractZip(archivePath, destinationDir) {
36
+ await mkdir(destinationDir, { recursive: true });
37
+ return new Promise((resolve, reject) => {
38
+ yauzl.open(archivePath, { lazyEntries: true }, (err, zipfile) => {
39
+ if (err || !zipfile) return reject(err || /* @__PURE__ */ new Error("Could not open zip file"));
40
+ zipfile.on("error", reject);
41
+ zipfile.readEntry();
42
+ zipfile.on("entry", (entry) => {
43
+ const entryPath = join(destinationDir, entry.fileName);
44
+ if (/\/$/.test(entry.fileName)) mkdir(entryPath, { recursive: true }).then(() => zipfile.readEntry()).catch(reject);
45
+ else mkdir(dirname(entryPath), { recursive: true }).then(() => {
46
+ zipfile.openReadStream(entry, (err, readStream) => {
47
+ if (err || !readStream) return reject(err || /* @__PURE__ */ new Error("Could not open read stream"));
48
+ readStream.on("error", reject);
49
+ const writeStream = createWriteStream(entryPath);
50
+ writeStream.on("error", reject);
51
+ writeStream.on("close", () => zipfile.readEntry());
52
+ readStream.pipe(writeStream);
53
+ });
54
+ }).catch(reject);
55
+ });
56
+ zipfile.on("end", () => resolve());
57
+ });
58
+ });
59
+ }
60
+ /**
61
+ * Zips a folder.
62
+ */
63
+ const zipFolder = async (from, to, log = console.log) => {
64
+ const output = createWriteStream(to);
65
+ const archive = archiver("zip", { zlib: { level: 9 } });
66
+ return new Promise((resolve, reject) => {
67
+ output.on("close", () => {
68
+ log(archive.pointer() + " total bytes");
69
+ resolve(to);
70
+ });
71
+ archive.on("error", reject);
72
+ archive.pipe(output);
73
+ archive.directory(from, false);
74
+ archive.finalize();
75
+ });
76
+ };
77
+ const downloadFile = async (url, localPath, hooks, abortSignal) => {
78
+ const response = await fetch(url, { signal: abortSignal });
79
+ if (!response.ok) throw new Error(`Failed to fetch file: ${response.statusText}`);
80
+ const contentLength = response.headers.get("content-length");
81
+ if (!contentLength) throw new Error("Content-Length header is missing");
82
+ const totalSize = parseInt(contentLength, 10);
83
+ let downloadedSize = 0;
84
+ const fileStream = createWriteStream(localPath);
85
+ const progressStream = new TransformStream({ transform(chunk, controller) {
86
+ downloadedSize += chunk.length;
87
+ const progress = downloadedSize / totalSize * 100;
88
+ hooks?.onProgress?.({
89
+ progress,
90
+ downloadedSize
91
+ });
92
+ controller.enqueue(chunk);
93
+ } });
94
+ const readable = response.body?.pipeThrough(progressStream);
95
+ if (!readable) throw new Error("Failed to create a readable stream");
96
+ await pipeline(readable, fileStream);
97
+ };
98
+ const runWithLiveLogs = async (command, args, execaOptions, log, hooks, abortSignal) => {
99
+ const subprocess = execa(command, args, {
100
+ ...execaOptions,
101
+ stdout: "pipe",
102
+ stderr: "pipe",
103
+ stdin: "pipe",
104
+ env: {
105
+ ...process.env,
106
+ ...execaOptions.env,
107
+ TERM: "xterm-256color",
108
+ FORCE_STDERR_LOGGING: "1"
109
+ },
110
+ cancelSignal: abortSignal
111
+ });
112
+ hooks?.onCreated?.(subprocess);
113
+ subprocess.stdout?.on("data", (data) => {
114
+ hooks?.onStdout?.(data.toString(), subprocess);
115
+ });
116
+ subprocess.stderr?.on("data", (data) => {
117
+ hooks?.onStderr?.(data.toString(), subprocess);
118
+ });
119
+ try {
120
+ const { exitCode } = await subprocess;
121
+ hooks?.onExit?.(exitCode ?? 0);
122
+ } catch (error) {
123
+ const code = error.exitCode ?? 1;
124
+ hooks?.onExit?.(code);
125
+ throw new Error(`Command failed with exit code ${code}: ${error.message}`);
126
+ }
127
+ };
128
+ /**
129
+ * Calculates the total size of a directory recursively.
130
+ */
131
+ async function getFolderSize(dirPath) {
132
+ try {
133
+ const ArrayOfPromises = (await readdir(dirPath, { withFileTypes: true })).map(async (file) => {
134
+ const path = join(dirPath, file.name);
135
+ if (file.isDirectory()) try {
136
+ return await getFolderSize(path);
137
+ } catch {
138
+ return 0;
139
+ }
140
+ try {
141
+ const { size } = await stat(path);
142
+ return size;
143
+ } catch {
144
+ return 0;
145
+ }
146
+ });
147
+ return (await Promise.all(ArrayOfPromises)).reduce((acc, size) => acc + size, 0);
148
+ } catch {
149
+ return 0;
150
+ }
151
+ }
152
+ //#endregion
153
+ //#region src/config.ts
154
+ const setupConfigFile = async (filesPath, options) => {
155
+ options.context;
156
+ const parsedPath = path.parse(filesPath);
157
+ const migrator = options.migrator;
158
+ await ensure(filesPath, JSON.stringify(migrator.defaultValue));
159
+ return {
160
+ setConfig: async (config) => {
161
+ const { logger } = useLogger();
162
+ try {
163
+ await fs$1.writeFile(filesPath, JSON.stringify(config));
164
+ return true;
165
+ } catch (e) {
166
+ logger().error(`Error saving config ${parsedPath.name}:`, e);
167
+ return false;
168
+ }
169
+ },
170
+ getConfig: async () => {
171
+ const { logger } = useLogger();
172
+ let content = void 0;
173
+ let originalJson = void 0;
174
+ let parseFailed = false;
175
+ try {
176
+ content = await fs$1.readFile(filesPath, "utf8");
177
+ if (content !== void 0) originalJson = JSON.parse(content);
178
+ } catch (e) {
179
+ logger().error(`Error reading or parsing config ${parsedPath.name}:`, e);
180
+ parseFailed = true;
181
+ }
182
+ let json = void 0;
183
+ let migrationFailed = false;
184
+ try {
185
+ if (!parseFailed) json = await migrator.migrate(originalJson, {
186
+ debug: false,
187
+ onStep: async (state, version) => {
188
+ const versionedPath = path.join(parsedPath.dir, `${parsedPath.name}.v${version}.json`);
189
+ try {
190
+ await fs$1.writeFile(versionedPath, JSON.stringify(state));
191
+ logger().info(`Intermediate backup created for ${parsedPath.name} at ${versionedPath}`);
192
+ } catch (e) {
193
+ logger().error(`Failed to create intermediate backup for ${parsedPath.name} at v${version}:`, e);
194
+ }
195
+ }
196
+ });
197
+ else json = migrator.defaultValue;
198
+ } catch (e) {
199
+ logger().error(`Error migrating config ${parsedPath.name}:`, e);
200
+ migrationFailed = true;
201
+ json = migrator.defaultValue;
202
+ }
203
+ if (originalJson?.version !== json?.version || content === void 0 || parseFailed || migrationFailed) {
204
+ if (parseFailed || migrationFailed) try {
205
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
206
+ const corruptedPath = path.join(parsedPath.dir, `${parsedPath.name}.corrupted.${timestamp}.json`);
207
+ const backupContent = parseFailed ? content || "" : JSON.stringify(originalJson, null, 2);
208
+ await fs$1.writeFile(corruptedPath, backupContent);
209
+ logger().info(`Corrupted config file preserved at ${corruptedPath}`);
210
+ } catch (e) {
211
+ logger().error(`Failed to backup corrupted config ${parsedPath.name}:`, e);
212
+ }
213
+ try {
214
+ await fs$1.writeFile(filesPath, JSON.stringify(json));
215
+ } catch (e) {
216
+ logger().error(`Error saving migrated config ${parsedPath.name}:`, e);
217
+ }
218
+ }
219
+ return json;
220
+ }
221
+ };
222
+ };
223
+ const setupSettingsConfigFile = (context) => {
224
+ return setupConfigFile(context.getSettingsPath(), {
225
+ context,
226
+ migrator: appSettingsMigrator
227
+ });
228
+ };
229
+ const setupConnectionsConfigFile = (context) => {
230
+ return setupConfigFile(context.getConnectionsPath(), {
231
+ context,
232
+ migrator: connectionsMigrator
233
+ });
234
+ };
235
+ const setupProjectsConfigFile = (context) => {
236
+ return setupConfigFile(context.getProjectsPath(), {
237
+ context,
238
+ migrator: fileRepoMigrations
239
+ });
240
+ };
241
+ const setupPipelineConfigFileByName = (name, context) => {
242
+ return setupConfigFile(context.getConfigPath(`${name}.json`), {
243
+ context,
244
+ migrator: savedFileMigrator
245
+ });
246
+ };
247
+ const setupPipelineConfigFileByPath = (absolutePath, context) => {
248
+ return setupConfigFile(absolutePath, {
249
+ context,
250
+ migrator: savedFileMigrator
251
+ });
252
+ };
253
+ const deleteConfigFile = async (filesPath) => {
254
+ await fs$1.rm(filesPath, { force: true });
255
+ };
256
+ const deletePipelineConfigFileByName = async (name, context) => {
257
+ await deleteConfigFile(context.getConfigPath(`${name}.json`));
258
+ };
259
+ const deletePipelineConfigFileByPath = async (absolutePath, context) => {
260
+ await deleteConfigFile(absolutePath);
261
+ };
262
+ //#endregion
263
+ export { setupPipelineConfigFileByName as a, setupSettingsConfigFile as c, extractTarGz as d, extractZip as f, zipFolder as h, setupConnectionsConfigFile as i, downloadFile as l, runWithLiveLogs as m, deletePipelineConfigFileByPath as n, setupPipelineConfigFileByPath as o, getFolderSize as p, setupConfigFile as r, setupProjectsConfigFile as s, deletePipelineConfigFileByName as t, ensure as u };
264
+
265
+ //# sourceMappingURL=config-CFgGRD9U.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-CFgGRD9U.mjs","names":["mkdirP","fs"],"sources":["../src/utils/fs-extras.ts","../src/config.ts"],"sourcesContent":["import { createWriteStream } from \"node:fs\";\nimport { execa, Options, Subprocess } from \"execa\";\nimport { mkdir as mkdirP, writeFile, stat, readdir } from \"node:fs/promises\";\nimport { join, dirname } from \"node:path\";\nimport tar from \"tar\";\nimport yauzl from \"yauzl\";\nimport archiver from \"archiver\";\nimport { pipeline } from \"node:stream/promises\";\n\n/**\n * Ensures a directory exists and a file is created with default content if missing.\n */\nexport const ensure = async (filesPath: string, defaultContent = \"{}\") => {\n await mkdirP(dirname(filesPath), { recursive: true });\n try {\n const s = await stat(filesPath);\n if (s.size === 0) {\n await writeFile(filesPath, defaultContent);\n }\n } catch {\n await writeFile(filesPath, defaultContent);\n }\n};\n\n/**\n * Extracts a .tar.gz archive.\n */\nexport async function extractTarGz(archivePath: string, destinationDir: string): Promise<void> {\n await mkdirP(destinationDir, { recursive: true });\n await tar.x({\n file: archivePath,\n cwd: destinationDir,\n });\n}\n\n/**\n * Extracts a .zip archive.\n */\nexport async function extractZip(archivePath: string, destinationDir: string): Promise<void> {\n await mkdirP(destinationDir, { recursive: true });\n return new Promise((resolve, reject) => {\n yauzl.open(archivePath, { lazyEntries: true }, (err, zipfile) => {\n if (err || !zipfile) return reject(err || new Error(\"Could not open zip file\"));\n zipfile.on(\"error\", reject);\n zipfile.readEntry();\n zipfile.on(\"entry\", (entry) => {\n const entryPath = join(destinationDir, entry.fileName);\n if (/\\/$/.test(entry.fileName)) {\n mkdirP(entryPath, { recursive: true })\n .then(() => zipfile.readEntry())\n .catch(reject);\n } else {\n mkdirP(dirname(entryPath), { recursive: true })\n .then(() => {\n zipfile.openReadStream(entry, (err, readStream) => {\n if (err || !readStream)\n return reject(err || new Error(\"Could not open read stream\"));\n readStream.on(\"error\", reject);\n const writeStream = createWriteStream(entryPath);\n writeStream.on(\"error\", reject);\n writeStream.on(\"close\", () => zipfile.readEntry());\n readStream.pipe(writeStream);\n });\n })\n .catch(reject);\n }\n });\n zipfile.on(\"end\", () => resolve());\n });\n });\n}\n\n/**\n * Zips a folder.\n */\nexport const zipFolder = async (\n from: string,\n to: string,\n log: typeof console.log = console.log,\n) => {\n const output = createWriteStream(to);\n const archive = archiver(\"zip\", { zlib: { level: 9 } });\n return new Promise<string>((resolve, reject) => {\n output.on(\"close\", () => {\n log(archive.pointer() + \" total bytes\");\n resolve(to);\n });\n archive.on(\"error\", reject);\n archive.pipe(output);\n archive.directory(from, false);\n archive.finalize();\n });\n};\n\n/**\n * Downloads a file with progress tracking.\n */\nexport interface DownloadHooks {\n onProgress?: (data: { progress: number; downloadedSize: number }) => void;\n}\n\nexport const downloadFile = async (\n url: string,\n localPath: string,\n hooks?: DownloadHooks,\n abortSignal?: AbortSignal,\n): Promise<void> => {\n const response = await fetch(url, { signal: abortSignal });\n if (!response.ok) throw new Error(`Failed to fetch file: ${response.statusText}`);\n const contentLength = response.headers.get(\"content-length\");\n if (!contentLength) throw new Error(\"Content-Length header is missing\");\n const totalSize = parseInt(contentLength, 10);\n let downloadedSize = 0;\n const fileStream = createWriteStream(localPath);\n const progressStream = new TransformStream({\n transform(chunk, controller) {\n downloadedSize += chunk.length;\n const progress = (downloadedSize / totalSize) * 100;\n hooks?.onProgress?.({ progress, downloadedSize });\n controller.enqueue(chunk);\n },\n });\n const readable = response.body?.pipeThrough(progressStream);\n if (!readable) throw new Error(\"Failed to create a readable stream\");\n await pipeline(readable, fileStream);\n};\n\nexport const runWithLiveLogs = async (\n command: string,\n args: string[],\n execaOptions: Options,\n log: typeof console.log,\n hooks?: {\n onStdout?: (data: string, subprocess: Subprocess) => void;\n onStderr?: (data: string, subprocess: Subprocess) => void;\n onExit?: (code: number) => void;\n onCreated?: (subprocess: Subprocess) => void;\n },\n abortSignal?: AbortSignal,\n): Promise<void> => {\n const subprocess = execa(command, args, {\n ...execaOptions,\n stdout: \"pipe\",\n stderr: \"pipe\",\n stdin: \"pipe\",\n env: {\n ...process.env,\n ...execaOptions.env,\n TERM: \"xterm-256color\",\n FORCE_STDERR_LOGGING: \"1\",\n },\n cancelSignal: abortSignal,\n });\n\n hooks?.onCreated?.(subprocess);\n\n subprocess.stdout?.on(\"data\", (data: Buffer) => {\n hooks?.onStdout?.(data.toString(), subprocess);\n });\n\n subprocess.stderr?.on(\"data\", (data: Buffer) => {\n hooks?.onStderr?.(data.toString(), subprocess);\n });\n\n try {\n const { exitCode } = await subprocess;\n hooks?.onExit?.(exitCode ?? 0);\n } catch (error: any) {\n const code = error.exitCode ?? 1;\n hooks?.onExit?.(code);\n throw new Error(`Command failed with exit code ${code}: ${error.message}`);\n }\n};\n\n/**\n * Calculates the total size of a directory recursively.\n */\nexport async function getFolderSize(dirPath: string): Promise<number> {\n try {\n const files = await readdir(dirPath, { withFileTypes: true });\n const ArrayOfPromises = files.map(async (file) => {\n const path = join(dirPath, file.name);\n if (file.isDirectory()) {\n try {\n return await getFolderSize(path);\n } catch {\n return 0;\n }\n }\n try {\n const { size } = await stat(path);\n return size;\n } catch {\n return 0;\n }\n });\n const results = await Promise.all(ArrayOfPromises);\n return results.reduce((acc, size) => acc + size, 0);\n } catch {\n return 0;\n }\n}\n","import { PipelabContext } from \"./context\";\nimport path from \"node:path\";\nimport { ensure } from \"./utils/fs-extras\";\nimport fs from \"node:fs/promises\";\nimport {\n useLogger,\n Migrator,\n AppConfig,\n ConnectionsConfig,\n FileRepo,\n SavedFile,\n appSettingsMigrator,\n connectionsMigrator,\n fileRepoMigrations,\n savedFileMigrator,\n} from \"@pipelab/shared\";\n\nexport const setupConfigFile = async <T>(\n filesPath: string,\n options: { context: PipelabContext; migrator: Migrator<T> },\n) => {\n const ctx = options.context;\n const parsedPath = path.parse(filesPath);\n const migrator = options.migrator;\n\n await ensure(filesPath, JSON.stringify(migrator.defaultValue));\n\n return {\n setConfig: async (config: T) => {\n const { logger } = useLogger();\n try {\n await fs.writeFile(filesPath, JSON.stringify(config));\n return true;\n } catch (e) {\n logger().error(`Error saving config ${parsedPath.name}:`, e);\n return false;\n }\n },\n getConfig: async () => {\n const { logger } = useLogger();\n let content = undefined;\n let originalJson: any = undefined;\n let parseFailed = false;\n\n try {\n content = await fs.readFile(filesPath, \"utf8\");\n if (content !== undefined) {\n originalJson = JSON.parse(content);\n }\n } catch (e) {\n logger().error(`Error reading or parsing config ${parsedPath.name}:`, e);\n parseFailed = true;\n }\n\n let json: any = undefined;\n let migrationFailed = false;\n try {\n if (!parseFailed) {\n json = await migrator.migrate(originalJson, {\n debug: false,\n onStep: async (state: any, version: string) => {\n const versionedPath = path.join(parsedPath.dir, `${parsedPath.name}.v${version}.json`);\n try {\n await fs.writeFile(versionedPath, JSON.stringify(state));\n logger().info(`Intermediate backup created for ${parsedPath.name} at ${versionedPath}`);\n } catch (e) {\n logger().error(\n `Failed to create intermediate backup for ${parsedPath.name} at v${version}:`,\n e,\n );\n }\n },\n });\n } else {\n json = migrator.defaultValue;\n }\n } catch (e) {\n logger().error(`Error migrating config ${parsedPath.name}:`, e);\n migrationFailed = true;\n json = migrator.defaultValue;\n }\n\n const originalVersion = originalJson?.version;\n const newVersion = json?.version;\n\n const shouldSaveBack =\n originalVersion !== newVersion || content === undefined || parseFailed || migrationFailed;\n\n if (shouldSaveBack) {\n if (parseFailed || migrationFailed) {\n try {\n const timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n const corruptedPath = path.join(\n parsedPath.dir,\n `${parsedPath.name}.corrupted.${timestamp}.json`,\n );\n const backupContent = parseFailed\n ? content || \"\"\n : JSON.stringify(originalJson, null, 2);\n await fs.writeFile(corruptedPath, backupContent);\n logger().info(`Corrupted config file preserved at ${corruptedPath}`);\n } catch (e) {\n logger().error(`Failed to backup corrupted config ${parsedPath.name}:`, e);\n }\n }\n\n try {\n await fs.writeFile(filesPath, JSON.stringify(json));\n } catch (e) {\n logger().error(`Error saving migrated config ${parsedPath.name}:`, e);\n }\n }\n\n return json as T;\n },\n };\n};\n\nexport const setupSettingsConfigFile = (context: PipelabContext) => {\n return setupConfigFile<AppConfig>(context.getSettingsPath(), {\n context,\n migrator: appSettingsMigrator,\n });\n};\n\nexport const setupConnectionsConfigFile = (context: PipelabContext) => {\n return setupConfigFile<ConnectionsConfig>(context.getConnectionsPath(), {\n context,\n migrator: connectionsMigrator,\n });\n};\n\nexport const setupProjectsConfigFile = (context: PipelabContext) => {\n return setupConfigFile<FileRepo>(context.getProjectsPath(), {\n context,\n migrator: fileRepoMigrations,\n });\n};\n\nexport const setupPipelineConfigFileByName = (name: string, context: PipelabContext) => {\n const filesPath = context.getConfigPath(`${name}.json`);\n return setupConfigFile<SavedFile>(filesPath, {\n context,\n migrator: savedFileMigrator,\n });\n};\n\nexport const setupPipelineConfigFileByPath = (absolutePath: string, context: PipelabContext) => {\n return setupConfigFile<SavedFile>(absolutePath, {\n context,\n migrator: savedFileMigrator,\n });\n};\n\nconst deleteConfigFile = async (filesPath: string) => {\n await fs.rm(filesPath, { force: true });\n};\n\nexport const deletePipelineConfigFileByName = async (name: string, context: PipelabContext) => {\n const filesPath = context.getConfigPath(`${name}.json`);\n await deleteConfigFile(filesPath);\n};\n\nexport const deletePipelineConfigFileByPath = async (absolutePath: string, context: PipelabContext) => {\n await deleteConfigFile(absolutePath);\n};\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAa,SAAS,OAAO,WAAmB,iBAAiB,SAAS;AACxE,OAAMA,MAAO,QAAQ,UAAU,EAAE,EAAE,WAAW,MAAM,CAAC;AACrD,KAAI;AAEF,OADU,MAAM,KAAK,UAAU,EACzB,SAAS,EACb,OAAM,UAAU,WAAW,eAAe;SAEtC;AACN,QAAM,UAAU,WAAW,eAAe;;;;;;AAO9C,eAAsB,aAAa,aAAqB,gBAAuC;AAC7F,OAAMA,MAAO,gBAAgB,EAAE,WAAW,MAAM,CAAC;AACjD,OAAM,IAAI,EAAE;EACV,MAAM;EACN,KAAK;EAC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