how to run jscodeshift from node api?
Asked Answered
I

0

6

I've seen some developers wondering how to run jscodeshift without CLI, there are some issues but they're very simple, so If someone wants to effectively run it with options, here's an example.

const path = require('path');
const Runner = require('jscodeshift/src/Runner');

// just the paths of the files to apply the transformation
const paths = ['path/to/example1.js', 'path/to/example2.js'];

/**
 * taken from
 * @link https://github.com/facebook/jscodeshift/blob/48f5d6d6e5e769639b958f1a955c83c68157a5fa/bin/jscodeshift.js#L18
 */
const options = {
  transform: 'path/to/transformerFile.js',
  verbose: 0,
  dry: false,
  print: true,
  babel: true,
  extensions: 'js',
  ignorePattern: [],
  ignoreConfig: [],
  runInBand: false,
  silent: false,
  parser: 'babel',
  stdin: false
}

/**
 * taken from
 * @link https://github.com/facebook/jscodeshift/blob/48f5d6d6e5e769639b958f1a955c83c68157a5fa/bin/jscodeshift.js#L135
 */
Runner.run(
  /^https?/.test(options.transform) ? options.transform : path.resolve(options.transform),
  paths,
  options
);

Incudes answered 10/11, 2020 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.