@prenta/cli 1.1.0 → 1.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.
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { readFileSync } from 'node:fs'
2
3
  import { Command } from 'commander'
3
4
  import { registerMigrateCommand } from './commands/migrate.js'
4
5
  import { registerMigrateSectionsCommand } from './commands/migrate-sections.js'
@@ -7,6 +8,7 @@ import { registerMigrateMetaCommand } from './commands/migrate-meta.js'
7
8
  import { registerMigrateMediaUsageCommand } from './commands/migrate-media-usage.js'
8
9
  import { registerMigrateMediaOptimizeCommand } from './commands/migrate-media-optimize.js'
9
10
  import { registerMigrateContentHashCommand } from './commands/migrate-content-hash.js'
11
+ import { registerMigrateMediaStorageKeyCommand } from './commands/migrate-media-storage-key.js'
10
12
  import { registerGenerateCommand } from './commands/generate.js'
11
13
  import { registerSeedCommand } from './commands/seed.js'
12
14
  import { registerImportCommand } from './commands/import.js'
@@ -28,12 +30,19 @@ import { registerVercelBlobLinkCommand } from './commands/vercel-blob-link.js'
28
30
  import { registerMcpCommand } from './commands/mcp.js'
29
31
  import { registerChromeCommand } from './commands/chrome.js'
30
32
 
33
+ const { version } = JSON.parse(
34
+ readFileSync(new URL('../package.json', import.meta.url), 'utf-8'),
35
+ ) as { version: string }
36
+
31
37
  const program = new Command()
32
38
 
33
39
  program
34
40
  .name('prenta')
35
41
  .description('Prenta CMS — CLI toolkit for migrations, codegen, and more')
36
- .version('0.1.0')
42
+ .version(version)
43
+ // Program options only before the subcommand, so `prenta upgrade --version <ver>`
44
+ // reaches upgrade instead of printing the CLI version.
45
+ .enablePositionalOptions()
37
46
 
38
47
  registerMigrateCommand(program)
39
48
  registerMigrateSectionsCommand(program)
@@ -42,6 +51,7 @@ registerMigrateMetaCommand(program)
42
51
  registerMigrateMediaUsageCommand(program)
43
52
  registerMigrateMediaOptimizeCommand(program)
44
53
  registerMigrateContentHashCommand(program)
54
+ registerMigrateMediaStorageKeyCommand(program)
45
55
  registerGenerateCommand(program)
46
56
  registerSeedCommand(program)
47
57
  registerImportCommand(program)
