javascript: set external plugin dir to within package location

Signed-off-by: Mislav Novakovic <mislav.novakovic@sartura.hr>
diff --git a/swig/javascript/CMakeLists.txt b/swig/javascript/CMakeLists.txt
index acb1729..322204c 100644
--- a/swig/javascript/CMakeLists.txt
+++ b/swig/javascript/CMakeLists.txt
@@ -61,10 +61,21 @@
 	file(REMOVE "${SWIG_DEST}/libyang_javascript_wrap.cxx")
 	file(COPY "${CMAKE_SOURCE_DIR}/models" DESTINATION ${SWIG_DEST})
 	file(COPY "${CMAKE_SOURCE_DIR}/swig/javascript/files" DESTINATION "${SWIG_DEST}")
+	file(COPY "${CMAKE_SOURCE_DIR}/swig/javascript/run.sh" DESTINATION "${SWIG_DEST}")
 	configure_file("${CMAKE_SOURCE_DIR}/swig/javascript/test.js.in" "${SWIG_DEST}/test.js" ESCAPE_QUOTES @ONLY)
 	configure_file("${CMAKE_SOURCE_DIR}/swig/javascript/index.js.in" "${SWIG_DEST}/index.js" ESCAPE_QUOTES @ONLY)
 	configure_file("${CMAKE_SOURCE_DIR}/swig/javascript/package.json.in" "${SWIG_DEST}/package.json" ESCAPE_QUOTES @ONLY)
 
+	macro(EXTENSION_PLUGIN PLUGIN_NAME SRCS)
+		add_library("linked_${PLUGIN_NAME}" SHARED "${SWIG_DEST}/src/extensions/${SRCS}" ${libsrc})
+		set_target_properties("linked_${PLUGIN_NAME}" PROPERTIES PREFIX "" OUTPUT_NAME ${PLUGIN_NAME})
+		target_link_libraries("linked_${PLUGIN_NAME}" ${PCRE_LIBRARIES})
+		install(TARGETS "linked_${PLUGIN_NAME}" DESTINATION "${SWIG_DEST}/plugin")
+	endmacro(EXTENSION_PLUGIN)
+
+	EXTENSION_PLUGIN(nacm "nacm.c")
+	EXTENSION_PLUGIN(metadata "metadata.c")
+
 else()
 	message(${ERROR})
 endif()
diff --git a/swig/javascript/README.md b/swig/javascript/README.md
index 38688e4..bd9ef89 100644
--- a/swig/javascript/README.md
+++ b/swig/javascript/README.md
@@ -10,6 +10,7 @@
 ```
 $ cmake -DJAVASCRIPT_BINDING=ON ..
 $ make
+$ make install #necessary for plugin's
 $ cd javascript
 $ npm install --unsafe-perm
 ```
diff --git a/swig/javascript/binding.gyp.in b/swig/javascript/binding.gyp.in
index c668e4d..0628115 100644
--- a/swig/javascript/binding.gyp.in
+++ b/swig/javascript/binding.gyp.in
@@ -6,6 +6,15 @@
 			'src/libyang_javascript_wrap.cxx' ],
 		'include_dirs': [@libyang_LIB_INCLUDE_DIRS_GYP@],
 		'libraries': ['-lpcre',],
+		'actions': [
+			{
+				'action_name': 'set_plugin_path',
+				'message': 'Set the new plugin path for libyang',
+				'inputs': ['src/extensions_config.h'],
+				'outputs': [ 'src/extensions_config.h'],
+				'action': ['bash', 'run.sh'],
+			},
+		],
 		'conditions': [
 			[ 'OS!="win"', {
 				'include_dirs': [ 'config/win' ],
diff --git a/swig/javascript/package.json.in b/swig/javascript/package.json.in
index 6ba02c4..f691ccc 100644
--- a/swig/javascript/package.json.in
+++ b/swig/javascript/package.json.in
@@ -27,6 +27,8 @@
 		"files",
 		"test.js",
 		"index.js",
+		"run.sh",
+		"plugin",
 		"README.md"
 	]
 }
diff --git a/swig/javascript/run.sh b/swig/javascript/run.sh
new file mode 100644
index 0000000..a243367
--- /dev/null
+++ b/swig/javascript/run.sh
@@ -0,0 +1,6 @@
+dir=$(pwd)
+echo $dir
+line_number=$(grep -Fn 'LYEXT_PLUGINS_DIR' src/extensions_config.h | cut -f1 -d:)
+
+# set new libyang plugin path
+sed -i "${line_number}s/.*/#define LYEXT_PLUGINS_DIR \"${dir//\//\\/}\/plugin\"/" src/extensions_config.h