dh-remixer-sdk 0.0.31-cfbcd84 → 0.0.31

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dh-remixer-sdk",
3
- "version": "0.0.31-cfbcd84",
3
+ "version": "0.0.31",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "templates",
@@ -1,35 +1,150 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from "node:fs/promises";
3
+ import fssync from "node:fs";
3
4
  import path from "node:path";
4
- import {
5
- extractFile,
6
- extractJsonFile,
7
- getTargetPackageJson,
8
- } from "../sdk-update/fs-utils.mjs";
9
- import {
10
- getDeletionList,
11
- getExtractionList,
12
- } from "../sdk-update/sdk-utils.mjs";
13
- import { PROJECT_ROOT, SDK_ROOT } from "../sdk-update/vars.mjs";
5
+ import { fileURLToPath } from "node:url";
6
+
7
+ // SDK_ROOT is the dh-remixer-sdk package that provides the canonical template
8
+ // files and the version to pin. It defaults to this package's own root, but can
9
+ // be pointed at a different dh-remixer-sdk
10
+ const SDK_ROOT = process.env.REMIXER_SDK_ROOT
11
+ ? path.resolve(process.env.REMIXER_SDK_ROOT)
12
+ : path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
13
+ const PROJECT_ROOT = path.resolve(process.env.INIT_CWD ?? process.cwd());
14
+ const BASE_ROOT = path.join(SDK_ROOT, "templates", "base");
15
+
16
+ function isObj(x) {
17
+ return x != null && typeof x === "object" && !Array.isArray(x);
18
+ }
19
+
20
+ function jsonMerge(low, high) {
21
+ if (Array.isArray(low) && Array.isArray(high)) {
22
+ return [...low, ...high];
23
+ }
24
+
25
+ if (!isObj(low) || !isObj(high)) {
26
+ return high === undefined ? low : high;
27
+ }
28
+
29
+ return Object.fromEntries(
30
+ Object.keys({ ...low, ...high }).map((key) => {
31
+ const lv = low[key];
32
+ const hv = high[key];
33
+
34
+ if (hv === undefined) return [key, lv];
35
+ if (Array.isArray(lv) && Array.isArray(hv)) return [key, [...lv, ...hv]];
36
+ if (Array.isArray(lv) || Array.isArray(hv)) return [key, hv];
37
+ if (isObj(lv) && isObj(hv)) return [key, jsonMerge(lv, hv)];
38
+ return [key, hv];
39
+ }),
40
+ );
41
+ }
42
+
43
+ function assertTemplateExists(templateType) {
44
+ if (templateType === "base") return;
45
+ const templateRoot = path.join(SDK_ROOT, "templates", templateType);
46
+ if (!fssync.existsSync(templateRoot)) {
47
+ throw new Error(
48
+ `Template "${templateType}" not found in dh-remixer-sdk (${templateRoot}).`,
49
+ );
50
+ }
51
+ }
52
+
53
+ // Merges a base + template-specific JSON list/map (cleanup.json / filemap.json).
54
+ async function getMergedTemplateJson(templateType, fileName) {
55
+ assertTemplateExists(templateType);
56
+ const basePath = path.join(BASE_ROOT, fileName);
57
+ const templatePath = path.join(SDK_ROOT, "templates", templateType, fileName);
58
+ let merged = JSON.parse(await fs.readFile(basePath, "utf8"));
59
+ if (templateType !== "base" && fssync.existsSync(templatePath)) {
60
+ merged = jsonMerge(merged, JSON.parse(await fs.readFile(templatePath, "utf8")));
61
+ }
62
+ return merged;
63
+ }
64
+
65
+ function getDeletionList(templateType) {
66
+ return getMergedTemplateJson(templateType, "cleanup.json");
67
+ }
68
+
69
+ function getExtractionList(templateType) {
70
+ return getMergedTemplateJson(templateType, "filemap.json");
71
+ }
72
+
73
+ async function extractFile(fileName, target, templateType) {
74
+ const targetPath = path.join(PROJECT_ROOT, target);
75
+ const templateRoot = path.join(SDK_ROOT, "templates", templateType);
76
+ const baseFileName = path.join(BASE_ROOT, fileName);
77
+ const tmplFileName = path.join(templateRoot, fileName);
78
+ await fs.mkdir(path.dirname(targetPath), { recursive: true });
79
+
80
+ if (fssync.existsSync(tmplFileName)) {
81
+ await fs.copyFile(tmplFileName, targetPath);
82
+ return;
83
+ }
84
+
85
+ if (fssync.existsSync(baseFileName)) {
86
+ await fs.copyFile(baseFileName, targetPath);
87
+ }
88
+ }
89
+
90
+ async function extractJsonFile(fileName, target, templateType) {
91
+ const targetPath = path.join(PROJECT_ROOT, target);
92
+ const templateRoot = path.join(SDK_ROOT, "templates", templateType);
93
+ const baseFileName = path.join(BASE_ROOT, fileName);
94
+ const tmplFileName = path.join(templateRoot, fileName);
95
+ const baseExists = fssync.existsSync(baseFileName);
96
+ const templateExists = fssync.existsSync(tmplFileName);
97
+
98
+ if (!baseExists && !templateExists) {
99
+ return;
100
+ }
101
+
102
+ let jsonContents = {};
103
+
104
+ if (baseExists) {
105
+ jsonContents = jsonMerge(
106
+ jsonContents,
107
+ JSON.parse(await fs.readFile(baseFileName, "utf8")),
108
+ );
109
+ }
110
+
111
+ if (templateExists) {
112
+ jsonContents = jsonMerge(
113
+ jsonContents,
114
+ JSON.parse(await fs.readFile(tmplFileName, "utf8")),
115
+ );
116
+ }
117
+
118
+ const stringContents = JSON.stringify(jsonContents, null, 2) + "\n";
119
+ await fs.mkdir(path.dirname(targetPath), { recursive: true });
120
+ await fs.writeFile(targetPath, stringContents, "utf8");
121
+ }
14
122
 
