@positronic/cli 0.0.2

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 (193) hide show
  1. package/dist/src/cli.js +739 -0
  2. package/dist/src/commands/backend.js +199 -0
  3. package/dist/src/commands/brain.js +446 -0
  4. package/dist/src/commands/brain.test.js +2936 -0
  5. package/dist/src/commands/helpers.js +1315 -0
  6. package/dist/src/commands/helpers.test.js +832 -0
  7. package/dist/src/commands/project-config-manager.js +197 -0
  8. package/dist/src/commands/project.js +130 -0
  9. package/dist/src/commands/project.test.js +1201 -0
  10. package/dist/src/commands/resources.js +272 -0
  11. package/dist/src/commands/resources.test.js +2511 -0
  12. package/dist/src/commands/schedule.js +73 -0
  13. package/dist/src/commands/schedule.test.js +1235 -0
  14. package/dist/src/commands/secret.js +87 -0
  15. package/dist/src/commands/secret.test.d.js +1 -0
  16. package/dist/src/commands/secret.test.js +761 -0
  17. package/dist/src/commands/server.js +816 -0
  18. package/dist/src/commands/server.test.js +1237 -0
  19. package/dist/src/commands/test-utils.js +737 -0
  20. package/dist/src/components/brain-history.js +169 -0
  21. package/dist/src/components/brain-list.js +108 -0
  22. package/dist/src/components/brain-rerun.js +313 -0
  23. package/dist/src/components/brain-show.js +65 -0
  24. package/dist/src/components/error.js +19 -0
  25. package/dist/src/components/project-add.js +95 -0
  26. package/dist/src/components/project-create.js +276 -0
  27. package/dist/src/components/project-list.js +88 -0
  28. package/dist/src/components/project-remove.js +91 -0
  29. package/dist/src/components/project-select.js +224 -0
  30. package/dist/src/components/project-show.js +41 -0
  31. package/dist/src/components/resource-clear.js +152 -0
  32. package/dist/src/components/resource-delete.js +189 -0
  33. package/dist/src/components/resource-list.js +174 -0
  34. package/dist/src/components/resource-sync.js +386 -0
  35. package/dist/src/components/resource-types.js +243 -0
  36. package/dist/src/components/resource-upload.js +366 -0
  37. package/dist/src/components/schedule-create.js +259 -0
  38. package/dist/src/components/schedule-delete.js +161 -0
  39. package/dist/src/components/schedule-list.js +176 -0
  40. package/dist/src/components/schedule-runs.js +103 -0
  41. package/dist/src/components/secret-bulk.js +262 -0
  42. package/dist/src/components/secret-create.js +199 -0
  43. package/dist/src/components/secret-delete.js +190 -0
  44. package/dist/src/components/secret-list.js +190 -0
  45. package/dist/src/components/secret-sync.js +303 -0
  46. package/dist/src/components/watch.js +184 -0
  47. package/dist/src/hooks/useApi.js +512 -0
  48. package/dist/src/positronic.js +33 -0
  49. package/dist/src/test/mock-api-client.js +371 -0
  50. package/dist/src/test/test-dev-server.js +1376 -0
  51. package/dist/types/cli.d.ts +9 -0
  52. package/dist/types/cli.d.ts.map +1 -0
  53. package/dist/types/commands/backend.d.ts +6 -0
  54. package/dist/types/commands/backend.d.ts.map +1 -0
  55. package/dist/types/commands/brain.d.ts +35 -0
  56. package/dist/types/commands/brain.d.ts.map +1 -0
  57. package/dist/types/commands/helpers.d.ts +55 -0
  58. package/dist/types/commands/helpers.d.ts.map +1 -0
  59. package/dist/types/commands/project-config-manager.d.ts +37 -0
  60. package/dist/types/commands/project-config-manager.d.ts.map +1 -0
  61. package/dist/types/commands/project.d.ts +55 -0
  62. package/dist/types/commands/project.d.ts.map +1 -0
  63. package/dist/types/commands/resources.d.ts +13 -0
  64. package/dist/types/commands/resources.d.ts.map +1 -0
  65. package/dist/types/commands/schedule.d.ts +27 -0
  66. package/dist/types/commands/schedule.d.ts.map +1 -0
  67. package/dist/types/commands/secret.d.ts +23 -0
  68. package/dist/types/commands/secret.d.ts.map +1 -0
  69. package/dist/types/commands/server.d.ts +12 -0
  70. package/dist/types/commands/server.d.ts.map +1 -0
  71. package/dist/types/commands/test-utils.d.ts +45 -0
  72. package/dist/types/commands/test-utils.d.ts.map +1 -0
  73. package/dist/types/components/brain-history.d.ts +7 -0
  74. package/dist/types/components/brain-history.d.ts.map +1 -0
  75. package/dist/types/components/brain-list.d.ts +2 -0
  76. package/dist/types/components/brain-list.d.ts.map +1 -0
  77. package/dist/types/components/brain-rerun.d.ts +9 -0
  78. package/dist/types/components/brain-rerun.d.ts.map +1 -0
  79. package/dist/types/components/brain-show.d.ts +6 -0
  80. package/dist/types/components/brain-show.d.ts.map +1 -0
  81. package/dist/types/components/error.d.ts +10 -0
  82. package/dist/types/components/error.d.ts.map +1 -0
  83. package/dist/types/components/project-add.d.ts +9 -0
  84. package/dist/types/components/project-add.d.ts.map +1 -0
  85. package/dist/types/components/project-create.d.ts +6 -0
  86. package/dist/types/components/project-create.d.ts.map +1 -0
  87. package/dist/types/components/project-list.d.ts +7 -0
  88. package/dist/types/components/project-list.d.ts.map +1 -0
  89. package/dist/types/components/project-remove.d.ts +8 -0
  90. package/dist/types/components/project-remove.d.ts.map +1 -0
  91. package/dist/types/components/project-select.d.ts +8 -0
  92. package/dist/types/components/project-select.d.ts.map +1 -0
  93. package/dist/types/components/project-show.d.ts +7 -0
  94. package/dist/types/components/project-show.d.ts.map +1 -0
  95. package/dist/types/components/resource-clear.d.ts +2 -0
  96. package/dist/types/components/resource-clear.d.ts.map +1 -0
  97. package/dist/types/components/resource-delete.d.ts +9 -0
  98. package/dist/types/components/resource-delete.d.ts.map +1 -0
  99. package/dist/types/components/resource-list.d.ts +2 -0
  100. package/dist/types/components/resource-list.d.ts.map +1 -0
  101. package/dist/types/components/resource-sync.d.ts +8 -0
  102. package/dist/types/components/resource-sync.d.ts.map +1 -0
  103. package/dist/types/components/resource-types.d.ts +7 -0
  104. package/dist/types/components/resource-types.d.ts.map +1 -0
  105. package/dist/types/components/resource-upload.d.ts +8 -0
  106. package/dist/types/components/resource-upload.d.ts.map +1 -0
  107. package/dist/types/components/schedule-create.d.ts +7 -0
  108. package/dist/types/components/schedule-create.d.ts.map +1 -0
  109. package/dist/types/components/schedule-delete.d.ts +7 -0
  110. package/dist/types/components/schedule-delete.d.ts.map +1 -0
  111. package/dist/types/components/schedule-list.d.ts +6 -0
  112. package/dist/types/components/schedule-list.d.ts.map +1 -0
  113. package/dist/types/components/schedule-runs.d.ts +8 -0
  114. package/dist/types/components/schedule-runs.d.ts.map +1 -0
  115. package/dist/types/components/secret-bulk.d.ts +8 -0
  116. package/dist/types/components/secret-bulk.d.ts.map +1 -0
  117. package/dist/types/components/secret-create.d.ts +9 -0
  118. package/dist/types/components/secret-create.d.ts.map +1 -0
  119. package/dist/types/components/secret-delete.d.ts +8 -0
  120. package/dist/types/components/secret-delete.d.ts.map +1 -0
  121. package/dist/types/components/secret-list.d.ts +7 -0
  122. package/dist/types/components/secret-list.d.ts.map +1 -0
  123. package/dist/types/components/secret-sync.d.ts +9 -0
  124. package/dist/types/components/secret-sync.d.ts.map +1 -0
  125. package/dist/types/components/watch.d.ts +7 -0
  126. package/dist/types/components/watch.d.ts.map +1 -0
  127. package/dist/types/hooks/useApi.d.ts +29 -0
  128. package/dist/types/hooks/useApi.d.ts.map +1 -0
  129. package/dist/types/positronic.d.ts +3 -0
  130. package/dist/types/positronic.d.ts.map +1 -0
  131. package/dist/types/test/mock-api-client.d.ts +25 -0
  132. package/dist/types/test/mock-api-client.d.ts.map +1 -0
  133. package/dist/types/test/test-dev-server.d.ts +129 -0
  134. package/dist/types/test/test-dev-server.d.ts.map +1 -0
  135. package/package.json +37 -0
  136. package/src/cli.ts +981 -0
  137. package/src/commands/backend.ts +63 -0
  138. package/src/commands/brain.test.ts +1004 -0
  139. package/src/commands/brain.ts +215 -0
  140. package/src/commands/helpers.test.ts +487 -0
  141. package/src/commands/helpers.ts +870 -0
  142. package/src/commands/project-config-manager.ts +152 -0
  143. package/src/commands/project.test.ts +502 -0
  144. package/src/commands/project.ts +109 -0
  145. package/src/commands/resources.test.ts +1052 -0
  146. package/src/commands/resources.ts +97 -0
  147. package/src/commands/schedule.test.ts +481 -0
  148. package/src/commands/schedule.ts +65 -0
  149. package/src/commands/secret.test.ts +210 -0
  150. package/src/commands/secret.ts +50 -0
  151. package/src/commands/server.test.ts +493 -0
  152. package/src/commands/server.ts +353 -0
  153. package/src/commands/test-utils.ts +324 -0
  154. package/src/components/brain-history.tsx +198 -0
  155. package/src/components/brain-list.tsx +105 -0
  156. package/src/components/brain-rerun.tsx +111 -0
  157. package/src/components/brain-show.tsx +92 -0
  158. package/src/components/error.tsx +24 -0
  159. package/src/components/project-add.tsx +59 -0
  160. package/src/components/project-create.tsx +83 -0
  161. package/src/components/project-list.tsx +83 -0
  162. package/src/components/project-remove.tsx +55 -0
  163. package/src/components/project-select.tsx +200 -0
  164. package/src/components/project-show.tsx +58 -0
  165. package/src/components/resource-clear.tsx +127 -0
  166. package/src/components/resource-delete.tsx +160 -0
  167. package/src/components/resource-list.tsx +177 -0
  168. package/src/components/resource-sync.tsx +170 -0
  169. package/src/components/resource-types.tsx +55 -0
  170. package/src/components/resource-upload.tsx +182 -0
  171. package/src/components/schedule-create.tsx +90 -0
  172. package/src/components/schedule-delete.tsx +116 -0
  173. package/src/components/schedule-list.tsx +186 -0
  174. package/src/components/schedule-runs.tsx +151 -0
  175. package/src/components/secret-bulk.tsx +79 -0
  176. package/src/components/secret-create.tsx +49 -0
  177. package/src/components/secret-delete.tsx +41 -0
  178. package/src/components/secret-list.tsx +41 -0
  179. package/src/components/watch.tsx +155 -0
  180. package/src/hooks/useApi.ts +183 -0
  181. package/src/positronic.ts +40 -0
  182. package/src/test/data/resources/config.json +1 -0
  183. package/src/test/data/resources/data/config.json +1 -0
  184. package/src/test/data/resources/data/logo.png +2 -0
  185. package/src/test/data/resources/docs/api.md +3 -0
  186. package/src/test/data/resources/docs/readme.md +3 -0
  187. package/src/test/data/resources/example.md +3 -0
  188. package/src/test/data/resources/file with spaces.txt +1 -0
  189. package/src/test/data/resources/readme.md +3 -0
  190. package/src/test/data/resources/test.txt +1 -0
  191. package/src/test/mock-api-client.ts +145 -0
  192. package/src/test/test-dev-server.ts +1003 -0
  193. package/tsconfig.json +11 -0
