source: trip-planner-front/node_modules/node-addon-api/tools/conversion.js@ b738035

Last change on this file since b738035 was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

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