|
| 1 | +// Most of the code from here is from bin/jscodeshift.js |
| 2 | +// It's kept that way so that users can reuse jscodeshift options. |
| 3 | + |
| 4 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 5 | +// @ts-nocheck |
| 6 | + |
| 7 | +import { readFileSync } from "fs"; |
| 8 | +import argsParser from "jscodeshift/dist/argsParser"; |
| 9 | +import { dirname, join } from "path"; |
| 10 | + |
| 11 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 12 | +// @ts-ignore: package.json will be imported from dist folders |
| 13 | +import { version } from "../../package.json"; |
| 14 | + |
| 15 | +const requirePackage = (name: string) => { |
| 16 | + const entry = require.resolve(name); |
| 17 | + let dir = dirname(entry); |
| 18 | + while (dir !== "/") { |
| 19 | + try { |
| 20 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 21 | + const pkg = require(join(dir, "package.json")); |
| 22 | + return pkg.name === name ? pkg : {}; |
| 23 | + } catch (error) {} // eslint-disable-line no-empty |
| 24 | + dir = dirname(dir); |
| 25 | + } |
| 26 | + return {}; |
| 27 | +}; |
| 28 | + |
| 29 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 30 | +export const getJsCodeshiftParser = () => |
| 31 | + argsParser.options({ |
| 32 | + transform: { |
| 33 | + display_index: 15, |
| 34 | + abbr: "t", |
| 35 | + default: "./transform.js", |
| 36 | + help: "path to the transform file. Can be either a local path or url", |
| 37 | + metavar: "FILE", |
| 38 | + required: true, |
| 39 | + }, |
| 40 | + cpus: { |
| 41 | + display_index: 1, |
| 42 | + abbr: "c", |
| 43 | + help: "start at most N child processes to process source files", |
| 44 | + defaultHelp: "max(all - 1, 1)", |
| 45 | + metavar: "N", |
| 46 | + process: Number, |
| 47 | + }, |
| 48 | + verbose: { |
| 49 | + display_index: 16, |
| 50 | + abbr: "v", |
| 51 | + choices: [0, 1, 2], |
| 52 | + default: 0, |
| 53 | + help: "show more information about the transform process", |
| 54 | + metavar: "N", |
| 55 | + process: Number, |
| 56 | + }, |
| 57 | + dry: { |
| 58 | + display_index: 2, |
| 59 | + abbr: "d", |
| 60 | + flag: true, |
| 61 | + default: false, |
| 62 | + help: "dry run (no changes are made to files)", |
| 63 | + }, |
| 64 | + print: { |
| 65 | + display_index: 11, |
| 66 | + abbr: "p", |
| 67 | + flag: true, |
| 68 | + default: false, |
| 69 | + help: "print transformed files to stdout, useful for development", |
| 70 | + }, |
| 71 | + babel: { |
| 72 | + display_index: 0, |
| 73 | + flag: true, |
| 74 | + default: true, |
| 75 | + help: "apply babeljs to the transform file", |
| 76 | + }, |
| 77 | + extensions: { |
| 78 | + display_index: 3, |
| 79 | + default: "js", |
| 80 | + help: "transform files with these file extensions (comma separated list)", |
| 81 | + metavar: "EXT", |
| 82 | + }, |
| 83 | + ignorePattern: { |
| 84 | + display_index: 7, |
| 85 | + full: "ignore-pattern", |
| 86 | + list: true, |
| 87 | + help: "ignore files that match a provided glob expression", |
| 88 | + metavar: "GLOB", |
| 89 | + }, |
| 90 | + ignoreConfig: { |
| 91 | + display_index: 6, |
| 92 | + full: "ignore-config", |
| 93 | + list: true, |
| 94 | + help: "ignore files if they match patterns sourced from a configuration file (e.g. a .gitignore)", |
| 95 | + metavar: "FILE", |
| 96 | + }, |
| 97 | + gitignore: { |
| 98 | + display_index: 8, |
| 99 | + flag: true, |
| 100 | + default: false, |
| 101 | + help: "adds entries the current directory's .gitignore file", |
| 102 | + }, |
| 103 | + runInBand: { |
| 104 | + display_index: 12, |
| 105 | + flag: true, |
| 106 | + default: false, |
| 107 | + full: "run-in-band", |
| 108 | + help: "run serially in the current process", |
| 109 | + }, |
| 110 | + silent: { |
| 111 | + display_index: 13, |
| 112 | + abbr: "s", |
| 113 | + flag: true, |
| 114 | + default: false, |
| 115 | + help: "do not write to stdout or stderr", |
| 116 | + }, |
| 117 | + parser: { |
| 118 | + display_index: 9, |
| 119 | + choices: ["babel", "babylon", "flow", "ts", "tsx"], |
| 120 | + default: "babel", |
| 121 | + help: "the parser to use for parsing the source files", |
| 122 | + }, |
| 123 | + parserConfig: { |
| 124 | + display_index: 10, |
| 125 | + full: "parser-config", |
| 126 | + help: "path to a JSON file containing a custom parser configuration for flow or babylon", |
| 127 | + metavar: "FILE", |
| 128 | + process: (file: string) => JSON.parse(readFileSync(file).toString()), |
| 129 | + }, |
| 130 | + failOnError: { |
| 131 | + display_index: 4, |
| 132 | + flag: true, |
| 133 | + help: "Return a non-zero code when there are errors", |
| 134 | + full: "fail-on-error", |
| 135 | + default: false, |
| 136 | + }, |
| 137 | + version: { |
| 138 | + display_index: 17, |
| 139 | + help: "print version and exit", |
| 140 | + callback: function () { |
| 141 | + return [ |
| 142 | + `aws-sdk-js-codemod: ${version}`, |
| 143 | + `- jscodeshift: ${requirePackage("jscodeshift").version}`, |
| 144 | + `- recast: ${requirePackage("recast").version}\n`, |
| 145 | + ].join("\n"); |
| 146 | + }, |
| 147 | + }, |
| 148 | + stdin: { |
| 149 | + display_index: 14, |
| 150 | + help: "read file/directory list from stdin", |
| 151 | + flag: true, |
| 152 | + default: false, |
| 153 | + }, |
| 154 | + }); |
0 commit comments