diff --git a/packages/mobx-undecorate/__tests__/cli.spec.tsx b/packages/mobx-undecorate/__tests__/cli.spec.tsx index 826bc737e4..cd16f3455e 100644 --- a/packages/mobx-undecorate/__tests__/cli.spec.tsx +++ b/packages/mobx-undecorate/__tests__/cli.spec.tsx @@ -1,20 +1,21 @@ import { execSync } from "child_process" import { join, dirname } from "path" -import { readFileSync, writeFileSync, mkdirSync } from "fs" +import { readFileSync, writeFileSync, mkdirSync, accessSync } from "fs" // TODO accessync const dedent = require("dedent-js") - -test("run cli #2506 #3142", () => { +// TODO +test.only("run cli #2506 #3142", () => { // #3142 - the white space must be in cwd - const cwd = join(__dirname, "fixtures", "some path"); + const cwd = join(__dirname, "fixtures", "some path") const testFile = join(cwd, "some file.tsx") const baseContent = dedent(`import { observable } from "mobx"; class Test { @observable x = 1; } - `) + `) mkdirSync(dirname(testFile), { recursive: true }) writeFileSync(testFile, baseContent) - execSync("node ../../../cli.js", { cwd }) + execSync("node ../../../cli.js", { cwd, stdio: "inherit" }) // TODO stdio + console.log("### some file.tsx", accessSync(testFile)) // TODO del; expect(readFileSync(testFile, "utf8")).toMatchInlineSnapshot(` "import { observable, makeObservable } from \\"mobx\\"; class Test { diff --git a/packages/mobx-undecorate/cli.js b/packages/mobx-undecorate/cli.js index 710d7e1442..6fcd671a81 100755 --- a/packages/mobx-undecorate/cli.js +++ b/packages/mobx-undecorate/cli.js @@ -2,54 +2,49 @@ const path = require("path") const cp = require("child_process") const fs = require("fs") - +console.log("### cli.js") // TODO /** * @example getCommandPath("jscodeshift") * //-> C:\Users\name\AppData\Local\npm-cache\_npx\234242somehash\node_modules\.bin\jscodeshift.cmd * //-> linux/path/npm-cache/_npx/234242somehash/node_modules/.bin/jscodeshift */ const getCommandPath = binCommand => { - const cmd = process.platform === 'win32' ? `${binCommand}.cmd` : binCommand; - /** - * Normally, for executing bins from a project you would use path.resolve(__dirname, 'node_modules', '.bin', cmd) - * but NPX is wierd. You might think running npx mobx-undecorate installs mobx-undecorate, BUT IT DOESNT. - * It creates a randomly hashed folder with an unnamed package.json with mobx-undecorate as its only dependency. - * This causes a flattening of all peers in the same node_modules dir. - * They probably did it this way to dedupe nested deps. - * - * This following logic checks for both folder structure and platform bin file extension. - */ - let COMMAND_PATH_SIBLING = path.resolve(__dirname, '..', '.bin', cmd) - let COMMAND_PATH_NESTED = path.resolve(__dirname, 'node_modules', '.bin', cmd) - - var COMMAND_PATH - - if (fs.existsSync(COMMAND_PATH_NESTED)) { - COMMAND_PATH = COMMAND_PATH_NESTED - } - else if (fs.existsSync(COMMAND_PATH_SIBLING)) { - COMMAND_PATH = COMMAND_PATH_SIBLING - } - else { - throw new Error("cannot find jscodeshift path") - process.exit(0) - } - return COMMAND_PATH - + const cmd = process.platform === "win32" ? `${binCommand}.cmd` : binCommand + /** + * Normally, for executing bins from a project you would use path.resolve(__dirname, 'node_modules', '.bin', cmd) + * but NPX is wierd. You might think running npx mobx-undecorate installs mobx-undecorate, BUT IT DOESNT. + * It creates a randomly hashed folder with an unnamed package.json with mobx-undecorate as its only dependency. + * This causes a flattening of all peers in the same node_modules dir. + * They probably did it this way to dedupe nested deps. + * + * This following logic checks for both folder structure and platform bin file extension. + */ + let COMMAND_PATH_SIBLING = path.resolve(__dirname, "..", ".bin", cmd) + let COMMAND_PATH_NESTED = path.resolve(__dirname, "node_modules", ".bin", cmd) + + var COMMAND_PATH + + if (fs.existsSync(COMMAND_PATH_NESTED)) { + COMMAND_PATH = COMMAND_PATH_NESTED + } else if (fs.existsSync(COMMAND_PATH_SIBLING)) { + COMMAND_PATH = COMMAND_PATH_SIBLING + } else { + throw new Error("cannot find jscodeshift path") + process.exit(0) + } + return COMMAND_PATH } const spawnBin = (binCommand, args) => { - return cp.spawn(getCommandPath(binCommand), args, { - cwd: path.resolve(__dirname), - stdio: 'inherit', - shell: true - }) - + return cp.spawn(getCommandPath(binCommand), args, { + cwd: path.resolve(__dirname), + stdio: "inherit", + shell: true + }) } - if (process.argv.includes("--help")) { - console.log(`[MOBX-UNDECORATE]: + console.log(`[MOBX-UNDECORATE]: If experiencing problems, you may also install jscodeshift and mobx-undecorate locally and run npx jscodeshift -t ./node_modules/mobx-undecorate/src/undecorate.ts --extensions=js,jsx,ts,tsx @@ -57,62 +52,53 @@ if (process.argv.includes("--help")) { `) } - function interpret_cli_args() { + //first 2 args of argv are the node.exe path and the path of this file. + var USER_ARGS = process.argv.slice(2) - //first 2 args of argv are the node.exe path and the path of this file. - var USER_ARGS = process.argv.slice(2) - - /** - * find args that dont include a "=" and set the input to the next index value in process.argv. - * Gotta do this because process.argv is delimited by spaces, so --dir src is actually 2 separate args - * so if an arg starts with -- and doesn't include a "=" - * we can just search for the arg by its --name and add 1 to the index position for lookup - * This will return -1 if nothing is found - */ - - let arg_without_equal = USER_ARGS - .slice() - .filter(v => !v.includes("=")) - .findIndex(kwarg => (kwarg.includes("--dir") || kwarg.includes("--path"))) - ; + /** + * find args that dont include a "=" and set the input to the next index value in process.argv. + * Gotta do this because process.argv is delimited by spaces, so --dir src is actually 2 separate args + * so if an arg starts with -- and doesn't include a "=" + * we can just search for the arg by its --name and add 1 to the index position for lookup + * This will return -1 if nothing is found + */ - var arg_with_equal = USER_ARGS - .slice() - .find(v => (v.includes("--dir=") || v.includes("--path="))) + let arg_without_equal = USER_ARGS.slice() + .filter(v => !v.includes("=")) + .findIndex(kwarg => kwarg.includes("--dir") || kwarg.includes("--path")) + var arg_with_equal = USER_ARGS.slice().find(v => v.includes("--dir=") || v.includes("--path=")) + //use cwd as default, but will override it with user args if they exist for --dir or --path + var PARSED_INPUT = "" - //use cwd as default, but will override it with user args if they exist for --dir or --path - var PARSED_INPUT = "" + let is_arg_directory_only = + process.argv[2] && fs.existsSync(path.resolve(process.cwd(), process.argv[2] || "")) - let is_arg_directory_only = process.argv[2] && fs.existsSync(path.resolve(process.cwd(), (process.argv[2] || ""))) + if (is_arg_directory_only) { + PARSED_INPUT = process.argv[2] + } - if (is_arg_directory_only) { - PARSED_INPUT = process.argv[2] - } + if (arg_without_equal > -1) { + PARSED_INPUT = USER_ARGS[arg_without_equal + 1] + } - if (arg_without_equal > -1) { - PARSED_INPUT = USER_ARGS[arg_without_equal + 1] - } - - if (arg_with_equal) { - PARSED_INPUT = arg_with_equal.split("=")[1] - } - - return PARSED_INPUT + if (arg_with_equal) { + PARSED_INPUT = arg_with_equal.split("=")[1] + } + return PARSED_INPUT } - +console.log("### undecorate.ts", fs.accessSync(path.join(__dirname, "src", "undecorate.ts"))) // TODO del +console.log("### args", fs.accessSync(path.join(process.cwd(), interpret_cli_args()))) // TODO del; spawnBin("jscodeshift", [ - "--extensions=js,jsx,ts,tsx", - ...process.argv.filter(arg => arg.startsWith("--")), - "-t", `"${path.join(__dirname, "src", "undecorate.ts")}"`, - - //this is arg to tell jscodeshift the dir to transform or fallback to process.cwd() - //originally just hard coded to process.cwd() - `"${path.join(process.cwd(), interpret_cli_args())}"` - -]); - - + "--extensions=js,jsx,ts,tsx", + ...process.argv.filter(arg => arg.startsWith("--")), + "-t", + `"${path.join(__dirname, "src", "undecorate.ts")}"`, + + //this is arg to tell jscodeshift the dir to transform or fallback to process.cwd() + //originally just hard coded to process.cwd() + `"${path.join(process.cwd(), interpret_cli_args())}"` +])