Skip to content

Commit

Permalink
Merge 11576d7 into ebb3c7d
Browse files Browse the repository at this point in the history
  • Loading branch information
akaguny committed Sep 24, 2020
2 parents ebb3c7d + 11576d7 commit ac32a79
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 14 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,23 @@ const options = {
};
```

#### Supported encodings

* Node.js Native encodings: utf8, ucs2 / utf16le, ascii, binary, base64, hex
* Unicode: UTF-16BE, UTF-16 (with BOM)
* Single-byte:
* Windows codepages: 874, 1250-1258 (aliases: cpXXX, winXXX, windowsXXX)
* ISO codepages: ISO-8859-1 - ISO-8859-16
* IBM codepages: 437, 737, 775, 808, 850, 852, 855-858, 860-866, 869, 922, 1046, 1124, 1125, 1129, 1133, 1161-1163 (aliases cpXXX, ibmXXX)
* Mac codepages: maccroatian, maccyrillic, macgreek, maciceland, macroman, macromania, macthai, macturkish, macukraine, maccenteuro, macintosh
* KOI8 codepages: koi8-r, koi8-u, koi8-ru, koi8-t
* Miscellaneous: armscii8, rk1048, tcvn, georgianacademy, georgianps, pt154, viscii, iso646cn, iso646jp, hproman8, tis620
* Multi-byte:
* Japanese: Shift_JIS, Windows-31j, Windows932, EUC-JP
* Chinese: GB2312, GBK, GB18030, Windows936, EUC-CN
* Korean: KS_C_5601, Windows949, EUC-KR
* Taiwan/Hong Kong: Big5, Big5-HKSCS, Windows950

### Dry run
To do a dry run without actually making replacements, for testing purposes. Defaults to `false`.

Expand Down
10 changes: 5 additions & 5 deletions lib/helpers/parse-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const iconv = require('iconv-lite');

/**
* Defaults
*/
Expand Down Expand Up @@ -43,6 +45,9 @@ module.exports = function parseConfig(config) {
if (typeof to === 'undefined') {
throw new Error('Must specify a replacement (can be blank string)');
}
if (encoding in config && !iconv.encodingExists(encoding)) {
throw new Error(`Encoding: ${encoding} not supported yet`);
}

//Ensure arrays
if (!Array.isArray(files)) {
Expand All @@ -57,11 +62,6 @@ module.exports = function parseConfig(config) {
}
}

//Use default encoding if invalid
if (typeof encoding !== 'string' || encoding === '') {
config.encoding = 'utf-8';
}

//Merge config with defaults
return Object.assign({}, defaults, config);
};
9 changes: 6 additions & 3 deletions lib/helpers/replace-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Dependencies
*/
const fs = require('fs');
const iconv = require('iconv-lite');
const makeReplacements = require('./make-replacements');

/**
Expand All @@ -16,15 +17,17 @@ module.exports = function replaceAsync(file, from, to, config) {

//Wrap in promise
return new Promise((resolve, reject) => {
fs.readFile(file, encoding, (error, contents) => {
fs.readFile(file, (error, contents) => {
//istanbul ignore if
if (error) {
return reject(error);
}

const contentsEncoded = iconv.decode(contents, encoding);

//Make replacements
const [result, newContents] = makeReplacements(
contents, from, to, file, countMatches
contentsEncoded, from, to, file, countMatches,
);

//Not changed or dry run?
Expand All @@ -33,7 +36,7 @@ module.exports = function replaceAsync(file, from, to, config) {
}

//Write to file
fs.writeFile(file, newContents, encoding, error => {
fs.writeFile(file, iconv.encode(newContents, encoding), error => {
//istanbul ignore if
if (error) {
return reject(error);
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@
"dependencies": {
"chalk": "^4.0.0",
"glob": "^7.1.6",
"iconv-lite": "^0.6.2",
"yargs": "^15.3.1"
},
"devDependencies": {
"@babel/core": "^7.7.7",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.9.6",
"@babel/register": "^7.9.0",
"babel-plugin-istanbul": "^6.0.0",
"bluebird": "^3.7.2",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"eslint": "^7.9.0",
"mocha": "^7.1.2",
"nyc": "^15.0.1"
},
Expand Down

0 comments on commit ac32a79

Please sign in to comment.