1.0.1 • Published 2 years ago

infactor v1.0.1

Weekly downloads
-
License
APACHE-2.0
Repository
-
Last release
2 years ago

** Rationale

Ever read a guide that required you to perform manual code changes to multiple source files in an unfamiliar code base? Ever seen people using sed, awk or perl for the same purpose?

Using tools like sed, awk and perl for performing minor changes may work but its hard for users to understand and geeks to maintain. Doing manual changes is error prone and is not a reproducible user experience.

This tool is an experiment to see if performing minor code changes using the CLI can improve the user experience

** Features

** Example usage

* Get Line ** Usage #+BEGIN_EXAMPLE Usage: infactor get-line options

Get the line or number of the last (or optionally the first) line that matches the expression

Arguments: file The file to add the import to expression The regular expression to use for matching

Options: --text Flag to display the actual line instead of the number --first Flag to return the first line matching --in-class Search inside the specified class --in-method Search inside the specified function --in-function Search inside the specified function -h, --help #+END_EXAMPLE ** Examples *** Get line The following code will get the line number of the last line matching the regular expression ^[ ]conosole.

 #+BEGIN_SRC sh
   infactor.js get-line sample.js "^[ ]*console"
 #+END_SRC

 The output should be something like: `34` close to the end of the file.

* Get Line in class

  To limit the scode of the search inside a specific function:

  #+BEGIN_SRC sh
    infactor.js get-line sample.js "^[ ]*console" --in-class Op
  #+END_SRC

  The output should be `12` pointing the logging statemtent found inside the `Op` class.

* Get Line in method

  #+BEGIN_SRC sh
    infactor.js get-line sample.js "^[ ]*console" --in-class Op --in-method exec
  #+END_SRC

  The output should be `12` pointing the logging statemtent found inside the `Op` class (same as above).

* Get Line in function

  #+BEGIN_SRC sh
    infactor.js get-line sample.js "^[ ]*console" --in-function demo
  #+END_SRC

  In this case the resut should be `-1` as `demo` function contains a logging statement.

* Add Line ** Usage

 #+BEGIN_EXAMPLE

Usage: infactor add-line options

Add a line at the bottom of the block

Arguments: file The file to add the import to code The code to add

Options: --out Write result to stdout instead of saving to the file --top Flag to enable adding the code at the top of the block --before The expression to match the line before which the code will be added --after The expression to match the line after which the code will be added --in-class Search inside the specified class --in-method Search inside the specified method --in-function Search inside the specified function -h, --help display help for command #+END_EXAMPLE ** Example *** Adding code

 #+BEGIN_SRC sh
   infactor.js add-line sample.js "    console.log('Now demo is also logging!');" --in-function demo
 #+END_SRC


 #+BEGIN_SRC js
   function demo () {
       var a = 2;
       var b = 3
       new Op(a, b, sum).exec();
       new Op(a, b, mul).exec();
       console.log('Now demo is also logging!');
   }
 #+END_SRC

* Adding code to the top of the function

  Using the `--top` flag it's now possible to add code to the top of a function, method, class etc.

  #+BEGIN_SRC sh
    infactor.js add-line sample.js "    console.log('On top!');" --in-function demo --top
  #+END_SRC


  #+BEGIN_SRC js
    function demo () {
        console.log('On top!');
        var a = 2;
        var b = 3
        new Op(a, b, sum).exec();
        new Op(a, b, mul).exec();
        console.log('Now demo is also logging!');
    }
  #+END_SRC

* Remove Line ** Usage

 #+BEGIN_EXAMPLE

Usage: infactor remove-line options

Remove the line of the last (or optionally the first) line that matches the expression

Arguments: file The file to add the import to expression The regular expression to use for matching

Options: --out Write result to stdout instead of saving to the file --first Flag to return the first line matching --in-class Search inside the specified class --in-method Search inside the specified method --in-function Search inside the specified function -h, --help display help for command #+END_EXAMPLE **** Example

* Removing a line

  Given the following code contained in the [[./sample.js]] file:

 #+BEGIN_SRC js
   function demo () {
       var a = 2;
       var b = 3;
       var array = [1, 2, 3, 4, 5];
       new Op(a, b, sum).exec();
       new Op(a, b, mul).exec();
   }
 #+END_SRC

 We shall remove the uneeded array declaration using:

 #+BEGIN_SRC sh
   infactor.js remove-line sample.js "^[ ]*var array" --in-function demo --out
 #+END_SRC

 The output should be something like:

 #+BEGIN_SRC js
   function demo () {
       var a = 2;
       var b = 3;
       new Op(a, b, sum).exec();
       new Op(a, b, mul).exec();
   }
 #+END_SRC

* Set value To set the value of a variable you can use the set subcommand: ** Usage #+BEGIN_EXAMPLE Usage: infactor set options

Set the value of a variable

Arguments: file The file to add the import to variable The variable to set value The value to set

Options: --out Write result to stdout instead of saving to the file --in-class Search inside the specified class --in-method Search inside the specified method --in-function Search inside the specified function -h, --help display help for command #+END_EXAMPLE ** Examples *** Setting the value of a var in a specific class

  #+BEGIN_SRC sh
  infactor set sample.js a "\"bar\"" --in-class Op
  #+END_SRC

* Setting the value of a var in a specific function

  #+BEGIN_SRC sh
  infactor set sample.js a "\"bar\"" --in-function demo
  #+END_SRC

* Append value To append the value to an array variable you can use the append subcommand: ** Usage #+BEGIN_EXAMPLE Usage: infactor append options

Append the value to a variable (e.g. array)

Arguments: file The file to add the import to variable The variable to append to value The value to append

Options: --out Write result to stdout instead of saving to the file --in-class Search inside the specified class --in-method Search inside the specified method --in-function Search inside the specified function --in-element Search inside the specified element (e.g. jsx etc) -h, --help display help for command #+END_EXAMPLE

** Examples *** Appending the value to an array

  #+BEGIN_SRC sh
  infactor set sample.js array 7 --in-class Op
  #+END_SRC