@@ -0,0 +1,816 @@
1
+ function _array_like_to_array(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _array_without_holes(arr) {
7
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
8
+ }
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
+ try {
11
+ var info = gen[key](arg);
12
+ var value = info.value;
13
+ } catch (error) {
14
+ reject(error);
15
+ return;
16
+ }
17
+ if (info.done) {
18
+ resolve(value);
19
+ } else {
20
+ Promise.resolve(value).then(_next, _throw);
21
+ }
22
+ }
23
+ function _async_to_generator(fn) {
24
+ return function() {
25
+ var self = this, args = arguments;
26
+ return new Promise(function(resolve, reject) {
27
+ var gen = fn.apply(self, args);
28
+ function _next(value) {
29
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
+ }
31
+ function _throw(err) {
32
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
33
+ }
34
+ _next(undefined);
35
+ });
36
+ };
37
+ }
38
+ function _class_call_check(instance, Constructor) {
39
+ if (!(instance instanceof Constructor)) {
40
+ throw new TypeError("Cannot call a class as a function");
41
+ }
42
+ }
43
+ function _defineProperties(target, props) {
44
+ for(var i = 0; i < props.length; i++){
45
+ var descriptor = props[i];
46
+ descriptor.enumerable = descriptor.enumerable || false;
47
+ descriptor.configurable = true;
48
+ if ("value" in descriptor) descriptor.writable = true;
49
+ Object.defineProperty(target, descriptor.key, descriptor);
50
+ }
51
+ }
52
+ function _create_class(Constructor, protoProps, staticProps) {
53
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
54
+ if (staticProps) _defineProperties(Constructor, staticProps);
55
+ return Constructor;
56
+ }
57
+ function _define_property(obj, key, value) {
58
+ if (key in obj) {
59
+ Object.defineProperty(obj, key, {
60
+ value: value,
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true
64
+ });
65
+ } else {
66
+ obj[key] = value;
67
+ }
68
+ return obj;
69
+ }
70
+ function _instanceof(left, right) {
71
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
72
+ return !!right[Symbol.hasInstance](left);
73
+ } else {
74
+ return left instanceof right;
75
+ }
76
+ }
77
+ function _iterable_to_array(iter) {
78
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
79
+ }
80
+ function _non_iterable_spread() {
81
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
82
+ }
83
+ function _object_spread(target) {
84
+ for(var i = 1; i < arguments.length; i++){
85
+ var source = arguments[i] != null ? arguments[i] : {};
86
+ var ownKeys = Object.keys(source);
87
+ if (typeof Object.getOwnPropertySymbols === "function") {
88
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
89
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
90
+ }));
91
+ }
92
+ ownKeys.forEach(function(key) {
93
+ _define_property(target, key, source[key]);
94
+ });
95
+ }
96
+ return target;
97
+ }
98
+ function ownKeys(object, enumerableOnly) {
99
+ var keys = Object.keys(object);
100
+ if (Object.getOwnPropertySymbols) {
101
+ var symbols = Object.getOwnPropertySymbols(object);
102
+ if (enumerableOnly) {
103
+ symbols = symbols.filter(function(sym) {
104
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
105
+ });
106
+ }
107
+ keys.push.apply(keys, symbols);
108
+ }
109
+ return keys;
110
+ }
111
+ function _object_spread_props(target, source) {
112
+ source = source != null ? source : {};
113
+ if (Object.getOwnPropertyDescriptors) {
114
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
115
+ } else {
116
+ ownKeys(Object(source)).forEach(function(key) {
117
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
118
+ });
119
+ }
120
+ return target;
121
+ }
122
+ function _to_consumable_array(arr) {
123
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
124
+ }
125
+ function _unsupported_iterable_to_array(o, minLen) {
126
+ if (!o) return;
127
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
128
+ var n = Object.prototype.toString.call(o).slice(8, -1);
129
+ if (n === "Object" && o.constructor) n = o.constructor.name;
130
+ if (n === "Map" || n === "Set") return Array.from(n);
131
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
132
+ }
133
+ function _ts_generator(thisArg, body) {
134
+ var f, y, t, _ = {
135
+ label: 0,
136
+ sent: function() {
137
+ if (t[0] & 1) throw t[1];
138
+ return t[1];
139
+ },
140
+ trys: [],
141
+ ops: []
142
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
143
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
144
+ return this;
145
+ }), g;
146
+ function verb(n) {
147
+ return function(v) {
148
+ return step([
149
+ n,
150
+ v
151
+ ]);
152
+ };
153
+ }
154
+ function step(op) {
155
+ if (f) throw new TypeError("Generator is already executing.");
156
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
157
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
158
+ if (y = 0, t) op = [
159
+ op[0] & 2,
160
+ t.value
161
+ ];
162
+ switch(op[0]){
163
+ case 0:
164
+ case 1:
165
+ t = op;
166
+ break;
167
+ case 4:
168
+ _.label++;
169
+ return {
170
+ value: op[1],
171
+ done: false
172
+ };
173
+ case 5:
174
+ _.label++;
175
+ y = op[1];
176
+ op = [
177
+ 0
178
+ ];
179
+ continue;
180
+ case 7:
181
+ op = _.ops.pop();
182
+ _.trys.pop();
183
+ continue;
184
+ default:
185
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
186
+ _ = 0;
187
+ continue;
188
+ }
189
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
190
+ _.label = op[1];
191
+ break;
192
+ }
193
+ if (op[0] === 6 && _.label < t[1]) {
194
+ _.label = t[1];
195
+ t = op;
196
+ break;
197
+ }
198
+ if (t && _.label < t[2]) {
199
+ _.label = t[2];
200
+ _.ops.push(op);
201
+ break;
202
+ }
203
+ if (t[2]) _.ops.pop();
204
+ _.trys.pop();
205
+ continue;
206
+ }
207
+ op = body.call(thisArg, _);
208
+ } catch (e) {
209
+ op = [
210
+ 6,
211
+ e
212
+ ];
213
+ y = 0;
214
+ } finally{
215
+ f = t = 0;
216
+ }
217
+ if (op[0] & 5) throw op[1];
218
+ return {
219
+ value: op[0] ? op[1] : void 0,
220
+ done: true
221
+ };
222
+ }
223
+ }
224
+ import * as path from 'path';
225
+ import * as fs from 'fs';
226
+ import { spawn } from 'child_process';
227
+ import chokidar from 'chokidar';
228
+ import { syncResources, generateTypes } from './helpers.js';
229
+ export var ServerCommand = /*#__PURE__*/ function() {
230
+ "use strict";
231
+ function ServerCommand(server) {
232
+ _class_call_check(this, ServerCommand);
233
+ _define_property(this, "server", void 0);
234
+ this.server = server;
235
+ }
236
+ _create_class(ServerCommand, [
237
+ {
238
+ key: "handle",
239
+ value: function handle(argv) {
240
+ return _async_to_generator(function() {
241
+ var _this, pidFile, existingPid, brainsDir, resourcesDir, serverHandle, watcher, logStream, logFilePath, logDir, logBoth, cleanup, isReady, syncResult, error, watchPaths, handleResourceChange, error1;
242
+ return _ts_generator(this, function(_state) {
243
+ switch(_state.label){
244
+ case 0:
245
+ _this = this;
246
+ // Handle kill option
247
+ if (argv.k) {
248
+ return [
249
+ 2,
250
+ this.handleKill(argv)
251
+ ];
252
+ }
253
+ // Validate arguments
254
+ if (argv.port && argv.d && !argv.logFile) {
255
+ throw new Error('When using --port with -d, you must also specify --log-file');
256
+ }
257
+ // Check for existing PID file (skip if we're a detached child process)
258
+ pidFile = this.getPidFilePath(argv.port);
259
+ if (!process.env.POSITRONIC_DETACHED_CHILD && fs.existsSync(pidFile)) {
260
+ existingPid = parseInt(fs.readFileSync(pidFile, 'utf8').trim());
261
+ if (this.isProcessRunning(existingPid)) {
262
+ throw new Error("Server already running (PID: ".concat(existingPid, "). Stop it with: px server -k"));
263
+ } else {
264
+ console.log('WARNING: Removing stale PID file');
265
+ fs.unlinkSync(pidFile);
266
+ }
267
+ }
268
+ // If -d flag is set, spawn a detached process
269
+ if (argv.d) {
270
+ return [
271
+ 2,
272
+ this.handleDetached(argv)
273
+ ];
274
+ }
275
+ // Write PID file for foreground process too
276
+ fs.writeFileSync(pidFile, String(process.pid));
277
+ brainsDir = path.join(this.server.projectRootDir, 'brains');
278
+ resourcesDir = path.join(this.server.projectRootDir, 'resources');
279
+ serverHandle = null;
280
+ watcher = null;
281
+ logStream = null;
282
+ // Always create a log file (use default if not specified)
283
+ logFilePath = argv.logFile ? path.resolve(argv.logFile) : path.join(this.server.projectRootDir, '.positronic-server.log');
284
+ // Ensure directory exists
285
+ logDir = path.dirname(logFilePath);
286
+ if (!fs.existsSync(logDir)) {
287
+ fs.mkdirSync(logDir, {
288
+ recursive: true
289
+ });
290
+ }
291
+ // Create log stream (append mode)
292
+ logStream = fs.createWriteStream(logFilePath, {
293
+ flags: 'a'
294
+ });
295
+ // Helper function to log to both console and file
296
+ logBoth = function(level, message) {
297
+ var timestamp = new Date().toISOString();
298
+ var logLine = "[".concat(timestamp, "] [").concat(level, "] ").concat(message, "\n");
299
+ if (logStream && !logStream.destroyed) {
300
+ logStream.write(logLine);
301
+ }
302
+ // Console output is handled by the dev server's direct forwarding
303
+ // to avoid duplication
304
+ };
305
+ // Always register log callbacks to capture server logs
306
+ // The server is responsible for generating logs through these callbacks
307
+ this.server.onLog(function(message) {
308
+ return logBoth('INFO', message);
309
+ });
310
+ this.server.onError(function(message) {
311
+ return logBoth('ERROR', message);
312
+ });
313
+ this.server.onWarning(function(message) {
314
+ return logBoth('WARN', message);
315
+ });
316
+ cleanup = function() {
317
+ return _async_to_generator(function() {
318
+ return _ts_generator(this, function(_state) {
319
+ switch(_state.label){
320
+ case 0:
321
+ if (!watcher) return [
322
+ 3,
323
+ 2
324
+ ];
325
+ return [
326
+ 4,
327
+ watcher.close()
328
+ ];
329
+ case 1:
330
+ _state.sent();
331
+ watcher = null;
332
+ _state.label = 2;
333
+ case 2:
334
+ if (serverHandle && !serverHandle.killed) {
335
+ serverHandle.kill();
336
+ serverHandle = null;
337
+ }
338
+ // Close log stream
339
+ if (logStream) {
340
+ logStream.end();
341
+ logStream = null;
342
+ }
343
+ // Remove PID file
344
+ if (fs.existsSync(pidFile)) {
345
+ fs.unlinkSync(pidFile);
346
+ }
347
+ process.exit(0);
348
+ return [
349
+ 2
350
+ ];
351
+ }
352
+ });
353
+ })();
354
+ };
355
+ process.on('SIGINT', cleanup); // Catches Ctrl+C
356
+ process.on('SIGTERM', cleanup); // Catches kill commands
357
+ _state.label = 1;
358
+ case 1:
359
+ _state.trys.push([
360
+ 1,
361
+ 10,
362
+ ,
363
+ 12
364
+ ]);
365
+ // Use the dev server's setup method
366
+ return [
367
+ 4,
368
+ this.server.setup(argv.force)
369
+ ];
370
+ case 2:
371
+ _state.sent();
372
+ return [
373
+ 4,
374
+ this.server.start(argv.port)
375
+ ];
376
+ case 3:
377
+ // Use the dev server's start method
378
+ serverHandle = _state.sent();
379
+ serverHandle.onClose(function(code) {
380
+ if (watcher) {
381
+ watcher.close();
382
+ watcher = null;
383
+ }
384
+ process.exit(code !== null && code !== void 0 ? code : 1); // Exit with server's code or 1 if null
385
+ });
386
+ serverHandle.onError(function(err) {
387
+ console.error('Failed to start dev server:', err);
388
+ if (watcher) {
389
+ watcher.close();
390
+ watcher = null;
391
+ }
392
+ process.exit(1);
393
+ });
394
+ return [
395
+ 4,
396
+ serverHandle.waitUntilReady(15000)
397
+ ];
398
+ case 4:
399
+ isReady = _state.sent();
400
+ if (!isReady) {
401
+ console.error('⚠️ Server startup timeout: The server is taking longer than expected to initialize.');
402
+ // Clean up and exit
403
+ if (serverHandle && !serverHandle.killed) {
404
+ serverHandle.kill();
405
+ }
406
+ process.exit(1);
407
+ }
408
+ _state.label = 5;
409
+ case 5:
410
+ _state.trys.push([
411
+ 5,
412
+ 8,
413
+ ,
414
+ 9
415
+ ]);
416
+ return [
417
+ 4,
418
+ syncResources(this.server.projectRootDir)
419
+ ];
420
+ case 6:
421
+ syncResult = _state.sent();
422
+ if (syncResult.errorCount > 0) {
423
+ console.log("⚠️ Resource sync completed with ".concat(syncResult.errorCount, " errors:"));
424
+ syncResult.errors.forEach(function(error) {
425
+ console.log(" • ".concat(error.file, ": ").concat(error.message));
426
+ });
427
+ } else {
428
+ console.log("✅ Synced ".concat(syncResult.uploadCount, " resources (").concat(syncResult.skipCount, " up to date, ").concat(syncResult.deleteCount, " deleted)"));
429
+ }
430
+ return [
431
+ 4,
432
+ generateTypes(this.server.projectRootDir)
433
+ ];
434
+ case 7:
435
+ _state.sent();
436
+ return [
437
+ 3,
438
+ 9
439
+ ];
440
+ case 8:
441
+ error = _state.sent();
442
+ console.error('❌ Error during resource synchronization:', _instanceof(error, Error) ? error.message : String(error));
443
+ console.error('\nThe server is running, but resources may not be available to your brains.');
444
+ console.error('\nYou can manually sync resources by running: px resources sync');
445
+ return [
446
+ 3,
447
+ 9
448
+ ];
449
+ case 9:
450
+ // Watcher setup - target the user's brains and resources directories
451
+ watchPaths = [
452
+ path.join(brainsDir, '*.ts'),
453
+ path.join(resourcesDir, '**/*')
454
+ ];
455
+ watcher = chokidar.watch(watchPaths, {
456
+ ignored: [
457
+ /(^|[\/\\])\../,
458
+ '**/node_modules/**'
459
+ ],
460
+ persistent: true,
461
+ ignoreInitial: true,
462
+ awaitWriteFinish: {
463
+ stabilityThreshold: 200,
464
+ pollInterval: 100
465
+ }
466
+ });
467
+ handleResourceChange = function() {
468
+ return _async_to_generator(function() {
469
+ return _ts_generator(this, function(_state) {
470
+ switch(_state.label){
471
+ case 0:
472
+ return [
473
+ 4,
474
+ syncResources(this.server.projectRootDir)
475
+ ];
476
+ case 1:
477
+ _state.sent();
478
+ return [
479
+ 4,
480
+ generateTypes(this.server.projectRootDir)
481
+ ];
482
+ case 2:
483
+ _state.sent();
484
+ return [
485
+ 2
486
+ ];
487
+ }
488
+ });
489
+ }).call(_this);
490
+ };
491
+ watcher.on('add', function(filePath) {
492
+ return _async_to_generator(function() {
493
+ return _ts_generator(this, function(_state) {
494
+ switch(_state.label){
495
+ case 0:
496
+ if (!filePath.startsWith(resourcesDir)) return [
497
+ 3,
498
+ 2
499
+ ];
500
+ return [
501
+ 4,
502
+ handleResourceChange()
503
+ ];
504
+ case 1:
505
+ _state.sent();
506
+ return [
507
+ 3,
508
+ 4
509
+ ];
510
+ case 2:
511
+ if (!filePath.startsWith(brainsDir)) return [
512
+ 3,
513
+ 4
514
+ ];
515
+ if (!this.server.watch) return [
516
+ 3,
517
+ 4
518
+ ];
519
+ return [
520
+ 4,
521
+ this.server.watch(filePath, 'add')
522
+ ];
523
+ case 3:
524
+ _state.sent();
525
+ _state.label = 4;
526
+ case 4:
527
+ return [
528
+ 2
529
+ ];
530
+ }
531
+ });
532
+ }).call(_this);
533
+ }).on('change', function(filePath) {
534
+ return _async_to_generator(function() {
535
+ return _ts_generator(this, function(_state) {
536
+ switch(_state.label){
537
+ case 0:
538
+ if (!filePath.startsWith(resourcesDir)) return [
539
+ 3,
540
+ 2
541
+ ];
542
+ return [
543
+ 4,
544
+ handleResourceChange()
545
+ ];
546
+ case 1:
547
+ _state.sent();
548
+ return [
549
+ 3,
550
+ 4
551
+ ];
552
+ case 2:
553
+ if (!filePath.startsWith(brainsDir)) return [
554
+ 3,
555
+ 4
556
+ ];
557
+ if (!this.server.watch) return [
558
+ 3,
559
+ 4
560
+ ];
561
+ return [
562
+ 4,
563
+ this.server.watch(filePath, 'change')
564
+ ];
565
+ case 3:
566
+ _state.sent();
567
+ _state.label = 4;
568
+ case 4:
569
+ return [
570
+ 2
571
+ ];
572
+ }
573
+ });
574
+ }).call(_this);
575
+ }).on('unlink', function(filePath) {
576
+ return _async_to_generator(function() {
577
+ return _ts_generator(this, function(_state) {
578
+ switch(_state.label){
579
+ case 0:
580
+ if (!filePath.startsWith(resourcesDir)) return [
581
+ 3,
582
+ 2
583
+ ];
584
+ return [
585
+ 4,
586
+ handleResourceChange()
587
+ ];
588
+ case 1:
589
+ _state.sent();
590
+ return [
591
+ 3,
592
+ 4
593
+ ];
594
+ case 2:
595
+ if (!filePath.startsWith(brainsDir)) return [
596
+ 3,
597
+ 4
598
+ ];
599
+ if (!this.server.watch) return [
600
+ 3,
601
+ 4
602
+ ];
603
+ return [
604
+ 4,
605
+ this.server.watch(filePath, 'unlink')
606
+ ];
607
+ case 3:
608
+ _state.sent();
609
+ _state.label = 4;
610
+ case 4:
611
+ return [
612
+ 2
613
+ ];
614
+ }
615
+ });
616
+ }).call(_this);
617
+ }).on('error', function(error) {
618
+ return console.error("Watcher error: ".concat(error));
619
+ });
620
+ return [
621
+ 3,
622
+ 12
623
+ ];
624
+ case 10:
625
+ error1 = _state.sent();
626
+ console.error('An error occurred during server startup:', error1);
627
+ return [
628
+ 4,
629
+ cleanup()
630
+ ];
631
+ case 11:
632
+ _state.sent(); // Attempt cleanup on error
633
+ return [
634
+ 3,
635
+ 12
636
+ ];
637
+ case 12:
638
+ return [
639
+ 2
640
+ ];
641
+ }
642
+ });
643
+ }).call(this);
644
+ }
645
+ },
646
+ {
647
+ key: "handleDetached",
648
+ value: function handleDetached(argv) {
649
+ return _async_to_generator(function() {
650
+ var cliPath, args, logFile, out, err, child, pidFile;
651
+ return _ts_generator(this, function(_state) {
652
+ // Get the path to the current CLI executable
653
+ cliPath = process.argv[1];
654
+ // Build the command arguments
655
+ args = [
656
+ 'server'
657
+ ];
658
+ // Add optional arguments if they were provided
659
+ if (argv.force) args.push('--force');
660
+ if (argv.port) args.push('--port', String(argv.port));
661
+ if (argv.logFile) args.push('--log-file', argv.logFile);
662
+ // Determine output file for logs
663
+ logFile = argv.logFile || path.join(this.server.projectRootDir, '.positronic-server.log');
664
+ // Open log file in append mode
665
+ out = fs.openSync(logFile, 'a');
666
+ err = fs.openSync(logFile, 'a');
667
+ // Spawn the detached process with a special environment variable to skip PID check
668
+ child = spawn(process.execPath, [
669
+ cliPath
670
+ ].concat(_to_consumable_array(args)), {
671
+ detached: true,
672
+ stdio: [
673
+ 'ignore',
674
+ out,
675
+ err
676
+ ],
677
+ cwd: this.server.projectRootDir,
678
+ env: _object_spread_props(_object_spread({}, process.env), {
679
+ POSITRONIC_DETACHED_CHILD: 'true'
680
+ })
681
+ });
682
+ // Write the PID to a file for later reference
683
+ pidFile = this.getPidFilePath(argv.port);
684
+ fs.writeFileSync(pidFile, String(child.pid));
685
+ // Detach from the child process
686
+ child.unref();
687
+ console.log("✅ Server started in background (PID: ".concat(child.pid, ")"));
688
+ console.log(" Logs: ".concat(logFile));
689
+ console.log(" To stop: px server -k");
690
+ // Exit the parent process
691
+ process.exit(0);
692
+ return [
693
+ 2
694
+ ];
695
+ });
696
+ }).call(this);
697
+ }
698
+ },
699
+ {
700
+ key: "getPidFilePath",
701
+ value: function getPidFilePath(port) {
702
+ if (port) {
703
+ return path.join(this.server.projectRootDir, ".positronic-server-".concat(port, ".pid"));
704
+ }
705
+ return path.join(this.server.projectRootDir, '.positronic-server.pid');
706
+ }
707
+ },
708
+ {
709
+ key: "isProcessRunning",
710
+ value: function isProcessRunning(pid) {
711
+ try {
712
+ // This sends signal 0 which doesn't kill the process, just checks if it exists
713
+ process.kill(pid, 0);
714
+ return true;
715
+ } catch (e) {
716
+ return false;
717
+ }
718
+ }
719
+ },
720
+ {
721
+ key: "handleKill",
722
+ value: function handleKill(argv) {
723
+ return _async_to_generator(function() {
724
+ var pidFile, pid, killed, i, error;
725
+ return _ts_generator(this, function(_state) {
726
+ switch(_state.label){
727
+ case 0:
728
+ pidFile = path.join(this.server.projectRootDir, '.positronic-server.pid');
729
+ if (!fs.existsSync(pidFile)) {
730
+ console.error("❌ No default server is running");
731
+ console.error(" PID file not found: ".concat(pidFile));
732
+ process.exit(1);
733
+ }
734
+ _state.label = 1;
735
+ case 1:
736
+ _state.trys.push([
737
+ 1,
738
+ 6,
739
+ ,
740
+ 7
741
+ ]);
742
+ pid = parseInt(fs.readFileSync(pidFile, 'utf8').trim());
743
+ if (!this.isProcessRunning(pid)) {
744
+ console.log('⚠️ Server process not found, removing stale PID file');
745
+ fs.unlinkSync(pidFile);
746
+ process.exit(0);
747
+ }
748
+ // Kill the process
749
+ process.kill(pid, 'SIGTERM');
750
+ // Wait a moment to see if the process stops
751
+ killed = false;
752
+ i = 0;
753
+ _state.label = 2;
754
+ case 2:
755
+ if (!(i < 10)) return [
756
+ 3,
757
+ 5
758
+ ];
759
+ return [
760
+ 4,
761
+ new Promise(function(resolve) {
762
+ return setTimeout(resolve, 100);
763
+ })
764
+ ];
765
+ case 3:
766
+ _state.sent();
767
+ if (!this.isProcessRunning(pid)) {
768
+ killed = true;
769
+ return [
770
+ 3,
771
+ 5
772
+ ];
773
+ }
774
+ _state.label = 4;
775
+ case 4:
776
+ i++;
777
+ return [
778
+ 3,
779
+ 2
780
+ ];
781
+ case 5:
782
+ if (!killed) {
783
+ // Force kill if SIGTERM didn't work
784
+ console.log('⚠️ Server did not stop gracefully, forcing shutdown');
785
+ process.kill(pid, 'SIGKILL');
786
+ }
787
+ // Clean up PID file
788
+ if (fs.existsSync(pidFile)) {
789
+ fs.unlinkSync(pidFile);
790
+ }
791
+ console.log("✅ Server stopped (PID: ".concat(pid, ")"));
792
+ process.exit(0);
793
+ return [
794
+ 3,
795
+ 7
796
+ ];
797
+ case 6:
798
+ error = _state.sent();
799
+ console.error('❌ Failed to kill server:', _instanceof(error, Error) ? error.message : String(error));
800
+ process.exit(1);
801
+ return [
802
+ 3,
803
+ 7
804
+ ];
805
+ case 7:
806
+ return [
807
+ 2
808
+ ];
809
+ }
810
+ });
811
+ }).call(this);
812
+ }
813
+ }
814
+ ]);
815
+ return ServerCommand;
816
+ }();