@nodegx/backend 0.0.0-stage → 0.1.1

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/README.md CHANGED
@@ -1,3 +1,57 @@
1
- # Temporary Holding Version
1
+ # @nodegx/backend
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Puts a NodeGX app online on your own Cloudflare account. The app's pages, its backend (records, accounts, rules,
4
+ files), its cloud functions and its schedules all go on one Cloudflare Worker, at
5
+ `https://<app name>.<your subdomain>.workers.dev`. Running it again after a change keeps the same address and keeps
6
+ every record, account and file.
7
+
8
+ A free Cloudflare account is enough. Past the free plan's daily limit, Cloudflare refuses requests rather than
9
+ charging you.
10
+
11
+ ## Use
12
+
13
+ ```sh
14
+ npx @nodegx/backend sign-in cloudflare
15
+ npx @nodegx/backend deploy cloudflare ./my-app --no-bucket
16
+ ```
17
+
18
+ The first command opens a Cloudflare page in your browser. You sign in (or make a free account) and click Allow. It
19
+ is Cloudflare's own command-line sign-in (wrangler's), kept on this computer.
20
+
21
+ The second puts the app online and prints its address. It runs four steps:
22
+
23
+ 1. it bundles the app's cloud functions,
24
+ 2. it builds the site with NodeGX's production engine,
25
+ 3. it uploads the site and the backend to one Worker,
26
+ 4. it applies the app's collections, rules and roles to the running backend.
27
+
28
+ `--no-bucket` leaves out file storage. Leave it off when the app lets people upload files. Then the
29
+ deploy makes an R2 bucket for them, and R2 has to be switched on in your Cloudflare dashboard first.
30
+
31
+ Other options:
32
+
33
+ | option | what it does |
34
+ |---|---|
35
+ | `--name <worker>` | the Worker's name, which is the first part of the address (default: the app's name) |
36
+ | `--dry-run` | builds everything and checks nothing private would be uploaded, then stops |
37
+ | `--local` | runs the same Worker on this computer (Cloudflare's `workerd`), at `http://127.0.0.1:8787` |
38
+ | `--domain <name>` | the app answers at a domain on your Cloudflare account too |
39
+ | `--new-admin-credential` | replaces the backend's admin credential |
40
+
41
+ The deployment's folder is `~/.nodegx/cloudflare/<name>/`. Its `deployment.json` holds the address and the
42
+ backend's admin credential, readable only by you. The credential is never uploaded; the deploy checks every file
43
+ before it uploads anything. The backend's dashboard is at `<address>/_admin`.
44
+
45
+ `npx @nodegx/backend domain find <words>` finds a name that is free and its price. `domain buy` buys one only when
46
+ you repeat its exact price back. Buying needs Cloudflare's own `cf` command signed in, because wrangler's sign-in
47
+ cannot buy names.
48
+
49
+ ## What it needs
50
+
51
+ - Node.js 20 or newer, and npm.
52
+ - A Cloudflare account. wrangler (Cloudflare's command line) is fetched by `npx` the first time.
53
+
54
+ ## Licence
55
+
56
+ GPL-3.0-only (it carries the NodeGX editor's cloud-function bundler). The backend inside the Worker
57
+ (`dist/worker/`) is MIT; its licence is beside it.
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * `npx @nodegx/backend …` — put a NodeGX app online on the person's own Cloudflare account.
4
+ *
5
+ * npx @nodegx/backend sign-in cloudflare wrangler's own sign-in: a Cloudflare page opens, click Allow
6
+ * npx @nodegx/backend whoami cloudflare which Cloudflare account this computer is signed in to
7
+ * npx @nodegx/backend deploy cloudflare <app folder> […] the pages, the backend, its rules and functions, one Worker
8
+ * npx @nodegx/backend domain find|check|buy|status|email … the app's own name (see the domain command's help)
9
+ *
10
+ * Everything that is not the app's own was built when the package was packed (`build.mjs`): the deploy, the Worker,
11
+ * the cloud-function bundler. This file only tells the deploy where those are, and where the two packages it runs
12
+ * are (`@nodegx/export`'s `nodegx deploy`, over `@nodegx/preview`'s engine and production viewer).
13
+ */
14
+ 'use strict';
15
+
16
+ const fs = require('fs');
17
+ const path = require('path');
18
+ const { spawnSync } = require('child_process');
19
+
20
+ const DIST = path.join(__dirname, '..', 'dist');
21
+ const PKG = require(path.join(__dirname, '..', 'package.json'));
22
+
23
+ const HELP = `@nodegx/backend ${PKG.version} — put a NodeGX app online on your own Cloudflare account.
24
+
25
+ npx @nodegx/backend sign-in cloudflare
26
+ Signs this computer in to Cloudflare (wrangler's sign-in): a Cloudflare page opens, you click Allow.
27
+ npx @nodegx/backend whoami cloudflare
28
+ Says which Cloudflare account this computer is signed in to.
29
+ npx @nodegx/backend deploy cloudflare <app folder> [--no-bucket] [--name <worker>] [--dry-run] [--local] …
30
+ Puts the app online: its pages, its backend, its rules and its functions, on one Worker, at
31
+ https://<name>.<your subdomain>.workers.dev. Run it again after a change: same address, nothing wiped.
32
+ npx @nodegx/backend domain find|check|buy|status|email …
33
+ Finds and buys the app's own name on your Cloudflare account, and sets up its email.
34
+
35
+ Every option of deploy: npx @nodegx/backend deploy cloudflare --help`;
36
+
37
+ /** A file inside an installed package, found the way Node finds the package (no `exports` map in the way). */
38
+ function packageFile(name, rel) {
39
+ for (const dir of require.resolve.paths(name) || []) {
40
+ const file = path.join(dir, name, rel);
41
+ if (fs.existsSync(file)) return file;
42
+ }
43
+ return null;
44
+ }
45
+
46
+ function need(file, what) {
47
+ if (file && fs.existsSync(file)) return file;
48
+ process.stderr.write(`@nodegx/backend: ${what} is missing, so this install is incomplete. Run it again with @latest:\n npx -y @nodegx/backend@latest …\n`);
49
+ process.exit(2);
50
+ }
51
+
52
+ /** The deploy's settings, unless the person (or a checkout) already set them. */
53
+ function deployEnvironment() {
54
+ const set = (k, v) => {
55
+ if (!process.env[k]) process.env[k] = v;
56
+ };
57
+ // The deploy's own folder, as it sees it (where it looks for a wrangler installed beside it).
58
+ set('NODEGX_CLOUDFLARE_SRC', path.join(DIST, 'cloudflare'));
59
+ set('NODEGX_CLOUDFLARE_WORKER', need(path.join(DIST, 'worker', 'worker.mjs'), 'The Worker (dist/worker/worker.mjs)'));
60
+ set('NODEGX_CLOUD_BUNDLE', need(path.join(DIST, 'cloud-bundle.cjs'), 'The cloud-function bundler (dist/cloud-bundle.cjs)'));
61
+ set('NODEGX_CLI', need(packageFile('@nodegx/export', path.join('dist', 'cli.mjs')), "@nodegx/export's nodegx command"));
62
+ set('NODEGX_DEPLOY_CLI', need(packageFile('@nodegx/preview', path.join('dist', 'nodegx-deploy.cjs')), "@nodegx/preview's deploy engine"));
63
+ set('NODEGX_CLOUDFLARE_COMMAND', 'npx @nodegx/backend deploy cloudflare');
64
+ }
65
+
66
+ function provider(word, verb) {
67
+ if (word === 'cloudflare') return;
68
+ process.stderr.write(
69
+ word ? `@nodegx/backend: "${word}" is not a host it can ${verb} yet. Cloudflare is the one: ${verb} cloudflare …\n` : `@nodegx/backend: name the host: ${verb} cloudflare …\n`
70
+ );
71
+ process.exit(2);
72
+ }
73
+
74
+ /** wrangler, as the deploy itself runs it (one installed nearby, else `npx wrangler@4`), with the terminal attached. */
75
+ function wrangler(args) {
76
+ deployEnvironment();
77
+ const { wranglerCommand } = require(need(path.join(DIST, 'cloudflare-deploy.cjs'), 'The deploy (dist/cloudflare-deploy.cjs)'));
78
+ const [cmd, ...pre] = wranglerCommand();
79
+ const run = spawnSync(cmd, [...pre, ...args], { stdio: 'inherit', env: { ...process.env, WRANGLER_SEND_METRICS: 'false' } });
80
+ return run.status ?? 1;
81
+ }
82
+
83
+ async function main(argv) {
84
+ const [verb, ...rest] = argv;
85
+ if (!verb || verb === 'help' || verb === '--help' || verb === '-h') {
86
+ process.stdout.write(HELP + '\n');
87
+ return verb ? 0 : 2;
88
+ }
89
+ if (verb === '--version' || verb === '-v') {
90
+ process.stdout.write(PKG.version + '\n');
91
+ return 0;
92
+ }
93
+ if (verb === 'sign-in' || verb === 'login') {
94
+ provider(rest[0], 'sign in to');
95
+ return wrangler(['login']);
96
+ }
97
+ if (verb === 'whoami') {
98
+ provider(rest[0], 'say the account of');
99
+ return wrangler(['whoami']);
100
+ }
101
+ if (verb === 'deploy') {
102
+ provider(rest[0], 'deploy to');
103
+ deployEnvironment();
104
+ const { main: deploy } = require(path.join(DIST, 'cloudflare-deploy.cjs'));
105
+ const args = rest.slice(1);
106
+ if (!args.length || args.includes('--help') || args.includes('-h')) args.length = 0;
107
+ return deploy(args);
108
+ }
109
+ if (verb === 'domain') {
110
+ const { main: domain } = require(need(path.join(DIST, 'cloudflare-domain.cjs'), 'The domain command (dist/cloudflare-domain.cjs)'));
111
+ return domain(rest);
112
+ }
113
+ process.stderr.write(`@nodegx/backend: "${verb}" is not a command.\n\n${HELP}\n`);
114
+ return 2;
115
+ }
116
+
117
+ main(process.argv.slice(2)).then(
118
+ (code) => process.exit(code),
119
+ (e) => {
120
+ process.stderr.write(`${(e && e.stack) || e}\n`);
121
+ process.exit(1);
122
+ }
123
+ );