@hollis-labs/surface-ui 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/ui.mjs +33 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hollis-labs/surface-ui",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Keep an app's copied components identical to the Surface registry: init, add, sync, verify, status and a drift report across apps.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/ui.mjs CHANGED
@@ -13,6 +13,7 @@ import { execFileSync } from "node:child_process"
13
13
  // write ui.lock.json naming the registry on GitHub at a tag
14
14
  // node scripts/ui.mjs build registry maintainers: regenerate registry.json and registry/r
15
15
  // node scripts/ui.mjs add <item...> install items (and what they depend on) into the app
16
+ // node scripts/ui.mjs add --all install every item the registry has
16
17
  // node scripts/ui.mjs add --for <package>
17
18
  // install the items an installed package draws with
18
19
  // (@hollis-labs/surface-page lists its own)
@@ -56,6 +57,14 @@ const fail = (msg) => {
56
57
  process.exit(1)
57
58
  }
58
59
  const bare = (name) => name.replace(/^@surface\//, "")
60
+ // How this tool was started, for the commands its messages suggest.
61
+ const SELF = /scripts[\\/]ui\.mjs$/.test(process.argv[1] ?? "") ? "node scripts/ui.mjs" : "npx surface-ui"
62
+ const wantAll = (() => {
63
+ const i = args.indexOf("--all")
64
+ if (i < 0) return false
65
+ args.splice(i, 1)
66
+ return true
67
+ })()
59
68
 
60
69
  function readLock() {
61
70
  if (!existsSync(lockPath)) {
@@ -137,7 +146,9 @@ async function install(lock, names) {
137
146
  const have = { ...pkg.dependencies, ...pkg.devDependencies }
138
147
  const missing = [...npm].filter((d) => !have[d.replace(/(?<=.)@[^/]+$/, "")])
139
148
  if (missing.length)
140
- console.log(`these packages are needed and not in package.json:\n npm install ${missing.join(" ")}`)
149
+ console.log(
150
+ `these packages are needed and not in package.json (at the versions this registry was built with):\n npm install --save-exact ${missing.join(" ")}`,
151
+ )
141
152
  }
142
153
  }
143
154
 
@@ -152,11 +163,12 @@ async function verify(lock) {
152
163
  const abs = join(appDir, rel)
153
164
  if (!existsSync(abs)) problems.push(`${rel}: missing (item ${name})`)
154
165
  else if (sha(readFileSync(abs, "utf8")) !== hash)
155
- problems.push(`${rel}: edited in place. Change it in the registry, then run "ui sync"`)
156
- else if (want.get(rel) !== hash) problems.push(`${rel}: behind the registry (item ${name}). Run "ui sync"`)
166
+ problems.push(`${rel}: edited in place. Change it in the registry, then run "${SELF} sync"`)
167
+ else if (want.get(rel) !== hash) problems.push(`${rel}: behind the registry (item ${name}). Run "${SELF} sync"`)
157
168
  }
158
169
  for (const rel of want.keys())
159
- if (!entry.files[rel]) problems.push(`${rel}: the registry's ${name} has a file the app lacks. Run "ui sync"`)
170
+ if (!entry.files[rel])
171
+ problems.push(`${rel}: the registry's ${name} has a file the app lacks. Run "${SELF} sync"`)
160
172
  }
161
173
  // Nothing lives beside the copied components that the registry did not put there.
162
174
  const dir = join(appDir, lock.uiDir)
@@ -262,6 +274,14 @@ function build() {
262
274
  const root = resolve(appDir, "registry")
263
275
  const uiDir = join(root, "ui")
264
276
  const meta = existsSync(join(root, "items.json")) ? JSON.parse(readFileSync(join(root, "items.json"), "utf8")) : {}
277
+ // Every package an item imports is named at the version this repository has installed, so
278
+ // an app outside it installs the same one instead of guessing.
279
+ const lockFile = join(appDir, "package-lock.json")
280
+ const locked = existsSync(lockFile) ? (JSON.parse(readFileSync(lockFile, "utf8")).packages ?? {}) : {}
281
+ const versioned = (pkg, pin) => {
282
+ const version = pin ?? locked[`node_modules/${pkg}`]?.version
283
+ return version ? `${pkg}@${version}` : pkg
284
+ }
265
285
  const items = []
266
286
  for (const f of readdirSync(uiDir)
267
287
  .filter((x) => x.endsWith(".tsx"))
@@ -277,7 +297,7 @@ function build() {
277
297
  else if (!spec.startsWith(".") && !spec.startsWith("@/") && spec !== "react" && spec !== "react-dom") {
278
298
  const pkg = spec.startsWith("@") ? spec.split("/").slice(0, 2).join("/") : spec.split("/")[0]
279
299
  // A package the item needs at one exact version says so in items.json ("pins").
280
- npm.add(meta[name]?.pins?.[pkg] ? `${pkg}@${meta[name].pins[pkg]}` : pkg)
300
+ npm.add(versioned(pkg, meta[name]?.pins?.[pkg]))
281
301
  }
282
302
  }
283
303
  // An item is its .tsx and any file beside it of the same name (its stylesheet).
@@ -301,7 +321,7 @@ function build() {
301
321
  const npm = new Set()
302
322
  for (const m of text.matchAll(/^@import\s+"([^".][^"]*)"/gm)) {
303
323
  if (m[1] !== "tailwindcss")
304
- npm.add(m[1].startsWith("@") ? m[1].split("/").slice(0, 2).join("/") : m[1].split("/")[0])
324
+ npm.add(versioned(m[1].startsWith("@") ? m[1].split("/").slice(0, 2).join("/") : m[1].split("/")[0]))
305
325
  }
306
326
  const name = f === "surface.css" ? "theme" : `theme-${f.replace(/\.css$/, "")}`
307
327
  items.push({
@@ -360,9 +380,12 @@ switch (cmd) {
360
380
  build()
361
381
  break
362
382
  case "add": {
363
- const names = [...args, ...(forPackage ? itemsFor(forPackage) : [])]
364
- if (!names.length) fail("add needs at least one item name (or --for <package>)")
365
- await install(readLock(), names)
383
+ const lock = readLock()
384
+ // --all: every item the registry has, at the lock's ref (its index is the item "registry").
385
+ const all = wantAll ? ((await fetchItem(lock, "registry")).items ?? []).map((i) => i.name) : []
386
+ const names = [...args, ...(forPackage ? itemsFor(forPackage) : []), ...all]
387
+ if (!names.length) fail("add needs at least one item name (or --for <package>, or --all)")
388
+ await install(lock, names)
366
389
  break
367
390
  }
368
391
  case "sync": {
@@ -395,6 +418,6 @@ switch (cmd) {
395
418
  }
396
419
  default:
397
420
  fail(
398
- "usage: ui.mjs init [--ref <tag>] [--dir <dir>] | build | add <item...> [--for <package>] | sync | verify | status | report <app dir...> [--from <file>] [--json] [--app <dir>] [--registry <dir or URL>]",
421
+ "usage: ui.mjs init [--ref <tag>] [--dir <dir>] | build | add <item...> [--for <package>] [--all] | sync | verify | status | report <app dir...> [--from <file>] [--json] [--app <dir>] [--registry <dir or URL>]",
399
422
  )
400
423
  }