|
| 1 | +import { View, ViewStore } from '@_all_docs/view'; |
| 2 | +import { encodeOrigin } from '@_all_docs/cache'; |
| 3 | + |
| 4 | +export const usage = `Usage: _all_docs view define <name> [options] |
| 5 | +
|
| 6 | +Define a named view over cached registry data. |
| 7 | +
|
| 8 | +A view is a predicate (origin filter) plus a projection (field selection). |
| 9 | +Views are stored as JSON files and can be queried or joined. |
| 10 | +
|
| 11 | +Options: |
| 12 | + --origin <key> Origin key (e.g., npm, paces.exale.com~javpt) |
| 13 | + --registry <url> Registry URL (converted to origin key internally) |
| 14 | + --type <type> Entity type: packument (default) or partition |
| 15 | + --select <expr> Field selection expression |
| 16 | +
|
| 17 | +Select Expression Syntax: |
| 18 | + Simple fields: name, version, description |
| 19 | + Nested fields: time.modified, repository.url |
| 20 | + With transforms: versions|keys, dependencies|length |
| 21 | + With aliases: versions|keys as version_list |
| 22 | +
|
| 23 | +Available Transforms: |
| 24 | + keys, values Object to array |
| 25 | + length Array/string length |
| 26 | + first, last First/last element |
| 27 | + sort, reverse Sort/reverse array |
| 28 | + unique, compact Dedupe/remove nulls |
| 29 | + flatten Flatten nested arrays |
| 30 | +
|
| 31 | +Examples: |
| 32 | + _all_docs view define npm-packages --origin npm |
| 33 | + _all_docs view define npm-versions --origin npm --select 'name, versions|keys as versions, time' |
| 34 | + _all_docs view define private --registry https://npm.company.com --select 'name, versions|keys' |
| 35 | +`; |
| 36 | + |
| 37 | +export const command = async (cli) => { |
| 38 | + if (cli.values.help) { |
| 39 | + console.log(usage); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const name = cli._[0]; |
| 44 | + if (!name) { |
| 45 | + console.error('Error: View name required'); |
| 46 | + console.error('Usage: _all_docs view define <name> --origin <key> [--select <expr>]'); |
| 47 | + process.exit(1); |
| 48 | + } |
| 49 | + |
| 50 | + // Validate name |
| 51 | + if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(name)) { |
| 52 | + console.error('Error: View name must start with a letter and contain only letters, numbers, underscores, and hyphens'); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + // Prioritize --registry if specified, otherwise use --origin |
| 57 | + // Note: cli.values.origin has a default from jack.js, so we check registry first |
| 58 | + let origin; |
| 59 | + if (cli.values.registry) { |
| 60 | + origin = encodeOrigin(cli.values.registry); |
| 61 | + } else if (cli.values.origin && cli.values.origin !== 'https://replicate.npmjs.com') { |
| 62 | + // User explicitly set --origin (not the default) |
| 63 | + origin = cli.values.origin; |
| 64 | + } else { |
| 65 | + console.error('Error: --origin or --registry required'); |
| 66 | + console.error('Example: _all_docs view define my-view --origin npm'); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + const view = new View({ |
| 71 | + name, |
| 72 | + origin, |
| 73 | + registry: cli.values.registry || null, |
| 74 | + type: cli.values.type || 'packument', |
| 75 | + select: cli.values.select || null |
| 76 | + }); |
| 77 | + |
| 78 | + const store = new ViewStore(cli.dir('config')); |
| 79 | + |
| 80 | + // Check if view already exists |
| 81 | + if (await store.exists(name)) { |
| 82 | + if (!cli.values.force) { |
| 83 | + console.error(`Error: View '${name}' already exists. Use --force to overwrite.`); |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + await store.save(view); |
| 89 | + |
| 90 | + console.log(`View '${name}' defined:`); |
| 91 | + console.log(` Origin: ${origin}`); |
| 92 | + if (cli.values.registry) { |
| 93 | + console.log(` Registry: ${cli.values.registry}`); |
| 94 | + } |
| 95 | + console.log(` Type: ${view.type}`); |
| 96 | + if (view.select) { |
| 97 | + console.log(` Select: ${view.select}`); |
| 98 | + } |
| 99 | +}; |
0 commit comments