We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6811c09 commit 3912e19Copy full SHA for 3912e19
README.md
@@ -77,3 +77,18 @@ To use the CJS patch you can run your Node.js application with the `--require` f
77
node --require cjs-patch.js your-app.js
78
```
79
80
+## Debugging
81
+
82
+The [debug module](https://www.npmjs.com/package/debug) is used to provide
83
+insight into the patching process. Set `DEBUG='@apm-js-collab*'` to view these
84
+logs.
85
86
+Additionally, any patched files can be written out by enabling dump mode. This
87
+is done by setting the environment variable `TRACING_DUMP` to any value. By
88
+default, it will write out file to the system's temporary directory as the
89
+parent directory. The target parent directory can be configured by setting
90
+the `TRACING_DUMP_DIR` environment variable to an absolute path. In either
91
+case, the resolved filename of the module being patched is appended. For
92
+example, if we are patching `lib/index.js` in the `foo` package, and we set
93
+a base directory of `/tmp/dump/`, then the patched code will be written to
94
+`/tmp/dump/foo/lib/index.js`.
index.js
@@ -41,6 +41,9 @@ class ModulePatch {
41
try {
42
const transformedCode = transformer.transform(content, 'unknown')
43
args[0] = transformedCode?.code
44
+ if (process.env.TRACING_DUMP) {
45
+ dump(args[0], filename)
46
+ }
47
} catch (error) {
48
debug('Error transforming module %s: %o', filename, error)
49
} finally {
@@ -63,5 +66,21 @@ class ModulePatch {
63
66
}
64
67
65
68
69
+function dump(code, filename) {
70
+ const os = require('node:os')
71
+ const path = require('node:path')
72
+ const fs = require('node:fs')
73
74
+ const base = process.env.TRACING_DUMP_DIR ?? os.tmpdir()
75
+ const dirname = path.dirname(filename)
76
+ const basename = path.basename(filename)
+ const targetDir = path.join(base, dirname)
+ const targetFile = path.join(targetDir, basename)
+ debug('Dumping patched code to: %s', targetFile)
+ fs.mkdirSync(targetDir, { recursive: true })
+ fs.writeFileSync(targetFile, code)
+}
module.exports = ModulePatch
0 commit comments