15
123
  async function main() {
16
- const projectRoot = path.resolve(process.env.INIT_CWD ?? process.cwd());
17
- const projPkgPath = path.join(projectRoot, "package.json");
18
- const targetPackageJson = await getTargetPackageJson();
124
+ const projPkgPath = path.join(PROJECT_ROOT, "package.json");
19
125
 
20
- if (!targetPackageJson) {
126
+ let targetPackageJson;
127
+ try {
128
+ targetPackageJson = JSON.parse(await fs.readFile(projPkgPath, "utf8"));
129
+ } catch {
130
+ console.error(`[sdk-rewrite] Cannot read package.json at ${projPkgPath}`);
131
+ process.exit(1);
132
+ }
133
+
134
+ const templateType = targetPackageJson.remixerMetadata?.template;
135
+
136
+ if (!templateType) {
21
137
  console.error(
22
- `Cannot find package.json\nContents: ${JSON.stringify(targetPackageJson)}`,
138
+ "[sdk-rewrite] Missing remixerMetadata.template in package.json. " +
139
+ "Cannot determine which template to apply; aborting rewrite.",
23
140
  );
24
- return;
141
+ process.exit(1);
25
142
  }
26
143
 
27
144
  const sdkPkgPath = path.join(SDK_ROOT, "package.json");
28
145
  const sdkPkg = JSON.parse(await fs.readFile(sdkPkgPath, "utf8"));
29
146
  const currentVersion = sdkPkg.version;
30
147
 
31
- const templateType = targetPackageJson.remixerMetadata?.template;
32
-
33
148
  const deletionList = await getDeletionList(templateType);
34
149
  const extractionList = await getExtractionList(templateType);
35
150
 
@@ -45,17 +160,18 @@ async function main() {
45
160
  ),
46
161
  );
47
162
 
48
- // Extract new files
49
- await Promise.all([
50
- ...Object.entries(extractionList).map(([fileName, target]) => {
163
+ // Extract canonical files from the SDK templates
164
+ await Promise.all(
165
+ Object.entries(extractionList).map(([fileName, target]) => {
51
166
  console.log(`Extracting ${fileName}`);
52
167
  if (fileName.endsWith(".json")) {
53
168
  return extractJsonFile(fileName, target, templateType);
54
169
  }
55
170
  return extractFile(fileName, target, templateType);
56
171
  }),
57
- ]);
172
+ );
58
173
 
174
+ // Re-pin dh-remixer-sdk to the SDK_ROOT version (the templates' source version)
59
175
  const projPkg = JSON.parse(await fs.readFile(projPkgPath, "utf8"));
60
176
  const newPkg = JSON.stringify(
61
177
  {
@@ -72,4 +188,7 @@ async function main() {
72
188
  await fs.writeFile(projPkgPath, newPkg);
73
189
  }
74
190
 
75
- main();
191
+ main().catch((e) => {
192
+ console.error(`[sdk-rewrite] ${e?.message ?? e}`);
193
+ process.exit(1);
194
+ });
@@ -47,13 +47,12 @@ function injectRemixerBadgePlugin(): Plugin {
47
47
 
48
48
  transformIndexHtml(html) {
49
49
  try {
50
- const IS_SHOW_REMIXER_BADGE = process.env.IS_SHOW_REMIXER_BADGE === 'true';
51
50
  const REMIXER_BADGE_TYPE: string = process.env.REMIXER_BADGE_TYPE || 'DEFAULT';
52
51
 
53
52
  const domain = process.env.NEXT_PUBLIC_DOMAIN || '';
54
53
  const isPreviewService = PREVIEW_DOMAINS.some(pd => domain.endsWith(pd));
55
54
 
56
- if (!IS_SHOW_REMIXER_BADGE && !isPreviewService) return html
55
+ if (!isPreviewService) return html
57
56
 
58
57
  const DICTIONARY: Record<string, string> = {
59
58
  UPGRADE_PLAN: 'https://unpkg.com/dh-remixer-badge@latest/dist/upgradeBadge.js',