@jinshuju/cli 0.30.0 → 0.32.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 +8 -1
- package/dist/args.d.ts +20 -0
- package/dist/args.js +50 -0
- package/dist/cli.js +15 -8
- package/dist/commands/entry.js +30 -18
- package/dist/commands/field.js +177 -17
- package/dist/commands/folder.js +5 -4
- package/dist/commands/form.js +45 -0
- package/dist/commands/index.d.ts +1 -1
- package/dist/commands/index.js +26 -3
- package/dist/commands/shared.js +31 -3
- package/dist/commands/types.d.ts +9 -0
- package/dist/generated/operations.d.ts +1 -1
- package/dist/generated/operations.js +90 -5
- package/dist/help.js +32 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -235,6 +235,8 @@ jinshuju form theme set Kp7mQ2 --primary-color "#1F6FEB"
|
|
|
235
235
|
jinshuju form setting edit Kp7mQ2 --json '{"weixin_share":{"title":"2026 年会报名"}}' # the card a link shows in WeChat
|
|
236
236
|
jinshuju form redirect-rule edit Kp7mQ2 --json @redirect-rules.json # where a submission meeting conditions goes instead
|
|
237
237
|
jinshuju form cooperator add Kp7mQ2 --user 6512a1c2d3e4f5a6b7c8d9d3 --role data_viewer # account members; no invitation is sent
|
|
238
|
+
jinshuju form cooperator update Kp7mQ2 6512a1c2d3e4f5a6b7c8d9d3 --role data_maintainer # user_id from form cooperator list
|
|
239
|
+
jinshuju form cooperator remove Kp7mQ2 6512a1c2d3e4f5a6b7c8d9d3 --yes
|
|
238
240
|
jinshuju form result-share create Kp7mQ2 --fields field_2,field_3 --stats field_3 # a link to the results, answered in url
|
|
239
241
|
jinshuju table create --json @table.json --folder Nf7mDC
|
|
240
242
|
jinshuju table edit Vn4xR8 --json '{"name":"2026 ledger"}'
|
|
@@ -247,9 +249,14 @@ container:
|
|
|
247
249
|
jinshuju field add --form Kp7mQ2 --json '{"type":"TextField","label":"Notes"}'
|
|
248
250
|
jinshuju field update --form Kp7mQ2 field_3 --json '{"required":true}'
|
|
249
251
|
jinshuju field update-choices --form Kp7mQ2 field_7 --json '{"add":[{"label":"Third"}]}'
|
|
250
|
-
jinshuju field remove --form Kp7mQ2 field_9
|
|
252
|
+
jinshuju field remove --form Kp7mQ2 field_9
|
|
251
253
|
```
|
|
252
254
|
|
|
255
|
+
`field remove` asks the field check first: a field with no answers goes, and
|
|
256
|
+
one holding answers waits for `--yes`, since removing it deletes them. Choices
|
|
257
|
+
given to `field update` are the whole list the field should have, in its order,
|
|
258
|
+
sent as the `update-choices` operations that make it so.
|
|
259
|
+
|
|
253
260
|
`add`, `update` and `update-choices` answer with the fields they touched rather
|
|
254
261
|
than the whole container the PATCH returns. A form whose fields need a plan its
|
|
255
262
|
account lacks also answers with `unusable_features`, and these commands and
|
package/dist/args.d.ts
CHANGED
|
@@ -39,6 +39,26 @@ export declare function splitArgs(argv: readonly string[], specs?: readonly Opti
|
|
|
39
39
|
* how a caller learns it asked for something that was never going to happen.
|
|
40
40
|
*/
|
|
41
41
|
export declare function bindOptions(specs: readonly OptionSpec[], flags: Record<string, unknown>, label: string, stdin: () => string): Record<string, unknown>;
|
|
42
|
+
/**
|
|
43
|
+
* The flags an argument may also be given as, so they can be split off the
|
|
44
|
+
* line and bound like any other: `--serial-number 12` for `<serial>`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function argumentFlags(command: Command | undefined): OptionSpec[];
|
|
47
|
+
/**
|
|
48
|
+
* The words as the command reads them, once two other ways of writing them
|
|
49
|
+
* are taken in.
|
|
50
|
+
*
|
|
51
|
+
* An argument given as one of its flags goes back in its place among the
|
|
52
|
+
* positionals. And a token in front of every argument the command takes, on a
|
|
53
|
+
* command that is addressed by `--form` and was given neither container, is
|
|
54
|
+
* that container: `entry get Kp7mQ2 12` can only mean `entry get --form Kp7mQ2
|
|
55
|
+
* 12`, the way `form get Kp7mQ2` reads. It is taken as a form, the kind the
|
|
56
|
+
* habit comes from; a table still needs `--table`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function placedArgs(command: Command, words: readonly string[], options: Record<string, unknown>): {
|
|
59
|
+
words: string[];
|
|
60
|
+
options: Record<string, unknown>;
|
|
61
|
+
};
|
|
42
62
|
export declare function bindArgs(command: Command, words: readonly string[]): {
|
|
43
63
|
args: Record<string, string>;
|
|
44
64
|
rest: string[];
|
package/dist/args.js
CHANGED
|
@@ -145,6 +145,56 @@ function containerHint(command, extra) {
|
|
|
145
145
|
return '';
|
|
146
146
|
return `. Did you mean --form ${extra}? (a table: --table ${extra})`;
|
|
147
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* The flags an argument may also be given as, so they can be split off the
|
|
150
|
+
* line and bound like any other: `--serial-number 12` for `<serial>`.
|
|
151
|
+
*/
|
|
152
|
+
export function argumentFlags(command) {
|
|
153
|
+
return (command?.args ?? []).flatMap(flagsOf);
|
|
154
|
+
}
|
|
155
|
+
function flagsOf(arg) {
|
|
156
|
+
return (arg.flags ?? []).map((name) => ({
|
|
157
|
+
name,
|
|
158
|
+
type: 'string',
|
|
159
|
+
placeholder: `<${arg.name}>`,
|
|
160
|
+
description: `Same as <${arg.name}>`
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The words as the command reads them, once two other ways of writing them
|
|
165
|
+
* are taken in.
|
|
166
|
+
*
|
|
167
|
+
* An argument given as one of its flags goes back in its place among the
|
|
168
|
+
* positionals. And a token in front of every argument the command takes, on a
|
|
169
|
+
* command that is addressed by `--form` and was given neither container, is
|
|
170
|
+
* that container: `entry get Kp7mQ2 12` can only mean `entry get --form Kp7mQ2
|
|
171
|
+
* 12`, the way `form get Kp7mQ2` reads. It is taken as a form, the kind the
|
|
172
|
+
* habit comes from; a table still needs `--table`.
|
|
173
|
+
*/
|
|
174
|
+
export function placedArgs(command, words, options) {
|
|
175
|
+
const specs = command.args ?? [];
|
|
176
|
+
const positionals = words.slice(command.path.length);
|
|
177
|
+
const flagged = specs.map((arg) => flagsOf(arg)
|
|
178
|
+
.map((spec) => options[optionKey(spec)])
|
|
179
|
+
.find((value) => value !== undefined));
|
|
180
|
+
const container = (command.options ?? []).find((option) => option.name === '--form');
|
|
181
|
+
let placed = { ...options };
|
|
182
|
+
const free = specs.length - flagged.filter((value) => value !== undefined).length;
|
|
183
|
+
if (container &&
|
|
184
|
+
options.form === undefined &&
|
|
185
|
+
options.table === undefined &&
|
|
186
|
+
!specs.some((arg) => arg.variadic) &&
|
|
187
|
+
positionals.length === free + 1 &&
|
|
188
|
+
/^[A-Za-z0-9]{6}$/.test(positionals[0])) {
|
|
189
|
+
const token = positionals.shift();
|
|
190
|
+
placed = { ...placed, form: container.repeatable ? [token] : token };
|
|
191
|
+
}
|
|
192
|
+
flagged.forEach((value, index) => {
|
|
193
|
+
if (value !== undefined)
|
|
194
|
+
positionals.splice(index, 0, String(value));
|
|
195
|
+
});
|
|
196
|
+
return { words: [...command.path, ...positionals], options: placed };
|
|
197
|
+
}
|
|
148
198
|
export function bindArgs(command, words) {
|
|
149
199
|
const positionals = words.slice(command.path.length);
|
|
150
200
|
const specs = command.args ?? [];
|
package/dist/cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { bindArgs, bindOptions, leadingWords, readStdin, splitArgs } from './args.js';
|
|
1
|
+
import { argumentFlags, bindArgs, bindOptions, leadingWords, placedArgs, readStdin, splitArgs } from './args.js';
|
|
2
2
|
import { findCommand } from './commands/index.js';
|
|
3
3
|
import { defaultConfigPath, loadConfig } from './config.js';
|
|
4
4
|
import { helpFor, rootHelp } from './help.js';
|
|
@@ -9,13 +9,14 @@ import { checked } from './preflight.js';
|
|
|
9
9
|
import { progress } from './progress.js';
|
|
10
10
|
import { format, terminalWidth } from './render.js';
|
|
11
11
|
import { fail, ok, outputOf, unknown } from './result.js';
|
|
12
|
+
import { isRecord } from './values.js';
|
|
12
13
|
import { VERSION } from './version.js';
|
|
13
14
|
export async function runCli(args = [], runtime = {}) {
|
|
14
15
|
// The command has to be found before the line can be split, because only it
|
|
15
16
|
// says which flags take a value. Its own words come first and hold no flags,
|
|
16
17
|
// so they are readable without knowing anything.
|
|
17
18
|
const command = findCommand(leadingWords(args));
|
|
18
|
-
const specs = [...(command?.options ?? []), ...GLOBAL_OPTIONS, ...LOCAL_OPTIONS];
|
|
19
|
+
const specs = [...(command?.options ?? []), ...argumentFlags(command), ...GLOBAL_OPTIONS, ...LOCAL_OPTIONS];
|
|
19
20
|
const { words, flags } = splitArgs(args, specs);
|
|
20
21
|
const stdin = runtime.stdin ?? readStdin;
|
|
21
22
|
if (flags['--version'] || flags['-V'])
|
|
@@ -39,9 +40,10 @@ export async function runCli(args = [], runtime = {}) {
|
|
|
39
40
|
}
|
|
40
41
|
async function runRemote(command, words, flags, runtime, stdin) {
|
|
41
42
|
const label = `jinshuju ${command.path.join(' ')}`;
|
|
42
|
-
const specs = [...(command.options ?? []), ...GLOBAL_OPTIONS, ...LOCAL_OPTIONS];
|
|
43
|
-
const
|
|
44
|
-
const
|
|
43
|
+
const specs = [...(command.options ?? []), ...argumentFlags(command), ...GLOBAL_OPTIONS, ...LOCAL_OPTIONS];
|
|
44
|
+
const placed = placedArgs(command, words, bindOptions(specs, flags, label, stdin));
|
|
45
|
+
const options = placed.options;
|
|
46
|
+
const input = { ...bindArgs(command, placed.words), options };
|
|
45
47
|
const output = options.output ?? 'text';
|
|
46
48
|
const width = runtime.width ?? terminalWidth();
|
|
47
49
|
// Every request leaves through the preflight, a test's mock client's too:
|
|
@@ -62,6 +64,7 @@ async function runRemote(command, words, flags, runtime, stdin) {
|
|
|
62
64
|
const request = command.request?.(input);
|
|
63
65
|
if (!request)
|
|
64
66
|
throw new UsageError(`${label} is not available yet`);
|
|
67
|
+
const eachRow = (rows) => (command.row ? rows.map((row) => command.row(row, input)) : rows);
|
|
65
68
|
if (options.all && command.paginate) {
|
|
66
69
|
// jsonl is the one format that can be written before the end is known,
|
|
67
70
|
// so each page goes out as it arrives and nothing is kept. The other two
|
|
@@ -71,7 +74,7 @@ async function runRemote(command, words, flags, runtime, stdin) {
|
|
|
71
74
|
let gathered = '';
|
|
72
75
|
const write = runtime.stdout ?? ((chunk) => (gathered += chunk));
|
|
73
76
|
await readAllPages(client, request, command.paginate, progress(), (rows) => {
|
|
74
|
-
for (const row of rows)
|
|
77
|
+
for (const row of eachRow(rows))
|
|
75
78
|
write(`${JSON.stringify(row)}\n`);
|
|
76
79
|
});
|
|
77
80
|
return { exitCode: 0, stdout: gathered, stderr: '' };
|
|
@@ -80,7 +83,7 @@ async function runRemote(command, words, flags, runtime, stdin) {
|
|
|
80
83
|
let totals;
|
|
81
84
|
await readAllPages(client, request, command.paginate, progress(), (page, body) => {
|
|
82
85
|
totals ??= Object.fromEntries((command.paginate?.totals ?? []).map((key) => [key, body?.[key]]));
|
|
83
|
-
rows.push(...page);
|
|
86
|
+
rows.push(...eachRow(page));
|
|
84
87
|
});
|
|
85
88
|
const listing = { ...totals, count: rows.length, data: rows };
|
|
86
89
|
// Every page read is still one listing, and reads the way a single page does.
|
|
@@ -88,7 +91,11 @@ async function runRemote(command, words, flags, runtime, stdin) {
|
|
|
88
91
|
return ok(format(command.render(listing), output, width, hints));
|
|
89
92
|
return ok(format(listing, output, width, hints));
|
|
90
93
|
}
|
|
91
|
-
|
|
94
|
+
let result = await client.request(request);
|
|
95
|
+
const items = command.paginate?.items;
|
|
96
|
+
if (command.row && items && isRecord(result) && Array.isArray(result[items])) {
|
|
97
|
+
result = { ...result, [items]: eachRow(result[items]) };
|
|
98
|
+
}
|
|
92
99
|
const selected = command.select ? command.select(result, input) : result;
|
|
93
100
|
if (output === 'text')
|
|
94
101
|
return ok(format(command.render ? command.render(selected) : selected, output, width, hints));
|
package/dist/commands/entry.js
CHANGED
|
@@ -12,6 +12,20 @@ function list(value) {
|
|
|
12
12
|
const values = value;
|
|
13
13
|
return values && values.length > 0 ? values : undefined;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* A view's entries cut down to the fields named, which its endpoint cannot do:
|
|
17
|
+
* it takes no field list, only the view's own columns. A caller asking for a few
|
|
18
|
+
* columns out of habit is answered rather than refused. The two keys that say
|
|
19
|
+
* which entry a row is stay, as they do when the API narrows a listing itself.
|
|
20
|
+
*/
|
|
21
|
+
function onlyFields(row, fields) {
|
|
22
|
+
if (!fields || !isRecord(row))
|
|
23
|
+
return row;
|
|
24
|
+
const kept = new Set(['token', 'serial_number', ...fields]);
|
|
25
|
+
return Object.fromEntries(Object.entries(row).filter(([key]) => kept.has(key)));
|
|
26
|
+
}
|
|
27
|
+
/** How a serial number is reached for as a flag, which reads as the argument it means. */
|
|
28
|
+
const SERIAL_FLAGS = ['--serial', '--serial-number'];
|
|
15
29
|
function labels(input) {
|
|
16
30
|
return input.options.labels ? true : undefined;
|
|
17
31
|
}
|
|
@@ -306,7 +320,8 @@ export const ENTRY = [
|
|
|
306
320
|
{
|
|
307
321
|
path: ['entry', 'list'],
|
|
308
322
|
summary: 'List entries',
|
|
309
|
-
description: 'With --view the view carries its own filter and sort, so --filter, --keyword and --sort cannot be added on top
|
|
323
|
+
description: 'With --view the view carries its own filter and sort, so --filter, --keyword and --sort cannot be added on top; ' +
|
|
324
|
+
'it also decides which columns come back, and --fields keeps only those of them it names.',
|
|
310
325
|
options: [
|
|
311
326
|
...CONTAINER_OPTIONS,
|
|
312
327
|
{ name: '--view', type: 'string', placeholder: '<view>', description: 'Read the entries of this view' },
|
|
@@ -342,20 +357,9 @@ export const ENTRY = [
|
|
|
342
357
|
throw new UsageError(`--${flag} cannot be combined with --view: the view carries its own filter and sort`);
|
|
343
358
|
}
|
|
344
359
|
}
|
|
345
|
-
// The view decides its own columns and the endpoint takes no field
|
|
346
|
-
// list, so --fields here asked for something that was never going to
|
|
347
|
-
// happen. Refusing beats accepting it and answering every field.
|
|
348
|
-
if (input.options.fields !== undefined) {
|
|
349
|
-
throw new UsageError('--fields cannot be combined with --view: the view decides its own columns. ' +
|
|
350
|
-
'Change them with `view edit --columns`, or read the form without --view.');
|
|
351
|
-
}
|
|
352
|
-
// A view is read fifty rows a page, and the API takes no page size for it.
|
|
353
|
-
if (input.options.limit !== undefined) {
|
|
354
|
-
throw new UsageError('--limit cannot be combined with --view: a view is read 50 rows a page');
|
|
355
|
-
}
|
|
356
360
|
return onContainer(input, 'listFormViewEntries', 'listTableViewEntries', {
|
|
357
361
|
path: { view_token: view },
|
|
358
|
-
query: { include_labels: labels(input),
|
|
362
|
+
query: { include_labels: labels(input), ...paging(input) }
|
|
359
363
|
});
|
|
360
364
|
}
|
|
361
365
|
return onContainer(input, 'listEntries', 'listTableEntries', {
|
|
@@ -369,6 +373,7 @@ export const ENTRY = [
|
|
|
369
373
|
}
|
|
370
374
|
});
|
|
371
375
|
},
|
|
376
|
+
row: (row, input) => (input.options.view === undefined ? row : onlyFields(row, list(input.options.fields))),
|
|
372
377
|
paginate: LISTING,
|
|
373
378
|
examples: [
|
|
374
379
|
'jinshuju entry list --form Kp7mQ2',
|
|
@@ -635,7 +640,14 @@ export const ENTRY = [
|
|
|
635
640
|
DETAIL_URL +
|
|
636
641
|
' ' +
|
|
637
642
|
VALUE_SHAPES,
|
|
638
|
-
args: [
|
|
643
|
+
args: [
|
|
644
|
+
{
|
|
645
|
+
name: 'serial',
|
|
646
|
+
required: false,
|
|
647
|
+
description: 'Entry serial number; leave out with --batch',
|
|
648
|
+
flags: SERIAL_FLAGS
|
|
649
|
+
}
|
|
650
|
+
],
|
|
639
651
|
options: [
|
|
640
652
|
...CONTAINER_OPTIONS,
|
|
641
653
|
JSON_OPTION,
|
|
@@ -763,7 +775,7 @@ export const ENTRY = [
|
|
|
763
775
|
{
|
|
764
776
|
path: ['entry', 'delete'],
|
|
765
777
|
summary: 'Delete one entry',
|
|
766
|
-
args: [{ name: 'serial', required: true, description: 'Entry serial number' }],
|
|
778
|
+
args: [{ name: 'serial', required: true, description: 'Entry serial number', flags: SERIAL_FLAGS }],
|
|
767
779
|
options: [...CONTAINER_OPTIONS, YES_OPTION],
|
|
768
780
|
request: (input) => {
|
|
769
781
|
confirmed(input, `Deleting entry ${input.args.serial}`);
|
|
@@ -775,7 +787,7 @@ export const ENTRY = [
|
|
|
775
787
|
path: ['entry', 'get'],
|
|
776
788
|
summary: 'Show one entry',
|
|
777
789
|
description: DETAIL_URL,
|
|
778
|
-
args: [{ name: 'serial', required: true, description: 'Entry serial number' }],
|
|
790
|
+
args: [{ name: 'serial', required: true, description: 'Entry serial number', flags: SERIAL_FLAGS }],
|
|
779
791
|
options: [
|
|
780
792
|
...CONTAINER_OPTIONS,
|
|
781
793
|
{ name: '--fields', type: 'list', placeholder: '<api-code,...>', description: 'Return only these fields' },
|
|
@@ -792,7 +804,7 @@ export const ENTRY = [
|
|
|
792
804
|
description: "red_envelope_audit_state is the review of an entry's red envelope, reservation_status its " +
|
|
793
805
|
"reservation, and wheel_fortune_prize_audit_state the lucky wheel's winners, each with the id " +
|
|
794
806
|
'`entry state-flow trigger --target` takes. Forms only; takes the form creator, admin or data maintainer role.',
|
|
795
|
-
args: [{ name: 'serial', required: true, description: 'Entry serial number' }],
|
|
807
|
+
args: [{ name: 'serial', required: true, description: 'Entry serial number', flags: SERIAL_FLAGS }],
|
|
796
808
|
options: [FORM_OPTION, STATE_FLOW_OPTION],
|
|
797
809
|
request: (input) => call('getEntryStateFlow', { path: stateFlowPath(input) }),
|
|
798
810
|
examples: [
|
|
@@ -808,7 +820,7 @@ export const ENTRY = [
|
|
|
808
820
|
'and reda_refuse rejects it, cancel_all frees the reserved slots without refunding a paid entry, ' +
|
|
809
821
|
'winner_confirm redeems a winner, and delete_winner removes one for good: its prize and voucher ' +
|
|
810
822
|
'code do not go back to the pool. A winner is named by --target.',
|
|
811
|
-
args: [{ name: 'serial', required: true, description: 'Entry serial number' }],
|
|
823
|
+
args: [{ name: 'serial', required: true, description: 'Entry serial number', flags: SERIAL_FLAGS }],
|
|
812
824
|
options: [
|
|
813
825
|
FORM_OPTION,
|
|
814
826
|
STATE_FLOW_OPTION,
|
package/dist/commands/field.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CONTAINER_OPTIONS, JSON_OPTION, UsageError } from '../options.js';
|
|
2
2
|
import { call, choices } from '../spec.js';
|
|
3
|
+
import { isRecord } from '../values.js';
|
|
3
4
|
import { YES_OPTION, confirmed, onContainer, one, payload, requiredOption } from './shared.js';
|
|
4
5
|
/**
|
|
5
6
|
* One field, or a list of them — the same objects `fields` holds above. A
|
|
@@ -13,9 +14,9 @@ const FIELD_PAYLOAD = [
|
|
|
13
14
|
'A field as `form get` or `field list` returns it can be written back as it is, whole or',
|
|
14
15
|
'a few of its keys:',
|
|
15
16
|
' null for a setting clears it, or puts it back to its default',
|
|
16
|
-
' choices
|
|
17
|
-
'
|
|
18
|
-
'
|
|
17
|
+
' choices on `field update` are the whole list the field should have, in its order: each',
|
|
18
|
+
' is matched by api_code or text, a new one is added, one left out is removed (--yes if',
|
|
19
|
+
' it holds answers). `field update-choices` makes single changes',
|
|
19
20
|
" a key no write can change, like api_code_alias or a choice's hidden, goes back only as",
|
|
20
21
|
' it was read; the API answers which key when one differs',
|
|
21
22
|
'',
|
|
@@ -45,6 +46,145 @@ function checks(input, parse) {
|
|
|
45
46
|
throw new UsageError('name at least one target, as an argument or through --json');
|
|
46
47
|
return all;
|
|
47
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Whether each target may hold answers, asked of the same check `field check`
|
|
51
|
+
* makes. Only a plain "no data" counts as none: a target the check could not
|
|
52
|
+
* read, or a check that failed, is taken to hold some, so the caller is asked
|
|
53
|
+
* rather than an answer lost. The reason a check failed rides along.
|
|
54
|
+
*/
|
|
55
|
+
async function holdingData(client, input, targets) {
|
|
56
|
+
try {
|
|
57
|
+
const body = await client.request(onContainer(input, 'checkFormFieldData', 'checkTableFieldData', {
|
|
58
|
+
query: { checks: JSON.stringify(targets) }
|
|
59
|
+
}));
|
|
60
|
+
const results = isRecord(body) && Array.isArray(body.results) ? body.results : [];
|
|
61
|
+
return { holding: targets.map((_, index) => !(isRecord(results[index]) && results[index].has_data === false)) };
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return { holding: targets.map(() => true), unsure: error.message };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Removing what holds no answers loses nothing, so it goes ahead unasked; only
|
|
69
|
+
* what does, or might, waits for --yes. Asking for a confirmation that guards
|
|
70
|
+
* nothing taught callers to add --yes to every remove, which is the habit that
|
|
71
|
+
* would let the one that matters through.
|
|
72
|
+
*/
|
|
73
|
+
async function confirmedIfHolding(client, input, targets, what) {
|
|
74
|
+
if (input.options.yes || targets.length === 0)
|
|
75
|
+
return;
|
|
76
|
+
const { holding, unsure } = await holdingData(client, input, targets);
|
|
77
|
+
if (!holding.some(Boolean))
|
|
78
|
+
return;
|
|
79
|
+
const reason = unsure === undefined ? 'holds answers' : `may hold answers (the check failed: ${unsure})`;
|
|
80
|
+
confirmed(input, `${what} ${reason}, and removing it deletes them`, 'for good');
|
|
81
|
+
}
|
|
82
|
+
/** The operations as given, with a choice to update named by `name` read as its `value`. */
|
|
83
|
+
function choiceChanges(given) {
|
|
84
|
+
const changes = given;
|
|
85
|
+
return Array.isArray(changes.update) ? { ...changes, update: changes.update.map(withValue) } : changes;
|
|
86
|
+
}
|
|
87
|
+
/** A choice as a caller writes it: its text, from `value`, `name`, or the bare string. */
|
|
88
|
+
function choiceText(choice) {
|
|
89
|
+
if (typeof choice === 'string')
|
|
90
|
+
return choice;
|
|
91
|
+
if (!isRecord(choice))
|
|
92
|
+
return undefined;
|
|
93
|
+
const text = choice.value ?? choice.name;
|
|
94
|
+
return typeof text === 'string' ? text : undefined;
|
|
95
|
+
}
|
|
96
|
+
/** What `update_choices` calls `value`, written as `name`, which `add` takes and `update` does not. */
|
|
97
|
+
function withValue(item) {
|
|
98
|
+
if (!isRecord(item) || item.name === undefined || item.value !== undefined)
|
|
99
|
+
return item;
|
|
100
|
+
const { name, ...rest } = item;
|
|
101
|
+
return { ...rest, value: name };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A whole list of choices on `field update`, the shape `field add` takes, turned
|
|
105
|
+
* into the operations the API takes instead: one that names its choices in full
|
|
106
|
+
* is refused there, and "these are the choices now" has only one meaning. Each
|
|
107
|
+
* is matched to a choice the field has by api_code, else by its text; one with
|
|
108
|
+
* no match is added, and a choice the list leaves out is removed.
|
|
109
|
+
*
|
|
110
|
+
* The list's order is the order asked for. When removing and appending leave
|
|
111
|
+
* the choices in it already, no position is sent; otherwise every choice that
|
|
112
|
+
* stays and every one added gets one (`placed`).
|
|
113
|
+
*/
|
|
114
|
+
function choiceOperations(current, given) {
|
|
115
|
+
// Each slot of the list asked for: a choice the field has, by its index, or one to add.
|
|
116
|
+
const slots = [];
|
|
117
|
+
const changes = new Map();
|
|
118
|
+
for (const item of given) {
|
|
119
|
+
const code = isRecord(item) ? item.api_code : undefined;
|
|
120
|
+
const text = choiceText(item);
|
|
121
|
+
const index = current.findIndex((choice) => code !== undefined ? choice.api_code === code : choice.value === text);
|
|
122
|
+
if (index === -1 || slots.some((slot) => 'kept' in slot && slot.kept === index)) {
|
|
123
|
+
slots.push({ added: typeof item === 'string' ? { value: item } : item });
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
slots.push({ kept: index });
|
|
127
|
+
const choice = current[index];
|
|
128
|
+
if (!isRecord(item))
|
|
129
|
+
continue;
|
|
130
|
+
const changed = Object.entries(withValue(item)).filter(([key, value]) => key !== 'api_code' && JSON.stringify(choice[key]) !== JSON.stringify(value));
|
|
131
|
+
if (changed.length > 0)
|
|
132
|
+
changes.set(index, Object.fromEntries(changed));
|
|
133
|
+
}
|
|
134
|
+
const kept = slots.flatMap((slot) => ('kept' in slot ? [slot.kept] : []));
|
|
135
|
+
const remove = current.filter((_, index) => !kept.includes(index));
|
|
136
|
+
const positions = placed(slots, [...kept].sort((a, b) => a - b));
|
|
137
|
+
const add = slots.flatMap((slot, at) => ('added' in slot ? [{ at, item: slot.added }] : []));
|
|
138
|
+
const update = slots.flatMap((slot, at) => {
|
|
139
|
+
if (!('kept' in slot))
|
|
140
|
+
return [];
|
|
141
|
+
const change = { ...changes.get(slot.kept), ...(positions ? { position: at } : {}) };
|
|
142
|
+
return Object.keys(change).length > 0 ? [{ api_code: current[slot.kept]?.api_code, ...change }] : [];
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
add: (positions
|
|
146
|
+
? [...add].sort((a, b) => positions.get(a.at) - positions.get(b.at))
|
|
147
|
+
: add).map(({ at, item }) => (positions ? { ...item, position: positions.get(at) } : item)),
|
|
148
|
+
update,
|
|
149
|
+
remove
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Where each choice to add has to be inserted for the list to come out in the
|
|
154
|
+
* order asked, or undefined when appending already does. Keyed by slot.
|
|
155
|
+
*
|
|
156
|
+
* The API removes first, then inserts each addition at its position in turn,
|
|
157
|
+
* then moves each choice given a position, lowest position first: out of
|
|
158
|
+
* where it is, into that place. Every choice that stays is moved to its own
|
|
159
|
+
* slot, so the moves are fixed; what is left to choose is where the additions
|
|
160
|
+
* go in. That is found by playing the moves backwards from the list asked
|
|
161
|
+
* for: each choice moved is taken out of its slot and put back among the ones
|
|
162
|
+
* already put back in the order the field has them now. What is left is the
|
|
163
|
+
* list just before the moves, and the additions' places in it are where they
|
|
164
|
+
* are inserted, lowest first.
|
|
165
|
+
*/
|
|
166
|
+
function placed(slots, keptNow) {
|
|
167
|
+
const appended = [
|
|
168
|
+
...keptNow.map((index) => slots.findIndex((slot) => 'kept' in slot && slot.kept === index)),
|
|
169
|
+
...slots.flatMap((slot, at) => ('added' in slot ? [at] : []))
|
|
170
|
+
];
|
|
171
|
+
if (appended.every((at, index) => at === index))
|
|
172
|
+
return undefined;
|
|
173
|
+
const list = slots.map((_, at) => at);
|
|
174
|
+
const back = [];
|
|
175
|
+
const rank = (at) => keptNow.indexOf(slots[at].kept);
|
|
176
|
+
for (let at = slots.length - 1; at >= 0; at -= 1) {
|
|
177
|
+
if (!('kept' in slots[at]))
|
|
178
|
+
continue;
|
|
179
|
+
list.splice(list.indexOf(at), 1);
|
|
180
|
+
const after = back.filter((other) => rank(other) > rank(at)).map((other) => list.indexOf(other));
|
|
181
|
+
const before = back.filter((other) => rank(other) < rank(at)).map((other) => list.indexOf(other));
|
|
182
|
+
const where = after.length > 0 ? Math.min(...after) : before.length > 0 ? Math.max(...before) + 1 : list.length;
|
|
183
|
+
list.splice(where, 0, at);
|
|
184
|
+
back.push(at);
|
|
185
|
+
}
|
|
186
|
+
return new Map(slots.flatMap((slot, at) => ('added' in slot ? [[at, list.indexOf(at)]] : [])));
|
|
187
|
+
}
|
|
48
188
|
export const FIELD_SCOPES = ['normal', 'exam', 'customized'];
|
|
49
189
|
/**
|
|
50
190
|
* A listing has to stay scannable and one type has to be readable, and those
|
|
@@ -258,14 +398,34 @@ export const FIELD = [
|
|
|
258
398
|
'Write the keys to change, or the whole field as `field list` read it, as the payload below says. ' +
|
|
259
399
|
WRITE_ANSWER,
|
|
260
400
|
args: [{ name: 'api-code', required: true, description: 'Field api_code, e.g. field_3' }],
|
|
261
|
-
options: [...CONTAINER_OPTIONS, JSON_OPTION],
|
|
401
|
+
options: [...CONTAINER_OPTIONS, JSON_OPTION, YES_OPTION],
|
|
262
402
|
payload: FIELD_PAYLOAD,
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
403
|
+
run: async (input, client) => {
|
|
404
|
+
const code = input.args['api-code'];
|
|
405
|
+
const { choices: given, ...patch } = payload(input);
|
|
406
|
+
const fields = { update: [{ ...patch, api_code: code }] };
|
|
407
|
+
if (Array.isArray(given)) {
|
|
408
|
+
const field = selectFields(await client.request(onContainer(input, 'getForm', 'getTable'))).find((candidate) => candidate.api_code === code);
|
|
409
|
+
const current = field?.choices;
|
|
410
|
+
// A type the field does not have yet, or a cascade's nested levels,
|
|
411
|
+
// take the list as it is; the API replaces the choices there.
|
|
412
|
+
const replaces = !field || !Array.isArray(current) || (patch.type !== undefined && patch.type !== field.type);
|
|
413
|
+
if (replaces) {
|
|
414
|
+
fields.update = [{ ...patch, choices: given, api_code: code }];
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
const ops = choiceOperations(current, given);
|
|
418
|
+
await confirmedIfHolding(client, input, ops.remove.map((choice) => ({ field_api_code: code, choice_value: choice.value })), `Choice ${ops.remove.map((choice) => JSON.stringify(choice.value)).join(', ')} of ${code}`);
|
|
419
|
+
const operations = Object.fromEntries(Object.entries({ ...ops, remove: ops.remove.map((choice) => choice.api_code) }).filter(([, items]) => items.length > 0));
|
|
420
|
+
if (Object.keys(patch).length === 0)
|
|
421
|
+
delete fields.update;
|
|
422
|
+
if (Object.keys(operations).length > 0)
|
|
423
|
+
fields.update_choices = [{ ...operations, field_api_code: code }];
|
|
424
|
+
}
|
|
266
425
|
}
|
|
267
|
-
|
|
268
|
-
|
|
426
|
+
const body = await client.request(onContainer(input, 'updateForm', 'updateTable', { body: { fields } }));
|
|
427
|
+
return touchedFields(body, (candidate) => candidate.api_code === code);
|
|
428
|
+
},
|
|
269
429
|
examples: ['jinshuju field update --form Kp7mQ2 field_3 --json \'{"required":true}\'']
|
|
270
430
|
},
|
|
271
431
|
{
|
|
@@ -277,7 +437,7 @@ export const FIELD = [
|
|
|
277
437
|
request: (input) => onContainer(input, 'updateForm', 'updateTable', {
|
|
278
438
|
body: {
|
|
279
439
|
fields: {
|
|
280
|
-
update_choices: [{ ...payload(input), field_api_code: input.args['api-code'] }]
|
|
440
|
+
update_choices: [{ ...choiceChanges(payload(input)), field_api_code: input.args['api-code'] }]
|
|
281
441
|
}
|
|
282
442
|
}
|
|
283
443
|
}),
|
|
@@ -344,15 +504,15 @@ export const FIELD = [
|
|
|
344
504
|
{
|
|
345
505
|
path: ['field', 'remove'],
|
|
346
506
|
summary: 'Remove a field',
|
|
347
|
-
description: 'Removing a field that still holds answers deletes those answers with it, and cannot be undone
|
|
507
|
+
description: 'Removing a field that still holds answers deletes those answers with it, and cannot be undone, ' +
|
|
508
|
+
'so that one waits for --yes; a field with no answers goes without it.',
|
|
348
509
|
args: [{ name: 'api-code', required: true, description: 'Field api_code' }],
|
|
349
510
|
options: [...CONTAINER_OPTIONS, YES_OPTION],
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
});
|
|
511
|
+
run: async (input, client) => {
|
|
512
|
+
const code = input.args['api-code'];
|
|
513
|
+
await confirmedIfHolding(client, input, [{ field_api_code: code }], code);
|
|
514
|
+
return client.request(onContainer(input, 'updateForm', 'updateTable', { body: { fields: { remove: [code] } } }));
|
|
355
515
|
},
|
|
356
|
-
examples: ['jinshuju field remove --form Kp7mQ2 field_9 --yes']
|
|
516
|
+
examples: ['jinshuju field remove --form Kp7mQ2 field_9', 'jinshuju field remove --form Kp7mQ2 field_9 --yes']
|
|
357
517
|
}
|
|
358
518
|
];
|
package/dist/commands/folder.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PAGINATION_OPTIONS } from '../options.js';
|
|
2
2
|
import { bodyChoices, call } from '../spec.js';
|
|
3
|
-
import { given, paging } from './shared.js';
|
|
3
|
+
import { LISTING, given, paging } from './shared.js';
|
|
4
4
|
export const FOLDER = [
|
|
5
5
|
{
|
|
6
6
|
path: ['folder', 'list'],
|
|
7
7
|
summary: 'List folders',
|
|
8
|
-
options: [
|
|
9
|
-
request: (input) => call('listFolders', { query: paging(input) })
|
|
8
|
+
options: [...PAGINATION_OPTIONS],
|
|
9
|
+
request: (input) => call('listFolders', { query: paging(input) }),
|
|
10
|
+
paginate: LISTING
|
|
10
11
|
},
|
|
11
12
|
{
|
|
12
13
|
path: ['folder', 'get'],
|
package/dist/commands/form.js
CHANGED
|
@@ -325,6 +325,11 @@ const RESULT_SHARE_ARG = {
|
|
|
325
325
|
required: true,
|
|
326
326
|
description: 'Result share token, from `form result-share list`'
|
|
327
327
|
};
|
|
328
|
+
const COOPERATOR_ARG = {
|
|
329
|
+
name: 'user',
|
|
330
|
+
required: true,
|
|
331
|
+
description: "The cooperator's user_id, from `form cooperator list`"
|
|
332
|
+
};
|
|
328
333
|
/** A result share's body: `--json`, with the flags that were given laid over it. */
|
|
329
334
|
function resultShareBody(input) {
|
|
330
335
|
const json = input.options.json;
|
|
@@ -1038,5 +1043,45 @@ export const FORM = [
|
|
|
1038
1043
|
'jinshuju form cooperator add Kp7mQ2 --user 6512a1c2d3e4f5a6b7c8d9d3 --role data_viewer',
|
|
1039
1044
|
'jinshuju form cooperator add Kp7mQ2 --user 6512a1c2d3e4f5a6b7c8d9d3 --user 6512a1c2d3e4f5a6b7c8d9d4 --role manager'
|
|
1040
1045
|
]
|
|
1046
|
+
},
|
|
1047
|
+
{
|
|
1048
|
+
path: ['form', 'cooperator', 'update'],
|
|
1049
|
+
summary: "Change a cooperator's role on a form",
|
|
1050
|
+
description: "As changing a role on the form's cooperation page does. The form's creator and its only manager " +
|
|
1051
|
+
'keep the manager role. A cooperator whose role comes from a folder the form is in gets this role ' +
|
|
1052
|
+
"on the form itself, which takes precedence over the folder's. A table token changes a role on the " +
|
|
1053
|
+
'table, which has only the manager and data_viewer roles.',
|
|
1054
|
+
args: [{ name: 'form', required: true, description: 'Form token, or a table token' }, COOPERATOR_ARG],
|
|
1055
|
+
options: [
|
|
1056
|
+
{
|
|
1057
|
+
name: '--role',
|
|
1058
|
+
type: 'string',
|
|
1059
|
+
choices: bodyChoices('updateFormCooperator', 'role'),
|
|
1060
|
+
placeholder: '<role>',
|
|
1061
|
+
description: 'manager administers the form, data_maintainer adds, edits, deletes and exports entries, ' +
|
|
1062
|
+
'data_viewer reads and exports them'
|
|
1063
|
+
}
|
|
1064
|
+
],
|
|
1065
|
+
request: (input) => call('updateFormCooperator', {
|
|
1066
|
+
path: { form_token: input.args.form, user_id: input.args.user },
|
|
1067
|
+
body: { role: requiredOption(input, 'role') }
|
|
1068
|
+
}),
|
|
1069
|
+
examples: ['jinshuju form cooperator update Kp7mQ2 6512a1c2d3e4f5a6b7c8d9d3 --role data_maintainer']
|
|
1070
|
+
},
|
|
1071
|
+
{
|
|
1072
|
+
path: ['form', 'cooperator', 'remove'],
|
|
1073
|
+
summary: 'Remove a cooperator from a form',
|
|
1074
|
+
description: "As removing them on the form's cooperation page does; your own id leaves the form. The form's " +
|
|
1075
|
+
'creator and its only manager cannot be removed. A role that comes from a folder the form is in ' +
|
|
1076
|
+
'is removed on that folder, not here: when the user also has a role on the form itself, that one ' +
|
|
1077
|
+
"goes and the folder's applies again, as `role` in the answer says (null when they are no longer " +
|
|
1078
|
+
'a cooperator).',
|
|
1079
|
+
args: [{ name: 'form', required: true, description: 'Form token, or a table token' }, COOPERATOR_ARG],
|
|
1080
|
+
options: [{ ...YES_OPTION, description: 'Confirm the removal' }],
|
|
1081
|
+
request: (input) => {
|
|
1082
|
+
confirmed(input, `Removing ${input.args.user} from ${input.args.form}`, 'takes away their access');
|
|
1083
|
+
return call('deleteFormCooperator', { path: { form_token: input.args.form, user_id: input.args.user } });
|
|
1084
|
+
},
|
|
1085
|
+
examples: ['jinshuju form cooperator remove Kp7mQ2 6512a1c2d3e4f5a6b7c8d9d3 --yes']
|
|
1041
1086
|
}
|
|
1042
1087
|
];
|
package/dist/commands/index.d.ts
CHANGED
|
@@ -12,5 +12,5 @@ export type { ArgSpec, Command, CommandInput, Pagination, Resource } from './typ
|
|
|
12
12
|
/** Resource order in the root help, and the one-liner each gets. */
|
|
13
13
|
export declare const RESOURCES: readonly Resource[];
|
|
14
14
|
export declare const COMMANDS: readonly Command[];
|
|
15
|
-
/** The command whose path the words begin with, longest match first. */
|
|
15
|
+
/** The command whose path the words begin with, longest match first; a spelled-out match beats a synonym. */
|
|
16
16
|
export declare function findCommand(words: readonly string[], commands?: readonly Command[]): Command | undefined;
|