@prenta/cli 1.4.0 → 1.5.1

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.
@@ -0,0 +1,90 @@
1
+ import { access } from 'node:fs/promises'
2
+ import { basename, dirname, relative, resolve } from 'node:path'
3
+
4
+ /**
5
+ * Where the CMS host and the Astro public site live, wherever `prenta doctor`
6
+ * is run from (#1263).
7
+ *
8
+ * - Next monolith: everything in the current folder.
9
+ * - astro-split workspace: the CMS in `apps/cms`, the site in `apps/site`. Run
10
+ * from the repo root, from `apps/cms`, or from `apps/site`.
11
+ * - Older split layout: the site in `site/` next to the CMS.
12
+ */
13
+ export interface ProjectLayout {
14
+ /** Folder holding prenta.config.ts, prisma/ and the CMS app. */
15
+ cmsRoot: string
16
+ /** Folder holding the Astro site, when there is one. */
17
+ siteRoot?: string
18
+ /** `siteRoot` relative to the folder doctor runs in, for messages (`apps/site`). */
19
+ siteLabel?: string
20
+ }
21
+
22
+ async function exists(path: string): Promise<boolean> {
23
+ try {
24
+ await access(path)
25
+ return true
26
+ } catch {
27
+ return false
28
+ }
29
+ }
30
+
31
+ const CMS_MARKERS = [
32
+ 'prenta.config.ts',
33
+ 'prenta.config.mjs',
34
+ 'prenta.config.js',
35
+ 'app/api/cms/[[...cms]]/route.ts',
36
+ ]
37
+
38
+ const SITE_MARKERS = ['astro.config.mjs', 'astro.config.ts', 'astro.config.js', 'src/lib/cms.ts']
39
+
40
+ async function hasAny(root: string, markers: readonly string[]): Promise<boolean> {
41
+ for (const marker of markers) {
42
+ if (await exists(resolve(root, marker))) return true
43
+ }
44
+ return false
45
+ }
46
+
47
+ /** True when `dir` is a workspace package folder: `<root>/apps/<name>`. */
48
+ function isUnderApps(dir: string): boolean {
49
+ return basename(dirname(dir)) === 'apps'
50
+ }
51
+
52
+ export async function resolveProjectLayout(cwd: string): Promise<ProjectLayout> {
53
+ const cwdIsCms = await hasAny(cwd, CMS_MARKERS)
54
+ let cmsRoot = cwd
55
+
56
+ if (!cwdIsCms) {
57
+ // Run from an astro-split repo root: the CMS is a workspace package.
58
+ if (await hasAny(resolve(cwd, 'apps/cms'), CMS_MARKERS)) {
59
+ cmsRoot = resolve(cwd, 'apps/cms')
60
+ } else if (
61
+ // Run from apps/site: the CMS is its sibling.
62
+ isUnderApps(cwd) &&
63
+ (await hasAny(cwd, SITE_MARKERS)) &&
64
+ (await hasAny(resolve(cwd, '../cms'), CMS_MARKERS))
65
+ ) {
66
+ return { cmsRoot: resolve(cwd, '../cms'), siteRoot: cwd, siteLabel: '.' }
67
+ }
68
+ }
69
+
70
+ // Only folders that belong to this project: the workspace layout from the
71
+ // root, the older `site/` folder inside the project, and the sibling of a
72
+ // CMS that is itself a workspace package (apps/cms -> apps/site). A sibling
73
+ // `site/` next to a monolith is someone else's project, not this one's.
74
+ const candidates = [
75
+ resolve(cwd, 'apps/site'),
76
+ resolve(cwd, 'site'),
77
+ resolve(cmsRoot, 'site'),
78
+ ...(isUnderApps(cmsRoot) ? [resolve(cmsRoot, '../site')] : []),
79
+ ]
80
+ for (const candidate of candidates) {
81
+ if (await hasAny(candidate, SITE_MARKERS)) {
82
+ return {
83
+ cmsRoot,
84
+ siteRoot: candidate,
85
+ siteLabel: relative(cwd, candidate).replace(/\\/g, '/') || '.',
86
+ }
87
+ }
88
+ }
89
+ return { cmsRoot }
90
+ }