@cagov/ca-ds 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +600 -2
- package/bin/ca-ds.js +5 -0
- package/package.json +62 -4
- package/src/cli/add.js +134 -0
- package/src/cli/build.js +15 -0
- package/src/cli/diff.js +39 -0
- package/src/cli/eject.js +112 -0
- package/src/cli/exit.js +22 -0
- package/src/cli/format.js +52 -0
- package/src/cli/init.js +148 -0
- package/src/cli/install.js +178 -0
- package/src/cli/items.js +10 -0
- package/src/cli/login.js +331 -0
- package/src/cli/main.js +108 -0
- package/src/cli/new.js +230 -0
- package/src/cli/outdated.js +74 -0
- package/src/cli/pack.js +75 -0
- package/src/cli/preview.js +31 -0
- package/src/cli/publish.js +295 -0
- package/src/cli/pull.js +208 -0
- package/src/cli/remote.js +52 -0
- package/src/cli/remove.js +69 -0
- package/src/cli/update.js +223 -0
- package/src/cli/validate.js +131 -0
- package/src/lib/browser.js +12 -0
- package/src/lib/build.js +249 -0
- package/src/lib/bundle.js +148 -0
- package/src/lib/catalog.js +44 -0
- package/src/lib/checks.js +31 -0
- package/src/lib/collection.js +65 -0
- package/src/lib/emblem.js +29 -0
- package/src/lib/fixtures.js +59 -0
- package/src/lib/floor.js +79 -0
- package/src/lib/glyph.js +148 -0
- package/src/lib/image.js +138 -0
- package/src/lib/json-text.js +213 -0
- package/src/lib/keys.js +322 -0
- package/src/lib/links.js +321 -0
- package/src/lib/markdown.js +408 -0
- package/src/lib/merge.js +93 -0
- package/src/lib/pack.js +30 -0
- package/src/lib/page-schema.js +368 -0
- package/src/lib/page.js +372 -0
- package/src/lib/patch.js +9 -0
- package/src/lib/regions.js +173 -0
- package/src/lib/registry.js +371 -0
- package/src/lib/release.js +119 -0
- package/src/lib/repoints.js +120 -0
- package/src/lib/resolve.js +32 -0
- package/src/lib/schema.js +28 -0
- package/src/lib/serial.js +4 -0
- package/src/lib/serialize.js +444 -0
- package/src/lib/structured.js +72 -0
- package/src/lib/system.js +13 -0
- package/src/lib/targets.js +85 -0
- package/src/lib/terms.js +11 -0
- package/src/lib/token-view.js +289 -0
- package/src/lib/tokens.js +247 -0
- package/src/lib/version.js +34 -0
- package/src/preview/frame.js +75 -0
- package/src/preview/pages.js +128 -0
- package/src/preview/preview.css +58 -0
- package/src/preview/screenshots.js +72 -0
- package/src/preview/server.js +378 -0
- package/src/preview/shell.js +153 -0
- package/src/schema/a11y.schema.json +56 -0
- package/src/schema/collection.lock.schema.json +58 -0
- package/src/schema/collection.schema.json +65 -0
- package/src/schema/composition.schema.json +98 -0
- package/src/schema/contract.schema.json +116 -0
- package/src/schema/interface.schema.json +257 -0
- package/src/schema/item.schema.json +362 -0
- package/src/schema/release.schema.json +39 -0
- package/src/schema/system.schema.json +75 -0
- package/src/schema/variants.schema.json +60 -0
- package/src/validate/collection.js +19 -0
- package/src/validate/collisions.js +124 -0
- package/src/validate/context.js +357 -0
- package/src/validate/facts.js +253 -0
- package/src/validate/files.js +103 -0
- package/src/validate/findings.js +19 -0
- package/src/validate/glyphs.js +126 -0
- package/src/validate/html.js +215 -0
- package/src/validate/imports.js +29 -0
- package/src/validate/index.js +75 -0
- package/src/validate/item.js +47 -0
- package/src/validate/links.js +113 -0
- package/src/validate/manifest.js +147 -0
- package/src/validate/media.js +65 -0
- package/src/validate/page.js +74 -0
- package/src/validate/prefixes.js +58 -0
- package/src/validate/relations.js +310 -0
- package/src/validate/release.js +109 -0
- package/src/validate/skill.js +122 -0
- package/src/validate/structured.js +335 -0
- package/src/validate/tasc.js +80 -0
- package/src/validate/template.js +129 -0
- package/src/validate/tokens.js +154 -0
- package/src/validate/zip.js +40 -0
package/src/cli/new.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, relative, resolve as resolvePath, sep } from 'node:path';
|
|
3
|
+
import semver from 'semver';
|
|
4
|
+
import { usageError } from './exit.js';
|
|
5
|
+
import { itemNamed } from './items.js';
|
|
6
|
+
import { formatPage } from '../lib/page.js';
|
|
7
|
+
import { frontmatterFindings } from '../validate/skill.js';
|
|
8
|
+
|
|
9
|
+
const NAME = /^([a-z0-9]+(?:-[a-z0-9]+)*)\/([a-z0-9]+(?:-[a-z0-9]+)*)$/;
|
|
10
|
+
const json = (data) => `${JSON.stringify(data, null, 2)}\n`;
|
|
11
|
+
|
|
12
|
+
const titleOf = (base) => base.charAt(0).toUpperCase() + base.slice(1).replace(/-/g, ' ');
|
|
13
|
+
|
|
14
|
+
function component({ name, base, title }) {
|
|
15
|
+
const tag = `ca-${base}`;
|
|
16
|
+
const phrase = title.toLowerCase();
|
|
17
|
+
return {
|
|
18
|
+
manifest: {
|
|
19
|
+
kind: 'component',
|
|
20
|
+
description: `The ${phrase} component.`,
|
|
21
|
+
doc: `${base}.md`,
|
|
22
|
+
files: [
|
|
23
|
+
{ path: `${base}.md`, role: 'doc' },
|
|
24
|
+
{ path: `${base}.contract.json`, role: 'contract' },
|
|
25
|
+
{ path: `${base}.css`, role: 'css' },
|
|
26
|
+
{ path: 'examples/default.html', role: 'reference-html', title, description: `The ${phrase} in its default configuration, for a request that names no variant.` },
|
|
27
|
+
],
|
|
28
|
+
tasks: [phrase],
|
|
29
|
+
},
|
|
30
|
+
files: {
|
|
31
|
+
[`${base}.md`]: [
|
|
32
|
+
`# ${title}`,
|
|
33
|
+
'',
|
|
34
|
+
`The ${phrase} is \`<${tag}>\`, a component of the \`${name.split('/')[0]}\` namespace. This paragraph says what it is and what it's for, in a sentence or two.`,
|
|
35
|
+
'',
|
|
36
|
+
`The first decision a consumer makes about it follows here, with its choices. [The contract](${name}#rules) holds the rules its output meets, and [the default example](${name}/examples/default.html) holds its default markup.`,
|
|
37
|
+
'',
|
|
38
|
+
].join('\n'),
|
|
39
|
+
[`${base}.contract.json`]: json({
|
|
40
|
+
rules: [
|
|
41
|
+
{
|
|
42
|
+
id: 'has-content',
|
|
43
|
+
rule: `Every \`<${tag}>\` holds content.`,
|
|
44
|
+
why: 'An empty element renders nothing a reader can perceive.',
|
|
45
|
+
severity: 'must',
|
|
46
|
+
tags: ['content'],
|
|
47
|
+
check: { each: tag, has: ':not(:empty)' },
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
}),
|
|
51
|
+
[`${base}.css`]: [
|
|
52
|
+
`/* <${tag}>: the reference stylesheet. Every selector sits inside`,
|
|
53
|
+
' * :where(), so a consumer\'s own rule always wins, and every value is a',
|
|
54
|
+
' * --ca-* custom property. */',
|
|
55
|
+
'',
|
|
56
|
+
`:where(${tag}) {`,
|
|
57
|
+
' display: block;',
|
|
58
|
+
'}',
|
|
59
|
+
'',
|
|
60
|
+
].join('\n'),
|
|
61
|
+
'examples/default.html': `<${tag}>${title}</${tag}>\n`,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function guidance({ base, title }) {
|
|
67
|
+
return {
|
|
68
|
+
manifest: {
|
|
69
|
+
kind: 'guidance',
|
|
70
|
+
description: `A guide to ${title.toLowerCase()}.`,
|
|
71
|
+
doc: `${base}.md`,
|
|
72
|
+
files: [{ path: `${base}.md`, role: 'doc' }],
|
|
73
|
+
tasks: [title.toLowerCase()],
|
|
74
|
+
},
|
|
75
|
+
files: {
|
|
76
|
+
[`${base}.md`]: [
|
|
77
|
+
`# ${title}`,
|
|
78
|
+
'',
|
|
79
|
+
'Say what this doc covers and when to read it, in a sentence or two.',
|
|
80
|
+
'',
|
|
81
|
+
'## Do the first task',
|
|
82
|
+
'',
|
|
83
|
+
'Give the steps of one task per section, as a numbered list when their order matters. Head each section with a verb phrase.',
|
|
84
|
+
'',
|
|
85
|
+
].join('\n'),
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function template({ name, base, title }, collection) {
|
|
91
|
+
const layout = itemNamed(collection, 'standard/layout');
|
|
92
|
+
const version = semver.valid(layout?.manifest.version);
|
|
93
|
+
const page = `examples/${base}.html`;
|
|
94
|
+
return {
|
|
95
|
+
manifest: {
|
|
96
|
+
kind: 'template',
|
|
97
|
+
description: `A ${title.toLowerCase()} page.`,
|
|
98
|
+
doc: `${base}.md`,
|
|
99
|
+
files: [
|
|
100
|
+
{ path: `${base}.md`, role: 'doc' },
|
|
101
|
+
{ path: `${base}.composition.json`, role: 'composition' },
|
|
102
|
+
{ path: page, role: 'reference-html', title, description: `A whole ${title.toLowerCase()} page in document order.` },
|
|
103
|
+
],
|
|
104
|
+
...(version ? { dependencies: { 'standard/layout': `^${semver.major(version)}.${semver.minor(version)}.0` } } : {}),
|
|
105
|
+
tasks: [`${title.toLowerCase()} page`],
|
|
106
|
+
},
|
|
107
|
+
files: {
|
|
108
|
+
[`${base}.md`]: [
|
|
109
|
+
`# ${title}`,
|
|
110
|
+
'',
|
|
111
|
+
'Say what page this template is and who it serves, in a sentence or two. Then name what the page holds, in document order, as a list.',
|
|
112
|
+
'',
|
|
113
|
+
`[The composition](${name}/${base}.composition.json) holds the intent of each region, and [the page](${name}/${page}) holds the whole page.`,
|
|
114
|
+
'',
|
|
115
|
+
].join('\n'),
|
|
116
|
+
[`${base}.composition.json`]: json({
|
|
117
|
+
regions: [{ id: 'content', title: 'Content', contents: "What the page says, in place of the example's heading and paragraph." }],
|
|
118
|
+
}),
|
|
119
|
+
[page]: ['<ca-layout>', ' <main>', ' <section class="ca-prose">', ` <h1>${title}</h1>`, " <p>The page's opening paragraph.</p>", ' </section>', ' </main>', '</ca-layout>', ''].join('\n'),
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function media({ base, title }) {
|
|
125
|
+
const file = `${base}.svg`;
|
|
126
|
+
const [width, height] = [960, 540];
|
|
127
|
+
return {
|
|
128
|
+
manifest: {
|
|
129
|
+
kind: 'media',
|
|
130
|
+
description: `A placeholder for the ${title.toLowerCase()} image.`,
|
|
131
|
+
files: [{ path: file, role: 'image' }],
|
|
132
|
+
tasks: [`${title.toLowerCase()} image`],
|
|
133
|
+
media: {
|
|
134
|
+
alt: `A gray rectangle standing in for the ${title.toLowerCase()} image.`,
|
|
135
|
+
renditions: [{ file, width, height }],
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
files: {
|
|
139
|
+
[file]: `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img"><title>${title}</title><rect width="${width}" height="${height}" fill="#d4d4d4"/></svg>\n`,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function page({ base, title }) {
|
|
145
|
+
const doc = `${base}.html`;
|
|
146
|
+
return {
|
|
147
|
+
manifest: {
|
|
148
|
+
kind: 'page',
|
|
149
|
+
description: `The ${title.toLowerCase()} page.`,
|
|
150
|
+
doc,
|
|
151
|
+
files: [{ path: doc, role: 'doc' }],
|
|
152
|
+
slug: base,
|
|
153
|
+
},
|
|
154
|
+
files: {
|
|
155
|
+
[doc]: formatPage(`<section class="ca-prose"><h1>${title.replace(/&/g, '&').replace(/</g, '<')}</h1><p>The page's opening paragraph.</p></section>`),
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function skill({ base, skillName }) {
|
|
161
|
+
const name = skillName ?? base.replace(/-skill$/, '');
|
|
162
|
+
const phrase = name.replace(/-/g, ' ');
|
|
163
|
+
const refused = frontmatterFindings({ name, description: 'A skill.' })[0];
|
|
164
|
+
if (refused) throw usageError(`${refused.message} Name the skill with --skill-name.`);
|
|
165
|
+
return {
|
|
166
|
+
manifest: {
|
|
167
|
+
kind: 'skill',
|
|
168
|
+
description: `The ${phrase} skill.`,
|
|
169
|
+
doc: 'SKILL.md',
|
|
170
|
+
files: [{ path: 'SKILL.md', role: 'doc' }],
|
|
171
|
+
tasks: [phrase],
|
|
172
|
+
},
|
|
173
|
+
files: {
|
|
174
|
+
'SKILL.md': [
|
|
175
|
+
'---',
|
|
176
|
+
`name: ${name}`,
|
|
177
|
+
`description: Say what the ${name} skill does and when an agent uses it, in a sentence or two.`,
|
|
178
|
+
'---',
|
|
179
|
+
'',
|
|
180
|
+
`# ${titleOf(name)}`,
|
|
181
|
+
'',
|
|
182
|
+
'Give the steps an agent follows, as a numbered list when their order matters.',
|
|
183
|
+
'',
|
|
184
|
+
].join('\n'),
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export const KINDS = { component, guidance, template, media, page, skill };
|
|
190
|
+
|
|
191
|
+
export function scaffold(collection, { kind, name, dir, title = null, skillName = null }) {
|
|
192
|
+
const match = NAME.exec(name ?? '');
|
|
193
|
+
if (!match) throw usageError(`${name} is not a scoped name, such as myteam/status-badge: lowercase letters and digits, in words joined by hyphens, a slash between the namespace and the name.`);
|
|
194
|
+
const [, scope, base] = match;
|
|
195
|
+
if (!Object.hasOwn(KINDS, kind)) throw usageError(`${kind} is not a kind new scaffolds. The kinds are ${Object.keys(KINDS).join(', ')}.`);
|
|
196
|
+
const namespaces = collection.config?.namespaces ?? [];
|
|
197
|
+
if (!namespaces.includes(scope)) throw usageError(`${scope} is not among the namespaces collection.json authors${namespaces.length ? `, ${namespaces.join(', ')}` : ''}. Add it there first.`);
|
|
198
|
+
if (itemNamed(collection, name)) throw usageError(`The collection already holds ${name}.`);
|
|
199
|
+
if (base === 'notes' && kind !== 'media') throw usageError("A doc named notes.md is a folder's notes, which never ship. Pick another name.");
|
|
200
|
+
if (skillName !== null && kind !== 'skill') throw usageError('--skill-name names a skill, so it takes kind skill alone.');
|
|
201
|
+
const source = relative(collection.root, dir).split(sep).join('/');
|
|
202
|
+
if (!source || source.startsWith('..') || resolvePath(collection.root, source) !== dir) throw usageError(`${dir} is not a folder inside the collection.`);
|
|
203
|
+
if (existsSync(dir) && readdirSync(dir).length) throw usageError(`${source} already exists and holds files.`);
|
|
204
|
+
|
|
205
|
+
const shape = KINDS[kind]({ name, base, title: title ?? titleOf(base), skillName }, collection);
|
|
206
|
+
const { kind: k, description, ...rest } = shape.manifest;
|
|
207
|
+
const manifest = { name, kind: k, version: '0.1.0', title: title ?? titleOf(base), description, ...rest, source };
|
|
208
|
+
const files = { 'item.json': json(manifest), ...shape.files };
|
|
209
|
+
for (const [path, body] of Object.entries(files)) {
|
|
210
|
+
mkdirSync(dirname(join(dir, path)), { recursive: true });
|
|
211
|
+
writeFileSync(join(dir, path), body);
|
|
212
|
+
}
|
|
213
|
+
return { name, kind, dir: source, files: Object.keys(files).sort() };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export const command = {
|
|
217
|
+
synopsis: 'new <kind> <scope>/<name>',
|
|
218
|
+
summary: `scaffold an item folder that passes validate: ${Object.keys(KINDS).join(', ')}`,
|
|
219
|
+
usage: [`ca-ds new ${Object.keys(KINDS).join('|')} <scope>/<name> [--dir <folder>] [--title <title>] [--skill-name <name>]`],
|
|
220
|
+
options: { dir: { type: 'string' }, title: { type: 'string' }, 'skill-name': { type: 'string' } },
|
|
221
|
+
async run({ values, positionals, io, collection }) {
|
|
222
|
+
const [kind, name, ...extra] = positionals;
|
|
223
|
+
if (!kind || !name || extra.length) throw usageError('Name one kind and one scoped name.');
|
|
224
|
+
const c = collection();
|
|
225
|
+
const dir = resolvePath(values.dir ?? NAME.exec(name)?.[2] ?? name);
|
|
226
|
+
const result = scaffold(c, { kind, name, dir, title: values.title ?? null, skillName: values['skill-name'] ?? null });
|
|
227
|
+
io.log(`Wrote ${name}, a ${kind}, to ${result.dir}/: ${result.files.join(', ')}`);
|
|
228
|
+
return { result };
|
|
229
|
+
},
|
|
230
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import semver from 'semver';
|
|
2
|
+
import { isSystemItem } from '../lib/system.js';
|
|
3
|
+
import { CliError, EXIT, usageError } from './exit.js';
|
|
4
|
+
import { liveVersions } from './add.js';
|
|
5
|
+
import { connect, TOKEN } from './remote.js';
|
|
6
|
+
|
|
7
|
+
export async function targets(collection, client) {
|
|
8
|
+
const { config, lock } = collection;
|
|
9
|
+
const release = config.release ?? null;
|
|
10
|
+
let newest = null;
|
|
11
|
+
if (release) {
|
|
12
|
+
newest = await client.release('latest');
|
|
13
|
+
if (!newest) throw new CliError(`collection.json names the release ${release}, and the registry at ${client.url} records none.`, EXIT.registry);
|
|
14
|
+
}
|
|
15
|
+
const pins = new Map((newest?.items ?? []).map((e) => [e.name, e.version]));
|
|
16
|
+
const out = [];
|
|
17
|
+
for (const [name, entry] of Object.entries(lock.items).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
18
|
+
const record = await client.item(name);
|
|
19
|
+
const latest = record?.latest ?? null;
|
|
20
|
+
const range = config.items?.[name] ?? null;
|
|
21
|
+
let wanted = latest;
|
|
22
|
+
if (release && isSystemItem(name)) wanted = pins.get(name) ?? null;
|
|
23
|
+
else if (range) wanted = semver.maxSatisfying(liveVersions(record), range);
|
|
24
|
+
out.push({ name, current: entry.version, wanted, latest, requested: entry.requested });
|
|
25
|
+
}
|
|
26
|
+
return { items: out, release: newest };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const above = (v, current) => v !== null && semver.gt(v, current);
|
|
30
|
+
|
|
31
|
+
// The entries above from and at or below to, newest first.
|
|
32
|
+
export async function entriesBetween(client, name, from, to) {
|
|
33
|
+
const answer = await client.changelog(name, from);
|
|
34
|
+
return (answer?.versions ?? [])
|
|
35
|
+
.filter((v) => semver.gt(v.version, from) && semver.lte(v.version, to))
|
|
36
|
+
.map((v) => ({ version: v.version, yanked: v.yankedAt !== null, summary: v.changelog?.summary ?? null, entry: v.changelog?.entry ?? null, source: v.changelog?.source ?? null }));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const indented = (text) => text.split('\n').map((line) => (line ? ` ${line}` : '')).join('\n');
|
|
40
|
+
|
|
41
|
+
export function logEntries(io, name, entries) {
|
|
42
|
+
for (const e of entries) {
|
|
43
|
+
io.log(` ${name} ${e.version}${e.yanked ? ', yanked' : ''}${e.source === 'generated' ? ', generated' : ''}`);
|
|
44
|
+
if (e.summary) io.log(indented(e.summary));
|
|
45
|
+
if (e.entry) io.log(indented(e.entry));
|
|
46
|
+
if (!e.summary && !e.entry) io.log(' No changelog entry.');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const command = {
|
|
51
|
+
synopsis: 'outdated',
|
|
52
|
+
summary: 'list installed items with newer versions',
|
|
53
|
+
usage: ['ca-ds outdated [--token <token>]'],
|
|
54
|
+
options: { ...TOKEN },
|
|
55
|
+
async run({ values, positionals, io, collection, env, fetch }) {
|
|
56
|
+
if (positionals.length) throw usageError('ca-ds outdated takes no arguments.');
|
|
57
|
+
const c = collection();
|
|
58
|
+
const release = c.config.release ?? null;
|
|
59
|
+
const client = Object.keys(c.lock.items).length ? connect(c, { values, env, fetch }) : null;
|
|
60
|
+
const items = client ? await targets(c, client) : { items: [], release: null };
|
|
61
|
+
const listed = items.items.filter((i) => above(i.wanted, i.current) || above(i.latest, i.current));
|
|
62
|
+
for (const i of listed) i.changelog = await entriesBetween(client, i.name, i.current, [i.wanted, i.latest].filter(Boolean).sort(semver.rcompare)[0]);
|
|
63
|
+
if (release) io.log(`Against the latest release, ${items.release.tag}. collection.json names ${release}.`);
|
|
64
|
+
if (!listed.length) io.log('Every installed item is current.');
|
|
65
|
+
else {
|
|
66
|
+
const rows = [['name', 'current', 'wanted', 'latest'], ...listed.map((i) => [i.name, i.current, i.wanted ?? '-', i.latest ?? '-'])];
|
|
67
|
+
const widths = rows[0].map((_, col) => Math.max(...rows.map((r) => r[col].length)));
|
|
68
|
+
for (const r of rows) io.log(r.map((cell, col) => cell.padEnd(widths[col])).join(' ').trimEnd());
|
|
69
|
+
if (listed.some((i) => i.changelog.length)) io.log('\nChanges since each installed version, newest first:');
|
|
70
|
+
for (const i of listed) logEntries(io, i.name, i.changelog);
|
|
71
|
+
}
|
|
72
|
+
return { result: { release, latestRelease: items.release?.tag ?? null, items: listed } };
|
|
73
|
+
},
|
|
74
|
+
};
|
package/src/cli/pack.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join, relative, resolve as resolvePath, sep } from 'node:path';
|
|
3
|
+
import { loadContext, itemAt } from '../validate/index.js';
|
|
4
|
+
import { packItem, packRelease, ReleaseError } from '../lib/release.js';
|
|
5
|
+
import { CliError, EXIT, usageError } from './exit.js';
|
|
6
|
+
import { argPath } from './items.js';
|
|
7
|
+
|
|
8
|
+
const where = (f) => [f.file, f.line].filter((x) => x != null).join(':');
|
|
9
|
+
const rel = (root, p) => {
|
|
10
|
+
const r = relative(root, p);
|
|
11
|
+
return r.startsWith('..') ? p : r.split(sep).join('/');
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function printRejections(io, result) {
|
|
15
|
+
io.warn(`rejected ${result.name ?? '(unnamed)'} ${result.dir}`);
|
|
16
|
+
for (const f of result.rejections) io.warn(` ${f.check}/${f.rule} ${where(f)} ${f.message}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function release(c, values, io) {
|
|
20
|
+
try {
|
|
21
|
+
const out = values.out === undefined ? null : resolvePath(values.out);
|
|
22
|
+
const { tag, dir, manifest, results, rejected } = await packRelease({ root: c.root, tag: values.tag ?? null, out });
|
|
23
|
+
const warnings = results.reduce((n, r) => n + r.warnings.length, 0);
|
|
24
|
+
if (!dir) {
|
|
25
|
+
rejected.forEach((r) => printRejections(io, r));
|
|
26
|
+
io.warn(`packed nothing: ${rejected.length} items rejected`);
|
|
27
|
+
return { exit: EXIT.rejected, result: { tag, dir: null, items: 0, warnings, rejected } };
|
|
28
|
+
}
|
|
29
|
+
io.log(`packed release ${tag} ${rel(c.root, dir)} ${manifest.items.length} items, ${warnings} warnings`);
|
|
30
|
+
return { result: { tag, dir: rel(c.root, dir), items: manifest.items.length, warnings, rejected: [] } };
|
|
31
|
+
} catch (err) {
|
|
32
|
+
if (err instanceof ReleaseError) throw new CliError(err.message, EXIT.rejected);
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const command = {
|
|
38
|
+
synopsis: 'pack [name...]',
|
|
39
|
+
summary: 'write item ZIPs, with --release',
|
|
40
|
+
usage: ['ca-ds pack [<name or folder> ...] [--out <folder>]', 'ca-ds pack --release [--tag v<semver>] [--out <folder>]'],
|
|
41
|
+
options: { out: { type: 'string' }, release: { type: 'boolean' }, tag: { type: 'string' } },
|
|
42
|
+
async run({ values, positionals, io, collection }) {
|
|
43
|
+
const c = collection();
|
|
44
|
+
if (values.release) {
|
|
45
|
+
if (positionals.length) throw usageError('--release packs every item. Name no item with it.');
|
|
46
|
+
return release(c, values, io);
|
|
47
|
+
}
|
|
48
|
+
if (values.tag !== undefined) throw usageError('--tag names a release. Use it with --release.');
|
|
49
|
+
const out = values.out === undefined ? join(c.root, 'build/pack') : resolvePath(values.out);
|
|
50
|
+
const ctx = await loadContext(c);
|
|
51
|
+
const targets = positionals.length
|
|
52
|
+
? positionals.map((arg) => {
|
|
53
|
+
const abs = argPath(c, arg);
|
|
54
|
+
if (!existsSync(abs)) throw usageError(`${arg} is no item in the collection and no folder.`);
|
|
55
|
+
return itemAt(ctx, abs, arg);
|
|
56
|
+
})
|
|
57
|
+
: c.items.filter((i) => i.authored !== false).map((i) => itemAt(ctx, join(c.root, i.dir)));
|
|
58
|
+
const packed = [];
|
|
59
|
+
const rejected = [];
|
|
60
|
+
for (const item of targets) {
|
|
61
|
+
const { result, file, bytes, count } = await packItem(item, ctx);
|
|
62
|
+
if (!file) {
|
|
63
|
+
printRejections(io, result);
|
|
64
|
+
rejected.push(result);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
mkdirSync(out, { recursive: true });
|
|
68
|
+
writeFileSync(join(out, file), bytes);
|
|
69
|
+
const path = rel(c.root, join(out, file));
|
|
70
|
+
io.log(`packed ${result.name} ${path} ${count} files, ${result.warnings.length} warnings`);
|
|
71
|
+
packed.push({ name: result.name, version: item.manifest.version, file: path, files: count, warnings: result.warnings.length });
|
|
72
|
+
}
|
|
73
|
+
return { exit: rejected.length ? EXIT.rejected : EXIT.ok, result: { packed, rejected } };
|
|
74
|
+
},
|
|
75
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { relative, sep } from 'node:path';
|
|
2
|
+
import { usageError } from './exit.js';
|
|
3
|
+
|
|
4
|
+
export const command = {
|
|
5
|
+
synopsis: 'preview',
|
|
6
|
+
summary: 'serve items and pages with live reload, with --screenshots',
|
|
7
|
+
usage: ['ca-ds preview [--port <n>] [--open | --no-open]', 'ca-ds preview --screenshots [<scoped name> ...]'],
|
|
8
|
+
options: {
|
|
9
|
+
port: { type: 'string' },
|
|
10
|
+
open: { type: 'boolean' },
|
|
11
|
+
'no-open': { type: 'boolean' },
|
|
12
|
+
screenshots: { type: 'boolean' },
|
|
13
|
+
},
|
|
14
|
+
async run({ values, positionals, io, collection }) {
|
|
15
|
+
const c = collection();
|
|
16
|
+
if (values.screenshots) {
|
|
17
|
+
const { screenshots } = await import('../preview/screenshots.js');
|
|
18
|
+
const shots = await screenshots(c, { names: positionals, log: io.log });
|
|
19
|
+
const written = shots.map((s) => ({ name: s.name, theme: s.theme, file: relative(c.root, s.out).split(sep).join('/') }));
|
|
20
|
+
return { result: { written } };
|
|
21
|
+
}
|
|
22
|
+
if (positionals.length) throw usageError('ca-ds preview takes item names with --screenshots alone.');
|
|
23
|
+
const named = values.port ?? process.env.PORT;
|
|
24
|
+
const port = named === undefined ? 3000 : Number(named);
|
|
25
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw usageError(`The port ${named} is not a port number.`);
|
|
26
|
+
const { startPreview } = await import('../preview/server.js');
|
|
27
|
+
const openBrowser = values['no-open'] ? false : values.open ? true : io.json ? false : null;
|
|
28
|
+
const { url, running } = await startPreview({ root: c.root, port, explicit: named !== undefined, openBrowser, log: io.log });
|
|
29
|
+
return { result: { url, running } };
|
|
30
|
+
},
|
|
31
|
+
};
|