@astryxdesign/cli 0.7.4 → 0.7.5-canary.07605c2
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 +18 -18
- package/api/component/_adapter.mjs +16 -1
- package/api/component/component.test.mjs +31 -0
- package/api/discover/_adapter.d.mts +1 -1
- package/api/docs/_adapter.d.mts +5 -2
- package/api/docs/_adapter.mjs +24 -16
- package/api/docs/docs.test.mjs +18 -0
- package/api/docs/integrationDocs.test.mjs +2 -2
- package/api/experimental.d.mts +5 -0
- package/api/experimental.mjs +15 -0
- package/api/hook/list/list.d.mts +1 -1
- package/api/icon/_adapter.d.mts +3 -2
- package/api/icon/_adapter.mjs +16 -3
- package/api/icon/_authoring.d.mts +5 -1
- package/api/icon/_authoring.mjs +478 -63
- package/api/icon/_options.d.mts +11 -0
- package/api/icon/_options.mjs +39 -4
- package/api/icon/_records.d.mts +19 -0
- package/api/icon/_records.mjs +25 -0
- package/api/icon/add/add.d.mts +3 -3
- package/api/icon/add/add.mjs +227 -14
- package/api/icon/all-or-nothing.test.mjs +283 -0
- package/api/icon/detail/detail.mjs +2 -13
- package/api/icon/error-codes.test.mjs +14 -7
- package/api/icon/icon.d.mts +2 -2
- package/api/icon/icon.doc.mjs +30 -2
- package/api/icon/icon.mjs +57 -8
- package/api/icon/icon.test.mjs +133 -1
- package/api/icon/icon.type.d.mts +126 -1
- package/api/icon/icon.type.mjs +67 -3
- package/api/icon/iconAdd.doc.mjs +22 -6
- package/api/icon/iconMap.doc.mjs +24 -8
- package/api/icon/iconNewDefinition.doc.mjs +2 -2
- package/api/icon/iconNewSet.doc.mjs +30 -2
- package/api/icon/iconUse.doc.mjs +4 -1
- package/api/icon/map/map.d.mts +4 -4
- package/api/icon/map/map.mjs +134 -9
- package/api/icon/new-definition/new-definition.mjs +6 -2
- package/api/icon/new-set/new-set.mjs +2 -2
- package/api/icon/new-set/new-set.test.mjs +60 -10
- package/api/icon/option-pairs.test.mjs +368 -0
- package/api/icon/search/search.d.mts +10 -0
- package/api/icon/search/search.mjs +55 -14
- package/api/icon/use/use.mjs +2 -13
- package/api/integration/add-helpers.d.mts +15 -1
- package/api/integration/add-helpers.mjs +1 -1
- package/api/integration/pack-check.lifecycle-output.test.mjs +24 -1
- package/api/integration/pack-check.mjs +10 -4
- package/api/integration/pack-check.test.mjs +5 -1
- package/api/integration/validate-integration.type.d.mts +3 -2
- package/api/integration/validate-integration.type.mjs +3 -2
- package/api/render/_adapter.d.mts +28 -0
- package/api/render/_adapter.mjs +540 -0
- package/api/render/render.d.mts +15 -0
- package/api/render/render.doc.d.mts +9 -0
- package/api/render/render.doc.mjs +112 -0
- package/api/render/render.mjs +113 -0
- package/api/render/render.test.mjs +1155 -0
- package/api/render/render.type.d.mts +74 -0
- package/api/render/render.type.mjs +64 -0
- package/api/search/_adapter.test.mjs +21 -2
- package/api/search/_result.d.mts +5 -5
- package/api/search/search.d.mts +1 -1
- package/api/template/template.mjs +3 -1
- package/api/template/template.type.d.mts +2 -1
- package/api/template/template.type.mjs +1 -1
- package/api/template/unknown-name.test.mjs +63 -0
- package/assets/codemods/transforms/v0.7.0/__tests__/validate-data-token-ownership.test.mjs +123 -70
- package/assets/codemods/transforms/v0.7.0/validate-data-token-ownership.mjs +19 -77
- package/assets/discover/official-packages.json +4 -0
- package/assets/docs/tree/icon-definitions.doc.mjs +2 -2
- package/assets/docs/tree/icon-set-files.doc.mjs +1 -1
- package/assets/docs/tree/icon-sets.doc.mjs +50 -20
- package/assets/docs/tree/render-pages.doc.mjs +94 -0
- package/assets/icons/definitions/activity.doc.mjs +1 -1
- package/assets/icons/definitions/alignCenter.doc.mjs +9 -0
- package/assets/icons/definitions/alignEnd.doc.mjs +10 -0
- package/assets/icons/definitions/alignStart.doc.mjs +10 -0
- package/assets/icons/definitions/archive.doc.mjs +1 -1
- package/assets/icons/definitions/arrowsUpDown.doc.mjs +1 -1
- package/assets/icons/definitions/attach.doc.mjs +1 -1
- package/assets/icons/definitions/audio.doc.mjs +1 -1
- package/assets/icons/definitions/cart.doc.mjs +1 -1
- package/assets/icons/definitions/checkDouble.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsLeft.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsRight.doc.mjs +1 -1
- package/assets/icons/definitions/close.doc.mjs +1 -1
- package/assets/icons/definitions/collapse.doc.mjs +1 -1
- package/assets/icons/definitions/compose.doc.mjs +1 -1
- package/assets/icons/definitions/conversation.doc.mjs +1 -1
- package/assets/icons/definitions/copy.doc.mjs +1 -1
- package/assets/icons/definitions/cpu.doc.mjs +9 -0
- package/assets/icons/definitions/database.doc.mjs +1 -1
- package/assets/icons/definitions/desktop.doc.mjs +1 -1
- package/assets/icons/definitions/discount.doc.mjs +1 -1
- package/assets/icons/definitions/download.doc.mjs +1 -1
- package/assets/icons/definitions/emoji.doc.mjs +1 -1
- package/assets/icons/definitions/error.doc.mjs +1 -1
- package/assets/icons/definitions/expand.doc.mjs +1 -1
- package/assets/icons/definitions/experiment.doc.mjs +1 -1
- package/assets/icons/definitions/externalLink.doc.mjs +7 -1
- package/assets/icons/definitions/eyeSlash.doc.mjs +1 -1
- package/assets/icons/definitions/flipHorizontal.doc.mjs +9 -0
- package/assets/icons/definitions/flipVertical.doc.mjs +9 -0
- package/assets/icons/definitions/folderAdd.doc.mjs +1 -1
- package/assets/icons/definitions/gitBranch.doc.mjs +9 -0
- package/assets/icons/definitions/gitFork.doc.mjs +9 -0
- package/assets/icons/definitions/info.doc.mjs +1 -1
- package/assets/icons/definitions/location.doc.mjs +1 -1
- package/assets/icons/definitions/mention.doc.mjs +1 -1
- package/assets/icons/definitions/menu.doc.mjs +1 -1
- package/assets/icons/definitions/microphone.doc.mjs +1 -1
- package/assets/icons/definitions/mobile.doc.mjs +1 -1
- package/assets/icons/definitions/money.doc.mjs +1 -1
- package/assets/icons/definitions/moreHorizontal.doc.mjs +1 -1
- package/assets/icons/definitions/notificationActive.doc.mjs +1 -1
- package/assets/icons/definitions/notificationOff.doc.mjs +1 -1
- package/assets/icons/definitions/panelLeft.doc.mjs +10 -0
- package/assets/icons/definitions/panelRight.doc.mjs +10 -0
- package/assets/icons/definitions/raiseHand.doc.mjs +1 -1
- package/assets/icons/definitions/redo.doc.mjs +1 -1
- package/assets/icons/definitions/refresh.doc.mjs +1 -1
- package/assets/icons/definitions/rotate.doc.mjs +9 -0
- package/assets/icons/definitions/scale.doc.mjs +10 -0
- package/assets/icons/definitions/search.doc.mjs +1 -1
- package/assets/icons/definitions/send.doc.mjs +1 -1
- package/assets/icons/definitions/settings.doc.mjs +1 -1
- package/assets/icons/definitions/star.doc.mjs +1 -1
- package/assets/icons/definitions/stop.doc.mjs +1 -1
- package/assets/icons/definitions/success.doc.mjs +1 -1
- package/assets/icons/definitions/textCase.doc.mjs +9 -0
- package/assets/icons/definitions/thumbsDown.doc.mjs +1 -1
- package/assets/icons/definitions/thumbsUp.doc.mjs +1 -1
- package/assets/icons/definitions/trendDown.doc.mjs +7 -1
- package/assets/icons/definitions/trendUp.doc.mjs +1 -1
- package/assets/icons/definitions/undo.doc.mjs +1 -1
- package/assets/icons/definitions/upload.doc.mjs +1 -1
- package/assets/icons/definitions/userAdd.doc.mjs +1 -1
- package/assets/icons/definitions/warning.doc.mjs +1 -1
- package/assets/templates/blocks/components/Tokenizer/TokenizerClear.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerCreatable.tsx +30 -30
- package/assets/templates/blocks/components/Tokenizer/TokenizerEndContent.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerIcon.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerMaxEntries.tsx +17 -16
- package/assets/templates/blocks/components/Tokenizer/TokenizerOverflow.tsx +29 -29
- package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +9 -8
- package/assets/templates/blocks/components/Tokenizer/TokenizerStates.tsx +39 -41
- package/assets/templates/pages/canvas-editor/template.doc.mjs +9 -0
- package/assets/templates/pages/form-wizard-inline/template.doc.mjs +6 -1
- package/assets/templates/pages/table-comparison/template.doc.mjs +1 -0
- package/assets/templates/pages/table-tree/template.doc.mjs +3 -0
- package/authoring/debug/parse.d.mts +2 -2
- package/authoring/doctypes/icon-definition/icon-definition.doc.mjs +4 -4
- package/authoring/doctypes/icon-set/icon-set-data.doc.mjs +6 -0
- package/authoring/doctypes/icon-set/parse.d.mts +1 -0
- package/authoring/doctypes/icon-set/parse.mjs +1 -0
- package/authoring/doctypes/icon-set/type.ts +5 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +2 -1
- package/clients/cli/commands/doctor.mjs +16 -17
- package/clients/cli/commands/experimental/icon.mjs +167 -35
- package/clients/cli/commands/experimental/index.mjs +2 -0
- package/clients/cli/commands/experimental/render.mjs +113 -0
- package/clients/cli/commands/experimental-icon-add.doc.mjs +14 -4
- package/clients/cli/commands/experimental-icon-map.doc.mjs +12 -2
- package/clients/cli/commands/experimental-icon-new-definition.doc.mjs +1 -1
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +28 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +18 -2
- package/clients/cli/commands/experimental-render.doc.mjs +60 -0
- package/clients/cli/commands/experimental.doc.mjs +4 -2
- package/clients/cli/commands/icon-authoring.test.mjs +1823 -13
- package/clients/cli/commands/icon-options-wiring.test.mjs +158 -0
- package/clients/cli/commands/icon.test.mjs +1 -1
- package/clients/cli/commands/template.mjs +4 -1
- package/clients/cli/commands/text-json-parity.test.mjs +46 -0
- package/clients/cli/index.mjs +1 -0
- package/foundation/discovery/component-discovery.d.mts +2 -1
- package/foundation/discovery/component-discovery.export-names.test.mjs +41 -0
- package/foundation/discovery/component-discovery.mjs +13 -7
- package/foundation/discovery/icon-set-checks.d.mts +43 -9
- package/foundation/discovery/icon-set-checks.mjs +296 -21
- package/foundation/discovery/icon-set-checks.test.mjs +125 -12
- package/foundation/discovery/icon-set-writer.d.mts +192 -3
- package/foundation/discovery/icon-set-writer.mjs +608 -34
- package/foundation/discovery/icon-set-writer.test.mjs +26 -0
- package/foundation/discovery/module-exports.mjs +82 -25
- package/foundation/discovery/module-exports.test.mjs +38 -1
- package/foundation/discovery/package-exports.d.mts +13 -0
- package/foundation/discovery/package-exports.mjs +40 -1
- package/foundation/discovery/template-icons.d.mts +6 -0
- package/foundation/discovery/template-icons.mjs +98 -10
- package/foundation/discovery/template-icons.test.mjs +103 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +20 -5
- package/foundation/doc-compiler/import.mjs +81 -8
- package/foundation/doc-compiler/read.d.mts +4 -3
- package/foundation/doc-compiler/read.mjs +15 -10
- package/foundation/doc-compiler/read.test.mjs +59 -1
- package/foundation/doc-compiler/tree.mjs +3 -1
- package/foundation/fs/module-loader.d.mts +3 -2
- package/foundation/fs/module-loader.mjs +3 -2
- package/foundation/fs/publish-file-eperm-mutation.test.mjs +2 -0
- package/foundation/fs/read-record.d.mts +27 -0
- package/foundation/fs/read-record.mjs +83 -15
- package/foundation/fs/read-record.test.mjs +88 -1
- package/foundation/render/assets.d.mts +31 -0
- package/foundation/render/assets.mjs +102 -0
- package/foundation/render/component-map.d.mts +52 -0
- package/foundation/render/component-map.mjs +141 -0
- package/foundation/render/css-cut.d.mts +29 -0
- package/foundation/render/css-cut.mjs +144 -0
- package/foundation/render/embedded-files.d.mts +28 -0
- package/foundation/render/embedded-files.mjs +108 -0
- package/foundation/render/embedded-imports.d.mts +61 -0
- package/foundation/render/embedded-imports.mjs +315 -0
- package/foundation/render/html-reader.d.mts +37 -0
- package/foundation/render/html-reader.mjs +121 -0
- package/foundation/render/page-worker.d.mts +7 -0
- package/foundation/render/page-worker.mjs +519 -0
- package/foundation/render/rebuild.d.mts +19 -0
- package/foundation/render/rebuild.mjs +269 -0
- package/foundation/render/render-page.d.mts +85 -0
- package/foundation/render/render-page.mjs +612 -0
- package/foundation/render/render.test.mjs +117 -0
- package/foundation/render/source-block.d.mts +52 -0
- package/foundation/render/source-block.mjs +106 -0
- package/foundation/response/error-codes.d.mts +10 -1
- package/foundation/response/error-codes.doc.mjs +56 -0
- package/foundation/response/error-codes.mjs +61 -0
- package/foundation/response/response-types.doc.mjs +38 -1
- package/foundation/text/string-utils.mjs +33 -0
- package/foundation/text/string-utils.test.mjs +17 -0
- package/foundation/text/suggest-order.d.mts +17 -3
- package/foundation/text/suggest-order.mjs +58 -13
- package/foundation/text/suggest-order.test.mjs +53 -0
- package/package.json +14 -9
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import * as fs from 'node:fs';
|
|
25
25
|
import * as path from 'node:path';
|
|
26
26
|
import {randomUUID} from 'node:crypto';
|
|
27
|
+
import {AsyncLocalStorage} from 'node:async_hooks';
|
|
27
28
|
import {parse} from '@babel/parser';
|
|
28
29
|
import {parseIconSetData} from '../../authoring/doctypes/icon-set/parse.mjs';
|
|
29
30
|
import {ICON_NAMES} from '../../authoring/doctypes/icon-set/icon-names.mjs';
|
|
@@ -34,8 +35,11 @@ import {levenshteinDistance} from '../text/string-utils.mjs';
|
|
|
34
35
|
import {astryxDefinitionsByName} from './icon-definition-discovery.mjs';
|
|
35
36
|
import {
|
|
36
37
|
discoverIconSetDirectory,
|
|
38
|
+
NOTICE_FILE_RE,
|
|
37
39
|
resolveIconModule,
|
|
38
40
|
} from './icon-set-discovery.mjs';
|
|
41
|
+
import {findInstalledPackage} from '../fs/paths.mjs';
|
|
42
|
+
import {cliVersion} from '../release/run-context.mjs';
|
|
39
43
|
import {
|
|
40
44
|
exportScalarType,
|
|
41
45
|
exportValueKind,
|
|
@@ -64,16 +68,31 @@ export class IconSetAuthoringError extends Error {
|
|
|
64
68
|
|
|
65
69
|
/**
|
|
66
70
|
* `XMarkIcon` → `x-mark`; `Trash2` → `trash-2`; `ArrowUpDown` →
|
|
67
|
-
* `arrow-up-down
|
|
71
|
+
* `arrow-up-down`; `Squares2X2Icon` and `Grid2x2` → `squares-2x2`,
|
|
72
|
+
* `grid-2x2`: an icon id from an export name. A dimension (a digit, `x` or
|
|
73
|
+
* `X`, a digit) stays one word.
|
|
68
74
|
* @param {string} name
|
|
69
75
|
*/
|
|
70
76
|
export function iconIdOf(name) {
|
|
71
|
-
return
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
return (
|
|
78
|
+
name
|
|
79
|
+
.replace(/Icon$/u, '')
|
|
80
|
+
// `2X2` and `2x2` are one word: lower the x first.
|
|
81
|
+
.replace(/([0-9])[xX](?=[0-9])/gu, '$1x')
|
|
82
|
+
// A one-letter word before a digit ends there: `CalendarX2` is
|
|
83
|
+
// `calendar-x-2`, as its library names it.
|
|
84
|
+
.replace(/([a-z][A-Z])(?=[0-9])/gu, '$1-')
|
|
85
|
+
// A lower-case letter before a capital or a digit ends a word, except
|
|
86
|
+
// the x inside a dimension.
|
|
87
|
+
.replace(/([a-z])(?=[A-Z0-9])/gu, (match, letter, offset, whole) =>
|
|
88
|
+
letter === 'x' && /[0-9]/u.test(whole[offset - 1] ?? '')
|
|
89
|
+
? letter
|
|
90
|
+
: `${letter}-`,
|
|
91
|
+
)
|
|
92
|
+
.replace(/([0-9])([A-Z])/gu, '$1-$2')
|
|
93
|
+
.replace(/([A-Z])([A-Z][a-z])/gu, '$1-$2')
|
|
94
|
+
.toLowerCase()
|
|
95
|
+
);
|
|
77
96
|
}
|
|
78
97
|
|
|
79
98
|
/**
|
|
@@ -180,12 +199,17 @@ export async function findLocalIconSet(cwd, selector) {
|
|
|
180
199
|
? found.errors.find(error => error.name === selector)
|
|
181
200
|
: null;
|
|
182
201
|
if (broken) {
|
|
183
|
-
|
|
202
|
+
// The same report every icon entry point gives a withdrawn set
|
|
203
|
+
// (AST-084 FR17): withdrawn, with why, never "not there".
|
|
204
|
+
throw new IconSetAuthoringError(
|
|
205
|
+
`Icon set "${selector}" is in this package but withdrawn: ${broken.message}`,
|
|
206
|
+
'ERR_NOT_FOUND',
|
|
207
|
+
);
|
|
184
208
|
}
|
|
185
209
|
const names = found.sets.map(set => set.name);
|
|
186
210
|
if (named.length === 0 && selector) {
|
|
187
211
|
throw new IconSetAuthoringError(
|
|
188
|
-
`
|
|
212
|
+
`No icon set named "${selector}" in this package`,
|
|
189
213
|
'ERR_NOT_FOUND',
|
|
190
214
|
names.map(name => ({name, reason: 'an icon set in this package'})),
|
|
191
215
|
);
|
|
@@ -207,6 +231,7 @@ const DATA_KEYS = [
|
|
|
207
231
|
'schemaVersion',
|
|
208
232
|
'module',
|
|
209
233
|
'source',
|
|
234
|
+
'exclude',
|
|
210
235
|
'generated',
|
|
211
236
|
'adapter',
|
|
212
237
|
'variants',
|
|
@@ -306,6 +331,87 @@ export function writeIconSetData(set, next, {extraDefinitions = []} = {}) {
|
|
|
306
331
|
};
|
|
307
332
|
}
|
|
308
333
|
|
|
334
|
+
/**
|
|
335
|
+
* The files one authoring command has changed, each with the bytes it had
|
|
336
|
+
* before (null: it did not exist), and the folders it created. Every write of
|
|
337
|
+
* an icon authoring command records here first, so a command that fails
|
|
338
|
+
* after some of its writes puts every file back (AST-084 FR13).
|
|
339
|
+
* @typedef {{files: Map<string, Buffer | null>, dirs: string[]}} WriteJournal
|
|
340
|
+
*/
|
|
341
|
+
|
|
342
|
+
/** @type {AsyncLocalStorage<WriteJournal>} */
|
|
343
|
+
const journals = new AsyncLocalStorage();
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Record a file's bytes before the running command writes or removes it, and
|
|
347
|
+
* the folders above it that do not exist yet. Outside {@link allOrNothing}
|
|
348
|
+
* this does nothing.
|
|
349
|
+
* @param {string} file
|
|
350
|
+
*/
|
|
351
|
+
export function journalFile(file) {
|
|
352
|
+
const journal = journals.getStore();
|
|
353
|
+
if (!journal) return;
|
|
354
|
+
const full = path.resolve(file);
|
|
355
|
+
if (journal.files.has(full)) return;
|
|
356
|
+
journal.files.set(full, fs.existsSync(full) ? fs.readFileSync(full) : null);
|
|
357
|
+
/** @type {string | null} */
|
|
358
|
+
let missing = null;
|
|
359
|
+
for (
|
|
360
|
+
let dir = path.dirname(full);
|
|
361
|
+
!fs.existsSync(dir) && path.dirname(dir) !== dir;
|
|
362
|
+
dir = path.dirname(dir)
|
|
363
|
+
) {
|
|
364
|
+
missing = dir;
|
|
365
|
+
}
|
|
366
|
+
if (missing && !journal.dirs.includes(missing)) journal.dirs.push(missing);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Run one authoring command so its writes are all or nothing: when it throws,
|
|
371
|
+
* every file it wrote gets its old bytes back (a file it created is removed,
|
|
372
|
+
* with the folders it created), and the error is rethrown. A file system
|
|
373
|
+
* error becomes `ERR_WRITE_FAILED`.
|
|
374
|
+
* @template T
|
|
375
|
+
* @param {() => T | Promise<T>} step
|
|
376
|
+
* @returns {Promise<T>}
|
|
377
|
+
*/
|
|
378
|
+
export async function allOrNothing(step) {
|
|
379
|
+
/** @type {WriteJournal} */
|
|
380
|
+
const journal = {files: new Map(), dirs: []};
|
|
381
|
+
try {
|
|
382
|
+
return await journals.run(journal, step);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
for (const [file, bytes] of [...journal.files].reverse()) {
|
|
385
|
+
try {
|
|
386
|
+
if (bytes == null) fs.rmSync(file, {force: true});
|
|
387
|
+
else if (!fs.existsSync(file) || !fs.readFileSync(file).equals(bytes)) {
|
|
388
|
+
fs.writeFileSync(file, bytes);
|
|
389
|
+
}
|
|
390
|
+
} catch {
|
|
391
|
+
// Put back what can be put back; the error below names the cause.
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
for (const dir of [...journal.dirs].reverse()) {
|
|
395
|
+
try {
|
|
396
|
+
fs.rmSync(dir, {recursive: true, force: true});
|
|
397
|
+
} catch {
|
|
398
|
+
// A path that never became a folder has nothing to remove.
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const code =
|
|
402
|
+
error && typeof error === 'object' && 'code' in error
|
|
403
|
+
? String(error.code)
|
|
404
|
+
: '';
|
|
405
|
+
if (!(error instanceof IconSetAuthoringError) && /^E[A-Z]+$/u.test(code)) {
|
|
406
|
+
throw new IconSetAuthoringError(
|
|
407
|
+
`Could not write the icon set: ${error instanceof Error ? error.message : String(error)}. No file was changed.`,
|
|
408
|
+
'ERR_WRITE_FAILED',
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
309
415
|
/**
|
|
310
416
|
* Replace a file's text atomically.
|
|
311
417
|
* @param {string} file
|
|
@@ -316,6 +422,7 @@ function replaceText(file, text) {
|
|
|
316
422
|
path.dirname(file),
|
|
317
423
|
`.${path.basename(file)}.tmp-${randomUUID()}`,
|
|
318
424
|
);
|
|
425
|
+
journalFile(file);
|
|
319
426
|
try {
|
|
320
427
|
fs.writeFileSync(temporary, text, 'utf-8');
|
|
321
428
|
fs.renameSync(temporary, file);
|
|
@@ -386,6 +493,51 @@ export function resolveIconSource(from, {packageDir, pkg}) {
|
|
|
386
493
|
return {file, module: from};
|
|
387
494
|
}
|
|
388
495
|
|
|
496
|
+
/**
|
|
497
|
+
* The installed library a package specifier reads from: its name, version,
|
|
498
|
+
* license, and license notice files, from its own package.json. Null for a
|
|
499
|
+
* ./ path, or when the library is not installed or has no version.
|
|
500
|
+
* @param {string} from a package or package subpath
|
|
501
|
+
* @param {string} packageDir the set's package
|
|
502
|
+
* @returns {{package: string, version: string, license: string | null, dir: string, notices: string[]} | null}
|
|
503
|
+
*/
|
|
504
|
+
export function librarySource(from, packageDir) {
|
|
505
|
+
if (from.startsWith('.') || path.isAbsolute(from)) return null;
|
|
506
|
+
const parts = from.split('/');
|
|
507
|
+
const name = from.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
508
|
+
const dir = findInstalledPackage(packageDir, name, Infinity);
|
|
509
|
+
if (!dir) return null;
|
|
510
|
+
/** @type {Record<string, any>} */
|
|
511
|
+
let pkg;
|
|
512
|
+
try {
|
|
513
|
+
pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
|
|
514
|
+
} catch {
|
|
515
|
+
return null;
|
|
516
|
+
}
|
|
517
|
+
if (typeof pkg.version !== 'string' || pkg.version === '') return null;
|
|
518
|
+
/** @type {string[]} */
|
|
519
|
+
let notices = [];
|
|
520
|
+
try {
|
|
521
|
+
notices = fs
|
|
522
|
+
.readdirSync(dir, {withFileTypes: true})
|
|
523
|
+
.filter(file => file.isFile() && NOTICE_FILE_RE.test(file.name))
|
|
524
|
+
.map(file => file.name)
|
|
525
|
+
.sort();
|
|
526
|
+
} catch {
|
|
527
|
+
// No readable notices.
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
package: name,
|
|
531
|
+
version: pkg.version,
|
|
532
|
+
license:
|
|
533
|
+
typeof pkg.license === 'string' && pkg.license.trim() !== ''
|
|
534
|
+
? pkg.license
|
|
535
|
+
: null,
|
|
536
|
+
dir,
|
|
537
|
+
notices,
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
|
|
389
541
|
/** @param {string} base */
|
|
390
542
|
function resolveFile(base) {
|
|
391
543
|
const extensions = ['', '.mjs', '.js', '.cjs', '.ts', '.tsx', '.jsx', '.mts'];
|
|
@@ -419,7 +571,16 @@ export function assertIconExport(file, module, exportName) {
|
|
|
419
571
|
? `"${module}" has no default export.`
|
|
420
572
|
: `"${module}" does not export "${exportName}".`,
|
|
421
573
|
'ERR_NOT_FOUND',
|
|
422
|
-
|
|
574
|
+
[
|
|
575
|
+
...new Set([
|
|
576
|
+
// `XMark` for `XMarkIcon`: the library's name with its suffix.
|
|
577
|
+
...names.filter(
|
|
578
|
+
name =>
|
|
579
|
+
name === `${exportName}Icon` || `${name}Icon` === exportName,
|
|
580
|
+
),
|
|
581
|
+
...closestNames(exportName, names),
|
|
582
|
+
]),
|
|
583
|
+
].map(name => ({
|
|
423
584
|
name,
|
|
424
585
|
reason: `an export of ${module}`,
|
|
425
586
|
})),
|
|
@@ -434,7 +595,7 @@ const SCREAMING_CASE_RE = /^[A-Z][A-Z0-9_]+$/u;
|
|
|
434
595
|
* The library exports that can be icons: PascalCase components and, when the
|
|
435
596
|
* set declares an adapter, also data exports, camelCase included: objects,
|
|
436
597
|
* and plain strings only in a library whose candidates are all strings.
|
|
437
|
-
* Left out: `default`, `Icon`, SCREAMING_CASE constants, number and boolean
|
|
598
|
+
* Left out: `default`, `Icon`, `…Provider` and `…Context` components, SCREAMING_CASE constants, number and boolean
|
|
438
599
|
* exports, and `exclude`.
|
|
439
600
|
* @param {string} file
|
|
440
601
|
* @param {{adapter?: boolean, exclude?: string[]}} [options] `exclude`: export
|
|
@@ -447,6 +608,9 @@ function iconCandidates(file, {adapter = false, exclude = []} = {}) {
|
|
|
447
608
|
const out = [];
|
|
448
609
|
for (const name of followedExportNames(file)) {
|
|
449
610
|
if (name === 'default' || name === 'Icon' || skipped.has(name)) continue;
|
|
611
|
+
// A context provider or context (`LucideProvider`) is a component that
|
|
612
|
+
// draws nothing.
|
|
613
|
+
if (/(?:Provider|Context)$/u.test(name)) continue;
|
|
450
614
|
// SCREAMING_CASE names are constants (`README`, `VERSION`), not icons.
|
|
451
615
|
if (SCREAMING_CASE_RE.test(name)) continue;
|
|
452
616
|
const kind = exportValueKind(file, name);
|
|
@@ -470,6 +634,21 @@ function iconCandidates(file, {adapter = false, exclude = []} = {}) {
|
|
|
470
634
|
: out;
|
|
471
635
|
}
|
|
472
636
|
|
|
637
|
+
/**
|
|
638
|
+
* The exports of a library that are not icons and that wrapping it leaves
|
|
639
|
+
* out (`Icon`, `LucideProvider`, `createLucideIcon`, constants), so the
|
|
640
|
+
* receipt can say so. Another name of an icon is not listed.
|
|
641
|
+
* @param {string} file
|
|
642
|
+
* @param {{adapter?: boolean, exclude?: string[]}} [options]
|
|
643
|
+
* @returns {string[]}
|
|
644
|
+
*/
|
|
645
|
+
export function nonIconExports(file, options = {}) {
|
|
646
|
+
const icons = new Set(iconCandidates(file, options).map(each => each.name));
|
|
647
|
+
return followedExportNames(file).filter(
|
|
648
|
+
name => name !== 'default' && !icons.has(name),
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
|
|
473
652
|
/**
|
|
474
653
|
* The names the library prefix is computed over: the icon-like candidates
|
|
475
654
|
* (stray constants are already left out by {@link iconCandidates}).
|
|
@@ -550,7 +729,9 @@ export function modulePrefix(file, options) {
|
|
|
550
729
|
* @param {string} file
|
|
551
730
|
* @param {{adapter?: boolean, exclude?: string[]}} [options] `adapter`:
|
|
552
731
|
* whether the set declares one; `exclude`: export names that are not icons
|
|
553
|
-
* @returns {Array<{export: string, id: string, name: string, adapt: boolean}>}
|
|
732
|
+
* @returns {Array<{export: string, id: string, name: string, adapt: boolean, aliases: string[]}>}
|
|
733
|
+
* `aliases`: the ids of the other names the library exports the icon as,
|
|
734
|
+
* leaving out the library's own name (`lucide-trash`)
|
|
554
735
|
*/
|
|
555
736
|
export function libraryIcons(file, options = {}) {
|
|
556
737
|
const candidates = iconCandidates(file, options);
|
|
@@ -564,7 +745,7 @@ export function libraryIcons(file, options = {}) {
|
|
|
564
745
|
entry.names.push(name);
|
|
565
746
|
byOrigin.set(key, entry);
|
|
566
747
|
}
|
|
567
|
-
/** @type {Array<[string, string, boolean, string | null]>} */
|
|
748
|
+
/** @type {Array<[string, string, boolean, string | null, string[]]>} */
|
|
568
749
|
const chosen = [];
|
|
569
750
|
for (const [key, {names, adapt, scalar}] of byOrigin) {
|
|
570
751
|
const local = key.split('\0')[1];
|
|
@@ -575,17 +756,48 @@ export function libraryIcons(file, options = {}) {
|
|
|
575
756
|
[...(withIcon.length > 0 ? withIcon : names)].sort(
|
|
576
757
|
(a, b) => a.length - b.length || (a < b ? -1 : 1),
|
|
577
758
|
)[0];
|
|
578
|
-
chosen.push([key, preferred, adapt, scalar]);
|
|
759
|
+
chosen.push([key, preferred, adapt, scalar, names]);
|
|
579
760
|
}
|
|
580
761
|
const prefix = libraryPrefix(
|
|
581
762
|
prefixNames(chosen.map(([, name, , scalar]) => ({name, scalar}))),
|
|
582
763
|
);
|
|
583
|
-
/** @type {Map<string, {export: string, id: string, name: string, adapt: boolean}>} */
|
|
764
|
+
/** @type {Map<string, {export: string, id: string, name: string, adapt: boolean, aliases: string[]}>} */
|
|
584
765
|
const byId = new Map();
|
|
585
|
-
for (const [, preferred, adapt] of chosen) {
|
|
766
|
+
for (const [, preferred, adapt, , names] of chosen) {
|
|
586
767
|
const naming = iconNaming(preferred, prefix);
|
|
587
768
|
if (!naming.id || byId.has(naming.id)) continue;
|
|
588
|
-
|
|
769
|
+
// The other names the library exports the same icon as (Lucide's
|
|
770
|
+
// `Trash2` for `Trash`), as ids: an icon is found and mapped by them too.
|
|
771
|
+
const aliases = [
|
|
772
|
+
...new Set(names.map(name => iconNaming(name, prefix).id)),
|
|
773
|
+
].filter(alias => alias && alias !== naming.id);
|
|
774
|
+
byId.set(naming.id, {export: preferred, ...naming, adapt, aliases});
|
|
775
|
+
}
|
|
776
|
+
// A word every alias of most icons starts with (`lucide` in
|
|
777
|
+
// `LucideTrash`) is the library's name, not an alias.
|
|
778
|
+
/** @type {Map<string, number>} */
|
|
779
|
+
const leads = new Map();
|
|
780
|
+
for (const icon of byId.values()) {
|
|
781
|
+
for (const alias of icon.aliases) {
|
|
782
|
+
if (alias.endsWith(`-${icon.id}`)) {
|
|
783
|
+
const lead = alias.slice(0, -icon.id.length - 1);
|
|
784
|
+
leads.set(lead, (leads.get(lead) ?? 0) + 1);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
const brand = new Set(
|
|
789
|
+
[...leads]
|
|
790
|
+
.filter(([, count]) => count >= Math.max(2, byId.size / 2))
|
|
791
|
+
.map(([lead]) => lead),
|
|
792
|
+
);
|
|
793
|
+
for (const icon of byId.values()) {
|
|
794
|
+
icon.aliases = icon.aliases
|
|
795
|
+
.filter(
|
|
796
|
+
alias =>
|
|
797
|
+
![...brand].some(lead => alias.startsWith(`${lead}-`)) &&
|
|
798
|
+
!byId.has(alias),
|
|
799
|
+
)
|
|
800
|
+
.sort();
|
|
589
801
|
}
|
|
590
802
|
return [...byId.values()].sort((a, b) =>
|
|
591
803
|
a.id < b.id ? -1 : a.id > b.id ? 1 : 0,
|
|
@@ -677,6 +889,7 @@ export function writeDefinitionFile(set, doc) {
|
|
|
677
889
|
);
|
|
678
890
|
}
|
|
679
891
|
const text = iconDefinitionSource(doc);
|
|
892
|
+
journalFile(file);
|
|
680
893
|
fs.mkdirSync(path.dirname(file), {recursive: true});
|
|
681
894
|
try {
|
|
682
895
|
fs.writeFileSync(file, text, {encoding: 'utf-8', flag: 'wx'});
|
|
@@ -695,8 +908,10 @@ export function writeDefinitionFile(set, doc) {
|
|
|
695
908
|
|
|
696
909
|
/** The comment the generated icons module opens with. */
|
|
697
910
|
const GENERATED_HEADER = [
|
|
698
|
-
'// Generated by `astryx experimental icon add
|
|
699
|
-
'// Edit with
|
|
911
|
+
'// Generated by `astryx experimental icon` (new-set, add, map): one export per icon of this set.',
|
|
912
|
+
'// Edit with those commands; a statement added by hand is kept.',
|
|
913
|
+
'// themeIcons has one entry per built-in icon definition in map (the names Core renders), not one per icon;',
|
|
914
|
+
'// the import lines above it give it local names for the re-exported icons.',
|
|
700
915
|
];
|
|
701
916
|
|
|
702
917
|
/**
|
|
@@ -917,8 +1132,11 @@ export function generatedModuleText(
|
|
|
917
1132
|
/** @param {string} name */
|
|
918
1133
|
const existing = name =>
|
|
919
1134
|
[...exports].find(line =>
|
|
1135
|
+
// The name the module exports: after `as`, alone in braces, or a
|
|
1136
|
+
// const; never the source name of another export (`TrashIcon as
|
|
1137
|
+
// TrashSolidIcon` exports TrashSolidIcon).
|
|
920
1138
|
new RegExp(
|
|
921
|
-
`(?:\\
|
|
1139
|
+
`(?:\\bas\\s+${name}\\s*\\}|^export\\s+\\{\\s*${name}\\s*\\}|^export const\\s+${name}\\b)`,
|
|
922
1140
|
'u',
|
|
923
1141
|
).test(line),
|
|
924
1142
|
);
|
|
@@ -1075,6 +1293,98 @@ export function generatedNamesOf(packageDir, generated, from, source) {
|
|
|
1075
1293
|
return names;
|
|
1076
1294
|
}
|
|
1077
1295
|
|
|
1296
|
+
/**
|
|
1297
|
+
* Where each of the set's exports comes from, read from its generated
|
|
1298
|
+
* module: the module it re-exports or adapts (a package specifier, or a
|
|
1299
|
+
* `./` path from the package root) and the export it reads there
|
|
1300
|
+
* (`default` for a default export). `export {HiTrashIcon as TrashIcon} from
|
|
1301
|
+
* '@acme/hero'` gives `TrashIcon` → `{module: '@acme/hero', source:
|
|
1302
|
+
* 'HiTrashIcon'}`. An existing icon is known by this first: ids and the
|
|
1303
|
+
* set's export names are derived from it, and a newer rule may spell them
|
|
1304
|
+
* differently; and a refresh changes only the icons its module supplies.
|
|
1305
|
+
* @param {string} packageDir
|
|
1306
|
+
* @param {string} generated
|
|
1307
|
+
* @returns {Map<string, {module: string, source: string}>}
|
|
1308
|
+
*/
|
|
1309
|
+
export function generatedOrigins(packageDir, generated) {
|
|
1310
|
+
/** @type {Map<string, {module: string, source: string}>} */
|
|
1311
|
+
const origins = new Map();
|
|
1312
|
+
const file = path.join(packageDir, generated);
|
|
1313
|
+
if (!fs.existsSync(file)) return origins;
|
|
1314
|
+
let statements;
|
|
1315
|
+
try {
|
|
1316
|
+
statements = generatedStatements(fs.readFileSync(file, 'utf-8'), generated);
|
|
1317
|
+
} catch {
|
|
1318
|
+
return origins;
|
|
1319
|
+
}
|
|
1320
|
+
/** @param {string} specifier as the generated module writes it */
|
|
1321
|
+
const fromRoot = specifier => {
|
|
1322
|
+
if (!specifier.startsWith('.')) return specifier;
|
|
1323
|
+
const rel = path
|
|
1324
|
+
.relative(packageDir, path.resolve(path.dirname(file), specifier))
|
|
1325
|
+
.split(path.sep)
|
|
1326
|
+
.join('/');
|
|
1327
|
+
return `./${rel}`;
|
|
1328
|
+
};
|
|
1329
|
+
for (const statement of statements) {
|
|
1330
|
+
const reexport = /^export \{(\w+)(?: as (\w+))?\} from '([^']+)';$/u.exec(
|
|
1331
|
+
statement,
|
|
1332
|
+
);
|
|
1333
|
+
if (reexport) {
|
|
1334
|
+
origins.set(reexport[2] ?? reexport[1], {
|
|
1335
|
+
module: fromRoot(reexport[3]),
|
|
1336
|
+
source: reexport[1],
|
|
1337
|
+
});
|
|
1338
|
+
continue;
|
|
1339
|
+
}
|
|
1340
|
+
const adapted = /^import \{(\w+) as (\w+)Source\} from '([^']+)';$/u.exec(
|
|
1341
|
+
statement,
|
|
1342
|
+
);
|
|
1343
|
+
if (adapted) {
|
|
1344
|
+
origins.set(adapted[2], {
|
|
1345
|
+
module: fromRoot(adapted[3]),
|
|
1346
|
+
source: adapted[1],
|
|
1347
|
+
});
|
|
1348
|
+
continue;
|
|
1349
|
+
}
|
|
1350
|
+
const adaptedDefault = /^import (\w+)Source from '([^']+)';$/u.exec(
|
|
1351
|
+
statement,
|
|
1352
|
+
);
|
|
1353
|
+
if (adaptedDefault) {
|
|
1354
|
+
origins.set(adaptedDefault[1], {
|
|
1355
|
+
module: fromRoot(adaptedDefault[2]),
|
|
1356
|
+
source: 'default',
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
return origins;
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
/**
|
|
1364
|
+
* Whether two module specifiers name the same module: the same package
|
|
1365
|
+
* specifier, or the same `./` path from the package root.
|
|
1366
|
+
* @param {string} a
|
|
1367
|
+
* @param {string} b
|
|
1368
|
+
*/
|
|
1369
|
+
export function sameIconModule(a, b) {
|
|
1370
|
+
const norm = (/** @type {string} */ value) =>
|
|
1371
|
+
value.startsWith('.')
|
|
1372
|
+
? `./${path.posix.normalize(value).replace(/^\.\//u, '')}`
|
|
1373
|
+
: value;
|
|
1374
|
+
return norm(a) === norm(b);
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* The package a module specifier belongs to (`@acme/hero` for
|
|
1379
|
+
* `@acme/hero/24/solid`), or `.` for a `./` path in the package itself.
|
|
1380
|
+
* @param {string} specifier
|
|
1381
|
+
*/
|
|
1382
|
+
export function iconModulePackage(specifier) {
|
|
1383
|
+
if (specifier.startsWith('.')) return '.';
|
|
1384
|
+
const parts = specifier.split('/');
|
|
1385
|
+
return specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1078
1388
|
/**
|
|
1079
1389
|
* Write the set's generated module and its declaration file
|
|
1080
1390
|
* ({@link generatedModuleText}). Returns whether either changed, and a
|
|
@@ -1123,6 +1433,7 @@ export function writeGeneratedIcons(
|
|
|
1123
1433
|
*/
|
|
1124
1434
|
const put = (target, text, prior, rel) => {
|
|
1125
1435
|
if (prior === text) return;
|
|
1436
|
+
journalFile(target);
|
|
1126
1437
|
fs.mkdirSync(path.dirname(target), {recursive: true});
|
|
1127
1438
|
replaceText(target, text);
|
|
1128
1439
|
files.push(rel.replace(/^\.\//u, ''));
|
|
@@ -1153,9 +1464,122 @@ export function writeGeneratedIcons(
|
|
|
1153
1464
|
};
|
|
1154
1465
|
}
|
|
1155
1466
|
|
|
1467
|
+
/**
|
|
1468
|
+
* A package entry the CLI names no line for: a built file (under `dist` or
|
|
1469
|
+
* `build`) or CommonJS (`.cjs`, `.cts`, or a `.js` file that uses
|
|
1470
|
+
* `require` or `module.exports` and no `export`). Its source is what the
|
|
1471
|
+
* author edits.
|
|
1472
|
+
* @param {string} packageDir
|
|
1473
|
+
* @param {string} entry
|
|
1474
|
+
*/
|
|
1475
|
+
function isBuiltOrCommonJs(packageDir, entry) {
|
|
1476
|
+
const parts = path.relative(packageDir, entry).split(path.sep);
|
|
1477
|
+
if (parts.some(part => part === 'dist' || part === 'build')) return true;
|
|
1478
|
+
if (/\.c[jt]s$/u.test(entry)) return true;
|
|
1479
|
+
if (!/\.js$/u.test(entry)) return false;
|
|
1480
|
+
/** @type {string} */
|
|
1481
|
+
let text;
|
|
1482
|
+
try {
|
|
1483
|
+
text = fs.readFileSync(entry, 'utf-8');
|
|
1484
|
+
} catch {
|
|
1485
|
+
return false;
|
|
1486
|
+
}
|
|
1487
|
+
return (
|
|
1488
|
+
/\bmodule\.exports\b|\bexports\.[$\w]+\s*=|\brequire\(/u.test(text) &&
|
|
1489
|
+
!/^\s*export\s/mu.test(text)
|
|
1490
|
+
);
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
/**
|
|
1494
|
+
* The exact change that makes a set's generated module part of the
|
|
1495
|
+
* package's public entry, so its icons and theme map import from the package
|
|
1496
|
+
* by name (FR14), or null when the entry already exports `name`. The CLI
|
|
1497
|
+
* never makes this change itself: `exports`, `main`, `types`, `files`, and the
|
|
1498
|
+
* entry file are the author's (architecture:cli-surface INV16). `new-set`
|
|
1499
|
+
* prints it in its receipt and `integration check` in its finding, the same
|
|
1500
|
+
* words. A built or CommonJS entry is never named as the file to edit.
|
|
1501
|
+
* @param {string} packageDir
|
|
1502
|
+
* @param {Record<string, any>} pkg the package's package.json
|
|
1503
|
+
* @param {string} generated the generated module's ./ path from the package root
|
|
1504
|
+
* @param {{module?: string, name?: string}} [options] `module` the set's
|
|
1505
|
+
* module (default the package name); `name` an export the entry must have
|
|
1506
|
+
* (default the theme map)
|
|
1507
|
+
* @returns {string | null}
|
|
1508
|
+
*/
|
|
1509
|
+
export function entryWiringNote(packageDir, pkg, generated, options = {}) {
|
|
1510
|
+
const packageName = typeof pkg.name === 'string' ? pkg.name : '';
|
|
1511
|
+
const module = options.module ?? packageName;
|
|
1512
|
+
const name = options.name ?? THEME_MAP_NAME;
|
|
1513
|
+
const target = generated.replace(/\.[cm]?[jt]sx?$/u, '.js');
|
|
1514
|
+
const entry = module
|
|
1515
|
+
? resolveIconModule(module, packageDir, {
|
|
1516
|
+
name: packageName,
|
|
1517
|
+
dir: packageDir,
|
|
1518
|
+
})
|
|
1519
|
+
: null;
|
|
1520
|
+
if (entry && moduleExportOrigin(entry, name)) return null;
|
|
1521
|
+
/** @param {string} from a file in the package */
|
|
1522
|
+
const lineFor = from => {
|
|
1523
|
+
let relative = path
|
|
1524
|
+
.relative(path.dirname(from), path.join(packageDir, target))
|
|
1525
|
+
.split(path.sep)
|
|
1526
|
+
.join('/');
|
|
1527
|
+
if (!relative.startsWith('.')) relative = `./${relative}`;
|
|
1528
|
+
return `export * from '${relative}';`;
|
|
1529
|
+
};
|
|
1530
|
+
const rel = (/** @type {string} */ file) =>
|
|
1531
|
+
path.relative(packageDir, file).split(path.sep).join('/');
|
|
1532
|
+
const inside = (/** @type {string} */ file) => {
|
|
1533
|
+
const relative = path.relative(packageDir, file);
|
|
1534
|
+
return (
|
|
1535
|
+
!relative.startsWith('..') &&
|
|
1536
|
+
!path.isAbsolute(relative) &&
|
|
1537
|
+
!relative.split(path.sep).includes('node_modules')
|
|
1538
|
+
);
|
|
1539
|
+
};
|
|
1540
|
+
if (!entry) {
|
|
1541
|
+
if (
|
|
1542
|
+
pkg.exports === undefined &&
|
|
1543
|
+
pkg.main === undefined &&
|
|
1544
|
+
!fs.existsSync(path.join(packageDir, 'index.js'))
|
|
1545
|
+
) {
|
|
1546
|
+
const line = lineFor(path.join(packageDir, 'index.js'));
|
|
1547
|
+
const files = Array.isArray(pkg.files)
|
|
1548
|
+
? ` Add "index.js" and "index.d.ts" to "files".`
|
|
1549
|
+
: '';
|
|
1550
|
+
return `"${module}" has no entry, so its icons cannot be imported by name. Create index.js and index.d.ts, each with \`${line}\`, and add to package.json: \`"main": "./index.js", "types": "./index.d.ts", "exports": {".": {"types": "./index.d.ts", "default": "./index.js"}}\`.${files}`;
|
|
1551
|
+
}
|
|
1552
|
+
return `"${module}" does not resolve: point its package.json "exports" at a file that has \`export * from '${target}';\` (relative to that file), or build the package.`;
|
|
1553
|
+
}
|
|
1554
|
+
if (!inside(entry) || isBuiltOrCommonJs(packageDir, entry)) {
|
|
1555
|
+
return `${inside(entry) ? rel(entry) : `"${module}"`} is built or CommonJS, so the CLI names no file to edit: add \`export * from '${target}';\` (relative to that file) to the source module your build compiles into it, and to its types, then build, so the icons and \`${THEME_MAP_NAME}\` import from ${module}.`;
|
|
1556
|
+
}
|
|
1557
|
+
const rootExport =
|
|
1558
|
+
pkg.exports && typeof pkg.exports === 'object' ? pkg.exports['.'] : null;
|
|
1559
|
+
const typesPath =
|
|
1560
|
+
(typeof pkg.types === 'string' && pkg.types) ||
|
|
1561
|
+
(typeof pkg.typings === 'string' && pkg.typings) ||
|
|
1562
|
+
(rootExport &&
|
|
1563
|
+
typeof rootExport === 'object' &&
|
|
1564
|
+
typeof rootExport.types === 'string'
|
|
1565
|
+
? rootExport.types
|
|
1566
|
+
: null);
|
|
1567
|
+
const typesFile = typesPath ? path.resolve(packageDir, typesPath) : null;
|
|
1568
|
+
const alsoTypes =
|
|
1569
|
+
typesFile &&
|
|
1570
|
+
typesFile !== entry &&
|
|
1571
|
+
inside(typesFile) &&
|
|
1572
|
+
fs.existsSync(typesFile) &&
|
|
1573
|
+
!isBuiltOrCommonJs(packageDir, typesFile) &&
|
|
1574
|
+
!fs.readFileSync(typesFile, 'utf-8').includes(lineFor(typesFile))
|
|
1575
|
+
? ` and \`${lineFor(typesFile)}\` to ${rel(typesFile)}`
|
|
1576
|
+
: '';
|
|
1577
|
+
return `Add \`${lineFor(entry)}\` to ${rel(entry)}${alsoTypes}, so the icons import from ${module}.`;
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1156
1580
|
/**
|
|
1157
1581
|
* Whether the set's own module exports `name`, read without running it, and
|
|
1158
|
-
* the line its entry needs when it does not.
|
|
1582
|
+
* the line its entry needs when it does not ({@link entryWiringNote}).
|
|
1159
1583
|
* @param {DiscoveredIconSet} set
|
|
1160
1584
|
* @param {{packageDir: string, pkg: Record<string, any>}} where
|
|
1161
1585
|
* @param {string} generated the generated module's ./ path from the package root
|
|
@@ -1163,19 +1587,169 @@ export function writeGeneratedIcons(
|
|
|
1163
1587
|
* @returns {string | null} a note for the author, or null when nothing is needed
|
|
1164
1588
|
*/
|
|
1165
1589
|
export function setModuleNote(set, where, generated, name) {
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1590
|
+
return entryWiringNote(where.packageDir, where.pkg, generated, {
|
|
1591
|
+
module: set.data.module,
|
|
1592
|
+
name,
|
|
1169
1593
|
});
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
/**
|
|
1597
|
+
* The peers an icon-set package publishes with, as the guide's "Before you
|
|
1598
|
+
* publish" asks, that package.json does not declare yet, as the exact JSON
|
|
1599
|
+
* to add, or null when it declares them all: `react` (the theme map and
|
|
1600
|
+
* adapter use it), and as optional peers `@types/react` (the generated
|
|
1601
|
+
* declaration file imports it) and `@astryxdesign/cli` from this release.
|
|
1602
|
+
* The CLI never edits them (architecture:cli-surface INV16).
|
|
1603
|
+
* @param {Record<string, any>} pkg
|
|
1604
|
+
* @returns {string | null}
|
|
1605
|
+
*/
|
|
1606
|
+
export function peerNote(pkg) {
|
|
1607
|
+
/** @type {Array<[string, string, boolean]>} */
|
|
1608
|
+
const wanted = [
|
|
1609
|
+
['react', '>=18', false],
|
|
1610
|
+
['@types/react', '>=18', true],
|
|
1611
|
+
['@astryxdesign/cli', `>=${cliVersion()}`, true],
|
|
1612
|
+
];
|
|
1613
|
+
const missing = wanted.filter(
|
|
1614
|
+
([name]) => pkg.peerDependencies?.[name] == null,
|
|
1615
|
+
);
|
|
1616
|
+
if (missing.length === 0) return null;
|
|
1617
|
+
const peers = missing.map(([name, range]) => `"${name}": "${range}"`);
|
|
1618
|
+
const optional = missing
|
|
1619
|
+
.filter(([, , isOptional]) => isOptional)
|
|
1620
|
+
.map(([name]) => `"${name}": {"optional": true}`);
|
|
1621
|
+
return `Declare the peers your set needs; add to package.json "peerDependencies": \`{${peers.join(', ')}}\`${optional.length > 0 ? ` and to "peerDependenciesMeta": \`{${optional.join(', ')}}\`` : ''}.`;
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
/**
|
|
1625
|
+
* The `typecheck` script every integration runs in its build, as the line to
|
|
1626
|
+
* add, or null when package.json has one.
|
|
1627
|
+
* @param {Record<string, any>} pkg
|
|
1628
|
+
* @returns {string | null}
|
|
1629
|
+
*/
|
|
1630
|
+
export function typecheckNote(pkg) {
|
|
1631
|
+
if (pkg.scripts?.typecheck != null) return null;
|
|
1632
|
+
return 'Run `integration check --strict` in your build; add to package.json "scripts": `"typecheck": "astryx integration check --strict"`.';
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
/**
|
|
1636
|
+
* A definition name as `map` keys it: camelCase. The docs list each
|
|
1637
|
+
* definition by its kebab id too (`eye-slash`), so that spelling resolves.
|
|
1638
|
+
* @param {string} name
|
|
1639
|
+
* @param {Set<string>} known the definitions the set may map
|
|
1640
|
+
* @returns {string}
|
|
1641
|
+
*/
|
|
1642
|
+
export function definitionNameOf(name, known) {
|
|
1643
|
+
if (known.has(name)) return name;
|
|
1644
|
+
const camel = name.replace(/-([a-z0-9])/gu, (_, c) => c.toUpperCase());
|
|
1645
|
+
return known.has(camel) ? camel : name;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
/**
|
|
1649
|
+
* An icon id in the set, named by its id, its export name in any case
|
|
1650
|
+
* (`XMarkIcon`, `xmark`), or another name the library exports the same icon
|
|
1651
|
+
* as, as an id or as the library spells it (`trash-2` or `Trash2` for
|
|
1652
|
+
* `trash`, recorded in its words; AST-084 FR13). Never by a word alone:
|
|
1653
|
+
* null when none of these names an icon.
|
|
1654
|
+
* @param {{data: {icons: Array<{id: string, export: string, words: string[]}>}}} set
|
|
1655
|
+
* @param {string} id
|
|
1656
|
+
* @returns {string | null}
|
|
1657
|
+
*/
|
|
1658
|
+
export function iconIdIn(set, id) {
|
|
1659
|
+
if (set.data.icons.some(icon => icon.id === id)) return id;
|
|
1660
|
+
// The export, or the name the library exports it by (`XMarkIcon`,
|
|
1661
|
+
// `XMark`, `X`), in any case.
|
|
1662
|
+
const lower = id.toLowerCase();
|
|
1663
|
+
const byExport = set.data.icons.find(
|
|
1664
|
+
icon =>
|
|
1665
|
+
icon.export.toLowerCase() === lower ||
|
|
1666
|
+
icon.export.toLowerCase() === `${lower}icon`,
|
|
1667
|
+
);
|
|
1668
|
+
if (byExport) return byExport.id;
|
|
1669
|
+
// A library export name (`Trash2`, `Search`) is spelled as the id it
|
|
1670
|
+
// gives (`trash-2`, `search`), which is the icon's id or one of the other
|
|
1671
|
+
// names its library exports it as.
|
|
1672
|
+
const candidates = [id, ...(/[A-Z]/u.test(id) ? [iconIdOf(id)] : [])];
|
|
1673
|
+
for (const candidate of candidates) {
|
|
1674
|
+
if (set.data.icons.some(icon => icon.id === candidate)) return candidate;
|
|
1675
|
+
// Another name the library exports the icon as: one of its words that
|
|
1676
|
+
// is not a part of its own id. A part of an id (`glass` in
|
|
1677
|
+
// `magnifying-glass`) names no icon, and neither does a name two icons
|
|
1678
|
+
// share.
|
|
1679
|
+
const holders = set.data.icons.filter(icon =>
|
|
1680
|
+
otherNamesOf(icon).includes(candidate),
|
|
1681
|
+
);
|
|
1682
|
+
if (holders.length === 1) return holders[0].id;
|
|
1683
|
+
}
|
|
1684
|
+
return null;
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
/**
|
|
1688
|
+
* The other names a library exports an icon as, recorded among its words:
|
|
1689
|
+
* each word that is not a part of its id.
|
|
1690
|
+
* @param {{id: string, words?: string[]}} icon
|
|
1691
|
+
* @returns {string[]}
|
|
1692
|
+
*/
|
|
1693
|
+
export function otherNamesOf(icon) {
|
|
1694
|
+
const parts = icon.id.split('-');
|
|
1695
|
+
return (icon.words ?? []).filter(word => !parts.includes(word));
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
/**
|
|
1699
|
+
* The icons that carry `word` but are not named by it, for a refusal's
|
|
1700
|
+
* suggestions: `glass` is a word of `magnifying-glass`, not its name.
|
|
1701
|
+
* @param {{data: {icons: Array<{id: string, words?: string[]}>}}} set
|
|
1702
|
+
* @param {string} word
|
|
1703
|
+
* @returns {string[]}
|
|
1704
|
+
*/
|
|
1705
|
+
export function iconsWithWord(set, word) {
|
|
1706
|
+
return set.data.icons
|
|
1707
|
+
.filter(
|
|
1708
|
+
icon =>
|
|
1709
|
+
(icon.words ?? []).includes(word) || icon.id.split('-').includes(word),
|
|
1710
|
+
)
|
|
1711
|
+
.map(icon => icon.id)
|
|
1712
|
+
.slice(0, 5);
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
/**
|
|
1716
|
+
* The package.json `files` change a ./ source needs to ship, or null when
|
|
1717
|
+
* the package lists no `files` or one covers it: its own folder (`src/icons`
|
|
1718
|
+
* for `./src/icons/x.js`), as the exact array. The CLI never edits `files`
|
|
1719
|
+
* (architecture:cli-surface INV16).
|
|
1720
|
+
* @param {string} packageDir
|
|
1721
|
+
* @param {string} module the source specifier
|
|
1722
|
+
* @returns {string | null}
|
|
1723
|
+
*/
|
|
1724
|
+
export function localSourceNote(packageDir, module) {
|
|
1725
|
+
if (!module.startsWith('./')) return null;
|
|
1726
|
+
// The file's own folder (`src/icons`), never more: other files beside
|
|
1727
|
+
// the package's source stay unpublished.
|
|
1728
|
+
const parts = module.slice(2).split('/');
|
|
1729
|
+
const top = parts.length > 1 ? parts.slice(0, -1).join('/') : parts[0];
|
|
1730
|
+
if (!top) return null;
|
|
1731
|
+
/** @type {Record<string, any>} */
|
|
1732
|
+
let pkg;
|
|
1733
|
+
try {
|
|
1734
|
+
pkg = JSON.parse(
|
|
1735
|
+
fs.readFileSync(path.join(packageDir, 'package.json'), 'utf-8'),
|
|
1736
|
+
);
|
|
1737
|
+
} catch {
|
|
1738
|
+
return null;
|
|
1739
|
+
}
|
|
1740
|
+
if (!Array.isArray(pkg.files)) return null;
|
|
1741
|
+
const normalize = (/** @type {string} */ value) =>
|
|
1742
|
+
value.replace(/^\.\//u, '').replace(/\/$/u, '');
|
|
1743
|
+
const wanted = normalize(module);
|
|
1744
|
+
if (
|
|
1745
|
+
pkg.files.some(
|
|
1746
|
+
(/** @type {unknown} */ each) =>
|
|
1747
|
+
typeof each === 'string' &&
|
|
1748
|
+
(wanted === normalize(each) ||
|
|
1749
|
+
wanted.startsWith(`${normalize(each)}/`)),
|
|
1750
|
+
)
|
|
1751
|
+
) {
|
|
1752
|
+
return null;
|
|
1174
1753
|
}
|
|
1175
|
-
|
|
1176
|
-
.relative(path.dirname(entry), target)
|
|
1177
|
-
.split(path.sep)
|
|
1178
|
-
.join('/');
|
|
1179
|
-
if (!relative.startsWith('.')) relative = `./${relative}`;
|
|
1180
|
-
return `Add \`export * from '${relative}';\` to ${path.relative(where.packageDir, entry).split(path.sep).join('/')}, so the icons import from ${set.data.module}.`;
|
|
1754
|
+
return `${module} is not in package.json "files", so it would not be published; set "files" to \`${JSON.stringify([...pkg.files, top])}\`.`;
|
|
1181
1755
|
}
|