{"id":24112577,"url":"https://github.com/jopemachine-playground/code-replacer","last_synced_at":"2025-02-28T08:44:21.686Z","repository":{"id":47033500,"uuid":"295726830","full_name":"jopemachine-playground/code-replacer","owner":"jopemachine-playground","description":"Replace codes line by line with regex for target files","archived":false,"fork":false,"pushed_at":"2021-09-16T12:47:35.000Z","size":1460,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-11T03:58:52.401Z","etag":null,"topics":["code-replace","csv-columns","csv-option","regex-match"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jopemachine-playground.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-15T12:50:44.000Z","updated_at":"2023-03-17T16:48:59.000Z","dependencies_parsed_at":"2022-09-02T22:51:19.152Z","dependency_job_id":null,"html_url":"https://github.com/jopemachine-playground/code-replacer","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jopemachine-playground%2Fcode-replacer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jopemachine-playground%2Fcode-replacer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jopemachine-playground%2Fcode-replacer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jopemachine-playground%2Fcode-replacer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jopemachine-playground","download_url":"https://codeload.github.com/jopemachine-playground/code-replacer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241125423,"owners_count":19913845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-replace","csv-columns","csv-option","regex-match"],"created_at":"2025-01-11T03:59:05.144Z","updated_at":"2025-02-28T08:44:21.660Z","avatar_url":"https://github.com/jopemachine-playground.png","language":"TypeScript","readme":"# Code-replacer\n\n[![NPM version][npm-image]][npm-url]\n[![npm download total](https://img.shields.io/npm/dt/code-replacer)](https://www.npmjs.com/package/code-replacer)\n[![license](https://img.shields.io/github/license/jopemachine/code-replacer.svg)](https://github.com/jopemachine/code-replacer/blob/master/LICENSE)\n[![Build Status](https://travis-ci.com/jopemachine/code-replacer.svg?token=81JsWap8614c381MskPH\u0026branch=master)](https://travis-ci.com/jopemachine/code-replacer)\n[![codecov](https://codecov.io/gh/jopemachine/code-replacer/branch/master/graph/badge.svg?token=AM0XLWG78T)](https://codecov.io/gh/jopemachine/code-replacer/)\n[![CodeFactor](https://www.codefactor.io/repository/github/jopemachine/code-replacer/badge?s=ac4f10a333a3ba472ee3e87b02f9a5e00c6aeb29)](https://www.codefactor.io/repository/github/jopemachine/code-replacer)\n\n\n[npm-url]: https://npmjs.org/package/code-replacer\n[npm-image]: https://img.shields.io/npm/v/code-replacer.svg\n\nReplace codes line by line with regex for target files\n\n**Table of Contents**\n\n- [1. How to install](#installation)\n- [2. How to use](#how-to-use)\n    - [2.1 On Cli](#on-cli)\n    - [2.2 On Cli using inquirer](#on-cli-using-inquirer)\n    - [2.3 Using vscode plugin](#on-vscode)\n- [3. Terms description](#terms-description)\n- [4. Examples](#simple-example)\n    - [4.1 Example 1, using csv column key and template](#example-1)\n    - [4.2 Example 2, using only template](#example-2)\n    - [4.3 Example 3, using multiple csv column keys](#example-3)\n    - [4.4 Example 4, using multiple csv column keys with left reference keys](#example-4)\n- [5. Tips](#tips)\n- [6. Options](#options)\n- [7. Bug Reporting](#bug-reporting)\n\n## Installation\n\n* code-replacer\n\n```\nnpm i -g code-replacer\n```\n\n* [code-replacer-vscode-plugin](https://github.com/jopemachine/code-replacer-vscode-plugin)\n\nIf you like the idea of code-replacer, how about trying to use vscode plugin?\n\nIf it's annoying to type options on console every time, this plugin might help you.\n\n## Development environment\n\nTested on `Windows 10`, `macos Catalina`\n\n## How to use\n\n### On cli\n\n1. Write `csv` file to replace text or `template` to change your codes.\n\n2. Specify options you want to apply.\n\n3. Output files is `__replaced__.{original file name}`.\n\n### On cli using inquirer\n\nYou can use `inquirer`'s cli menu to select options.\n\n\u003cimg src=\"./image/inquirer.png\" /\u003e\n\n\n### On vscode\n\nSee [code-replacer-vscode-plugin](https://github.com/jopemachine/code-replacer-vscode-plugin).\n\n## Terms description\n\nTo help you understand how to use this program, here is some description of terms.\n\n1. **template**\n\n`template` is a string indicating which value in the code is to be replaced with which value.\n\nFor example, `A-\u003eB` option replace *A* with *B*.\n\nAnd for convenience, in this document, A is `templateLvalue` (string to be replaced), B is `templateRvalue` (string after replacing).\n\n2. **rlist.csv**\n\n* If you do not give the csv option, code-replacer uses the `rlist.csv` file **in src file's directory** as `csv` option.\n\n* If you use the dir option, the `rlist_${fileName}.csv` file will be used as a csv option.\n\n* If the csv file is not found, replace code with only the `template` value.\n\n3. **${key}**\n\nAbove key is treated as `csv column key`.\n\nkey is replaced with csv file's column data.\n\nTherefore, the key must be used with the `csv` option.\n\nAnd `templateLvalue` must include this key because this program should know which value should be replaced with which value with this csv file.\n\nSee `Examples` for examples of use.\n\nNote that the key can contain only **alphabetic and numeric characters.**\n\nAnd if the csv columns does not have this \"key\" column, it is treated as a string matching.\n\n4. **$[key]**\n\nAbove key is treated as `left reference key`.\n\nIn fact, this key is replaced with regexp's group key `([\\d\\w]+)`.\n\nSo, you can refer `templateLvalue`'s substring at `templateRvalue` using this like below example.\n\n```\n-t='require(\"$[key]\")-\u003eimport $[key] from \"$[key]\"'\n```\n\nAnd along with the `no-escape` option, you can refer regexp's group key like below example.\n\n```\n-t='(?\u003cfirst\u003e[0-9]{3})(?\u003csecond\u003e[0-9]{4})(?\u003cthird\u003e[0-9]{4})-\u003e$[first]-$[second]-$[third]'\n```\n\nNote that the key can contain only `alphabetic` and `numeric` characters.\n\n(And this applies for group keys of regexp)\n\n\n\n\n## Simple example\n\n### Example 1\n\nPass the path of the input file to the `csv` option if you need it.\n\nFor example, if you wanna change `Some message..` to `alert(i18n.t(\"some_msg\"))` for support i18n (supporting multi language feature), you can do this.\n\n```js\n// Original code, Assume this file path is ./msgAlert.js\n\n...\nalert(\"Some message..\");\nalert(\"Blah blah..\");\n...\n```\n\nBelow worksheet is the input file (`csv`).\n\nNote that `source` column is Message string including double quotes and `value` column is corresponding string key.\n\n\u003cimg src=\"./image/csv.png\" /\u003e\n\nAnd you need to forward some `template` value.\n\nWe assume this value is `i18n.t(${value})`.\n\nIn `template` option, `${value}` option means column data named `value`.\n\nOn each line in the source file (`msgAlert.js`), you can insert data in the csv column with the corresponding variable.\n\nThen type the template as a form of `A-\u003eB`.\n\nSo the `template` value we need to forward is as follows.\n\n```\n\"${source}\"-\u003ei18n.t(\"${value}\")\n```\n\nSo if you type below command into the console,\n\n```\ncode-replacer --src='example/example_1/msgAlert.js' --csv='example/example_1/rlist.csv' --template='\"${source}\"-\u003ei18n.t(\"${value}\")'\n```\n\nThen you can get to below file.\n\n```js\n// This file name is __replaced__.msgAlert.js\n\n...\nalert(i18n.t(\"some_msg\"));\nalert(i18n.t(\"blah_blah\"));\n...\n```\n\nFor more detailed instructions, see the topic `Options`.\n\n### Example 2\n\nIn certain situations, key, value pairs may not be required to change the string.\n\nFor example, if you need to replace all of *require* below with *import* statements,\n\nWe assume in this, the argument of require is `$[key]`. \n\n(You can name it `$[someKey1]` or `$[otherKey]` or other name (`Alphabet`, `Numeric` Available))\n\nSo, you can use this template. `require(\"$[key]\")-\u003eimport $[key] from \"$[key]\"`\n\n```js\n...\nrequire(\"abc\");\nimport def from \"def\";\nrequire(\"ghi\");\n...\n```\n\nThen, the command is as follows.\n\n```\ncode-replacer --src='./example/example_2/index.js' --template='require(\"$[key]\")-\u003eimport $[key] from \"$[key]\"'\n```\n\nAnd you can get below file.\n\n```js\n...\nimport abc from \"abc\";\nimport def from \"def\";\nimport ghi from \"ghi\";\n...\n```\n\n### Example 3\n\nMultiple `csv column key` are available in the following ways.\n\n```\ncode-replacer --src='example/example_3/index.js' --csv='example/example_3/rlist.csv' --template='${source}${index}-\u003e\u003cdiv id=\"${id}\" class=\"${class}\" /\u003e'\n```\n\n### Example 4\n\nYou can use the `csv column key` with the `left reference key`.\n\n```\ncode-replacer --src='example/example_4/index.js' --csv='example/example_4/rlist.csv' --template='$[key1] ${source}${index} $[key2]-\u003e$[key2] ${index}${source} $[key1]'\n```\n\n## Tips\n\n1. If there are more than one matching key, (which contain other key in the `rlist.csv` (e.g. test, tester)), \nThe **longest** key is selected (replaced) basically. \nIf you don't want these matching, give `conf` option and skip the longest one, then you can choose next candidate manually (test).\n\n2. Left side of `template` are treated as *regular expression*. But special characters are escaped automatically.\nSo, no separate escape processing is required. If you want to use raw `RegExp`, you can use `no-escape` option.\nyou also can use `leftReference key` and `csv column key` with this option, too.\n\n3. In `template` option, The `template` value is treated as a form of `A-\u003eB`\nIf A should contains `-\u003e` (arrow signature), you can escape that `-\u003e` by `\\-\u003e`.\n\n4. You can apply the `excludeReg` option as form of regular expression, to exclude lines that you don't want to replace, such as comments.\n(e.g. `x=(.*//.*)|(.*\u003c!--.*)|(.*\\/\\*.*)`)\n\n5. If you feel inconvenient to enter template value or option value each time, you can set some values you want as default by below way.\n`code-replacer set -t={some_value} -x -v --src={some_value} ...`\nIf these argument is not entered next time, these values are used as a default.\n\nAnd you can also check the default values with the command below.\n\n```\ncode-replacer default\n```\n\nAnd this vscode's plugin might helps.\n\n[code-replacer-vscode-plugin](https://github.com/jopemachine/code-replacer-vscode-plugin).\n\n## Options\n\n#### --src, -s (required)\ntype: `string`\n\nSpecify source code file.\nwhen `src` and `dir` are given,\ntarget the files corresponding to the name in the target directory.\n(no need to specify `ext` separately)\n\nInstead of using the `src` option, you can pass the value to the first argument.\n\n#### --template, --t (required)\ntype: `string`\n\nSpecify template.\nSee example for details.\n\n#### --conf, -c\ntype: `boolean`\n\nCheck the string values that you want to replace on each line.\n\ndefault is 'false'\n\n\u003cimg src=\"./image/conf.png\" /\u003e\n\n#### --verbose, -v\ntype: `boolean`\n\nOutputs information about replaced lines on the console.\ndefault is 'false'\n\n\u003cimg src=\"./image/verbose.png\" /\u003e\n\n#### --print, -p\ntype: `boolean`\n\nOutputs simple information about replaced lines on the console.\ndefault is 'true'\n\n\u003cimg src=\"./image/print.png\" /\u003e\n\n#### --dir, -d\ntype: `string`\n\nSpecify source file's directory\nFiles beginning with `__replaced__.` are excluded from the source files.\n\n#### --ext, -e\ntype: `string`\n\nSpecify source file's extension.\n(Only use this with `dir` option to target multiple files at once)\n\n#### --csv\ntype: `string`\n\nSpecify csv file,\nDefault value is `./rlist.csv`\n\nAnd when you specify `dir` option, name `./rlist_{fileName}.csv`, to apply different rlist files per file.\n\nInstead of using the `csv` option, you can pass the value to the second argument.\n\n#### --dst\ntype: `string`\n\nSpecify the name of the output file. \ndefault value is `__replaced__.{originalFileName}`.\n\n#### --once, -o\ntype: `boolean`\n\nEven if there are multiple substitution values in a line,\nthey are replaced only once at first.\n\ndefault is 'false'\n\n#### --startLine, -sl\ntype: `string`\n\nApply replacing from that line.\n\n(startLine will be replaced)\n\n#### --endLine, -el\ntype: `string`\n\nSpecify end line.\n\n(endLine will not be replaced)\n\n#### --overwrite, -w\ntype: `boolean`\n\nOverwrite the source file.\n\nIt works the same as when you give dst the same value as src.\n\ndefault is 'false'\n\n#### --excludeReg, -x\ntype: `boolean`\n\nSpecify the regular expression of the line to be excluded from the replace.\n\nLines corresponding to the regular expression in regexp will not be replaced.\n\nFor example, comments might be excluded from replacement.\n\n## Bug reporting\n\nThis program still appears to contain lots of bugs.\n\nBug reports are always welcome.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjopemachine-playground%2Fcode-replacer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjopemachine-playground%2Fcode-replacer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjopemachine-playground%2Fcode-replacer/lists"}