@tabbr/module-cli 0.4.1 → 0.5.0
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 +26 -2
- package/bin/tabbr-module.js +38 -4
- package/package.json +1 -1
- package/src/config.js +89 -16
- package/src/source.js +6 -2
package/README.md
CHANGED
|
@@ -31,6 +31,30 @@ npx tabbr-module init # asks for the club code, module id and a fallback t
|
|
|
31
31
|
}
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
+
### Where you develop against: tabbr.config.local.json
|
|
35
|
+
|
|
36
|
+
`tabbr.config.json` describes the module and is committed. The club and API host *you* work against are yours, not
|
|
37
|
+
the module's: put them in `tabbr.config.local.json` - add it to `.gitignore` - and leave `tabbr.config.json` as it is
|
|
38
|
+
(e.g. on production, for the store). `init --local` writes it:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npx tabbr-module init --local # asks for the club code and the API host (default: your local backend,
|
|
42
|
+
# https://api.local.tabbr.be; https://api.tabbr.be for a club on production)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{ "club": "ABC", "apiHost": "https://api.local.tabbr.be" }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
It may set `club`, `apiHost`, `localApiHost` and `shellUrl` (other keys are ignored, with a warning) and overrides
|
|
50
|
+
`tabbr.config.json` for them in every command - except `publish --store`, which goes where `tabbr.config.json` says
|
|
51
|
+
unless you pass `--local` or `--api-host`. Command-line options still win, and the CLI says which file it used.
|
|
52
|
+
The one next to `tabbr.config.json` counts, else the one a folder up - one file for every module in that folder, as
|
|
53
|
+
the Tabbr repository's `modules/` has. The module's dev preview reads the same one: its `src/app/app.config.ts` imports
|
|
54
|
+
`tabbr.config.local.json`, which the `paths` in `tsconfig.local-config.json` resolve to that file, else to
|
|
55
|
+
`tabbr.config.json` itself (copy both from one of the Tabbr repository's modules). It's never part of a store
|
|
56
|
+
submission's source.
|
|
57
|
+
|
|
34
58
|
### The title and description of your module
|
|
35
59
|
|
|
36
60
|
The name Tabbr shows for your module (in the settings list and its page header) is translated like the one of its
|
|
@@ -127,7 +151,7 @@ rules, the club's extra fields, hooks, audit). Add an `"entities"` array to `tab
|
|
|
127
151
|
`publish` checks the declarations (names, keys, types) before uploading and sends them in the backend manifest - also
|
|
128
152
|
for a module that has no backend code (then only the entities are published). The frontend addresses records by field
|
|
129
153
|
key with the SDK's `injectModuleEntity()`, the backend with `tabbr.entities.<name>`. See `docs/module-entities.md` in the
|
|
130
|
-
Tabbr repository for the field types, access rules and what the club gets; `tabbr-groups-module/` is a complete example.
|
|
154
|
+
Tabbr repository for the field types, access rules and what the club gets; `modules/tabbr-groups-module/` is a complete example.
|
|
131
155
|
|
|
132
156
|
## Contracts
|
|
133
157
|
|
|
@@ -195,7 +219,7 @@ upload:
|
|
|
195
219
|
|
|
196
220
|
`npx tabbr-module publish --local` publishes into your own local tabbr (the environment behind
|
|
197
221
|
`<club>.local.tabbr.be`) instead of production: it uploads to `https://api.local.tabbr.be` (set
|
|
198
|
-
`"localApiHost"` in `tabbr.config.json` to use another URL) and trusts your mkcert root CA, which Node
|
|
222
|
+
`"localApiHost"` in `tabbr.config.local.json` to use another URL) and trusts your mkcert root CA, which Node
|
|
199
223
|
doesn't do on its own (`NODE_EXTRA_CA_CERTS` is set automatically; if mkcert's root CA can't be found,
|
|
200
224
|
set it yourself - `mkcert -CAROOT` shows where it lives). `--club` picks the club in that environment;
|
|
201
225
|
login still goes through the real Keycloak, which local backends validate too.
|
package/bin/tabbr-module.js
CHANGED
|
@@ -9,7 +9,9 @@ const { publish, publishToStore, submitSourceToStore, storeMeta } = require('../
|
|
|
9
9
|
const { packSource } = require('../src/source');
|
|
10
10
|
const { fetchScriptTypes, fetchAllEntityTypes, fetchContractTypes, VALID_KINDS } = require('../src/fetch-types');
|
|
11
11
|
const { usesOf } = require('../src/backend-build');
|
|
12
|
-
const {
|
|
12
|
+
const {
|
|
13
|
+
CONFIG_FILE, LOCAL_CONFIG_FILE, LOCAL_CONFIG_KEYS, DEFAULT_API_HOST, readConfig, readLocalConfig, requireClub, runInit,
|
|
14
|
+
} = require('../src/config');
|
|
13
15
|
const { DEFAULT_LOCAL_API_HOST, trustLocalCertificateOrRespawn } = require('../src/local');
|
|
14
16
|
const DEFAULT_KIND = 'HOOK'; // matches the server's own @RequestParam default for /meta/script
|
|
15
17
|
|
|
@@ -46,10 +48,22 @@ function printUsage() {
|
|
|
46
48
|
console.log(`Every option below can also be set in ${CONFIG_FILE} in the workspace root (option name
|
|
47
49
|
in camelCase, e.g. "club", "moduleId", "apiHost"); command-line options win.
|
|
48
50
|
|
|
51
|
+
Where YOU develop against goes in ${LOCAL_CONFIG_FILE} instead: not committed (add it to
|
|
52
|
+
.gitignore), next to ${CONFIG_FILE} or a folder up (then for every module in that folder; the
|
|
53
|
+
one in the workspace wins). Its ${LOCAL_CONFIG_KEYS.join(', ')} override those in ${CONFIG_FILE} -
|
|
54
|
+
here and in the module's dev preview; not the API host of "publish --store". E.g. { "club": "ABC",
|
|
55
|
+
"apiHost": "https://api.local.tabbr.be" } works against your local backend while ${CONFIG_FILE}
|
|
56
|
+
keeps production; "init --local" writes it.
|
|
57
|
+
|
|
49
58
|
Usage: tabbr-module init [--club <CODE>] [--module-id <id>] [--title <text>] [--api-host <url>]
|
|
50
59
|
|
|
51
60
|
Asks for the club code (and module id / title) and writes ${CONFIG_FILE}.
|
|
52
61
|
|
|
62
|
+
Usage: tabbr-module init --local [--club <CODE>] [--api-host <url>]
|
|
63
|
+
|
|
64
|
+
Asks for the club code and the API host (by default your own local backend, ${DEFAULT_LOCAL_API_HOST})
|
|
65
|
+
and writes only ${LOCAL_CONFIG_FILE}. ${CONFIG_FILE} is left as it is.
|
|
66
|
+
|
|
53
67
|
Usage: tabbr-module publish [options]
|
|
54
68
|
|
|
55
69
|
Builds and publishes whichever parts of the workspace are present, in ONE upload: a frontend (an
|
|
@@ -71,7 +85,9 @@ Backend part (all in ${CONFIG_FILE} under "backend", not as CLI flags):
|
|
|
71
85
|
secretFieldNames Names of your module's settings keys that hold secrets (masked from
|
|
72
86
|
every settings GET response once saved)
|
|
73
87
|
triggers [{type: "CRON", name, cronExpression, handler} |
|
|
74
|
-
{type: "ENTITY_HOOK", name, entity, event, handler}
|
|
88
|
+
{type: "ENTITY_HOOK", name, entity, event, handler} |
|
|
89
|
+
{type: "PUBLIC", name, handler, params} (module API 1.5:
|
|
90
|
+
GET /api/<club>/public/module/<moduleId>/data/<name>, no login)]
|
|
75
91
|
|
|
76
92
|
Optional:
|
|
77
93
|
--icon <name> Menu icon name
|
|
@@ -316,13 +332,31 @@ async function main() {
|
|
|
316
332
|
await runInit(flags.cwd, flags);
|
|
317
333
|
return;
|
|
318
334
|
}
|
|
319
|
-
const
|
|
335
|
+
const local = readLocalConfig(flags.cwd);
|
|
336
|
+
const localValues = { ...(local ? local.values : {}) };
|
|
337
|
+
// A store submission goes where tabbr.config.json says (production), not to a local file's API host: --local or
|
|
338
|
+
// --api-host for another one.
|
|
339
|
+
const storeHostKept = command === 'publish' && flags.store && localValues.apiHost !== undefined;
|
|
340
|
+
if (storeHostKept) delete localValues.apiHost;
|
|
341
|
+
const args = { apiHost: DEFAULT_API_HOST, ...readConfig(flags.cwd), ...localValues, ...flags };
|
|
320
342
|
if (args.local && !flags.apiHost) {
|
|
321
343
|
// --local targets your own local backend (https://api.local.tabbr.be, the same one
|
|
322
|
-
// <club>.local.tabbr.be talks to); "localApiHost" in tabbr.config.json overrides it.
|
|
344
|
+
// <club>.local.tabbr.be talks to); "localApiHost" in tabbr.config.local.json or tabbr.config.json overrides it.
|
|
323
345
|
args.apiHost = args.localApiHost || DEFAULT_LOCAL_API_HOST;
|
|
324
346
|
}
|
|
325
347
|
trustLocalCertificateOrRespawn(args.apiHost);
|
|
348
|
+
// after a respawn for the local certificate: said once
|
|
349
|
+
if (local) {
|
|
350
|
+
const where = path.relative(flags.cwd, local.file);
|
|
351
|
+
const set = Object.entries(localValues).map(([key, value]) => `${key} ${value}`).join(', ');
|
|
352
|
+
console.log(`Using ${where}${set ? `: ${set}` : ''}`);
|
|
353
|
+
if (storeHostKept) {
|
|
354
|
+
console.log(` not its apiHost: the module store at ${args.apiHost}${args.local || flags.apiHost ? '' : ' (--local for your local backend\'s)'}`);
|
|
355
|
+
}
|
|
356
|
+
if (local.ignored.length) {
|
|
357
|
+
console.warn(` ignored there: ${local.ignored.join(', ')} - only ${LOCAL_CONFIG_KEYS.join(', ')} can be set in ${LOCAL_CONFIG_FILE}`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
326
360
|
if (command === 'publish') {
|
|
327
361
|
await runPublish(args);
|
|
328
362
|
} else if (command === 'build') {
|
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -3,10 +3,23 @@
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const readline = require('readline/promises');
|
|
6
|
+
const { execFileSync } = require('child_process');
|
|
7
|
+
const { DEFAULT_LOCAL_API_HOST } = require('./local');
|
|
6
8
|
|
|
7
9
|
const CONFIG_FILE = 'tabbr.config.json';
|
|
10
|
+
const LOCAL_CONFIG_FILE = 'tabbr.config.local.json';
|
|
11
|
+
/** What a tabbr.config.local.json may set: where this checkout talks to - never what the module is. */
|
|
12
|
+
const LOCAL_CONFIG_KEYS = ['club', 'apiHost', 'localApiHost', 'shellUrl'];
|
|
8
13
|
const DEFAULT_API_HOST = 'https://api.tabbr.be';
|
|
9
14
|
|
|
15
|
+
function readJson(file) {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
18
|
+
} catch (err) {
|
|
19
|
+
throw new Error(`${file} is not valid JSON: ${err.message}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
10
23
|
/**
|
|
11
24
|
* Reads the workspace's tabbr.config.json - the one place a module project records which club it
|
|
12
25
|
* targets, its module id and so on. Both this CLI and the module's own dev-preview harness read it,
|
|
@@ -14,18 +27,31 @@ const DEFAULT_API_HOST = 'https://api.tabbr.be';
|
|
|
14
27
|
*/
|
|
15
28
|
function readConfig(cwd) {
|
|
16
29
|
const file = path.join(cwd, CONFIG_FILE);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
30
|
+
return fs.existsSync(file) ? readJson(file) : {};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The developer's own tabbr.config.local.json (not committed): the club and API host this checkout
|
|
35
|
+
* talks to, over those in tabbr.config.json - so a module whose tabbr.config.json targets
|
|
36
|
+
* production can be developed against a local backend without editing it. The one in the
|
|
37
|
+
* workspace, else the one a folder up (which then covers every module in that folder); the module's
|
|
38
|
+
* dev preview finds the same one (tsconfig.local-config.json). Only LOCAL_CONFIG_KEYS count, the
|
|
39
|
+
* other keys are returned as `ignored`. Returns null when there is none.
|
|
40
|
+
*/
|
|
41
|
+
function readLocalConfig(cwd) {
|
|
42
|
+
const file = [path.join(cwd, LOCAL_CONFIG_FILE), path.join(cwd, '..', LOCAL_CONFIG_FILE)].find(f => fs.existsSync(f));
|
|
43
|
+
if (!file) return null;
|
|
44
|
+
const all = readJson(file);
|
|
45
|
+
const values = Object.fromEntries(LOCAL_CONFIG_KEYS.filter(key => all[key] !== undefined).map(key => [key, all[key]]));
|
|
46
|
+
const ignored = Object.keys(all).filter(key => !LOCAL_CONFIG_KEYS.includes(key));
|
|
47
|
+
return { file, values, ignored };
|
|
23
48
|
}
|
|
24
49
|
|
|
25
50
|
/** Uppercase club code, or throws with instructions when none is configured. */
|
|
26
51
|
function requireClub(club) {
|
|
27
52
|
if (!club) {
|
|
28
|
-
throw new Error(`No club configured.
|
|
53
|
+
throw new Error(`No club configured. Set "club" in ${LOCAL_CONFIG_FILE} (yours, not committed) or ${CONFIG_FILE}, ` +
|
|
54
|
+
'or run "npx tabbr-module init".');
|
|
29
55
|
}
|
|
30
56
|
return club.toUpperCase();
|
|
31
57
|
}
|
|
@@ -34,19 +60,39 @@ function slug(text) {
|
|
|
34
60
|
return text.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
35
61
|
}
|
|
36
62
|
|
|
37
|
-
/**
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
63
|
+
/** False only when the workspace is in a git repository that would track `file`. */
|
|
64
|
+
function gitIgnores(cwd, file) {
|
|
65
|
+
try {
|
|
66
|
+
execFileSync('git', ['check-ignore', '-q', file], { cwd, stdio: 'ignore' });
|
|
67
|
+
return true;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
return err.status !== 1; // 1: not ignored; anything else: no git, or not a repository
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function asker() {
|
|
44
74
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
45
75
|
const ask = async (question, fallback) => {
|
|
46
76
|
if (!process.stdin.isTTY) return fallback;
|
|
47
77
|
const answer = (await rl.question(fallback ? `${question} [${fallback}]: ` : `${question}: `)).trim();
|
|
48
78
|
return answer || fallback;
|
|
49
79
|
};
|
|
80
|
+
return { ask, close: () => rl.close() };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Asks for the club code (and offers defaults for the rest) and writes tabbr.config.json, keeping
|
|
85
|
+
* any values already in it as the defaults. With --local: only where you develop against, in
|
|
86
|
+
* tabbr.config.local.json - see runInitLocal.
|
|
87
|
+
*/
|
|
88
|
+
async function runInit(cwd, overrides) {
|
|
89
|
+
if (overrides.local) {
|
|
90
|
+
await runInitLocal(cwd, overrides);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const file = path.join(cwd, CONFIG_FILE);
|
|
94
|
+
const current = readConfig(cwd);
|
|
95
|
+
const { ask, close } = asker();
|
|
50
96
|
|
|
51
97
|
try {
|
|
52
98
|
const club = (overrides.club || await ask('Club code (the part before .tabbr.be, e.g. abc)', current.club))?.toUpperCase();
|
|
@@ -68,8 +114,35 @@ async function runInit(cwd, overrides) {
|
|
|
68
114
|
fs.writeFileSync(file, JSON.stringify(config, null, 2) + '\n');
|
|
69
115
|
console.log(`\nWrote ${file}. Next: "npm start" to preview, "npm run publish-module" to publish.`);
|
|
70
116
|
} finally {
|
|
71
|
-
|
|
117
|
+
close();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* `init --local`: asks for the club code and the API host - by default your own local backend
|
|
123
|
+
* (https://api.local.tabbr.be) - and writes them in the workspace's tabbr.config.local.json, and
|
|
124
|
+
* nothing else: tabbr.config.json, the module's manifest, stays as it is (e.g. on production, for
|
|
125
|
+
* the store).
|
|
126
|
+
*/
|
|
127
|
+
async function runInitLocal(cwd, overrides) {
|
|
128
|
+
const file = path.join(cwd, LOCAL_CONFIG_FILE);
|
|
129
|
+
const current = fs.existsSync(file) ? readJson(file) : {};
|
|
130
|
+
const { ask, close } = asker();
|
|
131
|
+
try {
|
|
132
|
+
const club = (overrides.club || await ask('Club code (e.g. abc)', current.club || readConfig(cwd).club))?.toUpperCase();
|
|
133
|
+
if (!club) throw new Error('A club code is required.');
|
|
134
|
+
const apiHost = overrides.apiHost
|
|
135
|
+
|| await ask(`API host (${DEFAULT_API_HOST} for a club on production)`, current.apiHost || DEFAULT_LOCAL_API_HOST);
|
|
136
|
+
const local = { ...current, club, apiHost };
|
|
137
|
+
fs.writeFileSync(file, JSON.stringify(local, null, 2) + '\n');
|
|
138
|
+
console.log(`\nWrote ${file}: club ${club}, apiHost ${apiHost}. Next: "npm start" to preview, ` +
|
|
139
|
+
'"npm run publish-module" to publish there.');
|
|
140
|
+
if (!gitIgnores(cwd, file)) console.warn(`It's yours, not the module's: add ${LOCAL_CONFIG_FILE} to .gitignore.`);
|
|
141
|
+
} finally {
|
|
142
|
+
close();
|
|
72
143
|
}
|
|
73
144
|
}
|
|
74
145
|
|
|
75
|
-
module.exports = {
|
|
146
|
+
module.exports = {
|
|
147
|
+
CONFIG_FILE, LOCAL_CONFIG_FILE, LOCAL_CONFIG_KEYS, DEFAULT_API_HOST, readConfig, readLocalConfig, requireClub, runInit,
|
|
148
|
+
};
|
package/src/source.js
CHANGED
|
@@ -5,8 +5,12 @@ const path = require('path');
|
|
|
5
5
|
const { execFileSync } = require('child_process');
|
|
6
6
|
const archiver = require('archiver');
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Never part of a module's source: installed packages, build output, caches, editor state - and the developer's own
|
|
10
|
+
* tabbr.config.local.json (where they develop against), also when it isn't in .gitignore.
|
|
11
|
+
*/
|
|
12
|
+
const SKIP = new Set(['node_modules', 'dist', '.angular', 'out-tsc', '.git', '.yalc', '.idea', '.vscode', 'coverage',
|
|
13
|
+
'tabbr.config.local.json']);
|
|
10
14
|
|
|
11
15
|
/**
|
|
12
16
|
* The workspace's source files, as the module store builds them: the files git tracks (plus new ones git doesn't ignore)
|