echolist-csv2hpt
Advanced tools
Comparing version
136
api.js
@@ -9,3 +9,4 @@ var fs = require('fs'); | ||
var readFileOrDie = function(filename, encoding, rusMode){ | ||
// tribute to the “R.O.D” 2001—2002 anime OVA | ||
var readOrDie = function(filename, encoding, rusMode){ | ||
try { | ||
@@ -25,3 +26,3 @@ return fs.readFileSync(filename, {encoding: encoding}); | ||
var writeFileOrDie = function(filename, content, encoding, rusMode){ | ||
var writeOrDie = function(filename, content, encoding, rusMode){ | ||
try { | ||
@@ -41,37 +42,55 @@ fs.writeFileSync(filename, content, {encoding: encoding}); | ||
module.exports = function(filenameCSV, filenameHPT, options){ | ||
filenameCSV = path.resolve(__dirname, filenameCSV); | ||
var getDescriptionElementFromContentCSV = function(contentCSV, lcEchotag){ | ||
return contentCSV.map(function(fileCSV){ | ||
var foundElement = fileCSV.find(function(elementCSV){ | ||
return elementCSV.lcEchotag === lcEchotag; | ||
}); | ||
if( typeof foundElement === 'undefined' ) return null; | ||
return foundElement; | ||
}).filter(function(nextElement){ | ||
return nextElement !== null; | ||
})[0]; | ||
}; | ||
module.exports = function(filenamesCSV, filenameHPT, options){ | ||
filenamesCSV = filenamesCSV.map(function(nextFilename){ | ||
return path.resolve(__dirname, nextFilename); | ||
}); | ||
filenameHPT = path.resolve(__dirname, filenameHPT); | ||
var contentCSV = readFileOrDie( | ||
filenameCSV, options.inputEncoding, options.rusMode | ||
).split(/(?:\r|\n)+/).map(function(nextLine){ | ||
if( nextLine.length < 1 ) return null; // empty line | ||
if( nextLine.indexOf(';') === 0 ) return null; // comment line | ||
var contentCSV = filenamesCSV.map(function(nextFilename){ | ||
return readOrDie( | ||
nextFilename, options.inputEncoding, options.rusMode | ||
).split(/(?:\r|\n)+/).map(function(nextLine){ | ||
if( nextLine.length < 1 ) return null; // empty line | ||
if( nextLine.indexOf(';') === 0 ) return null; // comment line | ||
var lineParts = nextLine.split(','); | ||
if( lineParts.length !== 6 ){ | ||
clog(''); | ||
if( options.rusMode ){ | ||
clog('Обнаружена строка с неожиданным количеством запятых:'); | ||
} else { | ||
clog('A line with unexpected number of commas is detected:'); | ||
var lineParts = nextLine.split(','); | ||
if( lineParts.length !== 6 ){ | ||
clog(''); | ||
if( options.rusMode ){ | ||
clog('В файле ' + nextFilename); | ||
clog('обнаружена строка с неожиданным количеством запятых:'); | ||
} else { | ||
clog('In the file ' + nextFilename); | ||
clog('a line with unexpected number of commas is detected:'); | ||
} | ||
clog(nextLine); | ||
if( options.rusMode ){ | ||
clog('Эта строка проигнорирована.'); | ||
} else { | ||
clog('That line is ignored.'); | ||
} | ||
return null; // weird line | ||
} | ||
clog(nextLine); | ||
if( options.rusMode ){ | ||
clog('Эта строка проигнорирована.'); | ||
} else { | ||
clog('That line is ignored.'); | ||
} | ||
return null; // weird line | ||
} | ||
return { | ||
echotag: lineParts[1], | ||
description: lineParts[2].replace(/"/g, '') // prevent double quotes | ||
}; | ||
}).filter(function(nextLine){ | ||
return nextLine !== null; | ||
return { | ||
lcEchotag: lineParts[1].toLowerCase(), | ||
description: lineParts[2].replace(/"/g, '') // kill double quotes | ||
}; | ||
}).filter(function(nextLine){ | ||
return nextLine !== null; | ||
}); | ||
}); | ||
var contentHPT = readFileOrDie( | ||
var contentHPT = readOrDie( | ||
filenameHPT, options.outputEncoding, options.rusMode | ||
@@ -98,6 +117,45 @@ ).split(/([\r\n]+)/).map(function(nextLine, idx){ | ||
// do not attempt anything if an echoarea line already has a description | ||
if( lineParts.indexOf('-d') > -1 ) return nextLine; | ||
if( lineParts.indexOf('-D') > -1 ) return nextLine; | ||
var lcEchotagHPT; | ||
if( lineParts.indexOf('-d') > -1 || lineParts.indexOf('-D') > -1 ){ | ||
// this echoarea line already has a description | ||
if(!( options.replaceMode )) return nextLine; | ||
// entered the mode of replacement | ||
var parts = /^(\s*EchoArea\s+)(\S+)(\s.+\s-d\s+")([^"]+)(".*)$/.exec( | ||
nextLine | ||
); | ||
if( parts === null ){ | ||
clog(''); | ||
if( options.rusMode ){ | ||
clog('В файле ' + filenameHPT); | ||
clog('обнаружен неожиданный формат описания области:'); | ||
} else { | ||
clog('In the file ' + filenameHPT); | ||
clog('an unexpected format of area description is detected:'); | ||
} | ||
clog(nextLine); | ||
if( options.rusMode ){ | ||
clog('Эта строка проигнорирована.'); | ||
} else { | ||
clog('That line is ignored.'); | ||
} | ||
return nextLine; | ||
} | ||
lcEchotagHPT = parts[2].toLowerCase(); | ||
var elDescription = getDescriptionElementFromContentCSV( | ||
contentCSV, lcEchotagHPT | ||
); | ||
if( typeof elDescription === 'undefined' ) return nextLine; | ||
return [ | ||
parts[1], // EchoArea | ||
parts[2], // an echotag | ||
parts[3], // pre-description | ||
elDescription.description, | ||
parts[5] // post-description | ||
].join(''); | ||
} | ||
var echotagIDX, pathIDX; | ||
@@ -111,7 +169,7 @@ if( lineParts[0].length > 0 ){ | ||
} | ||
lcEchotagHPT = lineParts[echotagIDX].toLowerCase(); | ||
var descriptionElement = contentCSV.find(function(elementCSV){ | ||
return elementCSV.echotag.toLowerCase() === | ||
lineParts[echotagIDX].toLowerCase(); | ||
}); | ||
var descriptionElement = getDescriptionElementFromContentCSV( | ||
contentCSV, lcEchotagHPT | ||
); | ||
if( typeof descriptionElement === 'undefined' ) return nextLine; | ||
@@ -123,3 +181,3 @@ | ||
writeFileOrDie( | ||
writeOrDie( | ||
filenameHPT, contentHPT, options.outputEncoding, options.rusMode | ||
@@ -126,0 +184,0 @@ ); |
@@ -13,2 +13,3 @@ #!/usr/bin/env node | ||
var rusMode = false; | ||
var replaceMode = false; | ||
params = params.filter(function(nextParam){ | ||
@@ -24,2 +25,5 @@ if( nextParam.indexOf('--input=') === 0 ){ | ||
return false; | ||
} else if( nextParam.toLowerCase() === '--replace' ){ | ||
replaceMode = true; | ||
return false; | ||
} | ||
@@ -47,2 +51,8 @@ | ||
clog(''); | ||
clog('Необязательный параметр "--replace" предписывает приложению'); | ||
clog('заменять описания эхопочты в файле конфигурации областей'); | ||
clog('эхопочты HPT. (Без "--replace" описания из CSV используются'); | ||
clog('только для областей эхопочты, не описанных в файле конфигурации'); | ||
clog('областей эхопочты HPT заранее.)'); | ||
clog(''); | ||
clog('Необязательные параметры "--input=CP866" и "--output=CP866"'); | ||
@@ -56,2 +66,7 @@ clog('задают кодировки входного (CSV) и выходного (HPT) файла.'); | ||
clog('наиболее часто).'); | ||
clog(''); | ||
clog('Можно указать несколько параметров inputCSV пред заключительным'); | ||
clog('параметром configHPT. Описания областей эхопочты из более'); | ||
clog('ранних параметров inputCSV получают приоритет над описаниями'); | ||
clog('областей эхопочты из дальнейших параметров inputCSV.'); | ||
} else { | ||
@@ -72,2 +87,8 @@ clog('Usage:'); | ||
clog(''); | ||
clog('An optional "--replace" parameter dictates the application'); | ||
clog('to replace echomail descriptions inside the HPT echomail area'); | ||
clog('configuration file. (Without "--replace" desciptions from CSV'); | ||
clog('are used only for the echomail areas that are not described in'); | ||
clog('HPT echomail area configuration file beforehand.)'); | ||
clog(''); | ||
clog('Optional parameters "--input=CP866" and "--output=CP866" define'); | ||
@@ -80,2 +101,7 @@ clog('the encodings of input (CSV) and output (HPT) file. Encodings'); | ||
clog('in Russian Fidonet, which is the largest use case).'); | ||
clog(''); | ||
clog('Several inputCSV parameters can be given before the final'); | ||
clog('configHPT parameter. Echomail area descriptions from the'); | ||
clog('earlier inputCSV parameters take precedence over the echomail'); | ||
clog('area descriptions from the latter inputCSV parameters.'); | ||
} | ||
@@ -85,10 +111,11 @@ process.exit(1); | ||
var filenameCSV = params[0]; | ||
var filenameHPT = params[1]; | ||
var filenameHPT = params.pop(); | ||
var filenamesCSV = params; | ||
thisAPI( | ||
filenameCSV, | ||
filenamesCSV, | ||
filenameHPT, | ||
{ | ||
rusMode: rusMode, | ||
replaceMode: replaceMode, | ||
inputEncoding: inputEncoding, | ||
@@ -95,0 +122,0 @@ outputEncoding: outputEncoding |
@@ -6,3 +6,3 @@ { | ||
"preferGlobal": true, | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Copies Fidonet echomail area descriptions from a CSV echolist to an HPT areas file.", | ||
@@ -9,0 +9,0 @@ "keywords": ["Fidonet", "echomail", "echolist", "HPT", "area", "areas", "echoes"], |
@@ -7,4 +7,2 @@ [](https://npmjs.org/package/echolist-csv2hpt) | ||
This package is currently in an early phase of its development and thus does not have the desired level of feature completeness. | ||
## Installing echolist-csv2hpt | ||
@@ -30,2 +28,6 @@ | ||
## Locking files | ||
The application **does not** lock any files and **does not** create any “lock files” (flag files, semaphore files). The application's user should control the access to the HPT's configuration. | ||
## Testing echolist-csv2hpt | ||
@@ -32,0 +34,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16845
32.88%274
41.24%44
4.76%0
-100%