my-runner

Run CommonJS script in sandbox with customized style
How it works?
my-runner uses vm module for mocking an CommonJS's script environment. So we can make the environment as your wish.
For example, jest uses customized environment for running test files. And we could use it running browser's script.
Installation
npm install my-runner
yarn add my-runner
Usage
Package
const myRunner = require('my-runner')
myRunner.runFile('/path/to/file.js').module.exports
Cli
npm i my-runner -g
my-runner --help
API
run
index.js:53-69
Run code script
Parameters
code {string}
opts {{}} (optional, default {})
opts.rootDir string? The run environment's root directory path.
opts.myrunnerrc boolean? Whether or not to look up .my-runnerrc or my-runner.config.js file.
opts.preset ({} | Function | {name: string, options: any})? A preset that is used as a base for configuration.
opts.moduleNameMapper Object<string, string>? A map from regular expressions to module names that allow to stub out resources, like images or styles with a single module.
opts.modulePathIgnorePatterns Array<string>? An array of regexp pattern strings that are matched against all module paths before those paths are to be considered 'visible' to the module loader.
opts.modulePaths Array<string>? An array of absolute paths to additional locations to search when resolving modules. Use NODE_PATH env variable by default.
opts.moduleFileExtensions Array<string> An array of file extensions your modules use. (optional, default ['.js','.json','.jsx','.ts','.tsx','.node'])
opts.moduleDirectories Array<string> An array of directory names to be searched recursively up from the requiring module's location. (optional, default ['node_modules'])
opts.preserveSymlinks boolean? preserveSymlinks
opts.resolver Function? This option allows the use of a custom resolver. More information see jest.
opts.browser boolean Respect Browserify's "browser" field in package.json when resolving modules. (optional, default false)
opts.transformIgnorePatterns Array<string> An array of regexp pattern strings that are matched against all source file paths before transformation. If the test path matches any of the patterns, it will not be transformed. (optional, default ['/node_modules/'])
opts.transform Object<string, (string | Function | {name: string, options: any})> A map from regular expressions to paths to transformers. A transformer is a module that provides a synchronous function for transforming source files. (optional, default {})
opts.globalSetup string? This option allows the use of a custom global setup module which exports an async function that is triggered once.
opts.globals Object? A set of global variables that need to be available in environments.
opts.transformContext Function? A sync function for transforming context.
opts.global any The reference on global environment. (optional, default {...global})
opts.moduleCache {}? The reference of require.cache
opts.moduleMain {}? The reference of require.main
opts.fs Object The fs module. (optional, default require('fs'))
opts.vm Object The vm module. (optional, default require('vm'))
Returns RunResult
runFile
index.js:78-84
Run file script
Parameters
filename string
opts Object Same as run options parameter. (optional, default {})
Returns RunResult
RunResult
index.js:97-106
Type: {module: {exports}, exports, require, global}
defaultAdvancedOptions
index.js:105-105
The default options
How to mock module?
my-runner had injected myRunner in global.
for example, Mocking prop-types and setting moduleNameMapper to be {"^prop-types$": "/path/to/mock-prop-types.js"}
myRunner
requireActual
Requires the actual module rather than the mock module.
Contributing
- Fork it!
- Create your new branch:
git checkout -b feature-new or git checkout -b fix-which-bug
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
git commit -am 'feat: some description (close #123)' or git commit -am 'fix: some description (fix #123)'
- Push to the branch:
git push
- Submit a pull request :)
Authors
This library is written and maintained by imcuttle, moyuyc95@gmail.com.
License
MIT - imcuttle 🐟