Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
Use jschardet for file encoding detection (#99) (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Jul 28, 2019
1 parent ea3c8da commit 14411c3
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 53 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node_modules/
*.vsix
_build/
_packages/
_artifacts/
typings/*
!typings/customs/
task/*.js
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ If you want to use tokens in XML based configuration files to be replaced during
- replace tokens in your updated configuration file

## Release notes
**New in 3.2.0**
- Switch to [jschardet](https://github.com/aadsm/jschardet) for encoding detection when selecting `auto` in _File encoding_ ([#99](https://github.com/qetza/vsts-replacetokens-task/issues/99)).
- Switch to [azure-pipelines-task-lib](https://github.com/Microsoft/azure-pipelines-task-lib) v2.8.0.

**New in 3.1.0**
- Add _Verbosity_ parameter to allow detail logs without using `system.debug`.

Expand Down
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var del = require('del');
var merge = require('merge-stream');
var cp = require('child_process');

var _buildRoot = path.join(__dirname, '_build');
var _packagesRoot = path.join(__dirname, '_packages');
var _buildRoot = path.join(__dirname, '_artifacts', 'binaries');
var _packagesRoot = path.join(__dirname, '_artifacts', 'packages');

function errorHandler(err) {
process.exit(1);
Expand Down
45 changes: 25 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"typescript": "^2.9.2"
},
"dependencies": {
"azure-pipelines-task-lib": "^2.8.0",
"iconv-lite": "^0.4.15",
"vsts-task-lib": "^2.0.0"
"jschardet": "^2.1.0"
}
}
47 changes: 22 additions & 25 deletions task/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import tl = require('vsts-task-lib/task');
import trm = require('vsts-task-lib/toolrunner');
import path = require('path');
import tl = require('azure-pipelines-task-lib/task');
import fs = require('fs');
import iconv = require('iconv-lite');
import jschardet = require('jschardet');

const ENCODING_AUTO: string = 'auto';
const ENCODING_ASCII: string = 'ascii';
Expand Down Expand Up @@ -149,30 +148,28 @@ var mapEncoding = function (encoding: string): string {
}

var getEncoding = function (filePath: string): string {
let fd: number = fs.openSync(filePath, 'r');
let buffer: Buffer = fs.readFileSync(filePath, { flag: 'r' });
let charset: any = jschardet.detect(buffer);

try
switch (charset.encoding)
{
let bytes: Buffer = new Buffer(4);
fs.readSync(fd, bytes, 0, 4, 0);

let encoding: string = ENCODING_ASCII;
if (bytes[0] === 0x2b && bytes[1] === 0x2f && bytes[2] === 0x76 && (bytes[3] === 0x38 || bytes[3] === 0x39 || bytes[3] === 0x2b || bytes[3] === 0x2f))
encoding = ENCODING_UTF_7;
else if (bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf)
encoding = ENCODING_UTF_8
else if (bytes[0] === 0xfe && bytes[1] === 0xff)
encoding = ENCODING_UTF_16BE
else if (bytes[0] === 0xff && bytes[1] === 0xfe)
encoding = ENCODING_UTF_16LE
else
logger.debug('BOM no found: default to ascii.');

return encoding;
}
finally
{
fs.closeSync(fd);
case 'ascii':
return ENCODING_ASCII;

case 'UTF-8':
return ENCODING_UTF_8;

case 'UTF-16LE':
return ENCODING_UTF_16LE;

case 'UTF-16BE':
return ENCODING_UTF_16BE;

case 'windows-1252':
return ENCODING_WIN1252;

default:
return ENCODING_ASCII;
}
}

Expand Down
4 changes: 2 additions & 2 deletions task/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"author": "Guillaume Rouchon",
"version": {
"Major": 3,
"Minor": 1,
"Minor": 2,
"Patch": 0
},
"instanceNameFormat": "Replace tokens in $(targetFiles)",
"minimumAgentVersion": "2.105.0",
"groups": [
{
Expand Down Expand Up @@ -165,7 +166,6 @@
"helpMarkDown": "Characters in variable values to escape before replacing tokens."
}
],
"instanceNameFormat": "Replace tokens in $(targetFiles)",
"execution": {
"Node": {
"target": "index.js"
Expand Down
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 1,
"id": "replacetokens",
"name": "Replace Tokens",
"version": "3.1.0",
"version": "3.2.0",
"public": true,
"publisher": "qetza",
"targets": [
Expand Down

0 comments on commit 14411c3

Please sign in to comment.