Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Programmatically modify JavaScript and TypeScript source codes with a simplified, elegant and familiar syntax. Built on top of the AST parsed by recast and babel.
❯ 🧙🏼 Magical modify a JS/TS file and write back magically just like JSON!
❯ 🔀 Exports/Import manipulate module's imports and exports at ease
❯ 💼 Function Arguments easily manipulate arguments passed to a function call, like defineConfig()
❯ 🎨 Smart Formatting preseves the formatting style (quotes, tabs, etc.) from the original code
❯ 🧑💻 Readable get rid of the complexity of AST manipulation and make your code super readable
Install npm package:
# using yarn
yarn add --dev magicast
# using npm
npm install -D magicast
# using pnpm
pnpm add -D magicast
Import utilities:
// ESM / Bundler
import { parseModule, generateCode, builders, createNode } from "magicast";
// CommonJS
const { parseModule, generateCode, builders, createNode } = require("magicast");
Example: Modify a file:
config.js
:
export default {
foo: ["a"],
};
Code to modify and append b
to foo
prop of defaultExport:
import { loadFile, writeFile } from "magicast";
const mod = await loadFile("config.js");
mod.exports.default.foo.push("b");
await writeFile(mod);
Updated config.js
:
export default {
foo: ["a", "b"],
};
Example: Directly use AST utils:
import { parseModule, generateCode } from "magicast";
// Parse to AST
const mod = parseModule(`export default { }`);
// Ensure foo is an array
mod.exports.default.foo ||= [];
// Add a new array member
mod.exports.default.foo.push("b");
mod.exports.default.foo.unshift("a");
// Generate code
const { code, map } = generateCode(mod);
Generated code:
export default {
foo: ["a", "b"],
};
Example: Get the AST directly:
import { parseModule, generateCode } from "magicast";
const mod = parseModule(`export default { }`);
const ast = mod.exports.default.$ast
// do something with ast
Example: Function parameters:
import { parseModule, generateCode } from "magicast";
const mod = parseModule(`export default defineConfig({ foo: 'bar' })`);
// Support for both bare object export and `defineConfig` wrapper
const options = mod.exports.default.$type === 'function-call'
? mod.exports.default.$args[0]
: mod.exports.default;
console.log(options.foo) // bar
Example: Create a function call:
import { parseModule, generateCode, builders } from "magicast";
const mod = parseModule(`export default {}`);
const options = mod.exports.default.list = builders.functionCall('create', [1, 2, 3])
console.log(mod.generateCode()) // export default { list: create([1, 2, 3]) }
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:
import { loadFile, writeFile } from "magicast";
function updateConfig() {
try {
const mod = await loadFile("config.js");
mod.exports.default.foo.push("b");
await writeFile(mod);
} catch (e) {
console.error('Unable to update config.js')
console.error('Please update it manually with the following instructions: ...')
// handle error
}
}
We also experiment to provide a few high level helpers to make common tasks easier. You could import them from magicast/helpers
. They might be moved to a separate package in the future.
import {
deepMergeObject,
addNuxtModule,
addVitePlugin,
// ...
} from "magicast/helpers";
We recommend to check out the source code and test cases for more details.
corepack enable
pnpm install
pnpm dev
Made with 💛
Published under MIT License.
v0.2.10
addVitePlugin
(#69)FAQs
Modify a JS/TS file and write back magically just like JSON!
The npm package magicast receives a total of 1,254,496 weekly downloads. As such, magicast popularity was classified as popular.
We found that magicast demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.