@arghajit/dummy 0.3.8 → 0.3.9

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@arghajit/dummy",
3
3
  "author": "Arghajit Singha",
4
- "version": "0.3.8",
4
+ "version": "0.3.9",
5
5
  "description": "A Playwright reporter and dashboard for visualizing test results.",
6
6
  "homepage": "https://playwright-pulse-report.netlify.app/",
7
7
  "keywords": [
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env node
2
1
  import * as fs from "fs";
3
2
  import * as path from "path";
4
3
  import { pathToFileURL } from "url";
@@ -24,15 +23,69 @@ async function findPlaywrightConfig() {
24
23
  }
25
24
 
26
25
  async function extractOutputDirFromConfig(configPath) {
26
+ let fileContent = "";
27
27
  try {
28
- let config;
28
+ fileContent = fs.readFileSync(configPath, "utf-8");
29
+ } catch (e) {
30
+ // If we can't read the file, we can't parse or import it.
31
+ return null;
32
+ }
33
+
34
+ // 1. Strategy: Text Parsing (Safe & Fast)
35
+ // We try to read the file as text first. This finds the outputDir without
36
+ // triggering any Node.js warnings or errors.
37
+ try {
38
+ // Regex matches: outputDir: "value" or outputDir: 'value'
39
+ const match = fileContent.match(/outputDir:\s*["']([^"']+)["']/);
40
+
41
+ if (match && match[1]) {
42
+ return path.resolve(process.cwd(), match[1]);
43
+ }
44
+ } catch (e) {
45
+ // Ignore text reading errors
46
+ }
47
+
48
+ // 2. Safety Check: Detect ESM in CJS to Prevent Node Warnings
49
+ // The warning "To load an ES module..." happens when we try to import()
50
+ // a .js file containing ESM syntax (import/export) in a CJS package.
51
+ // We explicitly check for this and ABORT the import if found.
52
+ if (configPath.endsWith(".js")) {
53
+ let isModulePackage = false;
54
+ try {
55
+ const pkgPath = path.resolve(process.cwd(), "package.json");
56
+ if (fs.existsSync(pkgPath)) {
57
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
58
+ isModulePackage = pkg.type === "module";
59
+ }
60
+ } catch (e) {}
61
+
62
+ if (!isModulePackage) {
63
+ // Heuristic: Check for ESM syntax (import/export at start of lines)
64
+ const hasEsmSyntax =
65
+ /^\s*import\s+/m.test(fileContent) ||
66
+ /^\s*export\s+/m.test(fileContent);
67
+
68
+ if (hasEsmSyntax) {
69
+ // We found ESM syntax in a .js file within a CJS project.
70
+ // Attempting to import this WILL trigger the Node.js warning.
71
+ // Since regex failed to find outputDir, and we can't import safely, we abort now.
72
+ return null;
73
+ }
74
+ }
75
+ }
29
76
 
77
+ // 3. Strategy: Dynamic Import
78
+ // If we passed the safety check, we try to import the config.
79
+ try {
80
+ let config;
30
81
  const configDir = dirname(configPath);
31
- // const originalDirname = global.__dirname; // Not strictly needed in ESM context usually, but keeping if you rely on it elsewhere
32
- // const originalFilename = global.__filename;
82
+ const originalDirname = global.__dirname;
83
+ const originalFilename = global.__filename;
33
84
 
34
- // 1. Try Loading via Import (Existing Logic)
35
85
  try {
86
+ global.__dirname = configDir;
87
+ global.__filename = configPath;
88
+
36
89
  if (configPath.endsWith(".ts")) {
37
90
  try {
38
91
  const { register } = await import("node:module");
@@ -52,20 +105,19 @@ async function extractOutputDirFromConfig(configPath) {
52
105
  config = await import(pathToFileURL(configPath).href);
53
106
  }
54
107
 
55
- // Extract from default export or direct export
108
+ // Handle Default Export
56
109
  if (config && config.default) {
57
110
  config = config.default;
58
111
  }
59
112
 
60
113
  if (config) {
61
- // Check specific reporter config
114
+ // Check for Reporter Config
62
115
  if (config.reporter) {
63
116
  const reporters = Array.isArray(config.reporter)
64
117
  ? config.reporter
65
118
  : [config.reporter];
66
119
 
67
120
  for (const reporter of reporters) {
68
- // reporter can be ["list"] or ["html", { outputFolder: '...' }]
69
121
  const reporterName = Array.isArray(reporter)
70
122
  ? reporter[0]
71
123
  : reporter;
@@ -80,45 +132,28 @@ async function extractOutputDirFromConfig(configPath) {
80
132
  reporterName.includes("@arghajit/dummy"))
81
133
  ) {
82
134
  if (reporterOptions && reporterOptions.outputDir) {
83
- // Found it via Import!
84
135
  return path.resolve(process.cwd(), reporterOptions.outputDir);
85
136
  }
86
137
  }
87
138
  }
88
139
  }
89
140
 
90
- // Check global outputDir
141
+ // Check for Global outputDir
91
142
  if (config.outputDir) {
92
143
  return path.resolve(process.cwd(), config.outputDir);
93
144
  }
94
145
  }
95
- } catch (importError) {
96
- // Import failed (likely the SyntaxError you saw).
97
- // We suppress this error and fall through to the text-parsing fallback below.
146
+ } finally {
147
+ // Clean up globals
148
+ global.__dirname = originalDirname;
149
+ global.__filename = originalFilename;
98
150
  }
99
-
100
- // 2. Fallback: Parse file as text (New Logic)
101
- // This runs if import failed or if import worked but didn't have the specific config
102
- try {
103
- const fileContent = fs.readFileSync(configPath, "utf-8");
104
-
105
- // Regex to find: outputDir: "some/path" or 'some/path' inside the reporter config or global
106
- // This is a simple heuristic to avoid the "Cannot use import statement" error
107
- const match = fileContent.match(/outputDir:\s*["']([^"']+)["']/);
108
-
109
- if (match && match[1]) {
110
- console.log(`Found outputDir via text parsing: ${match[1]}`);
111
- return path.resolve(process.cwd(), match[1]);
112
- }
113
- } catch (readError) {
114
- // If reading fails, just return null silently
115
- }
116
-
117
- return null;
118
151
  } catch (error) {
119
- // Final safety net: Do not log the stack trace to avoid cluttering the console
152
+ // SILENT CATCH: Do NOT log anything here.
120
153
  return null;
121
154
  }
155
+
156
+ return null;
122
157
  }
123
158
 
124
159
  export async function getOutputDir(customOutputDirFromArgs = null) {