@lablup/ui-common-cli 0.2.0-alpha.15
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 +202 -0
- package/NOTICE +8 -0
- package/README.md +28 -0
- package/bin/ui-common.mjs +12 -0
- package/cli/agents.mjs +335 -0
- package/cli/diff.mjs +125 -0
- package/cli/lab-peer.mjs +226 -0
- package/cli/main.mjs +97 -0
- package/cli/passthrough.mjs +153 -0
- package/cli/paths.mjs +290 -0
- package/cli/report.mjs +157 -0
- package/cli/resolve.mjs +257 -0
- package/cli/rewrite.mjs +133 -0
- package/cli/semver.mjs +278 -0
- package/cli/shadow.mjs +161 -0
- package/cli/sync-astryx.mjs +417 -0
- package/cli/upgrade.mjs +776 -0
- package/codemods/0.2/components.mjs +903 -0
- package/codemods/0.2/elements.mjs +745 -0
- package/codemods/0.2/index.mjs +68 -0
- package/codemods/0.2/legacy-classes.json +292 -0
- package/codemods/0.2/local-modules.mjs +510 -0
- package/codemods/0.2/map.mjs +178 -0
- package/codemods/0.2/package-json.mjs +333 -0
- package/codemods/0.2/scan.mjs +397 -0
- package/codemods/0.2/stylesheets.mjs +479 -0
- package/codemods/0.2/theme.mjs +325 -0
- package/codemods/lib/jsx.mjs +352 -0
- package/codemods/lib/todo.mjs +151 -0
- package/codemods/registry.mjs +95 -0
- package/codemods/upstream.mjs +155 -0
- package/migration/0.1-to-0.2.json +629 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
@lablup/ui-common-cli
|
|
2
|
+
Copyright 2026 Lablup Inc.
|
|
3
|
+
|
|
4
|
+
This product includes software developed at Lablup Inc.
|
|
5
|
+
(https://www.lablup.com/).
|
|
6
|
+
|
|
7
|
+
It runs the Astryx CLI (@astryxdesign/cli, MIT-licensed by Meta Platforms,
|
|
8
|
+
Inc.), which it depends on and does not vendor.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @lablup/ui-common-cli
|
|
2
|
+
|
|
3
|
+
The `ui-common` command line for [`@lablup/ui-common`](https://www.npmjs.com/package/@lablup/ui-common):
|
|
4
|
+
the Astryx CLI that ui-common pins, with its output in `@lablup/ui-common`
|
|
5
|
+
terms, the agent block, and the upgrade codemods.
|
|
6
|
+
|
|
7
|
+
It is a separate package so the CLI's toolchain (the Astryx CLI, jscodeshift,
|
|
8
|
+
postcss) stays out of a consumer's production install. It is released at the
|
|
9
|
+
same version as `@lablup/ui-common` and takes it as a peer.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
pnpm dlx @lablup/ui-common-cli@next upgrade --from 0.1 # one-off (npx @lablup/ui-common-cli@next …)
|
|
13
|
+
|
|
14
|
+
pnpm add -D @lablup/ui-common-cli@<the @lablup/ui-common version> # or keep it next to ui-common
|
|
15
|
+
pnpm exec ui-common --help
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
While 0.2 is in prerelease, name the `next` dist-tag (or an exact version):
|
|
19
|
+
only prereleases are published, and npm points `latest` at a package's first
|
|
20
|
+
publish, so a bare `@lablup/ui-common-cli` resolves to its first alpha. Plain
|
|
21
|
+
`@lablup/ui-common-cli` works once 0.2.0 is published.
|
|
22
|
+
|
|
23
|
+
Documentation: [The ui-common CLI](https://github.com/lablup/ui-common#the-ui-common-cli)
|
|
24
|
+
in the repository README.
|
|
25
|
+
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
[Apache-2.0](LICENSE). See [NOTICE](NOTICE).
|
package/cli/agents.mjs
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ui-common agents`: the agent block for a project that uses ui-common.
|
|
3
|
+
*
|
|
4
|
+
* Built from the block `astryx init --features agents` would write, rendered
|
|
5
|
+
* in memory through the pinned CLI (nothing is written to disk to get it),
|
|
6
|
+
* then rewritten into ui-common terms and extended with ui-common's own rules.
|
|
7
|
+
* It lives between UI-COMMON markers, not ASTRYX ones, so `astryx init` and
|
|
8
|
+
* `astryx upgrade` never overwrite it.
|
|
9
|
+
*/
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { dirname, join, resolve } from "node:path";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
binInvocation,
|
|
15
|
+
customExports,
|
|
16
|
+
dependencyDir,
|
|
17
|
+
dependencyVersion,
|
|
18
|
+
findProjectDir,
|
|
19
|
+
forkedExports,
|
|
20
|
+
hiddenExports,
|
|
21
|
+
importAstryxInternal,
|
|
22
|
+
uiCommonPackageJson,
|
|
23
|
+
} from "./paths.mjs";
|
|
24
|
+
import { rewriteOutput } from "./rewrite.mjs";
|
|
25
|
+
|
|
26
|
+
export const MARKER_START = "<!-- UI-COMMON:START -->";
|
|
27
|
+
export const MARKER_END = "<!-- UI-COMMON:END -->";
|
|
28
|
+
const ASTRYX_START = "<!-- ASTRYX:START -->";
|
|
29
|
+
const ASTRYX_END = "<!-- ASTRYX:END -->";
|
|
30
|
+
|
|
31
|
+
export const LAYER_ORDER =
|
|
32
|
+
"@layer reset, theme, base, astryx-base, astryx-theme, ui-common, components, utilities;";
|
|
33
|
+
|
|
34
|
+
/** Files `--check` looks at when no `--write` target is given, in order. */
|
|
35
|
+
export const DEFAULT_AGENT_FILES = ["AGENTS.md", "CLAUDE.md", ".claude/CLAUDE.md"];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The block `astryx init --features agents` would install in `cwd`.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} cwd
|
|
41
|
+
*/
|
|
42
|
+
async function renderAstryxBlock(cwd) {
|
|
43
|
+
const { renderAgentDocsBlock } = await importAstryxInternal(
|
|
44
|
+
"foundation/agent-docs/agent-docs.mjs",
|
|
45
|
+
);
|
|
46
|
+
return /** @type {string} */ (await renderAgentDocsBlock(cwd));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Astryx counts components from the core it finds under the project. A
|
|
51
|
+
* consumer that depends on ui-common alone has no core of its own there, and
|
|
52
|
+
* the header falls back to "90+"; count from the core ui-common pins instead.
|
|
53
|
+
*/
|
|
54
|
+
async function coreComponentCount() {
|
|
55
|
+
const coreDir = dependencyDir("@astryxdesign/core");
|
|
56
|
+
if (!coreDir) return null;
|
|
57
|
+
try {
|
|
58
|
+
const { discoverComponents } = await importAstryxInternal(
|
|
59
|
+
"foundation/discovery/component-discovery.mjs",
|
|
60
|
+
);
|
|
61
|
+
let total = 0;
|
|
62
|
+
for (const list of Object.values(discoverComponents(coreDir))) {
|
|
63
|
+
total += /** @type {unknown[]} */ (list).length;
|
|
64
|
+
}
|
|
65
|
+
return total > 0 ? total : null;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** ui-common's own lines, appended to the rewritten Astryx block. */
|
|
72
|
+
function uiCommonSection({ version, astryxVersion, invocation }) {
|
|
73
|
+
const components = customExports()
|
|
74
|
+
.filter((c) => !c.legacy && !c.fork && /^[A-Z]/.test(c.name))
|
|
75
|
+
.map((c) => c.name);
|
|
76
|
+
const lines = [
|
|
77
|
+
`UI-COMMON (@lablup/ui-common v${version} wraps Astryx v${astryxVersion}):`,
|
|
78
|
+
"- Import only from @lablup/ui-common: the root, or the same subpath Astryx uses (@lablup/ui-common/Button, /theme/tokens.stylex, /lab). Never import @astryxdesign/* directly.",
|
|
79
|
+
`- Layers: declare \`${LAYER_ORDER}\` once, first, in the entry stylesheet. ui-common's styles sit in \`ui-common\`; yours go in \`components\` / \`utilities\`.`,
|
|
80
|
+
];
|
|
81
|
+
for (const entry of hiddenExports()) {
|
|
82
|
+
if (!entry.replacedBy) continue;
|
|
83
|
+
lines.push(
|
|
84
|
+
`- Use ${entry.replacedBy} (@lablup/ui-common/${entry.replacedBy}), not ${entry.name}: ui-common hides ${entry.name}.`,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
for (const fork of forkedExports()) {
|
|
88
|
+
lines.push(
|
|
89
|
+
`- ${fork.name} (${fork.from}) comes from ui-common: its own copy of Astryx's, same API and import path. ${fork.reason}`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
lines.push(
|
|
93
|
+
"- Theme: <Theme theme={lablupTheme}> with lablupTheme from @lablup/ui-common/theme/lablup/built, plus @lablup/ui-common/theme/lablup/theme.css. A product palette is its own defineTheme over lablupTheme.",
|
|
94
|
+
"- Strings: every built-in string is a prop; defaults come from ui-common's catalog. Pass uiCommonMessages from @lablup/ui-common/i18n-catalog to Astryx's InternationalizationProvider. Never a product i18n runtime.",
|
|
95
|
+
);
|
|
96
|
+
if (components.length > 0) {
|
|
97
|
+
lines.push(
|
|
98
|
+
`- ui-common's own components: ${components.join(", ")}. \`${invocation} docs ui-common\` explains them.`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
lines.push(
|
|
102
|
+
`- The \`ui-common\` bin is @lablup/ui-common-cli, a devDependency pinned to the same version as @lablup/ui-common; bump both together. Without it installed, \`pnpm dlx @lablup/ui-common-cli@next <cmd>\` (or \`npx @lablup/ui-common-cli@next <cmd>\`); drop \`@next\` once 0.2.0 is published.`,
|
|
103
|
+
`- After bumping @lablup/ui-common and @lablup/ui-common-cli: \`${invocation} upgrade --from <old version>\`, then read ui-common-upgrade-report.md.`,
|
|
104
|
+
);
|
|
105
|
+
return lines;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Turn the Astryx block into the ui-common block. Exported for the tests.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} astryxBlock
|
|
112
|
+
* @param {{version: string, astryxVersion: string, invocation: string, componentCount?: number|null}} ctx
|
|
113
|
+
*/
|
|
114
|
+
export function transformBlock(astryxBlock, ctx) {
|
|
115
|
+
const { invocation, componentCount } = ctx;
|
|
116
|
+
let lines = astryxBlock.split("\n");
|
|
117
|
+
|
|
118
|
+
// Markers.
|
|
119
|
+
lines = lines.filter((l) => l !== ASTRYX_START && l !== ASTRYX_END);
|
|
120
|
+
|
|
121
|
+
// Header.
|
|
122
|
+
lines = lines.map((l) => {
|
|
123
|
+
const header = /^Astryx v(\S+) · (\S+) components$/.exec(l);
|
|
124
|
+
if (!header) return l;
|
|
125
|
+
const count =
|
|
126
|
+
header[2] === "90+" && componentCount ? String(componentCount) : header[2];
|
|
127
|
+
return `@lablup/ui-common v${ctx.version} · Astryx v${header[1]} · ${count} components`;
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// SETUP: ui-common's stylesheet entry replaces Astryx's two imports.
|
|
131
|
+
const setupAt = lines.findIndex((l) => l.startsWith("SETUP"));
|
|
132
|
+
if (setupAt !== -1) {
|
|
133
|
+
let end = setupAt + 1;
|
|
134
|
+
while (end < lines.length && lines[end].trim() !== "") end++;
|
|
135
|
+
lines.splice(
|
|
136
|
+
setupAt,
|
|
137
|
+
end - setupAt,
|
|
138
|
+
"SETUP (once, first in your entry stylesheet) — without these, components render unstyled:",
|
|
139
|
+
` ${LAYER_ORDER}`,
|
|
140
|
+
' @import "@lablup/ui-common/reset.css";',
|
|
141
|
+
' @import "@lablup/ui-common/astryx.css";',
|
|
142
|
+
' @import "@lablup/ui-common/theme/lablup/theme.css";',
|
|
143
|
+
' @import "@lablup/ui-common/ui-common.css";',
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Integration lines ui-common contributes itself are restated below, in
|
|
148
|
+
// ui-common's own section; keep every other integration's.
|
|
149
|
+
lines = lines.filter((l) => !l.startsWith("- `@lablup/ui-common`:"));
|
|
150
|
+
const integrationsAt = lines.indexOf("INTEGRATIONS:");
|
|
151
|
+
if (integrationsAt !== -1 && !(lines[integrationsAt + 1] ?? "").startsWith("- ")) {
|
|
152
|
+
lines.splice(integrationsAt, 1);
|
|
153
|
+
if (lines[integrationsAt - 1] === "") lines.splice(integrationsAt - 1, 1);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
lines = lines.map((l) =>
|
|
157
|
+
l
|
|
158
|
+
.replace(/\(shown below as `astryx \.\.\.`\)/, "(shown below as `ui-common ...`)")
|
|
159
|
+
.replace(
|
|
160
|
+
/^ {2}upgrade --apply {4}.*$/,
|
|
161
|
+
" upgrade --from <v> run after bumping @lablup/ui-common: ui-common's codemods, then Astryx's",
|
|
162
|
+
),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
let text = rewriteOutput(lines.join("\n"), invocation);
|
|
166
|
+
// `ui-common ...` in the CLI line refers to the bin; bare `astryx` mentions
|
|
167
|
+
// left in prose name the design system and stay.
|
|
168
|
+
text = text.replace(/\n+$/, "");
|
|
169
|
+
|
|
170
|
+
return [MARKER_START, text, "", ...uiCommonSection(ctx), MARKER_END].join("\n");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Generate the block for the project at `cwd`.
|
|
175
|
+
*
|
|
176
|
+
* @param {string} cwd
|
|
177
|
+
*/
|
|
178
|
+
export async function generateBlock(cwd) {
|
|
179
|
+
const projectDir = findProjectDir(cwd) ?? cwd;
|
|
180
|
+
const astryxBlock = await renderAstryxBlock(projectDir);
|
|
181
|
+
return transformBlock(astryxBlock, {
|
|
182
|
+
version: uiCommonPackageJson().version,
|
|
183
|
+
astryxVersion: dependencyVersion("@astryxdesign/core") ?? "unknown",
|
|
184
|
+
invocation: binInvocation(projectDir),
|
|
185
|
+
componentCount: await coreComponentCount(),
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Locate the one UI-COMMON block in `content`.
|
|
191
|
+
*
|
|
192
|
+
* @param {string} content
|
|
193
|
+
* @returns {{start: number, end: number} | null}
|
|
194
|
+
*/
|
|
195
|
+
export function findBlock(content) {
|
|
196
|
+
const start = content.indexOf(MARKER_START);
|
|
197
|
+
if (start === -1) {
|
|
198
|
+
if (content.includes(MARKER_END)) {
|
|
199
|
+
throw new Error(`Malformed agent block: ${MARKER_END} without ${MARKER_START}.`);
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
if (content.indexOf(MARKER_START, start + MARKER_START.length) !== -1) {
|
|
204
|
+
throw new Error(`Malformed agent block: more than one ${MARKER_START}.`);
|
|
205
|
+
}
|
|
206
|
+
const endAt = content.indexOf(MARKER_END, start);
|
|
207
|
+
if (endAt === -1) {
|
|
208
|
+
throw new Error(`Malformed agent block: ${MARKER_START} without ${MARKER_END}.`);
|
|
209
|
+
}
|
|
210
|
+
return { start, end: endAt + MARKER_END.length };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The file's content with `block` in place of its UI-COMMON block, or with
|
|
215
|
+
* `block` appended when it has none. Text outside the markers is untouched.
|
|
216
|
+
*
|
|
217
|
+
* @param {string|null} content null when the file does not exist
|
|
218
|
+
* @param {string} block
|
|
219
|
+
*/
|
|
220
|
+
export function injectBlock(content, block) {
|
|
221
|
+
if (content == null || content === "") return `${block}\n`;
|
|
222
|
+
const found = findBlock(content);
|
|
223
|
+
if (found) return content.slice(0, found.start) + block + content.slice(found.end);
|
|
224
|
+
return `${content.replace(/\s+$/, "")}\n\n${block}\n`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* @param {string[]} argv arguments after `agents`
|
|
229
|
+
* @returns {Promise<number>}
|
|
230
|
+
*/
|
|
231
|
+
export async function agentsCommand(argv) {
|
|
232
|
+
/** @type {string|undefined} */
|
|
233
|
+
let write;
|
|
234
|
+
let check = false;
|
|
235
|
+
for (let i = 0; i < argv.length; i++) {
|
|
236
|
+
const arg = argv[i];
|
|
237
|
+
if (arg === "--write") {
|
|
238
|
+
write = argv[++i];
|
|
239
|
+
if (!write) {
|
|
240
|
+
process.stderr.write("ui-common agents: --write needs a file path\n");
|
|
241
|
+
return 2;
|
|
242
|
+
}
|
|
243
|
+
} else if (arg.startsWith("--write=")) {
|
|
244
|
+
write = arg.slice("--write=".length);
|
|
245
|
+
} else if (arg === "--check") {
|
|
246
|
+
check = true;
|
|
247
|
+
} else if (arg === "-h" || arg === "--help") {
|
|
248
|
+
process.stdout.write(AGENTS_HELP);
|
|
249
|
+
return 0;
|
|
250
|
+
} else {
|
|
251
|
+
process.stderr.write(
|
|
252
|
+
`ui-common agents: unknown argument "${arg}"\n${AGENTS_HELP}`,
|
|
253
|
+
);
|
|
254
|
+
return 2;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const cwd = process.cwd();
|
|
259
|
+
let block;
|
|
260
|
+
try {
|
|
261
|
+
block = await generateBlock(cwd);
|
|
262
|
+
} catch (err) {
|
|
263
|
+
process.stderr.write(`ui-common agents: ${/** @type {Error} */ (err).message}\n`);
|
|
264
|
+
return 1;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (check) {
|
|
268
|
+
const projectDir = findProjectDir(cwd) ?? cwd;
|
|
269
|
+
const candidates = write
|
|
270
|
+
? [resolve(cwd, write)]
|
|
271
|
+
: DEFAULT_AGENT_FILES.map((f) => join(projectDir, f));
|
|
272
|
+
for (const file of candidates) {
|
|
273
|
+
if (!existsSync(file)) continue;
|
|
274
|
+
const content = readFileSync(file, "utf8");
|
|
275
|
+
let found;
|
|
276
|
+
try {
|
|
277
|
+
found = findBlock(content);
|
|
278
|
+
} catch (err) {
|
|
279
|
+
process.stderr.write(
|
|
280
|
+
`ui-common agents: ${file}: ${/** @type {Error} */ (err).message}\n`,
|
|
281
|
+
);
|
|
282
|
+
return 1;
|
|
283
|
+
}
|
|
284
|
+
if (!found) continue;
|
|
285
|
+
if (content.slice(found.start, found.end) === block) {
|
|
286
|
+
process.stdout.write(`ui-common agents: ${file} is up to date.\n`);
|
|
287
|
+
return 0;
|
|
288
|
+
}
|
|
289
|
+
process.stderr.write(
|
|
290
|
+
`ui-common agents: the ui-common block in ${file} is stale. Run \`ui-common agents --write ${write ?? file}\`.\n`,
|
|
291
|
+
);
|
|
292
|
+
return 1;
|
|
293
|
+
}
|
|
294
|
+
process.stderr.write(
|
|
295
|
+
`ui-common agents: no ui-common block found in ${candidates.join(", ")}.\n`,
|
|
296
|
+
);
|
|
297
|
+
return 1;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (write) {
|
|
301
|
+
const file = resolve(cwd, write);
|
|
302
|
+
const before = existsSync(file) ? readFileSync(file, "utf8") : null;
|
|
303
|
+
let after;
|
|
304
|
+
try {
|
|
305
|
+
after = injectBlock(before, block);
|
|
306
|
+
} catch (err) {
|
|
307
|
+
process.stderr.write(
|
|
308
|
+
`ui-common agents: ${file}: ${/** @type {Error} */ (err).message}\n`,
|
|
309
|
+
);
|
|
310
|
+
return 1;
|
|
311
|
+
}
|
|
312
|
+
if (after === before) {
|
|
313
|
+
process.stdout.write(`ui-common agents: ${write} is already up to date.\n`);
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
317
|
+
writeFileSync(file, after);
|
|
318
|
+
process.stdout.write(`ui-common agents: wrote the ui-common block to ${write}.\n`);
|
|
319
|
+
return 0;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
process.stdout.write(`${block}\n`);
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export const AGENTS_HELP = `Usage: ui-common agents [--write <file>] [--check]
|
|
327
|
+
|
|
328
|
+
Print the ui-common agent block: Astryx's \`init --features agents\` block,
|
|
329
|
+
rewritten for @lablup/ui-common, between ${MARKER_START} and ${MARKER_END}.
|
|
330
|
+
|
|
331
|
+
--write <file> Put the block in <file>, replacing an existing ui-common block
|
|
332
|
+
and keeping everything outside the markers. Creates the file.
|
|
333
|
+
--check Exit 1 when the block in <file> (or the first of AGENTS.md,
|
|
334
|
+
CLAUDE.md, .claude/CLAUDE.md that has one) is stale or missing.
|
|
335
|
+
`;
|