@@ -0,0 +1,140 @@
1
+ import { existsSync } from 'node:fs'
2
+ import { readFile, readdir } from 'node:fs/promises'
3
+ import { dirname, join, relative, resolve, sep } from 'node:path'
4
+
5
+ const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', '.next', '.turbo'])
6
+
7
+ /**
8
+ * Workspace package patterns declared by `root`: npm/yarn/bun `workspaces`
9
+ * (array or `{ packages }`) and pnpm's `pnpm-workspace.yaml` `packages` list.
10
+ */
11
+ export async function readWorkspacePatterns(root: string): Promise<string[]> {
12
+ const patterns: string[] = []
13
+ const pkgPath = join(root, 'package.json')
14
+ if (existsSync(pkgPath)) {
15
+ const pkg = JSON.parse(await readFile(pkgPath, 'utf-8')) as {
16
+ workspaces?: string[] | { packages?: string[] }
17
+ }
18
+ const ws = Array.isArray(pkg.workspaces) ? pkg.workspaces : pkg.workspaces?.packages
19
+ if (Array.isArray(ws)) patterns.push(...ws.filter((p): p is string => typeof p === 'string'))
20
+ }
21
+ const pnpmPath = join(root, 'pnpm-workspace.yaml')
22
+ if (existsSync(pnpmPath))
23
+ patterns.push(...parsePnpmWorkspacePackages(await readFile(pnpmPath, 'utf-8')))
24
+ return patterns
25
+ }
26
+
27
+ function unquote(value: string): string {
28
+ return value.trim().replace(/^(['"])(.*)\1$/, '$2')
29
+ }
30
+
31
+ /**
32
+ * The `packages:` list of a pnpm-workspace.yaml: a block sequence (indented
33
+ * or at column 0) or a flow sequence (`packages: [a, "b"]`).
34
+ */
35
+ export function parsePnpmWorkspacePackages(yaml: string): string[] {
36
+ const out: string[] = []
37
+ let inPackages = false
38
+ for (const rawLine of yaml.split(/\r?\n/)) {
39
+ const line = rawLine.replace(/(^|\s)#.*$/, '').trimEnd()
40
+ const key = line.match(/^packages\s*:\s*(.*)$/)
41
+ if (key) {
42
+ const rest = key[1]!.trim()
43
+ const flow = rest.match(/^\[(.*)\]$/)
44
+ if (flow) out.push(...flow[1]!.split(',').map(unquote).filter(Boolean))
45
+ inPackages = rest === ''
46
+ continue
47
+ }
48
+ if (!inPackages || !line.trim()) continue
49
+ const item = line.match(/^\s*-\s+(.+)$/)
50
+ if (item) out.push(unquote(item[1]!))
51
+ else if (!/^\s/.test(line)) inPackages = false
52
+ }
53
+ return out
54
+ }
55
+
56
+ /** Directories under `root` matching `pattern` (`*` one level, `**` any depth). */
57
+ async function expandPattern(root: string, pattern: string): Promise<string[]> {
58
+ const segments = pattern.replace(/^\.\//, '').replace(/\/+$/, '').split('/').filter(Boolean)
59
+ let current = [root]
60
+ for (const segment of segments) {
61
+ const next: string[] = []
62
+ for (const dir of current) {
63
+ if (segment === '**') next.push(dir, ...(await subdirectories(dir, Infinity)))
64
+ else if (segment.includes('*')) {
65
+ const re = new RegExp(`^${segment.split('*').map(escapeRegExp).join('[^/]*')}$`)
66
+ for (const child of await subdirectories(dir, 1)) {
67
+ if (re.test(child.slice(dir.length + 1))) next.push(child)
68
+ }
69
+ } else next.push(join(dir, segment))
70
+ }
71
+ current = next
72
+ }
73
+ return current
74
+ }
75
+
76
+ async function subdirectories(dir: string, depth: number): Promise<string[]> {
77
+ if (depth <= 0) return []
78
+ const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
79
+ const out: string[] = []
80
+ for (const entry of entries) {
81
+ // Symlinked directories aren't followed (`isDirectory()` is false for links), so `**` can't loop.
82
+ if (!entry.isDirectory() || SKIP_DIRS.has(entry.name)) continue
83
+ const child = join(dir, entry.name)
84
+ out.push(child, ...(await subdirectories(child, depth - 1)))
85
+ }
86
+ return out
87
+ }
88
+
89
+ function escapeRegExp(value: string): string {
90
+ return value.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
91
+ }
92
+
93
+ /**
94
+ * `package.json` paths of the workspace packages under `root` (not including
95
+ * the root manifest), sorted. `!pattern` entries exclude matches.
96
+ */
97
+ export async function findWorkspaceManifests(root: string): Promise<string[]> {
98
+ const patterns = await readWorkspacePatterns(root)
99
+ const include = new Set<string>()
100
+ const exclude = new Set<string>()
101
+ for (const pattern of patterns) {
102
+ const negated = pattern.startsWith('!')
103
+ for (const dir of await expandPattern(root, negated ? pattern.slice(1) : pattern)) {
104
+ const manifest = join(dir, 'package.json')
105
+ if (resolve(dir) === resolve(root) || !existsSync(manifest)) continue
106
+ ;(negated ? exclude : include).add(resolve(manifest))
107
+ }
108
+ }
109
+ return [...include].filter((m) => !exclude.has(m)).sort()
110
+ }
111
+
112
+ /**
113
+ * The nearest ancestor of `cwd` that declares workspaces including `cwd`, or
114
+ * null. Lets `prenta upgrade` warn when it runs inside a workspace package.
115
+ */
116
+ export async function findEnclosingWorkspaceRoot(cwd: string): Promise<string | null> {
117
+ let dir = dirname(resolve(cwd))
118
+ while (dir !== dirname(dir)) {
119
+ if ((await readWorkspacePatterns(dir)).length > 0) {
120
+ const manifests = await findWorkspaceManifests(dir)
121
+ if (manifests.includes(resolve(cwd, 'package.json'))) return dir
122
+ }
123
+ dir = dirname(dir)
124
+ }
125
+ return null
126
+ }
127
+
128
+ /** Install command for the package manager whose lockfile is in `root`. */
129
+ export function detectInstallCommand(root: string): string {
130
+ if (existsSync(join(root, 'pnpm-lock.yaml'))) return 'pnpm install'
131
+ if (existsSync(join(root, 'yarn.lock'))) return 'yarn install'
132
+ if (existsSync(join(root, 'bun.lock')) || existsSync(join(root, 'bun.lockb')))
133
+ return 'bun install'
134
+ return 'npm install'
135
+ }
136
+
137
+ /** `root`-relative display path using forward slashes. */
138
+ export function displayPath(root: string, file: string): string {
139
+ return relative(root, file).split(sep).join('/')
140
+ }