1.0.0 • Published 4 years ago

jsmod v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

jsmod

Module to replace contents of javascript file using AST traversal! :rocket:

Getting Started

$ npm install jsmod

Usage

const jsmod = require("jsmod");

const { count, files } = await jsmod({
    files: ["/path/to/file-1", "/path/to/file-2", "/path/to/file-3"],
    filterFiles: ({ fileContents }) => fileContents.match("var a = 42;"),
    traverseOptions: {
        enter: function(node, parent) {
            if (node.type === "Literal" && node.value === "somePropertyName") {
                node.value = "some_property_name";
            }
        }
    },
    generateOptions: {
        format: {
            quotes: "double"
        }
    }
});

API

jsmod(options)

Reads and modifies an array of files and returns a promise that resolves when everything completes.

options.files

Type: Array Default: none Required: yes

Array of files paths to be read.

options.traverseOptions

Type: Object Default: none Required: yes

Traverse options passed to estraverse.

options.filterFiles({ fileName, fileContents })

Type: Function Default: none Required: no

Filter function to filter/ignore files based on file name or file contents.

This can be used to remove files which don't match certain criteria which you may wish to ignore.

Return true if file is to be considered for traversal/replacement, else return false if file is to be ignored.

options.generateOptions

Type: Object Default: none Required: no

Generate options passed to escodegen.

License

MIT © The Half Blood Prince