conversion.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #! /usr/bin/env node
  2. 'use strict'
  3. const fs = require('fs');
  4. const path = require('path');
  5. const args = process.argv.slice(2);
  6. const dir = args[0];
  7. if (!dir) {
  8. console.log('Usage: node ' + path.basename(__filename) + ' <target-dir>');
  9. process.exit(1);
  10. }
  11. const NodeApiVersion = require('../package.json').version;
  12. const disable = args[1];
  13. if (disable != "--disable" && dir != "--disable") {
  14. var ConfigFileOperations = {
  15. 'package.json': [
  16. [ /([ ]*)"dependencies": {/g, '$1"dependencies": {\n$1 "node-addon-api": "' + NodeApiVersion + '",'],
  17. [ /[ ]*"nan": *"[^"]+"(,|)[\n\r]/g, '' ]
  18. ],
  19. 'binding.gyp': [
  20. [ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!@(node -p "require(\\\'node-addon-api\\\').include")\',' ],
  21. [ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!@(node -p \\"require(\'node-addon-api\').include\\")",' ],
  22. [ /([ ]*)'dependencies': \[/g, '$1\'dependencies\': [\n$1 \'<!(node -p "require(\\\'node-addon-api\\\').gyp")\','],
  23. [ /([ ]*)"dependencies": \[/g, '$1"dependencies": [\n$1 "<!(node -p \'require(\\\"node-addon-api\\\").gyp\')",'],
  24. [ /[ ]*("|')<!\(node -e ("|'|\\"|\\')require\(("|'|\\"|\\')nan("|'|\\"|\\')\)("|'|\\"|\\')\)("|')(,|)[\r\n]/g, '' ],
  25. [ /([ ]*)("|')target_name("|'): ("|')(.+?)("|'),/g, '$1$2target_name$2: $4$5$6,\n $2cflags!$2: [ $2-fno-exceptions$2 ],\n $2cflags_cc!$2: [ $2-fno-exceptions$2 ],\n $2xcode_settings$2: { $2GCC_ENABLE_CPP_EXCEPTIONS$2: $2YES$2,\n $2CLANG_CXX_LIBRARY$2: $2libc++$2,\n $2MACOSX_DEPLOYMENT_TARGET$2: $210.7$2,\n },\n $2msvs_settings$2: {\n $2VCCLCompilerTool$2: { $2ExceptionHandling$2: 1 },\n },' ],
  26. ]
  27. };
  28. } else {
  29. var ConfigFileOperations = {
  30. 'package.json': [
  31. [ /([ ]*)"dependencies": {/g, '$1"dependencies": {\n$1 "node-addon-api": "' + NodeApiVersion + '",'],
  32. [ /[ ]*"nan": *"[^"]+"(,|)[\n\r]/g, '' ]
  33. ],
  34. 'binding.gyp': [
  35. [ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!@(node -p "require(\\\'node-addon-api\\\').include")\',' ],
  36. [ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!@(node -p \'require(\\\"node-addon-api\\\").include\')",' ],
  37. [ /([ ]*)'dependencies': \[/g, '$1\'dependencies\': [\n$1 \'<!(node -p "require(\\\'node-addon-api\\\').gyp")\','],
  38. [ /([ ]*)"dependencies": \[/g, '$1"dependencies": [\n$1 "<!(node -p \'require(\\\"node-addon-api\\\").gyp\')",'],
  39. [ /[ ]*("|')<!\(node -e ("|'|\\"|\\')require\(("|'|\\"|\\')nan("|'|\\"|\\')\)("|'|\\"|\\')\)("|')(,|)[\r\n]/g, '' ],
  40. [ /([ ]*)("|')target_name("|'): ("|')(.+?)("|'),/g, '$1$2target_name$2: $4$5$6,\n $2cflags!$2: [ $2-fno-exceptions$2 ],\n $2cflags_cc!$2: [ $2-fno-exceptions$2 ],\n $2defines$2: [ $2NAPI_DISABLE_CPP_EXCEPTIONS$2 ],\n $2conditions$2: [\n [\'OS==\"win\"\', { $2defines$2: [ $2_HAS_EXCEPTIONS=1$2 ] }]\n ]' ],
  41. ]
  42. };
  43. }
  44. var SourceFileOperations = [
  45. [ /Nan::SetMethod\(target,[\s]*\"(.*)\"[\s]*,[\s]*([^)]+)\)/g, 'exports.Set(Napi::String::New(env, \"$1\"), Napi::Function::New(env, $2))' ],
  46. [ /v8::Local<v8::FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<FunctionTemplate>\([\w\d:]+\);(?:\w+->Reset\(\1\))?\s+\1->SetClassName\(Nan::String::New\("(\w+)"\)\);/g, 'Napi::Function $1 = DefineClass(env, "$2", {' ],
  47. [ /Local<FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<FunctionTemplate>\([\w\d:]+\);\s+(\w+)\.Reset\((\1)\);\s+\1->SetClassName\((Nan::String::New|Nan::New<(v8::)*String>)\("(.+?)"\)\);/g, 'Napi::Function $1 = DefineClass(env, "$6", {'],
  48. [ /Local<FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<FunctionTemplate>\([\w\d:]+\);(?:\w+->Reset\(\1\))?\s+\1->SetClassName\(Nan::String::New\("(\w+)"\)\);/g, 'Napi::Function $1 = DefineClass(env, "$2", {' ],
  49. [ /Nan::New<v8::FunctionTemplate>\(([\w\d:]+)\)->GetFunction\(\)/g, 'Napi::Function::New(env, $1)' ],
  50. [ /Nan::New<FunctionTemplate>\(([\w\d:]+)\)->GetFunction()/g, 'Napi::Function::New(env, $1);' ],
  51. [ /Nan::New<v8::FunctionTemplate>\(([\w\d:]+)\)/g, 'Napi::Function::New(env, $1)' ],
  52. [ /Nan::New<FunctionTemplate>\(([\w\d:]+)\)/g, 'Napi::Function::New(env, $1)' ],
  53. // FunctionTemplate to FunctionReference
  54. [ /Nan::Persistent<(v8::)*FunctionTemplate>/g, 'Napi::FunctionReference' ],
  55. [ /Nan::Persistent<(v8::)*Function>/g, 'Napi::FunctionReference' ],
  56. [ /v8::Local<v8::FunctionTemplate>/g, 'Napi::FunctionReference' ],
  57. [ /Local<FunctionTemplate>/g, 'Napi::FunctionReference' ],
  58. [ /v8::FunctionTemplate/g, 'Napi::FunctionReference' ],
  59. [ /FunctionTemplate/g, 'Napi::FunctionReference' ],
  60. [ /([ ]*)Nan::SetPrototypeMethod\(\w+, "(\w+)", (\w+)\);/g, '$1InstanceMethod("$2", &$3),' ],
  61. [ /([ ]*)(?:\w+\.Reset\(\w+\);\s+)?\(target\)\.Set\("(\w+)",\s*Nan::GetFunction\((\w+)\)\);/gm,
  62. '});\n\n' +
  63. '$1constructor = Napi::Persistent($3);\n' +
  64. '$1constructor.SuppressDestruct();\n' +
  65. '$1target.Set("$2", $3);' ],
  66. // TODO: Other attribute combinations
  67. [ /static_cast<PropertyAttribute>\(ReadOnly\s*\|\s*DontDelete\)/gm,
  68. 'static_cast<napi_property_attributes>(napi_enumerable | napi_configurable)' ],
  69. [ /([\w\d:<>]+?)::Cast\((.+?)\)/g, '$2.As<$1>()' ],
  70. [ /\*Nan::Utf8String\(([^)]+)\)/g, '$1->As<Napi::String>().Utf8Value().c_str()' ],
  71. [ /Nan::Utf8String +(\w+)\(([^)]+)\)/g, 'std::string $1 = $2.As<Napi::String>()' ],
  72. [ /Nan::Utf8String/g, 'std::string' ],
  73. [ /v8::String::Utf8Value (.+?)\((.+?)\)/g, 'Napi::String $1(env, $2)' ],
  74. [ /String::Utf8Value (.+?)\((.+?)\)/g, 'Napi::String $1(env, $2)' ],
  75. [ /\.length\(\)/g, '.Length()' ],
  76. [ /Nan::MakeCallback\(([^,]+),[\s\\]+([^,]+),/gm, '$2.MakeCallback($1,' ],
  77. [ /class\s+(\w+)\s*:\s*public\s+Nan::ObjectWrap/g, 'class $1 : public Napi::ObjectWrap<$1>' ],
  78. [ /(\w+)\(([^\)]*)\)\s*:\s*Nan::ObjectWrap\(\)\s*(,)?/gm, '$1($2) : Napi::ObjectWrap<$1>()$3' ],
  79. // HandleOKCallback to OnOK
  80. [ /HandleOKCallback/g, 'OnOK' ],
  81. // HandleErrorCallback to OnError
  82. [ /HandleErrorCallback/g, 'OnError' ],
  83. // ex. .As<Function>() to .As<Napi::Object>()
  84. [ /\.As<v8::(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>\(\)/g, '.As<Napi::$1>()' ],
  85. [ /\.As<(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>\(\)/g, '.As<Napi::$1>()' ],
  86. // ex. Nan::New<Number>(info[0]) to Napi::Number::New(info[0])
  87. [ /Nan::New<(v8::)*Integer>\((.+?)\)/g, 'Napi::Number::New(env, $2)' ],
  88. [ /Nan::New\(([0-9\.]+)\)/g, 'Napi::Number::New(env, $1)' ],
  89. [ /Nan::New<(v8::)*String>\("(.+?)"\)/g, 'Napi::String::New(env, "$2")' ],
  90. [ /Nan::New\("(.+?)"\)/g, 'Napi::String::New(env, "$1")' ],
  91. [ /Nan::New<(v8::)*(.+?)>\(\)/g, 'Napi::$2::New(env)' ],
  92. [ /Nan::New<(.+?)>\(\)/g, 'Napi::$1::New(env)' ],
  93. [ /Nan::New<(v8::)*(.+?)>\(/g, 'Napi::$2::New(env, ' ],
  94. [ /Nan::New<(.+?)>\(/g, 'Napi::$1::New(env, ' ],
  95. [ /Nan::NewBuffer\(/g, 'Napi::Buffer<char>::New(env, ' ],
  96. // TODO: Properly handle this
  97. [ /Nan::New\(/g, 'Napi::New(env, ' ],
  98. [ /\.IsInt32\(\)/g, '.IsNumber()' ],
  99. [ /->IsInt32\(\)/g, '.IsNumber()' ],
  100. [ /(.+?)->BooleanValue\(\)/g, '$1.As<Napi::Boolean>().Value()' ],
  101. [ /(.+?)->Int32Value\(\)/g, '$1.As<Napi::Number>().Int32Value()' ],
  102. [ /(.+?)->Uint32Value\(\)/g, '$1.As<Napi::Number>().Uint32Value()' ],
  103. [ /(.+?)->IntegerValue\(\)/g, '$1.As<Napi::Number>().Int64Value()' ],
  104. [ /(.+?)->NumberValue\(\)/g, '$1.As<Napi::Number>().DoubleValue()' ],
  105. // ex. Nan::To<bool>(info[0]) to info[0].Value()
  106. [ /Nan::To<v8::(Boolean|String|Number|Object|Array|Symbol|Function)>\((.+?)\)/g, '$2.To<Napi::$1>()' ],
  107. [ /Nan::To<(Boolean|String|Number|Object|Array|Symbol|Function)>\((.+?)\)/g, '$2.To<Napi::$1>()' ],
  108. // ex. Nan::To<bool>(info[0]) to info[0].As<Napi::Boolean>().Value()
  109. [ /Nan::To<bool>\((.+?)\)/g, '$1.As<Napi::Boolean>().Value()' ],
  110. // ex. Nan::To<int>(info[0]) to info[0].As<Napi::Number>().Int32Value()
  111. [ /Nan::To<int>\((.+?)\)/g, '$1.As<Napi::Number>().Int32Value()' ],
  112. // ex. Nan::To<int32_t>(info[0]) to info[0].As<Napi::Number>().Int32Value()
  113. [ /Nan::To<int32_t>\((.+?)\)/g, '$1.As<Napi::Number>().Int32Value()' ],
  114. // ex. Nan::To<uint32_t>(info[0]) to info[0].As<Napi::Number>().Uint32Value()
  115. [ /Nan::To<uint32_t>\((.+?)\)/g, '$1.As<Napi::Number>().Uint32Value()' ],
  116. // ex. Nan::To<int64_t>(info[0]) to info[0].As<Napi::Number>().Int64Value()
  117. [ /Nan::To<int64_t>\((.+?)\)/g, '$1.As<Napi::Number>().Int64Value()' ],
  118. // ex. Nan::To<float>(info[0]) to info[0].As<Napi::Number>().FloatValue()
  119. [ /Nan::To<float>\((.+?)\)/g, '$1.As<Napi::Number>().FloatValue()' ],
  120. // ex. Nan::To<double>(info[0]) to info[0].As<Napi::Number>().DoubleValue()
  121. [ /Nan::To<double>\((.+?)\)/g, '$1.As<Napi::Number>().DoubleValue()' ],
  122. [ /Nan::New\((\w+)\)->HasInstance\((\w+)\)/g, '$2.InstanceOf($1.Value())' ],
  123. [ /Nan::Has\(([^,]+),\s*/gm, '($1).Has(' ],
  124. [ /\.Has\([\s|\\]*Nan::New<(v8::)*String>\(([^)]+)\)\)/gm, '.Has($1)' ],
  125. [ /\.Has\([\s|\\]*Nan::New\(([^)]+)\)\)/gm, '.Has($1)' ],
  126. [ /Nan::Get\(([^,]+),\s*/gm, '($1).Get(' ],
  127. [ /\.Get\([\s|\\]*Nan::New<(v8::)*String>\(([^)]+)\)\)/gm, '.Get($1)' ],
  128. [ /\.Get\([\s|\\]*Nan::New\(([^)]+)\)\)/gm, '.Get($1)' ],
  129. [ /Nan::Set\(([^,]+),\s*/gm, '($1).Set(' ],
  130. [ /\.Set\([\s|\\]*Nan::New<(v8::)*String>\(([^)]+)\)\s*,/gm, '.Set($1,' ],
  131. [ /\.Set\([\s|\\]*Nan::New\(([^)]+)\)\s*,/gm, '.Set($1,' ],
  132. // ex. node::Buffer::HasInstance(info[0]) to info[0].IsBuffer()
  133. [ /node::Buffer::HasInstance\((.+?)\)/g, '$1.IsBuffer()' ],
  134. // ex. node::Buffer::Length(info[0]) to info[0].Length()
  135. [ /node::Buffer::Length\((.+?)\)/g, '$1.As<Napi::Buffer<char>>().Length()' ],
  136. // ex. node::Buffer::Data(info[0]) to info[0].Data()
  137. [ /node::Buffer::Data\((.+?)\)/g, '$1.As<Napi::Buffer<char>>().Data()' ],
  138. [ /Nan::CopyBuffer\(/g, 'Napi::Buffer::Copy(env, ' ],
  139. // Nan::AsyncQueueWorker(worker)
  140. [ /Nan::AsyncQueueWorker\((.+)\);/g, '$1.Queue();' ],
  141. [ /Nan::(Undefined|Null|True|False)\(\)/g, 'env.$1()' ],
  142. // Nan::ThrowError(error) to Napi::Error::New(env, error).ThrowAsJavaScriptException()
  143. [ /([ ]*)return Nan::Throw(\w*?)Error\((.+?)\);/g, '$1Napi::$2Error::New(env, $3).ThrowAsJavaScriptException();\n$1return env.Null();' ],
  144. [ /Nan::Throw(\w*?)Error\((.+?)\);\n(\s*)return;/g, 'Napi::$1Error::New(env, $2).ThrowAsJavaScriptException();\n$3return env.Null();' ],
  145. [ /Nan::Throw(\w*?)Error\((.+?)\);/g, 'Napi::$1Error::New(env, $2).ThrowAsJavaScriptException();\n' ],
  146. // Nan::RangeError(error) to Napi::RangeError::New(env, error)
  147. [ /Nan::(\w*?)Error\((.+)\)/g, 'Napi::$1Error::New(env, $2)' ],
  148. [ /Nan::Set\((.+?),\n* *(.+?),\n* *(.+?),\n* *(.+?)\)/g, '$1.Set($2, $3, $4)' ],
  149. [ /Nan::(Escapable)?HandleScope\s+(\w+)\s*;/g, 'Napi::$1HandleScope $2(env);' ],
  150. [ /Nan::(Escapable)?HandleScope/g, 'Napi::$1HandleScope' ],
  151. [ /Nan::ForceSet\(([^,]+), ?/g, '$1->DefineProperty(' ],
  152. [ /\.ForceSet\(Napi::String::New\(env, "(\w+)"\),\s*?/g, '.DefineProperty("$1", ' ],
  153. // [ /Nan::GetPropertyNames\(([^,]+)\)/, '$1->GetPropertyNames()' ],
  154. [ /Nan::Equals\(([^,]+),/g, '$1.StrictEquals(' ],
  155. [ /(.+)->Set\(/g, '$1.Set\(' ],
  156. [ /Nan::Callback/g, 'Napi::FunctionReference' ],
  157. [ /Nan::Persistent<Object>/g, 'Napi::ObjectReference' ],
  158. [ /Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target/g, 'Napi::Env& env, Napi::Object& target' ],
  159. [ /(\w+)\*\s+(\w+)\s*=\s*Nan::ObjectWrap::Unwrap<\w+>\(info\.This\(\)\);/g, '$1* $2 = this;' ],
  160. [ /Nan::ObjectWrap::Unwrap<(\w+)>\((.*)\);/g, '$2.Unwrap<$1>();' ],
  161. [ /Nan::NAN_METHOD_RETURN_TYPE/g, 'void' ],
  162. [ /NAN_INLINE/g, 'inline' ],
  163. [ /Nan::NAN_METHOD_ARGS_TYPE/g, 'const Napi::CallbackInfo&' ],
  164. [ /NAN_METHOD\(([\w\d:]+?)\)/g, 'Napi::Value $1(const Napi::CallbackInfo& info)'],
  165. [ /static\s*NAN_GETTER\(([\w\d:]+?)\)/g, 'Napi::Value $1(const Napi::CallbackInfo& info)' ],
  166. [ /NAN_GETTER\(([\w\d:]+?)\)/g, 'Napi::Value $1(const Napi::CallbackInfo& info)' ],
  167. [ /static\s*NAN_SETTER\(([\w\d:]+?)\)/g, 'void $1(const Napi::CallbackInfo& info, const Napi::Value& value)' ],
  168. [ /NAN_SETTER\(([\w\d:]+?)\)/g, 'void $1(const Napi::CallbackInfo& info, const Napi::Value& value)' ],
  169. [ /void Init\((v8::)*Local<(v8::)*Object> exports\)/g, 'Napi::Object Init(Napi::Env env, Napi::Object exports)' ],
  170. [ /NAN_MODULE_INIT\(([\w\d:]+?)\);/g, 'Napi::Object $1(Napi::Env env, Napi::Object exports);' ],
  171. [ /NAN_MODULE_INIT\(([\w\d:]+?)\)/g, 'Napi::Object $1(Napi::Env env, Napi::Object exports)' ],
  172. [ /::(Init(?:ialize)?)\(target\)/g, '::$1(env, target, module)' ],
  173. [ /constructor_template/g, 'constructor' ],
  174. [ /Nan::FunctionCallbackInfo<(v8::)?Value>[ ]*& [ ]*info\)[ ]*{\n*([ ]*)/gm, 'Napi::CallbackInfo& info) {\n$2Napi::Env env = info.Env();\n$2' ],
  175. [ /Nan::FunctionCallbackInfo<(v8::)*Value>\s*&\s*info\);/g, 'Napi::CallbackInfo& info);' ],
  176. [ /Nan::FunctionCallbackInfo<(v8::)*Value>\s*&/g, 'Napi::CallbackInfo&' ],
  177. [ /Buffer::HasInstance\(([^)]+)\)/g, '$1.IsBuffer()' ],
  178. [ /info\[(\d+)\]->/g, 'info[$1].' ],
  179. [ /info\[([\w\d]+)\]->/g, 'info[$1].' ],
  180. [ /info\.This\(\)->/g, 'info.This().' ],
  181. [ /->Is(Object|String|Int32|Number)\(\)/g, '.Is$1()' ],
  182. [ /info.GetReturnValue\(\).SetUndefined\(\)/g, 'return env.Undefined()' ],
  183. [ /info\.GetReturnValue\(\)\.Set\(((\n|.)+?)\);/g, 'return $1;' ],
  184. // ex. Local<Value> to Napi::Value
  185. [ /v8::Local<v8::(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>/g, 'Napi::$1' ],
  186. [ /Local<(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>/g, 'Napi::$1' ],
  187. // Declare an env in helper functions that take a Napi::Value
  188. [ /(\w+)\(Napi::Value (\w+)(,\s*[^\()]+)?\)\s*{\n*([ ]*)/gm, '$1(Napi::Value $2$3) {\n$4Napi::Env env = $2.Env();\n$4' ],
  189. // delete #include <node.h> and/or <v8.h>
  190. [ /#include +(<|")(?:node|nan).h("|>)/g, "#include $1napi.h$2\n#include $1uv.h$2" ],
  191. // NODE_MODULE to NODE_API_MODULE
  192. [ /NODE_MODULE/g, 'NODE_API_MODULE' ],
  193. [ /Nan::/g, 'Napi::' ],
  194. [ /nan.h/g, 'napi.h' ],
  195. // delete .FromJust()
  196. [ /\.FromJust\(\)/g, '' ],
  197. // delete .ToLocalCheck()
  198. [ /\.ToLocalChecked\(\)/g, '' ],
  199. [ /^.*->SetInternalFieldCount\(.*$/gm, '' ],
  200. // replace using node; and/or using v8; to using Napi;
  201. [ /using (node|v8);/g, 'using Napi;' ],
  202. [ /using namespace (node|Nan|v8);/g, 'using namespace Napi;' ],
  203. // delete using v8::Local;
  204. [ /using v8::Local;\n/g, '' ],
  205. // replace using v8::XXX; with using Napi::XXX
  206. [ /using v8::([A-Za-z]+);/g, 'using Napi::$1;' ],
  207. ];
  208. var paths = listFiles(dir);
  209. paths.forEach(function(dirEntry) {
  210. var filename = dirEntry.split('\\').pop().split('/').pop();
  211. // Check whether the file is a source file or a config file
  212. // then execute function accordingly
  213. var sourcePattern = /.+\.h|.+\.cc|.+\.cpp/;
  214. if (sourcePattern.test(filename)) {
  215. convertFile(dirEntry, SourceFileOperations);
  216. } else if (ConfigFileOperations[filename] != null) {
  217. convertFile(dirEntry, ConfigFileOperations[filename]);
  218. }
  219. });
  220. function listFiles(dir, filelist) {
  221. var files = fs.readdirSync(dir);
  222. filelist = filelist || [];
  223. files.forEach(function(file) {
  224. if (file === 'node_modules') {
  225. return
  226. }
  227. if (fs.statSync(path.join(dir, file)).isDirectory()) {
  228. filelist = listFiles(path.join(dir, file), filelist);
  229. } else {
  230. filelist.push(path.join(dir, file));
  231. }
  232. });
  233. return filelist;
  234. }
  235. function convert(content, operations) {
  236. for (let i = 0; i < operations.length; i ++) {
  237. let operation = operations[i];
  238. content = content.replace(operation[0], operation[1]);
  239. }
  240. return content;
  241. }
  242. function convertFile(fileName, operations) {
  243. fs.readFile(fileName, "utf-8", function (err, file) {
  244. if (err) throw err;
  245. file = convert(file, operations);
  246. fs.writeFile(fileName, file, function(err){
  247. if (err) throw err;
  248. });
  249. });
  250. }