🧀 Paneer
Paneer allows you to 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.
Roadmap:
- Generic API
- [x] Working parser and code generation with TS support
- [ ] Access to comments
- ESM
- [x] Basic syntax support
- [x] Access to the named exports
- [ ] Access to imports
- Typescript
- [x] Basic syntax support
- [ ] Allow access to type nodes with shortcuts
- Objects
- [x] Iterate over properties
- [ ] Assign new properties
- Arrays
- [x] Push literal values
- [ ] Iterate and modify elements individually
- Functions
- [x] Access to call expression arguments
- [ ] Access to function body
- [ ] Access to function return
Usage
Install npm package:
# using yarn
yarn add --dev paneer
# using npm
npm install -D paneer
# using pnpm
pnpm add -D paneer
Import utilities:
// ESM / Bundler
import { parseCode, generateCode } from "paneer";
import * as p from "paneer";
// CommonJS
const { parseCode, generateCode } = require("panner");
const p = require("panner");
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 "paneer";
const _module = await loadFile("config.js");
_module.exports.default.props.foo.push("b");
await writeFile(_module);
Updated config.js
:
export default {
foo: ["a", "b"],
};
Example: Directly use AST utils:
import { parseCode, generateCode } from "paneer";
// Parse to AST
const _module = parseCode(`export default { foo: ['a'] }`);
// Add a new array member
_module.exports.default.props.foo.push("b");
// Generate code
const { code, map } = generateCode(_module);
Development
- Clone this repository
- Install latest LTS version of Node.js
- Enable Corepack using
corepack enable
- Install dependencies using
pnpm install
- Run interactive tests using
pnpm dev
License
Made with
Published under MIT License.