question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Splitting the API and the CLI

See original GitHub issue

I use the jquery-like API for transforming outside of the CLI (FWIW I use it in rollup).

Currently, my hacky workaround (along with some changes in my fork: https://github.com/mikob/jscodeshift) looks like this:

await JSCodeShift('/transforms/split.ts', [`dist/tmp/${pluginName}.joined.mjs`], {
        transform: './transforms/split.ts',
	verbose: 2,
	runInBand: true,

	dry: false,
	print: false,
	babel: true,
	extensions: 'js,mjs',
	ignorePattern: [],
	ignoreConfig: [],
	silent: false,
	parser: 'babel',
	stdin: false
});						

This is not ideal because I get CLI output, a pool of workers, filesystem R/W etc. I’m only interested in using the API internally. So I can do something like this:

const transformedSrc = await JSCodeShift([transformFn: (jscodeshift: JSCodeShift) => string], src: string);

Currently transforms have a function signature like this: module.exports = function (fileInfo: FileInfo, api: JSCodeshift, options) {

With the separated API, I would expect to be able to call a transform and only pass it the 2nd argument.

Related: https://github.com/facebook/jscodeshift/issues/179

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

8reactions
marcusrbrowncommented, Sep 16, 2019

I believe I came up with what you were looking for. The default jscodeshift export is the object passed as the api argument to a transform module from the Runner. If you don’t wan’t any of the Runner functionality, just use jscodeshift directly, however you’d like:

const jscodeshift = require('jscodeshift');

const source = `
function foo() {}

module.exports = foo;
`;

const transform = (j, source) =>
  j(source)
    .find(j.Identifier)
    .replaceWith(p =>
      j.identifier(
        p.node.name
          .split('')
          .reverse()
          .join('')
      )
    )
    .toSource();

console.log(`Original source =\n${source}\n`);

const transformedSource = transform(jscodeshift, source);

console.log(`Transformed source =\n${transformedSource}\n`);

Here I’ve defined my own transform function that does what the sample/reverse-identifiers.js does: reverse all identifiers in the given source.

Running this example produces the following output:

Original source =

function foo() {}

module.exports = foo;


Transformed source =

function oof() {}

eludom.stropxe = oof;

If you want to specify transform modules from files, then I recommend reusing the code in Worker.js, lines 78-81:

  transform = typeof module.default === 'function' ?
    module.default :
    module;

I’ve provided the sample here: https://github.com/igetgames/jscodeshift-demo. Let me know if you need any more information.

3reactions
mikobcommented, Sep 16, 2019

@igetgames yes, that’s great! Much simpler than I imagined! Thank you for the very detailed write up!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Split code for CLI and core functionality into separate ... - GitHub
Sceptre officially supports use via the CLI and as a Python module. This leads difficulty with versioning as we are versioning two APIs...
Read more >
Split a Twilio CLI Command into Multiple Lines
This guide explains how to split your commands, and gives sample code for different environments. Notice: CLI command examples may need to swap...
Read more >
Introducing the Split CLI - Split - Split Software
Using the Split CLI, you can go from setting up your Split account to creating and ramping your feature flags in minutes.
Read more >
split - Redocly
The split command takes an API definition file and creates a multi-file structure out of it by extracting referenced parts into standalone, ...
Read more >
split-shard — AWS CLI 1.27.37 Command Reference
Splits a shard into two new shards in the Kinesis data stream, ... This API is only supported for the data streams with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found