@nativescript/cloudflare-cli 0.1.0-beta.4 → 0.1.0-beta.6

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 (3) hide show
  1. package/README.md +1 -1
  2. package/dist/bin.js +42 -13
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -6,7 +6,7 @@ npx @nativescript/cloudflare-cli live --mac # a Mac app: opens the IDE and ru
6
6
  npx @nativescript/cloudflare-cli live --electron # an Electron app with native views, run the same way
7
7
  ```
8
8
 
9
- `live` creates a project on preview.nativescript.org (`--server` or `NS_EDGE_SERVER` for another edge), saves it to `.ns-edge.json` and opens its IDE. Run it again in the same folder to reopen that project, or add `--new` for another. Without a create token the project is free: a few hosted agent turns a day, no backend, deleted after 30 days unused. `--no-open` skips the browser; with `--mac` or `--electron`, `--allow-eval` lets the agent run code on this Mac.
9
+ `live` creates a project on preview.nativescript.org (`--server` or `NS_EDGE_SERVER` for another edge), saves it to `.ns-edge.json` and opens its IDE. Run it again in the same folder to reopen that project (no platform flag needed), or add `--new` for another. While `live` runs a Mac app it restarts the app if it crashes, unless it crashes three times in a minute. Without a create token the project is free: a few hosted agent turns a day, no backend, deleted after 30 days unused. `--no-open` skips the browser; with `--mac` or `--electron`, `--allow-eval` lets the agent run code on this Mac.
10
10
 
11
11
  ## Mirror a local project
12
12
 
package/dist/bin.js CHANGED
@@ -11,10 +11,13 @@ const DEBOUNCE_MS = 120;
11
11
  const LIVE_SERVER = process.env.NS_EDGE_SERVER ?? 'https://preview.nativescript.org';
12
12
  /** The hosts `live --mac` and `live --electron` run; bump them with each host release this CLI is tested against. */
13
13
  const DESKTOP_HOSTS = {
14
- macos: '@nativescript/cloudflare-desktop@0.1.0-alpha.3',
15
- electron: '@nativescript/cloudflare-electron@0.1.0-alpha.0',
14
+ macos: '@nativescript/cloudflare-desktop@0.1.0-alpha.5',
15
+ electron: '@nativescript/cloudflare-electron@0.1.0-alpha.2',
16
16
  };
17
17
  const DESKTOP_TEMPLATES = new Set(['macos', 'electron']);
18
+ const MAX_CRASHES = 3;
19
+ const CRASH_WINDOW_MS = 60_000;
20
+ const RESTART_DELAY_MS = 1000;
18
21
  const [cmd = 'help', ...args] = process.argv.slice(2);
19
22
  /**
20
23
  * Editors and tools write temp, swap and backup files next to the real one
@@ -75,21 +78,46 @@ function openInBrowser(url) {
75
78
  const [command, ...rest] = process.platform === 'darwin' ? ['open', url] : process.platform === 'win32' ? ['cmd', '/c', 'start', '""', url.replace(/&/g, '^&')] : ['xdg-open', url];
76
79
  spawn(command, rest, { stdio: 'ignore', detached: true }).on('error', () => { }).unref();
77
80
  }
78
- /** Runs the Mac host for a desktop project until it exits; the host says what joining allows. */
79
- function runDesktopHost(ref) {
81
+ /**
82
+ * Runs the Mac host for a desktop project until it quits or Ctrl+C. A host that
83
+ * crashes is started again, since nothing in the browser can reach this Mac to
84
+ * do it, unless it keeps crashing. The host says what joining allows.
85
+ */
86
+ async function runDesktopHost(ref) {
80
87
  if (process.platform !== 'darwin' || process.arch !== 'arm64') {
81
88
  throw new Error('Mac projects run on an Apple Silicon Mac (macOS 14.4 or later). Run the same command there.');
82
89
  }
83
90
  const device = `${projectUrl(ref)}#${ref.deviceKey}`;
84
91
  const extra = has('allow-eval') ? ['--allow-eval'] : [];
85
92
  const host = DESKTOP_HOSTS[ref.template ?? 'macos'] ?? DESKTOP_HOSTS.macos;
86
- const child = spawn('npx', ['-y', host, device, '--yes', ...extra], { stdio: 'inherit' });
87
- for (const signal of ['SIGINT', 'SIGTERM'])
88
- process.on(signal, () => child.kill(signal));
89
- return new Promise((resolve, reject) => {
90
- child.on('error', reject);
91
- child.on('exit', (code) => (code ? reject(new Error(`The Mac host exited with code ${code}.`)) : resolve()));
92
- });
93
+ let child;
94
+ let stopping = false;
95
+ for (const signal of ['SIGINT', 'SIGTERM']) {
96
+ process.on(signal, () => {
97
+ stopping = true;
98
+ child?.kill(signal);
99
+ });
100
+ }
101
+ const crashes = [];
102
+ for (;;) {
103
+ child = spawn('npx', ['-y', host, device, '--yes', ...extra], { stdio: 'inherit' });
104
+ const exit = await new Promise((resolve, reject) => {
105
+ child.on('error', reject);
106
+ child.on('exit', (code, signal) => resolve({ code, signal }));
107
+ });
108
+ if (stopping || exit.code === 0)
109
+ return;
110
+ const now = Date.now();
111
+ crashes.push(now);
112
+ while (crashes[0] < now - CRASH_WINDOW_MS)
113
+ crashes.shift();
114
+ const why = exit.signal ?? `code ${exit.code}`;
115
+ if (crashes.length > MAX_CRASHES) {
116
+ throw new Error(`The Mac host crashed ${crashes.length} times in a minute (last: ${why}), so it wasn't restarted. Fix the error above, then run this command again.`);
117
+ }
118
+ console.log(`\nThe Mac host stopped (${why}). Starting it again…\n`);
119
+ await new Promise((r) => setTimeout(r, RESTART_DELAY_MS));
120
+ }
93
121
  }
94
122
  const commands = {
95
123
  async live() {
@@ -97,9 +125,10 @@ const commands = {
97
125
  if (has(later))
98
126
  throw new Error(`--${later} isn't available yet. Run \`live\` for a phone app or \`live --mac\` for a Mac app.`);
99
127
  }
100
- const template = flag('template') ?? (has('electron') ? 'electron' : has('mac') ? 'macos' : 'core');
101
- const desktop = DESKTOP_TEMPLATES.has(template);
102
128
  let ref = has('new') ? null : await savedProject();
129
+ // Without a platform flag, this folder's project runs as whatever it was made as.
130
+ const template = flag('template') ?? (has('electron') ? 'electron' : has('mac') ? 'macos' : (ref?.template ?? 'core'));
131
+ const desktop = DESKTOP_TEMPLATES.has(template);
103
132
  if (ref && DESKTOP_TEMPLATES.has(ref.template ?? 'core') !== desktop) {
104
133
  throw new Error(`This folder's project (${CONFIG}) is a ${desktop ? 'phone' : 'Mac'} project. Add --new to replace it, or run this in another folder.`);
105
134
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativescript/cloudflare-cli",
3
- "version": "0.1.0-beta.4",
3
+ "version": "0.1.0-beta.6",
4
4
  "description": "Mirror a NativeScript project into an ns-edge project from a terminal or a Vite dev server",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",