@awesomeness-js/server 1.1.6 → 1.1.7

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": "@awesomeness-js/server",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "Awesomeness Multi-Site Server",
5
5
  "author": "Scott Forte",
6
6
  "type": "module",
@@ -4,7 +4,6 @@ import { each, getAllFiles } from "@awesomeness-js/utils";
4
4
  import { readFileSync } from "fs";
5
5
  import getConfig from "./getConfig.js";
6
6
 
7
-
8
7
  function urlToFsPath(u) {
9
8
 
10
9
  if (!(u instanceof URL)) {
@@ -51,12 +50,15 @@ function extractUiFirstParts(str) {
51
50
 
52
51
  }
53
52
 
54
- export default function componentDependencies(allComponents, {
55
- componentLocations = [],
56
- namespace = "ui",
57
- showDetails = false,
58
- ignore = [ "*.css.js" ],
59
- } = {}) {
53
+ export default function componentDependencies(
54
+ allComponents,
55
+ {
56
+ componentLocations = [],
57
+ namespace = "ui",
58
+ showDetails = false,
59
+ ignore = [ "*.css.js" ],
60
+ } = {}
61
+ ) {
60
62
 
61
63
  const awesomenessConfig = getConfig();
62
64
 
@@ -80,17 +82,12 @@ export default function componentDependencies(allComponents, {
80
82
 
81
83
  // baseUrl should point at a directory; we resolve component under it
82
84
  const componentUrl = new URL(`./${component}/`, baseUrl);
83
-
85
+
86
+
84
87
  return path.resolve(urlToFsPath(componentUrl));
85
88
 
86
89
  });
87
90
 
88
- // console.log({
89
- // "import.meta.url =": import.meta.url,
90
- // component,
91
- // candidateRoots
92
- // });
93
-
94
91
  let allFiles;
95
92
  let chosenRoot;
96
93
  let lastErr;
@@ -99,26 +96,24 @@ export default function componentDependencies(allComponents, {
99
96
 
100
97
  try {
101
98
 
99
+ // IMPORTANT: pass root so getAllFiles returns paths relative to the scan root
102
100
  allFiles = getAllFiles(".", {
103
101
  dir: root,
104
- ignore
102
+ root,
103
+ ignore,
105
104
  });
106
105
 
107
106
  chosenRoot = root;
108
-
109
107
  break; // first match wins
110
108
 
111
- } catch(e) {
109
+ } catch (e) {
112
110
 
113
- // try next
114
111
  lastErr = e;
115
112
 
116
113
  }
117
114
 
118
115
  }
119
116
 
120
-
121
-
122
117
  if (!allFiles) {
123
118
 
124
119
  throw {
@@ -131,48 +126,51 @@ export default function componentDependencies(allComponents, {
131
126
  }
132
127
 
133
128
  if (
134
- awesomenessConfig.debug_componentDependencies
135
- && Array.isArray(awesomenessConfig.debug_componentDependencies)
136
- && awesomenessConfig.debug_componentDependencies.includes(component)
129
+ awesomenessConfig.debug_componentDependencies &&
130
+ Array.isArray(awesomenessConfig.debug_componentDependencies) &&
131
+ awesomenessConfig.debug_componentDependencies.includes(component)
137
132
  ) {
138
133
 
139
134
  console.log("[awesomenessConfig.debug componentDependencies] chosenRoot:", chosenRoot);
140
135
  console.log("[awesomenessConfig.debug componentDependencies] allFiles count:", allFiles.length);
141
136
  console.log("[awesomenessConfig.debug componentDependencies] first 50 files:", allFiles.slice(0, 50));
142
- console.log("[awesomenessConfig.debug componentDependencies] any non-string:", allFiles.some((f) => typeof f !== "string"));
143
- console.log("[awesomenessConfig.debug componentDependencies] any absolute:", allFiles.some((f) => path.isAbsolute(f)));
144
-
137
+ console.log(
138
+ "[awesomenessConfig.debug componentDependencies] any non-string:",
139
+ allFiles.some((f) => typeof f !== "string")
140
+ );
141
+ console.log(
142
+ "[awesomenessConfig.debug componentDependencies] any absolute:",
143
+ allFiles.some((f) => path.isAbsolute(f))
144
+ );
145
+
145
146
  }
146
147
 
147
148
  allFiles.forEach((file) => {
148
149
 
149
150
  const normalizedPath = path.normalize(file);
150
- const pathSegments = normalizedPath.split(path.sep);
151
151
 
152
- const variableIndex = pathSegments.indexOf(component);
153
-
154
- let fileNameFull = pathSegments[pathSegments.length - 1];
152
+ const fileNameFull = path.basename(normalizedPath);
155
153
  const fileTypeArr = fileNameFull.split(".");
156
- const fileType = fileTypeArr[fileTypeArr.length - 1];
154
+ const fileType = fileTypeArr[fileTypeArr.length - 1].toLowerCase();
157
155
  const fileName = fileTypeArr.slice(0, -1).join(".");
158
156
 
159
157
  out[component] = out[component] || {};
160
158
  out[component][fileType] = out[component][fileType] || {};
161
159
 
162
- let arrAfterComponent = pathSegments.slice(variableIndex + 1);
163
-
164
- arrAfterComponent.pop();
160
+ // Build tail from the file's relative directory (NOT by searching for `component` in the path)
161
+ const dir = path.dirname(normalizedPath);
162
+ const dirParts = dir === "." ? [] : dir.split(path.sep);
165
163
 
166
164
  let tail = "";
167
165
 
168
166
  if (fileType === "js" || fileType === "css") {
169
167
 
170
- if (arrAfterComponent.length > 0) {
168
+ if (dirParts.length > 0) {
171
169
 
172
170
  tail =
173
- fileName === "index"
174
- ? "." + arrAfterComponent.join(".")
175
- : `.${arrAfterComponent.join(".")}.${fileName}`;
171
+ fileName === "index"
172
+ ? "." + dirParts.join(".")
173
+ : `.${dirParts.join(".")}.${fileName}`;
176
174
 
177
175
  } else {
178
176
 
@@ -186,7 +184,10 @@ export default function componentDependencies(allComponents, {
186
184
 
187
185
  try {
188
186
 
189
- const fileContent = readFileSync(path.join(chosenRoot, file), "utf-8");
187
+ // readFileSync must use chosenRoot + relative file path
188
+ const filePath = path.isAbsolute(file) ? file : path.join(chosenRoot, file);
189
+ const fileContent = readFileSync(filePath, "utf-8");
190
+
190
191
  const lines = fileContent.split("\n");
191
192
  let fileWithImportsStripped = "";
192
193
 
@@ -225,7 +226,9 @@ export default function componentDependencies(allComponents, {
225
226
 
226
227
  if (imports.length > 1) {
227
228
 
228
- const importComponents = imports[1].split(",").map((c) => c.trim());
229
+ const importComponents = imports[1]
230
+ .split(",")
231
+ .map((c) => c.trim());
229
232
 
230
233
  importComponents.forEach((importComponent) => {
231
234
 
@@ -243,15 +246,17 @@ export default function componentDependencies(allComponents, {
243
246
  }
244
247
 
245
248
  } else if (
246
- line.startsWith("// awesomeness import")
247
- || line.startsWith("/* awesomeness @import")
249
+ line.startsWith("// awesomeness import") ||
250
+ line.startsWith("/* awesomeness @import")
248
251
  ) {
249
252
 
250
253
  const importPathMatch = line.match(/['"]([^'"]+)['"]/);
251
254
 
252
255
  if (importPathMatch) {
253
256
 
254
- const importedComponentName = importPathMatch[1].replace(/;$/, "").trim();
257
+ const importedComponentName = importPathMatch[1]
258
+ .replace(/;$/, "")
259
+ .trim();
255
260
 
256
261
  if (!allComponents.includes(importedComponentName)) {
257
262
 
@@ -273,8 +278,8 @@ export default function componentDependencies(allComponents, {
273
278
  if (fileType === "js") {
274
279
 
275
280
  if (
276
- fileWithImportsStripped.startsWith("(function")
277
- || fileWithImportsStripped.startsWith("((")
281
+ fileWithImportsStripped.startsWith("(function") ||
282
+ fileWithImportsStripped.startsWith("((")
278
283
  ) {
279
284
 
280
285
  fileWithImportsStripped = `;${fileWithImportsStripped}`;
@@ -294,8 +299,7 @@ export default function componentDependencies(allComponents, {
294
299
 
295
300
  } catch (err) {
296
301
 
297
- console.log("Failed to get dependencies", { component });
298
- const full = path.join(chosenRoot, file);
302
+ const full = path.isAbsolute(file) ? file : path.join(chosenRoot, file);
299
303
 
300
304
  console.error("Failed to get dependencies", {
301
305
  component,