uniweb 1.31.5 → 1.31.7
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/package.json +5 -5
- package/partials/agents.md +1 -0
- package/partials/guides/data.md +2 -2
- package/partials/guides/foundation.md +12 -2
- package/src/backend/foundation-bring-along.js +5 -2
- package/src/backend/library-types.js +64 -0
- package/src/backend/unregistered-types.js +56 -0
- package/src/commands/add-library.js +400 -0
- package/src/commands/add.js +26 -9
- package/src/commands/build.js +11 -70
- package/src/commands/clone.js +1 -1
- package/src/commands/doctor.js +18 -0
- package/src/commands/publish.js +1 -1
- package/src/commands/push.js +1 -1
- package/src/commands/register.js +233 -95
- package/src/commands/status.js +101 -3
- package/src/framework-index.json +3 -3
- package/src/index.js +33 -2
- package/src/utils/closed-reader.js +6 -1
- package/src/utils/foundation-freshness.js +89 -0
- package/src/utils/placement.js +36 -12
- package/src/utils/stale-export.js +33 -0
- package/templates/workspace/README.md.hbs +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uniweb",
|
|
3
|
-
"version": "1.31.
|
|
3
|
+
"version": "1.31.7",
|
|
4
4
|
"description": "Create structured Vite + React sites with content/code separation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -41,13 +41,13 @@
|
|
|
41
41
|
"js-yaml": "^4.3.2",
|
|
42
42
|
"prompts": "^2.4.2",
|
|
43
43
|
"tar": "^7.5.21",
|
|
44
|
-
"@uniweb/content-writer": "~1.3.1",
|
|
45
|
-
"@uniweb/schemas": "~1.7.1",
|
|
46
44
|
"@uniweb/core": "~1.6.11",
|
|
47
|
-
"@uniweb/semantic-parser": "~1.13.2"
|
|
45
|
+
"@uniweb/semantic-parser": "~1.13.2",
|
|
46
|
+
"@uniweb/schemas": "~1.7.1",
|
|
47
|
+
"@uniweb/content-writer": "~1.3.1"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@uniweb/build": "~1.20.
|
|
50
|
+
"@uniweb/build": "~1.20.7",
|
|
51
51
|
"@uniweb/content-reader": "~1.5.9",
|
|
52
52
|
"@uniweb/semantic-parser": "~1.13.2"
|
|
53
53
|
},
|
package/partials/agents.md
CHANGED
|
@@ -107,6 +107,7 @@ When a rule in §2 feels like friction, that is the moment you are about to take
|
|
|
107
107
|
| A `Button` or `Card` in `sections/` | `components/`. Everything at the root of `sections/` becomes author vocabulary. | appears in the section picker |
|
|
108
108
|
| Helpers that take `{ content, params }` | Ordinary props. That signature is for section types only. | — |
|
|
109
109
|
| One section type per visual variant; or one with a conditional tree inside | A front desk (§1): one `type:`, a `variant` param, workers in `components/` with ordinary props. | author vocabulary bloats; variants invisible to the editor |
|
|
110
|
+
| Copy a worker, hook or data type into a second foundation | A library both depend on: `uniweb add library libraries/ui --foundation a,b`; types named in full (`@acme/plan`). `uniweb guide foundation` § Libraries. | silent: the copies drift; `doctor` names a type defined twice |
|
|
110
111
|
| Validate, enumerate or warn on content the component doesn't use; treat `content:` as an exact schema | Take what you expect and don't touch the rest (§1). `content:` declares what the type reads. | — |
|
|
111
112
|
| `params: { theme, background, grid, sticky, query }` | Section settings — read from `block`, never declared. | build warns |
|
|
112
113
|
| Write `meta.js` as internal metadata | It is the visual editor's UI and other agents' catalog. Labels for people, defaults for everything, author words for params (`columns`, not `gridCols`). | — |
|
package/partials/guides/data.md
CHANGED
|
@@ -123,7 +123,7 @@ members:
|
|
|
123
123
|
|
|
124
124
|
## The component side
|
|
125
125
|
|
|
126
|
-
**Keys and schemas.** `meta.js data:` declares each `content.data` key with its shape — a **named ref**, an **inline field map**, an **inline rich form** (`{ fields: [...] }`), or `{}` (a key with no schema — an external API's records). Refs resolve on disk at build time: `@/name` (this foundation's `schemas/`), `@std/name` (from `@uniweb/schemas`), `@org/name` (from `@org/schemas
|
|
126
|
+
**Keys and schemas.** `meta.js data:` declares each `content.data` key with its shape — a **named ref**, an **inline field map**, an **inline rich form** (`{ fields: [...] }`), or `{}` (a key with no schema — an external API's records). Refs resolve on disk at build time: `@/name` (this foundation's `schemas/`), `@std/name` (from `@uniweb/schemas`), `@org/name` (from the package that defines it: one of the workspace, a library the foundation bundles, or `@org/schemas`; below). A schema drives the editor, checks data (`validate`), lets a fetch of another name fill the key; it fills no field — a record arrives as it is (`record.status ?? 'published'` is yours). A named schema declares no `default:` (inline maps/forms may). `data: false` declares nothing. Keys a foundation's handlers read go in `main.js data:` and reach every section.
|
|
127
127
|
|
|
128
128
|
```js
|
|
129
129
|
data: {
|
|
@@ -136,7 +136,7 @@ data: {
|
|
|
136
136
|
|
|
137
137
|
`single`/`whole` are refused over a schema whose root is a list (`@std/nav`, `@std/form`). A `whole` key its source cannot answer whole is `null`. An optional `schemas.config.js` routes a scope to a directory: `export default { '@acme': '../shared/acme-schemas' }` — a routed scope wins, has no package fallback, and `@/`/`@uniweb` are never routable. Worked examples: `development/schemas-in-practice.md`.
|
|
138
138
|
|
|
139
|
-
**Any foundation can use a data schema another defines, by its full name** — `data: { members: '@acme/member' }`, `schema: '@acme/member'` in `queries.yml`. No backend is involved, and
|
|
139
|
+
**Any foundation can use a data schema another defines, by its full name** — `data: { members: '@acme/member' }`, `schema: '@acme/member'` in `queries.yml`. No backend is involved, and nothing to configure: a local build, `uniweb dev` included, reads the type from the package of the workspace that defines it (a foundation whose name gives the `@acme` scope, with the type in its own `schemas/`), or from a library the foundation bundles whose package.json gives that scope (`uniweb guide foundation` § Libraries). Two packages defining it stop the build, naming both. A type no such package defines comes from `schemas.config.js` (`'@acme/member': '../elsewhere/member.yml'`, or the scope's directory), which wins over both, or an installed `@acme/schemas` package. A scope is a namespace that keeps names from colliding, not a limit on who uses a type. So a type another foundation defines is named, never copied: a copy of its file is a second definition of the same type, which `doctor` points out where the two foundations' names give one scope (`type-defined-twice`). Registering, which only a site with a backend does, names such a type and sends no definition of it; a type a library defines is registered from the library, first (`uniweb guide foundation` § Libraries). A push also needs one of the site's foundations to declare the type, as a section type's data key or a schema of its own: the backend refuses a query whose type none of them declares, since nothing there would render it. A site can hold another site's records of a type too: `paths: { 'records/acme/member': '../../hub/site/records/acme/member' }` in its `site.yml` reads them as its own, a push adds them by reference rather than as copies once their own site has pushed them, and a pull leaves those files alone. A site's records of it sit in `records/acme/member/`; in `records/member/` they'd be the foundation's own `@/member`.
|
|
140
140
|
|
|
141
141
|
```jsx
|
|
142
142
|
function Article({ content, block }) { // data: { article: { schema: '@std/article', single: true } }
|
|
@@ -161,9 +161,19 @@ export default function Pricing({ content, params }) {
|
|
|
161
161
|
}
|
|
162
162
|
```
|
|
163
163
|
|
|
164
|
-
###
|
|
164
|
+
### Libraries: code several foundations share
|
|
165
165
|
|
|
166
|
-
|
|
166
|
+
A section type is a front desk; a **library** is the back office several foundations share: workers, hooks, logic and data types, with no section type of its own. Each foundation keeps its own section types, layouts and look, and calls on the same library. Take the first of these that holds what differs between two sites: the site's `theme.yml` (colors, fonts), foundation vars (radius, shadow, density), a param and the workers it picks, and only then a second foundation sharing a library with the first. A foundation is the vocabulary a kind of site is made with, so a second one is for another vocabulary, never for a smaller bundle.
|
|
167
|
+
|
|
168
|
+
`uniweb add library libraries/ui --foundation classic,modern` makes it and wires each foundation: `"ui": "file:../../libraries/ui"` in its dependencies, and `@import "ui/styles.css";` in its `styles.css`. The library's own stylesheet holds `@source "./**/*.{js,jsx}";`, as kit's does, so Tailwind reads its classes wherever it is installed; without the import they are in no CSS at all, and the build says which line to add. Run it again with another `--foundation` to wire one more. A library is any local package a foundation depends on: one made by hand, cloned or kept as a git submodule inside the workspace works the same, and `uniweb add library <its folder>` wires it.
|
|
169
|
+
|
|
170
|
+
- Export workers from its `index.js` and import them as from any package: `import { PlanCard } from 'ui'`.
|
|
171
|
+
- A worker takes ordinary props and reads every design decision from foundation vars and theme tokens (`var(--card-radius)`, `bg-card`), never a fixed radius or color. That is what lets one worker look classic in one foundation and modern in another.
|
|
172
|
+
- Import `@uniweb/core` by its bare name only, as kit does: a subpath bundles a second core.
|
|
173
|
+
- A section type the library holds stays each foundation's to offer: a directory in the foundation's `sections/` whose `index.jsx` is `export { default } from 'ui/sections/Banner/index.jsx'`, with a `meta.js` that re-exports the library's the same way. Share one only when both vocabularies offer that type with the same content and params; otherwise each foundation writes its own over the same workers.
|
|
174
|
+
- Data types: `uniweb add library libraries/data --schemas --scope @acme` gives it a `schemas/` folder and its scope (`"uniweb": { "scope": "@acme" }` in its package.json). Foundations name its types in full (`data: { plans: '@acme/plan' }`), with nothing to configure (`uniweb guide data`). On a synced site, `register` in a foundation registers the types it names from the library first, since a foundation is taken only once they are; `push` and `publish` release the foundation through `register`, so they do too, and a change to a type releases each foundation that names it on its site's next push. `uniweb register` in the library registers them alone.
|
|
175
|
+
- Each foundation bundles the library, so a build after a change in the library rebuilds every foundation that uses it, and `uniweb status` run in the library names those foundations and their sites. A registered foundation version keeps the library as it was built, as it keeps kit: a fix reaches a hosted site when that site's foundation releases again, which `uniweb publish` from the site does.
|
|
176
|
+
- Keep a library inside the workspace. Outside it, under npm a build cannot resolve what the library's own code imports (kit); under pnpm a `file:` library is a copy that misses new files until a reinstall, and a `link:` one has the site load a copy of the foundation.
|
|
167
177
|
|
|
168
178
|
### The Front Desk pattern
|
|
169
179
|
|
|
@@ -37,8 +37,11 @@
|
|
|
37
37
|
* posture as `uniweb status` on a 404).
|
|
38
38
|
*
|
|
39
39
|
* "Release" here is literally `uniweb register` run in the foundation directory
|
|
40
|
-
* — same build-if-stale →
|
|
41
|
-
* verb does, so there is exactly one
|
|
40
|
+
* — same build-if-stale → the types its libraries define → schema submit → code
|
|
41
|
+
* upload → digest the standalone verb does, so there is exactly one
|
|
42
|
+
* foundation-release path. A library's types reach the registry this way: a change
|
|
43
|
+
* to one changes the foundation's built schema, which the digest folds in, so the
|
|
44
|
+
* foundation is released, and its register sends them first.
|
|
42
45
|
*
|
|
43
46
|
* ⭐ **`push` runs it too, and for a reason publish's framing does not cover.**
|
|
44
47
|
* [Diego, 2026-08-19] — *"A published site can only reference a registered
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ⭐ THE TYPES A FOUNDATION NAMES THAT A LIBRARY DEFINES ARE REGISTERED WITH IT. A foundation is
|
|
3
|
+
* released only once the data types it names are registered: the registry refuses one that names a
|
|
4
|
+
* Model it doesn't hold. A type a library defines is read by the foundation's build from that
|
|
5
|
+
* library's `schemas/` (`workspaceTypeFiles`), so `register` in the foundation registers it from
|
|
6
|
+
* there first, and `push` and `publish`, which release a site's foundation with `register`, do too
|
|
7
|
+
* *[Diego, 2026-11-10: "yes, push and publish should register a library's types automatically"]*.
|
|
8
|
+
*
|
|
9
|
+
* A library here is what the build read the type from: a schemas package (`isSchemasPackage`, not a
|
|
10
|
+
* foundation) outside the foundation that gives its scope, bundled by the foundation or a package of
|
|
11
|
+
* its workspace. A type another FOUNDATION defines is registered when that foundation is, and a
|
|
12
|
+
* refused release names it (`unregistered-types.js`).
|
|
13
|
+
*
|
|
14
|
+
* ⚖️ Registered whole, as `uniweb register` in the library would: what is unchanged the registry
|
|
15
|
+
* answers as such, and a changed definition replaces the registered one, which every site naming the
|
|
16
|
+
* type then reads. The registry judges the change: one it can't make where records of the type are
|
|
17
|
+
* kept it refuses (`destructive_republish`), and the release stops there.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { existsSync } from 'node:fs'
|
|
21
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
22
|
+
|
|
23
|
+
const within = (dir, path) => {
|
|
24
|
+
const rel = relative(resolve(dir), resolve(path))
|
|
25
|
+
return rel === '' || (!rel.startsWith(`..${sep}`) && rel !== '..' && !isAbsolute(rel))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The package `file` is in: the nearest folder above it with a package.json. */
|
|
29
|
+
function packageDirOf(file) {
|
|
30
|
+
for (let at = dirname(resolve(file)); ; at = dirname(at)) {
|
|
31
|
+
if (existsSync(join(at, 'package.json'))) return at
|
|
32
|
+
if (dirname(at) === at) return null
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The libraries that define types the foundation names, each once, with the types it names from
|
|
38
|
+
* each. A type its own `schemas/` defines (`@/x`), one nothing local defines, and one two packages
|
|
39
|
+
* define (which the build refused) are left out.
|
|
40
|
+
*
|
|
41
|
+
* @param {object} o
|
|
42
|
+
* @param {string[]} o.named - every type the foundation names (its schema's `dataSchemas`)
|
|
43
|
+
* @param {string} o.foundationDir
|
|
44
|
+
* @returns {Promise<Array<{ dir: string, scope: string, refs: string[] }>>} in the order named
|
|
45
|
+
*/
|
|
46
|
+
export async function typesFromLibraries({ named, foundationDir }) {
|
|
47
|
+
const { workspaceTypeFiles, packageTypeScope, classifyPackage, isSchemasPackage } = await import('@uniweb/build')
|
|
48
|
+
if (typeof workspaceTypeFiles !== 'function' || typeof packageTypeScope !== 'function') return []
|
|
49
|
+
const byDir = new Map()
|
|
50
|
+
for (const ref of named) {
|
|
51
|
+
// `@uniweb` is reserved, and nothing registers under it.
|
|
52
|
+
if (!/^@[^/]+\/./.test(ref) || ref.startsWith('@uniweb/')) continue
|
|
53
|
+
const files = await workspaceTypeFiles(ref, foundationDir).catch(() => [])
|
|
54
|
+
if (files.length !== 1) continue
|
|
55
|
+
const dir = packageDirOf(files[0])
|
|
56
|
+
if (!dir || within(foundationDir, dir)) continue
|
|
57
|
+
if (classifyPackage(dir) === 'foundation' || !isSchemasPackage(dir)) continue
|
|
58
|
+
const scope = await packageTypeScope(dir, 'schemas')
|
|
59
|
+
if (!scope) continue
|
|
60
|
+
if (!byDir.has(dir)) byDir.set(dir, { dir, scope, refs: [] })
|
|
61
|
+
byDir.get(dir).refs.push(ref)
|
|
62
|
+
}
|
|
63
|
+
return [...byDir.values()]
|
|
64
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ⭐ THE TYPES A FOUNDATION NAMES THAT ANOTHER LOCAL PACKAGE DEFINES, AND THE REGISTRY DOESN'T HOLD.
|
|
3
|
+
* A foundation is released only once the data types it names are registered: the registry refuses
|
|
4
|
+
* one that depends on a Model it doesn't have. Measured 2026-11-10 on a local backend: a site's push
|
|
5
|
+
* released its foundation, which names `@acme/libplan` from a library it bundles, and the registry
|
|
6
|
+
* answered 400, "no such data-schema is registered on this deployment and this package does not
|
|
7
|
+
* publish one"; after `uniweb register` in the library, the push went through.
|
|
8
|
+
*
|
|
9
|
+
* A library's types `register` now sends before the foundation (`library-types.js`), so what this
|
|
10
|
+
* names after a refusal is a type ANOTHER FOUNDATION of the workspace defines: that foundation
|
|
11
|
+
* registers it, with `uniweb register` there.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { existsSync } from 'node:fs'
|
|
15
|
+
import { dirname, join, relative, resolve, sep, isAbsolute } from 'node:path'
|
|
16
|
+
|
|
17
|
+
const within = (dir, path) => {
|
|
18
|
+
const rel = relative(resolve(dir), resolve(path))
|
|
19
|
+
return rel === '' || (!rel.startsWith(`..${sep}`) && rel !== '..' && !isAbsolute(rel))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The package `file` is in: the nearest folder above it with a package.json. */
|
|
23
|
+
function packageDirOf(file) {
|
|
24
|
+
for (let at = dirname(resolve(file)); ; at = dirname(at)) {
|
|
25
|
+
if (existsSync(join(at, 'package.json'))) return at
|
|
26
|
+
if (dirname(at) === at) return null
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Each type `named` that the foundation does not define, that one other local package does, and that
|
|
32
|
+
* the registry answers it does not hold (`readDataSchema` → null). A type the registry could not be
|
|
33
|
+
* asked about is left out: this explains a refusal, and says nothing it could not check.
|
|
34
|
+
*
|
|
35
|
+
* @param {object} o
|
|
36
|
+
* @param {string[]} o.named - every type the foundation names (its schema's `dataSchemas`)
|
|
37
|
+
* @param {string[]} o.defined - the ones it defines, which its register sends
|
|
38
|
+
* @param {string} o.foundationDir
|
|
39
|
+
* @param {{ readDataSchema(name: string): Promise<object|null> }} o.client
|
|
40
|
+
* @returns {Promise<Array<{ ref: string, dir: string }>>} each type and the folder of the package that defines it
|
|
41
|
+
*/
|
|
42
|
+
export async function unregisteredLocalTypes({ named, defined, foundationDir, client }) {
|
|
43
|
+
const { workspaceTypeFiles } = await import('@uniweb/build')
|
|
44
|
+
if (typeof workspaceTypeFiles !== 'function') return []
|
|
45
|
+
const out = []
|
|
46
|
+
for (const ref of named) {
|
|
47
|
+
if (defined.includes(ref) || /^@(?:std|uniweb)\//.test(ref)) continue
|
|
48
|
+
const files = await workspaceTypeFiles(ref, foundationDir).catch(() => [])
|
|
49
|
+
if (files.length !== 1) continue
|
|
50
|
+
const dir = packageDirOf(files[0])
|
|
51
|
+
if (!dir || within(foundationDir, dir)) continue
|
|
52
|
+
const held = await client.readDataSchema(ref).catch(() => undefined)
|
|
53
|
+
if (held === null) out.push({ ref, dir })
|
|
54
|
+
}
|
|
55
|
+
return out
|
|
56
|
+
}
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `uniweb add library <name> [--foundation a,b] [--schemas] [--scope @org]`
|
|
3
|
+
*
|
|
4
|
+
* ⭐ A LIBRARY IS ANY LOCAL PACKAGE A FOUNDATION DEPENDS ON (`librariesOf`, `@uniweb/build`): the
|
|
5
|
+
* components, logic and data types several foundations share, each keeping its own section types,
|
|
6
|
+
* layouts and look. This makes one in the workspace and wires the foundations named, which is all a
|
|
7
|
+
* library takes: a `file:` dependency in each foundation, and its stylesheet imported in each
|
|
8
|
+
* foundation's, so that Tailwind reads its classes. A foundation's build bundles it, its freshness
|
|
9
|
+
* counts its files, and `uniweb dev` serves it, because each finds it through that dependency.
|
|
10
|
+
*
|
|
11
|
+
* Placed by the rule every `add` follows (`utils/placement.js`): a bare name is a folder at the
|
|
12
|
+
* workspace root, a path is that folder, and `--path` is the folder it goes in. The package is named
|
|
13
|
+
* for its folder, as a foundation is, and nothing else marks it: so run on a folder that holds a
|
|
14
|
+
* package already, made by hand, cloned or kept as a git submodule, this wires it without making
|
|
15
|
+
* anything.
|
|
16
|
+
*
|
|
17
|
+
* `--schemas` gives it a `schemas/` folder, whose files are data types its foundations name in full
|
|
18
|
+
* (`@acme/plan`), under the scope in its `package.json` (`uniweb.scope`): `--scope`, or the one the
|
|
19
|
+
* foundations it is wired into share.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
23
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
24
|
+
import { join, relative, sep } from 'node:path'
|
|
25
|
+
import prompts from 'prompts'
|
|
26
|
+
import { checkRegistrationScope, classifyPackage, readFoundationName, resolveFoundationSrcPath, splitFoundationName } from '@uniweb/build'
|
|
27
|
+
import { resolvePlacement, LIBRARY_KIND } from '../utils/placement.js'
|
|
28
|
+
import { registerWorkspacePackage } from '../utils/config.js'
|
|
29
|
+
import { discoverFoundations } from '../utils/discover.js'
|
|
30
|
+
import { validatePackageName } from '../utils/names.js'
|
|
31
|
+
import { workspacePackages } from '../utils/workspace-packages.js'
|
|
32
|
+
import { writeJsonPreservingStyleAsync } from '../utils/json-file.js'
|
|
33
|
+
import { installCmd } from '../utils/pm.js'
|
|
34
|
+
import { getCliPrefix, isNonInteractive } from '../utils/interactive.js'
|
|
35
|
+
import { getVersionsForTemplates } from '../versions.js'
|
|
36
|
+
|
|
37
|
+
const colors = {
|
|
38
|
+
reset: '\x1b[0m',
|
|
39
|
+
bright: '\x1b[1m',
|
|
40
|
+
dim: '\x1b[2m',
|
|
41
|
+
cyan: '\x1b[36m',
|
|
42
|
+
green: '\x1b[32m',
|
|
43
|
+
yellow: '\x1b[33m',
|
|
44
|
+
red: '\x1b[31m'
|
|
45
|
+
}
|
|
46
|
+
const log = (message) => console.log(message)
|
|
47
|
+
const info = (message) => log(`${colors.dim}${message}${colors.reset}`)
|
|
48
|
+
const fail = (message, ...lines) => {
|
|
49
|
+
console.error(`${colors.red}✗${colors.reset} ${message}`)
|
|
50
|
+
for (const line of lines) log(line)
|
|
51
|
+
process.exit(1)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const posix = (path) => path.split(sep).join('/')
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A new library's `package.json`. Private, since foundations bundle it and npm never needs it; React
|
|
58
|
+
* and kit as peers, since the foundation that bundles it provides them.
|
|
59
|
+
*
|
|
60
|
+
* @param {{ name: string, projectName: string, scope?: string|null, versions?: Record<string,string> }} o
|
|
61
|
+
*/
|
|
62
|
+
export function libraryManifest({ name, projectName, scope = null, versions = getVersionsForTemplates() }) {
|
|
63
|
+
return {
|
|
64
|
+
name,
|
|
65
|
+
version: '0.1.0',
|
|
66
|
+
private: true,
|
|
67
|
+
description: `Components, logic and data types the foundations of ${projectName} share`,
|
|
68
|
+
type: 'module',
|
|
69
|
+
main: './index.js',
|
|
70
|
+
exports: {
|
|
71
|
+
'.': './index.js',
|
|
72
|
+
'./styles.css': './styles.css',
|
|
73
|
+
'./components/*': './components/*',
|
|
74
|
+
'./sections/*': './sections/*'
|
|
75
|
+
},
|
|
76
|
+
peerDependencies: {
|
|
77
|
+
'@uniweb/kit': versions['@uniweb/kit'],
|
|
78
|
+
react: versions.react
|
|
79
|
+
},
|
|
80
|
+
...(scope && { uniweb: { scope } })
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The two example lines are aligned on the longer, which the library's name decides.
|
|
85
|
+
const INDEX = (name) => {
|
|
86
|
+
const here = `export { default as PlanCard } from './components/PlanCard.jsx'`
|
|
87
|
+
const there = `import { PlanCard } from '${name}'`
|
|
88
|
+
const width = Math.max(here.length, there.length) + 3
|
|
89
|
+
return `// What this library shares with the foundations that depend on it. Export each component, hook
|
|
90
|
+
// or helper here, and a foundation imports it by the library's name:
|
|
91
|
+
//
|
|
92
|
+
// ${here.padEnd(width)}(here)
|
|
93
|
+
// ${there.padEnd(width)}(in a foundation)
|
|
94
|
+
//
|
|
95
|
+
// A component here takes ordinary props, and reads its look from the foundation's vars and the
|
|
96
|
+
// theme's tokens (var(--card-radius), bg-card), so each foundation that uses it keeps its own.
|
|
97
|
+
export {}
|
|
98
|
+
`
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The form kit's own stylesheet uses, which holds wherever a package manager puts the package.
|
|
102
|
+
const STYLES = `/* Tailwind reads this library's files wherever it is installed: each foundation that uses it imports this stylesheet. */
|
|
103
|
+
@source "./**/*.{js,jsx}";
|
|
104
|
+
`
|
|
105
|
+
|
|
106
|
+
const SCHEMAS_README = (scope) => `# Data types
|
|
107
|
+
|
|
108
|
+
Each file here is a data type the foundations share, by its full name: \`plan.yml\` is
|
|
109
|
+
\`${scope}/plan\`. A foundation names it in a section's \`meta.js\` (\`data: { plans: '${scope}/plan' }\`),
|
|
110
|
+
and a site keeps its records in \`records/${scope.slice(1)}/plan/\`. In this workspace nothing else is
|
|
111
|
+
configured: a package with a \`schemas/\` folder serves its scope.
|
|
112
|
+
`
|
|
113
|
+
|
|
114
|
+
const USES_TAILWIND = /@import\s+["']tailwindcss/
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The line that has a foundation's Tailwind read a library: an import of the library's own stylesheet
|
|
118
|
+
* when it has one a foundation can import, else a `@source` to its folder.
|
|
119
|
+
*
|
|
120
|
+
* @param {{ libraryDir: string, libraryName: string, stylesDir: string }} o
|
|
121
|
+
* @returns {string}
|
|
122
|
+
*/
|
|
123
|
+
export function libraryLine({ libraryDir, libraryName, stylesDir }) {
|
|
124
|
+
let pkg = null
|
|
125
|
+
try {
|
|
126
|
+
pkg = JSON.parse(readFileSync(join(libraryDir, 'package.json'), 'utf8'))
|
|
127
|
+
} catch {
|
|
128
|
+
// no manifest to refuse the import
|
|
129
|
+
}
|
|
130
|
+
const exp = pkg?.exports
|
|
131
|
+
// ⛔ An `exports` map without the stylesheet refuses the import, in Tailwind and in Node alike.
|
|
132
|
+
const importable = !exp || typeof exp !== 'object' || Object.keys(exp).some((k) => k === './styles.css' || k === './*')
|
|
133
|
+
if (existsSync(join(libraryDir, 'styles.css')) && importable) return `@import "${libraryName}/styles.css";`
|
|
134
|
+
const rel = posix(relative(stylesDir, libraryDir))
|
|
135
|
+
return `@source "${rel.startsWith('.') ? rel : `./${rel}`}";`
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A foundation's stylesheet with `line` after its last `@import`, or null when the stylesheet reads
|
|
140
|
+
* the library already: by that import, or by a `@source` to its folder.
|
|
141
|
+
*
|
|
142
|
+
* @param {string} css
|
|
143
|
+
* @param {string} line - from `libraryLine`
|
|
144
|
+
* @returns {string|null}
|
|
145
|
+
*/
|
|
146
|
+
export function withLibraryLine(css, line) {
|
|
147
|
+
const target = /^@(?:import|source) "(.+?)"/.exec(line)[1].replace(/\/+$/, '')
|
|
148
|
+
for (const [, , written] of css.matchAll(/@(import|source)\s+["'](.+?)["']/g)) {
|
|
149
|
+
if (written.replace(/\/+$/, '') === target) return null
|
|
150
|
+
}
|
|
151
|
+
const lines = css.split('\n')
|
|
152
|
+
let last = -1
|
|
153
|
+
lines.forEach((l, i) => {
|
|
154
|
+
if (/^\s*@import\b/.test(l)) last = i
|
|
155
|
+
})
|
|
156
|
+
lines.splice(last + 1, 0, line)
|
|
157
|
+
return lines.join('\n')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The name each workspace package goes by, with its folder. */
|
|
161
|
+
function workspacePackageNames(rootDir) {
|
|
162
|
+
const names = new Map()
|
|
163
|
+
for (const rel of workspacePackages(rootDir)) {
|
|
164
|
+
try {
|
|
165
|
+
const { name } = JSON.parse(readFileSync(join(rootDir, rel, 'package.json'), 'utf8'))
|
|
166
|
+
if (name) names.set(name, rel)
|
|
167
|
+
} catch {
|
|
168
|
+
// no manifest: no name to collide with
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return names
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The foundations `--foundation` names, or the one the workspace has, or the ones a person picks. */
|
|
175
|
+
async function foundationsToWire(rootDir, flag, relativePath) {
|
|
176
|
+
const foundations = await discoverFoundations(rootDir)
|
|
177
|
+
if (flag) {
|
|
178
|
+
const wanted = flag.split(',').map((s) => s.trim()).filter(Boolean)
|
|
179
|
+
const missing = wanted.filter((n) => !foundations.some((f) => f.name === n))
|
|
180
|
+
if (missing.length) {
|
|
181
|
+
fail(
|
|
182
|
+
`No foundation named ${missing.map((n) => `'${n}'`).join(', ')} in this workspace.`,
|
|
183
|
+
`Foundations: ${foundations.map((f) => f.name).join(', ') || 'none'}`
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
return { wire: wanted.map((n) => foundations.find((f) => f.name === n)) }
|
|
187
|
+
}
|
|
188
|
+
if (foundations.length === 0) return { wire: [], note: 'No foundation in this workspace yet. Wire one with --foundation once you add it.' }
|
|
189
|
+
if (foundations.length === 1) return { wire: foundations }
|
|
190
|
+
if (isNonInteractive(process.argv)) {
|
|
191
|
+
return {
|
|
192
|
+
wire: [],
|
|
193
|
+
note: `Several foundations in this workspace, so none was wired. Name them: ${getCliPrefix()} add library ${relativePath} --foundation ${foundations
|
|
194
|
+
.slice(0, 2)
|
|
195
|
+
.map((f) => f.name)
|
|
196
|
+
.join(',')}`
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const { picked } = await prompts(
|
|
200
|
+
{
|
|
201
|
+
type: 'multiselect',
|
|
202
|
+
name: 'picked',
|
|
203
|
+
message: 'Which foundations use this library?',
|
|
204
|
+
choices: foundations.map((f) => ({ title: f.name, description: f.path, value: f.name })),
|
|
205
|
+
min: 0
|
|
206
|
+
},
|
|
207
|
+
{ onCancel: () => process.exit(0) }
|
|
208
|
+
)
|
|
209
|
+
return { wire: (picked || []).map((n) => foundations.find((f) => f.name === n)) }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The scope the foundations share, from their names, or null. */
|
|
213
|
+
async function sharedScope(rootDir, foundations) {
|
|
214
|
+
const scopes = new Set()
|
|
215
|
+
for (const f of foundations) {
|
|
216
|
+
try {
|
|
217
|
+
scopes.add(splitFoundationName((await readFoundationName(join(rootDir, f.path))).name).scope)
|
|
218
|
+
} catch {
|
|
219
|
+
scopes.add(null)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return scopes.size === 1 ? [...scopes][0] : null
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Wire one foundation to the library: its dependency, and its stylesheet's line for the library's
|
|
227
|
+
* classes. Each step is skipped when it is done already, so wiring twice changes nothing.
|
|
228
|
+
*
|
|
229
|
+
* @returns {Promise<{ changed: boolean, notes: string[] }>} whether a file changed, and what was left
|
|
230
|
+
* as it was, and why
|
|
231
|
+
*/
|
|
232
|
+
export async function wireFoundation({ foundationDir, libraryDir, libraryName }) {
|
|
233
|
+
const notes = []
|
|
234
|
+
let changed = false
|
|
235
|
+
const pkgPath = join(foundationDir, 'package.json')
|
|
236
|
+
const src = await readFile(pkgPath, 'utf8')
|
|
237
|
+
const pkg = JSON.parse(src)
|
|
238
|
+
const spec = `file:${posix(relative(foundationDir, libraryDir))}`
|
|
239
|
+
const has = pkg.dependencies?.[libraryName] ?? pkg.devDependencies?.[libraryName]
|
|
240
|
+
if (has && has !== spec) {
|
|
241
|
+
notes.push(`its package.json depends on ${libraryName} as "${has}" already, so that was left as it is`)
|
|
242
|
+
} else if (!has) {
|
|
243
|
+
const deps = { ...(pkg.dependencies || {}), [libraryName]: spec }
|
|
244
|
+
pkg.dependencies = Object.fromEntries(Object.entries(deps).sort(([a], [b]) => a.localeCompare(b)))
|
|
245
|
+
await writeJsonPreservingStyleAsync(pkgPath, pkg, src)
|
|
246
|
+
changed = true
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const stylesDir = resolveFoundationSrcPath(foundationDir)
|
|
250
|
+
const stylesPath = join(stylesDir, 'styles.css')
|
|
251
|
+
if (!existsSync(stylesPath)) {
|
|
252
|
+
notes.push(`it has no styles.css, so nothing reads the library's classes: add ${libraryLine({ libraryDir, libraryName, stylesDir })} to its stylesheet`)
|
|
253
|
+
return { changed, notes }
|
|
254
|
+
}
|
|
255
|
+
const css = await readFile(stylesPath, 'utf8')
|
|
256
|
+
if (!USES_TAILWIND.test(css)) {
|
|
257
|
+
notes.push('its styles.css does not import Tailwind, so it reads no classes from the library')
|
|
258
|
+
return { changed, notes }
|
|
259
|
+
}
|
|
260
|
+
const wired = withLibraryLine(css, libraryLine({ libraryDir, libraryName, stylesDir }))
|
|
261
|
+
if (wired !== null) {
|
|
262
|
+
await writeFile(stylesPath, wired)
|
|
263
|
+
changed = true
|
|
264
|
+
}
|
|
265
|
+
return { changed, notes }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Add a library to the workspace, or wire one that is there.
|
|
270
|
+
*/
|
|
271
|
+
export async function addLibrary(rootDir, projectName, opts, pm = 'pnpm') {
|
|
272
|
+
const prefix = getCliPrefix()
|
|
273
|
+
if (opts.project) {
|
|
274
|
+
fail(
|
|
275
|
+
`A library is shared by foundations, so it goes in the folder you name, not in one project's:`,
|
|
276
|
+
` ${prefix} add library libraries/<name> --foundation <a>,<b>`
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
let name = opts.name
|
|
280
|
+
if (!name && !opts.path) {
|
|
281
|
+
if (isNonInteractive(process.argv)) fail('Missing library name.', '', `Usage: ${prefix} add library <name> [--foundation <a>,<b>]`)
|
|
282
|
+
const response = await prompts(
|
|
283
|
+
{ type: 'text', name: 'name', message: 'Library name:', validate: (v) => validatePackageName(v) },
|
|
284
|
+
{ onCancel: () => process.exit(0) }
|
|
285
|
+
)
|
|
286
|
+
name = response.name
|
|
287
|
+
}
|
|
288
|
+
if (name?.startsWith('@')) {
|
|
289
|
+
fail(
|
|
290
|
+
`A library is named for its folder, as a foundation is: name the folder, not a scoped package.`,
|
|
291
|
+
` ${prefix} add library ${name.split('/').pop()}`,
|
|
292
|
+
`To give its package a scoped name, change "name" in its package.json, and the import that names it.`
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const placement = resolvePlacement(rootDir, name, opts, LIBRARY_KIND)
|
|
297
|
+
if (placement.outsideRoot) {
|
|
298
|
+
fail(
|
|
299
|
+
`A library's folder must be inside the workspace: ${placement.outsideRoot}`,
|
|
300
|
+
'',
|
|
301
|
+
`A library from another repository goes inside the workspace, as a git submodule or a clone,`,
|
|
302
|
+
`and \`${prefix} add library <its folder>\` lists it in the workspace and wires it.`
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
const { relativePath } = placement
|
|
306
|
+
const libraryDir = join(rootDir, relativePath)
|
|
307
|
+
|
|
308
|
+
let libraryName = placement.packageName
|
|
309
|
+
let created = false
|
|
310
|
+
let scope = null
|
|
311
|
+
if (existsSync(libraryDir)) {
|
|
312
|
+
const kind = classifyPackage(libraryDir)
|
|
313
|
+
if (kind) fail(`${relativePath}/ is a ${kind}, not a library.`)
|
|
314
|
+
if (!existsSync(join(libraryDir, 'package.json'))) {
|
|
315
|
+
fail(`${relativePath}/ exists and holds no package.json, so it is no package to wire.`, `Pick another folder, or give it a package.json.`)
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
libraryName = JSON.parse(readFileSync(join(libraryDir, 'package.json'), 'utf8')).name || libraryName
|
|
319
|
+
} catch {
|
|
320
|
+
fail(`${relativePath}/package.json does not parse.`)
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
const valid = validatePackageName(libraryName)
|
|
324
|
+
if (valid !== true) fail(valid)
|
|
325
|
+
const taken = workspacePackageNames(rootDir).get(libraryName)
|
|
326
|
+
if (taken) {
|
|
327
|
+
fail(
|
|
328
|
+
`A package named ${colors.bright}${libraryName}${colors.reset} is in this workspace already, at ${taken}/.`,
|
|
329
|
+
`A foundation imports a library by its name, so name this one for another folder:`,
|
|
330
|
+
` ${prefix} add library <name>`
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const { wire, note } = await foundationsToWire(rootDir, opts.foundation, relativePath)
|
|
336
|
+
|
|
337
|
+
if (opts.schemas || opts.scope) {
|
|
338
|
+
scope = opts.scope ? `@${String(opts.scope).trim().replace(/^@/, '').replace(/\/.*$/, '')}` : await sharedScope(rootDir, wire)
|
|
339
|
+
if (!scope || scope === '@') {
|
|
340
|
+
fail(
|
|
341
|
+
`A library's data types are named in full, under its scope (${colors.bright}@acme/plan${colors.reset}), and none is known yet.`,
|
|
342
|
+
`Give it one:`,
|
|
343
|
+
` ${prefix} add library ${relativePath} --schemas --scope @<org>`
|
|
344
|
+
)
|
|
345
|
+
}
|
|
346
|
+
const reserved = checkRegistrationScope(scope)
|
|
347
|
+
if (reserved) fail(reserved)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (!existsSync(libraryDir)) {
|
|
351
|
+
await mkdir(join(libraryDir, 'components'), { recursive: true })
|
|
352
|
+
const manifest = libraryManifest({ name: libraryName, projectName, scope })
|
|
353
|
+
await writeFile(join(libraryDir, 'package.json'), `${JSON.stringify(manifest, null, 2)}\n`)
|
|
354
|
+
await writeFile(join(libraryDir, 'index.js'), INDEX(libraryName))
|
|
355
|
+
await writeFile(join(libraryDir, 'styles.css'), STYLES)
|
|
356
|
+
created = true
|
|
357
|
+
} else if (scope) {
|
|
358
|
+
const pkgPath = join(libraryDir, 'package.json')
|
|
359
|
+
const src = await readFile(pkgPath, 'utf8')
|
|
360
|
+
const pkg = JSON.parse(src)
|
|
361
|
+
if (pkg.uniweb?.scope && pkg.uniweb.scope !== scope) {
|
|
362
|
+
fail(`${relativePath}/package.json gives the scope ${pkg.uniweb.scope} already; its types are named under it.`)
|
|
363
|
+
}
|
|
364
|
+
if (!pkg.uniweb?.scope) await writeJsonPreservingStyleAsync(pkgPath, { ...pkg, uniweb: { ...(pkg.uniweb || {}), scope } }, src)
|
|
365
|
+
}
|
|
366
|
+
if (scope && !existsSync(join(libraryDir, 'schemas'))) {
|
|
367
|
+
await mkdir(join(libraryDir, 'schemas'), { recursive: true })
|
|
368
|
+
await writeFile(join(libraryDir, 'schemas', 'README.md'), SCHEMAS_README(scope))
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Listed by its path, unless a pattern the workspace lists covers it already.
|
|
372
|
+
await registerWorkspacePackage(rootDir, relativePath)
|
|
373
|
+
|
|
374
|
+
const wired = []
|
|
375
|
+
const already = []
|
|
376
|
+
const notes = []
|
|
377
|
+
for (const f of wire) {
|
|
378
|
+
const { changed, notes: left } = await wireFoundation({ foundationDir: join(rootDir, f.path), libraryDir, libraryName })
|
|
379
|
+
;(changed ? wired : already).push(f.name)
|
|
380
|
+
for (const n of left) notes.push(`${f.name}: ${n}`)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (created || wired.length) {
|
|
384
|
+
log(
|
|
385
|
+
`${colors.green}✓${colors.reset} ${created ? 'Created' : 'Wired'} library ${colors.bright}${libraryName}${colors.reset}` +
|
|
386
|
+
`${created ? ` at ${relativePath}/` : ''}${wired.length ? ` → ${wired.join(', ')}` : ''}`
|
|
387
|
+
)
|
|
388
|
+
} else if (!already.length) {
|
|
389
|
+
log(`Library ${colors.bright}${libraryName}${colors.reset} at ${relativePath}/: no foundation to wire.`)
|
|
390
|
+
}
|
|
391
|
+
if (already.length) log(` ${colors.dim}${already.join(', ')}: wired to ${libraryName} already${colors.reset}`)
|
|
392
|
+
for (const n of notes) log(` ${colors.yellow}⚠ ${n}${colors.reset}`)
|
|
393
|
+
if (note) log(` ${colors.yellow}⚠ ${note}${colors.reset}`)
|
|
394
|
+
if (created) {
|
|
395
|
+
info(` Export its components from ${relativePath}/index.js, and import them in a foundation: import { PlanCard } from '${libraryName}'`)
|
|
396
|
+
if (scope) info(` Its data types go in ${relativePath}/schemas/, named ${scope}/<file name>.`)
|
|
397
|
+
}
|
|
398
|
+
log('')
|
|
399
|
+
log(`Next: ${colors.cyan}${installCmd(pm)}${colors.reset}`)
|
|
400
|
+
}
|