https://github.com/jopemachine-playground/code-replacer
Replace codes line by line with regex for target files
https://github.com/jopemachine-playground/code-replacer
code-replace csv-columns csv-option regex-match
Last synced: 4 months ago
JSON representation
Replace codes line by line with regex for target files
- Host: GitHub
- URL: https://github.com/jopemachine-playground/code-replacer
- Owner: jopemachine-playground
- License: mit
- Created: 2020-09-15T12:50:44.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-16T12:47:35.000Z (almost 4 years ago)
- Last Synced: 2025-01-11T03:58:52.401Z (5 months ago)
- Topics: code-replace, csv-columns, csv-option, regex-match
- Language: TypeScript
- Homepage:
- Size: 1.39 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Code-replacer
[![NPM version][npm-image]][npm-url]
[](https://www.npmjs.com/package/code-replacer)
[](https://github.com/jopemachine/code-replacer/blob/master/LICENSE)
[](https://travis-ci.com/jopemachine/code-replacer)
[](https://codecov.io/gh/jopemachine/code-replacer/)
[](https://www.codefactor.io/repository/github/jopemachine/code-replacer)[npm-url]: https://npmjs.org/package/code-replacer
[npm-image]: https://img.shields.io/npm/v/code-replacer.svgReplace codes line by line with regex for target files
**Table of Contents**
- [1. How to install](#installation)
- [2. How to use](#how-to-use)
- [2.1 On Cli](#on-cli)
- [2.2 On Cli using inquirer](#on-cli-using-inquirer)
- [2.3 Using vscode plugin](#on-vscode)
- [3. Terms description](#terms-description)
- [4. Examples](#simple-example)
- [4.1 Example 1, using csv column key and template](#example-1)
- [4.2 Example 2, using only template](#example-2)
- [4.3 Example 3, using multiple csv column keys](#example-3)
- [4.4 Example 4, using multiple csv column keys with left reference keys](#example-4)
- [5. Tips](#tips)
- [6. Options](#options)
- [7. Bug Reporting](#bug-reporting)## Installation
* code-replacer
```
npm i -g code-replacer
```* [code-replacer-vscode-plugin](https://github.com/jopemachine/code-replacer-vscode-plugin)
If you like the idea of code-replacer, how about trying to use vscode plugin?
If it's annoying to type options on console every time, this plugin might help you.
## Development environment
Tested on `Windows 10`, `macos Catalina`
## How to use
### On cli
1. Write `csv` file to replace text or `template` to change your codes.
2. Specify options you want to apply.
3. Output files is `__replaced__.{original file name}`.
### On cli using inquirer
You can use `inquirer`'s cli menu to select options.
### On vscode
See [code-replacer-vscode-plugin](https://github.com/jopemachine/code-replacer-vscode-plugin).
## Terms description
To help you understand how to use this program, here is some description of terms.
1. **template**
`template` is a string indicating which value in the code is to be replaced with which value.
For example, `A->B` option replace *A* with *B*.
And for convenience, in this document, A is `templateLvalue` (string to be replaced), B is `templateRvalue` (string after replacing).
2. **rlist.csv**
* If you do not give the csv option, code-replacer uses the `rlist.csv` file **in src file's directory** as `csv` option.
* If you use the dir option, the `rlist_${fileName}.csv` file will be used as a csv option.
* If the csv file is not found, replace code with only the `template` value.
3. **${key}**
Above key is treated as `csv column key`.
key is replaced with csv file's column data.
Therefore, the key must be used with the `csv` option.
And `templateLvalue` must include this key because this program should know which value should be replaced with which value with this csv file.
See `Examples` for examples of use.
Note that the key can contain only **alphabetic and numeric characters.**
And if the csv columns does not have this "key" column, it is treated as a string matching.
4. **$[key]**
Above key is treated as `left reference key`.
In fact, this key is replaced with regexp's group key `([\d\w]+)`.
So, you can refer `templateLvalue`'s substring at `templateRvalue` using this like below example.
```
-t='require("$[key]")->import $[key] from "$[key]"'
```And along with the `no-escape` option, you can refer regexp's group key like below example.
```
-t='(?[0-9]{3})(?[0-9]{4})(?[0-9]{4})->$[first]-$[second]-$[third]'
```Note that the key can contain only `alphabetic` and `numeric` characters.
(And this applies for group keys of regexp)
## Simple example
### Example 1
Pass the path of the input file to the `csv` option if you need it.
For example, if you wanna change `Some message..` to `alert(i18n.t("some_msg"))` for support i18n (supporting multi language feature), you can do this.
```js
// Original code, Assume this file path is ./msgAlert.js...
alert("Some message..");
alert("Blah blah..");
...
```Below worksheet is the input file (`csv`).
Note that `source` column is Message string including double quotes and `value` column is corresponding string key.
And you need to forward some `template` value.
We assume this value is `i18n.t(${value})`.
In `template` option, `${value}` option means column data named `value`.
On each line in the source file (`msgAlert.js`), you can insert data in the csv column with the corresponding variable.
Then type the template as a form of `A->B`.
So the `template` value we need to forward is as follows.
```
"${source}"->i18n.t("${value}")
```So if you type below command into the console,
```
code-replacer --src='example/example_1/msgAlert.js' --csv='example/example_1/rlist.csv' --template='"${source}"->i18n.t("${value}")'
```Then you can get to below file.
```js
// This file name is __replaced__.msgAlert.js...
alert(i18n.t("some_msg"));
alert(i18n.t("blah_blah"));
...
```For more detailed instructions, see the topic `Options`.
### Example 2
In certain situations, key, value pairs may not be required to change the string.
For example, if you need to replace all of *require* below with *import* statements,
We assume in this, the argument of require is `$[key]`.
(You can name it `$[someKey1]` or `$[otherKey]` or other name (`Alphabet`, `Numeric` Available))
So, you can use this template. `require("$[key]")->import $[key] from "$[key]"`
```js
...
require("abc");
import def from "def";
require("ghi");
...
```Then, the command is as follows.
```
code-replacer --src='./example/example_2/index.js' --template='require("$[key]")->import $[key] from "$[key]"'
```And you can get below file.
```js
...
import abc from "abc";
import def from "def";
import ghi from "ghi";
...
```### Example 3
Multiple `csv column key` are available in the following ways.
```
'
code-replacer --src='example/example_3/index.js' --csv='example/example_3/rlist.csv' --template='${source}${index}->
```### Example 4
You can use the `csv column key` with the `left reference key`.
```
code-replacer --src='example/example_4/index.js' --csv='example/example_4/rlist.csv' --template='$[key1] ${source}${index} $[key2]->$[key2] ${index}${source} $[key1]'
```## Tips
1. If there are more than one matching key, (which contain other key in the `rlist.csv` (e.g. test, tester)),
The **longest** key is selected (replaced) basically.
If you don't want these matching, give `conf` option and skip the longest one, then you can choose next candidate manually (test).2. Left side of `template` are treated as *regular expression*. But special characters are escaped automatically.
So, no separate escape processing is required. If you want to use raw `RegExp`, you can use `no-escape` option.
you also can use `leftReference key` and `csv column key` with this option, too.3. In `template` option, The `template` value is treated as a form of `A->B`
If A should contains `->` (arrow signature), you can escape that `->` by `\->`.4. You can apply the `excludeReg` option as form of regular expression, to exclude lines that you don't want to replace, such as comments.
(e.g. `x=(.*//.*)|(.*