@effijs/forge 1.0.0-alpha.0 → 1.0.0-alpha.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effijs/forge",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.2",
4
4
  "type": "module",
5
5
  "description": "Modern scaffolding engine for EffiJS",
6
6
  "author": "Yevhenii Kyrychok",
package/src/Forge.js CHANGED
@@ -13,6 +13,15 @@ import { addFile } from "./fs/addFile.js";
13
13
  import { modifyFile, appendToFile } from "./fs/modifyFile.js";
14
14
  import { processTemplate, processDirectory } from "./fs/processTemplate.js";
15
15
 
16
+ function collectActionErrors(result, errors = []) {
17
+ if (!result || typeof result !== "object") return errors;
18
+ if (result.success === false && result.error) errors.push(String(result.error));
19
+ if (Array.isArray(result.results)) {
20
+ for (const child of result.results) collectActionErrors(child, errors);
21
+ }
22
+ return errors;
23
+ }
24
+
16
25
  async function applyPromptDefaults(questions, options) {
17
26
  const answers = {};
18
27
  const list = Array.isArray(questions) ? questions : [];
@@ -145,9 +154,15 @@ export class Forge {
145
154
  typeof generator.prompts === "function"
146
155
  ? await generator.prompts(this)
147
156
  : generator.prompts;
148
- answers = yes
157
+ const useDefaults = yes || dryRun;
158
+ answers = useDefaults
149
159
  ? await applyPromptDefaults(questions, options)
150
160
  : await this.prompt(questions);
161
+ if (dryRun && !yes && Array.isArray(questions) && questions.length) {
162
+ console.log(
163
+ chalk.dim("Dry run is using prompt defaults. Pass flags to override.\n"),
164
+ );
165
+ }
151
166
  }
152
167
 
153
168
  const data = { ...answers, ...options };
@@ -166,9 +181,16 @@ export class Forge {
166
181
  const { results, hasFailure } = await runActions(generator, ctx);
167
182
 
168
183
  if (hasFailure) {
184
+ const errors = [
185
+ ...new Set(results.flatMap((result) => collectActionErrors(result))),
186
+ ];
169
187
  console.error(
170
188
  chalk.red("\n❌ Generation failed — one or more actions failed\n"),
171
189
  );
190
+ for (const error of errors) {
191
+ console.error(chalk.red(`• ${error}`));
192
+ }
193
+ if (errors.length) console.error("");
172
194
  return { success: false, results };
173
195
  }
174
196
 
@@ -44,7 +44,8 @@ export class ForgeCLI {
44
44
 
45
45
  Object.assign(runOptions, parseUnknownFlags(this.argv ?? process.argv));
46
46
 
47
- await this.forge.run(generator, runOptions);
47
+ const result = await this.forge.run(generator, runOptions);
48
+ if (result?.success === false) process.exitCode = 1;
48
49
  });
49
50
 
50
51
  this.program
@@ -0,0 +1,37 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ const CONFIG_FILES = new Set(["forge.config.js", ".forgerc.js"]);
6
+
7
+ function isEsmSource(source) {
8
+ return /(^|\n)\s*(import|export)(\s|\{)/.test(source);
9
+ }
10
+
11
+ /**
12
+ * Node warns (MODULE_TYPELESS_PACKAGE_JSON) when a `.js` file uses ESM syntax
13
+ * and the nearest package.json has no `"type": "module"`. React Native apps
14
+ * stay CommonJS because babel, metro, and jest configs are `module.exports`.
15
+ * Force those Forge configs to load as ESM without changing the app package.
16
+ */
17
+ export async function load(url, context, nextLoad) {
18
+ if (!url.startsWith("file:")) return nextLoad(url, context);
19
+
20
+ let filePath;
21
+ try {
22
+ filePath = fileURLToPath(url);
23
+ } catch {
24
+ return nextLoad(url, context);
25
+ }
26
+
27
+ if (!CONFIG_FILES.has(path.basename(filePath))) return nextLoad(url, context);
28
+
29
+ const source = await readFile(filePath, "utf8");
30
+ if (!isEsmSource(source)) return nextLoad(url, context);
31
+
32
+ return {
33
+ format: "module",
34
+ source,
35
+ shortCircuit: true,
36
+ };
37
+ }
@@ -1,7 +1,16 @@
1
1
  import fs from "fs/promises";
2
+ import { register } from "node:module";
2
3
  import path from "path";
3
4
  import { pathToFileURL } from "url";
4
5
 
6
+ let esmConfigLoaderReady = false;
7
+
8
+ function ensureEsmConfigLoader() {
9
+ if (esmConfigLoaderReady) return;
10
+ register(new URL("./esmConfigLoader.js", import.meta.url));
11
+ esmConfigLoaderReady = true;
12
+ }
13
+
5
14
  const CONFIG_FILES = [
6
15
  "forge.config.js",
7
16
  "forge.config.mjs",
@@ -35,6 +44,7 @@ export async function loadLocalConfig(cwd = process.cwd()) {
35
44
  };
36
45
  }
37
46
 
47
+ if (configFile.endsWith(".js")) ensureEsmConfigLoader();
38
48
  const module = await import(pathToFileURL(fullPath).href);
39
49
  return {
40
50
  config: module.default ?? module,