ferst-core 0.5.0 → 0.5.2

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.
@@ -6,10 +6,25 @@
6
6
  // only content DATA, pages, the content config re-export, and a `custom/`
7
7
  // override sandbox. Shipped from core (its own single source) and run in each
8
8
  // client's CI/build: `ferst-check-thin [srcDir]` (defaults to ./src).
9
- import { readdirSync, statSync, existsSync } from 'node:fs';
9
+ import { readdirSync, statSync, existsSync, realpathSync } from 'node:fs';
10
10
  import { join, relative } from 'node:path';
11
11
  import { pathToFileURL } from 'node:url';
12
12
 
13
+ /**
14
+ * True when run as a CLI (not imported). Handles npm's `node_modules/.bin` symlink,
15
+ * where `process.argv[1]` is the symlink path but `import.meta.url` is the realpath —
16
+ * a plain `===` check silently skips `main()`, making this guard a false pass.
17
+ */
18
+ export function isDirectInvocation(argv1) {
19
+ if (!argv1) return false;
20
+ if (import.meta.url === pathToFileURL(argv1).href) return true;
21
+ try {
22
+ return import.meta.url === pathToFileURL(realpathSync(argv1)).href;
23
+ } catch {
24
+ return false;
25
+ }
26
+ }
27
+
13
28
  // Directories directly under src/ that belong to the core, not the client.
14
29
  const CORE_OWNED_DIRS = ['components', 'layouts', 'lib', 'utils', 'styles'];
15
30
  // Files under src/content/ that are the core's contract, not client data.
@@ -69,6 +84,6 @@ function main() {
69
84
  }
70
85
 
71
86
  // Run only when invoked directly as a bin (not when imported by a test).
72
- if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
87
+ if (isDirectInvocation(process.argv[1])) {
73
88
  main();
74
89
  }
@@ -14,11 +14,27 @@
14
14
  //
15
15
  // Output path: dist/admin/config.yml under the CWD (the client repo root at build
16
16
  // time). Override with a first CLI arg.
17
- import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
17
+ import { readFileSync, writeFileSync, mkdirSync, realpathSync } from 'node:fs';
18
18
  import { execSync } from 'node:child_process';
19
19
  import { dirname, resolve } from 'node:path';
20
20
  import { fileURLToPath, pathToFileURL } from 'node:url';
21
21
 
22
+ /**
23
+ * True when this file is being run as a CLI (not imported). Handles npm's
24
+ * `node_modules/.bin` **symlink**: there `process.argv[1]` is the symlink path but
25
+ * `import.meta.url` is the realpath, so a plain `===` check silently skips `main()`
26
+ * and the config is never generated — the bug this guards against.
27
+ */
28
+ export function isDirectInvocation(argv1) {
29
+ if (!argv1) return false;
30
+ if (import.meta.url === pathToFileURL(argv1).href) return true;
31
+ try {
32
+ return import.meta.url === pathToFileURL(realpathSync(argv1)).href;
33
+ } catch {
34
+ return false;
35
+ }
36
+ }
37
+
22
38
  const HERE = dirname(fileURLToPath(import.meta.url));
23
39
  export const TEMPLATE_PATH = resolve(HERE, '../cms/config.template.yml');
24
40
 
@@ -44,6 +60,27 @@ export function repoSlug() {
44
60
  return '';
45
61
  }
46
62
 
63
+ /**
64
+ * The platform git-proxy origin (CMS `base_url` / `api_root`) for THIS site:
65
+ * FERST_GIT_BASE env → committed `ferst-site.json` `gitBase` → prod default.
66
+ * The seeder writes `ferst-site.json` with the origin of the worker that provisioned
67
+ * the site, so a dev-provisioned site points its CMS backend at the dev worker and a
68
+ * prod one at prod — the "site respects the portal it was generated from" binding,
69
+ * restored at build time.
70
+ */
71
+ export function resolveGitBase(cwd = process.cwd()) {
72
+ const fromEnv = env('FERST_GIT_BASE');
73
+ if (fromEnv) return fromEnv;
74
+ try {
75
+ const raw = readFileSync(resolve(cwd, 'ferst-site.json'), 'utf8');
76
+ const site = JSON.parse(raw);
77
+ if (site && typeof site.gitBase === 'string' && site.gitBase.trim()) return site.gitBase.trim();
78
+ } catch {
79
+ /* no ferst-site.json (e.g. the template repo itself) — fall through to the default */
80
+ }
81
+ return 'https://signin.ferst.co.uk';
82
+ }
83
+
47
84
  /** The deployment branch: CF Pages env → current git branch → main. */
48
85
  export function deploymentBranch() {
49
86
  const cf = env('CF_PAGES_BRANCH');
@@ -86,9 +123,8 @@ function main() {
86
123
  console.error('ferst-cms-config: could not determine the repo — set GITHUB_REPOSITORY=owner/name.');
87
124
  process.exit(1);
88
125
  }
89
- // The platform git-proxy origin. Identical for every managed client; overridable for
90
- // dev/self-hosted via FERST_GIT_BASE.
91
- const base = env('FERST_GIT_BASE') || 'https://signin.ferst.co.uk';
126
+ // The platform git-proxy origin for this site (env → ferst-site.json → prod default).
127
+ const base = resolveGitBase();
92
128
  const branch = deploymentBranch();
93
129
 
94
130
  let tpl;
@@ -112,7 +148,7 @@ function main() {
112
148
  console.log(`ferst-cms-config: wrote ${outPath} (repo=${repo}, branch="${branch}", base=${base})`);
113
149
  }
114
150
 
115
- // Run only when invoked directly as a bin (not when imported by a test).
116
- if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
151
+ // Run when invoked directly as a bin — including via npm's node_modules/.bin symlink.
152
+ if (isDirectInvocation(process.argv[1])) {
117
153
  main();
118
154
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ferst-core",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "type": "module",
5
5
  "description": "Ferst Core — the shared, brand-agnostic client-site system: Astro components, layouts, content schemas, the layered config resolver, and a neutral styling architecture that every client overrides.",
6
6
  "license": "BUSL-1.1",