Skip to content

Commit 5c5cd52

Browse files
committed
docs: add notes about usage
1 parent 7fa88fc commit 5c5cd52

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ const options = mod.exports.default.list = builders.functionCall('create', [1, 2
137137
console.log(mod.generateCode()) // export default { list: create([1, 2, 3]) }
138138
```
139139

140+
## Notes
141+
142+
As JavaScript is a very dynamic language, you should be aware that Magicast's convention **CAN NOT cover all possible cases**. Magicast serves as a simple and maintainable interface to update static-ish JavaScript code. When interacting with Magicast node, be aware that every option might have chance to throw an error depending on the input code. We recommend to always wrap the code in a `try/catch` block (even better to do some defensive coding), for example:
143+
144+
```ts
145+
import { loadFile, writeFile } from "magicast";
146+
147+
function updateConfig() {
148+
try {
149+
const mod = await loadFile("config.js");
150+
151+
mod.exports.default.foo.push("b");
152+
153+
await writeFile(mod);
154+
} catch (e) {
155+
console.error('Unable to update config.js')
156+
console.error('Please update it manually with the following instructions: ...')
157+
// handle error
158+
}
159+
}
160+
```
140161

141162
## High Level Helpers
142163

0 commit comments

Comments
 (0)