Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jscodeshift node api example #398

Open
aneurysmjs opened this issue Nov 10, 2020 · 2 comments
Open

jscodeshift node api example #398

aneurysmjs opened this issue Nov 10, 2020 · 2 comments

Comments

@aneurysmjs
Copy link

Hi everybody, 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.

Привет всем, я видел, как некоторые разработчики задаются вопросом, как запустить jscodeshift без CLI, есть некоторые проблемы, но они очень простые, поэтому, если кто-то хочет эффективно запускать его с параметрами, вот пример.

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
);

the rest depends on how it will be used
остальное зависит от того, как будет использоваться

related #343

related #179

@abernier
Copy link
Contributor

abernier commented Oct 8, 2021

would be great to make this issue a PR about adding this to official doc

@abernier
Copy link
Contributor

abernier commented Oct 8, 2021

Actually, this is even better, run() returning a promise you can:

const res = await Runner.run(...)

console.log(res)
/*
{
  stats: {},
  timeElapsed: '0.001',
  error: 0,
  ok: 0,
  nochange: 0,
  skip: 0
}
*/

kiprasmel added a commit to kiprasmel/CodeshiftCommunity that referenced this issue Oct 29, 2021
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to kiprasmel/CodeshiftCommunity that referenced this issue Oct 29, 2021
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to pipedrive/CodeshiftCommunity that referenced this issue Nov 3, 2021
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to pipedrive/CodeshiftCommunity that referenced this issue Dec 17, 2021
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to pipedrive/CodeshiftCommunity that referenced this issue Dec 17, 2021
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to pipedrive/CodeshiftCommunity that referenced this issue Dec 18, 2021
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to pipedrive/CodeshiftCommunity that referenced this issue Dec 18, 2021
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to kiprasmel/CodeshiftCommunity that referenced this issue Jan 26, 2022
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)
kiprasmel added a commit to kiprasmel/CodeshiftCommunity that referenced this issue Jan 26, 2022
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)

Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
kiprasmel added a commit to kiprasmel/CodeshiftCommunity that referenced this issue Jan 28, 2022
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)

Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
kiprasmel added a commit to kiprasmel/CodeshiftCommunity that referenced this issue Feb 10, 2022
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)

Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
kiprasmel added a commit to kiprasmel/CodeshiftCommunity that referenced this issue Feb 10, 2022
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)

Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
kiprasmel added a commit to pipedrive/CodeshiftCommunity that referenced this issue Jul 8, 2022
in the issue you linked [1] from jscodeshift, i think you forgot the `path.resolve` for non-http(s) imports (not even sure how they'd work tbh).

i tried running a local transform and it didn't work, but with this fix it did regardless of where i called the `packages/cli/bin/codeshift-cli.js` from. i think you might've been using the packages and not the --transforms flag so i understand why you could've missed it 😅

tbh there are more improvements to make w/ the local transforms (see [2]) but this is the first step :D

[1] facebook/jscodeshift#398
[2] hypermod-io#48 (comment)

Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants