From de9e889b84d7904425775ca80b6c0e9815b85eba Mon Sep 17 00:00:00 2001
From: keicoon15 <keicoon15@gmail.com>
Date: Mon, 16 Jul 2018 11:07:24 +0900
Subject: [PATCH 1/2] Update latest npm path

---
 Examples/Content/Scripts/demos/lib/npm.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Examples/Content/Scripts/demos/lib/npm.js b/Examples/Content/Scripts/demos/lib/npm.js
index a7603b3..094af35 100644
--- a/Examples/Content/Scripts/demos/lib/npm.js
+++ b/Examples/Content/Scripts/demos/lib/npm.js
@@ -5,7 +5,7 @@ async function npm(what) {
     if (m) return m
 
     function locate_npm() {
-        let cmd = `-e console.log([...process.argv[0].replace(/\\\\/g,'/').split('/').slice(0,-1),'node_modules/npm/cli.js'].join('/'))`
+        let cmd = `-e console.log([...process.argv[0].replace(/\\\\/g,'/').split('/').slice(0,-1),'node_modules/npm/bin/npm-cli.js'].join('/'))`
         let p = JavascriptProcess.Create('node', cmd, false, true, true, 0, '', true)
         p.Wait()
         return p.ReadFromPipe().split('\n')[0]

From 0f078f7043458ca014afd83075c57e308ee347a0 Mon Sep 17 00:00:00 2001
From: keicoon15 <keicoon15@gmail.com>
Date: Mon, 16 Jul 2018 11:20:10 +0900
Subject: [PATCH 2/2] Add lib script 'loadnpm'

- Make 'node circular module' to 'non circular module' using browserify.
---
 .../Content/Scripts/demos/lib/fancyLoadNpm.js | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 Examples/Content/Scripts/demos/lib/fancyLoadNpm.js

diff --git a/Examples/Content/Scripts/demos/lib/fancyLoadNpm.js b/Examples/Content/Scripts/demos/lib/fancyLoadNpm.js
new file mode 100644
index 0000000..b6d137e
--- /dev/null
+++ b/Examples/Content/Scripts/demos/lib/fancyLoadNpm.js
@@ -0,0 +1,79 @@
+const delay = require('./delay');
+const npm = require('./npm');
+
+module.exports = (moduleName = '', global = {}) => {
+    const validModuleName = moduleName.replace(/\//g, "_");
+
+    const __scriptPath = Context.GetDir('GameContent') + 'Scripts';
+    const __srcScriptPath = `${__scriptPath}/${validModuleName}.js`;
+    const __desScriptPath = `${__scriptPath}/bundle_${validModuleName}.js`;
+
+    function IsNeedPrepare() {
+        return JavascriptLibrary.FileExists(__desScriptPath);
+    }
+
+    function Prepare() {
+        return new Promise(resolve => {
+            npm('browserify').then(() => npm(validModuleName)).then(() => {
+                const script = `global['cached_${validModuleName}'] = require('${moduleName}');`;
+                JavascriptLibrary.WriteStringToFile(Context, __srcScriptPath, script);
+                resolve()
+            });
+        });
+    }
+
+    function Process() {
+        const script = `(() => {
+            var browserify = require('browserify');
+            var b = browserify();
+            b.add('${__srcScriptPath}');
+
+            var fs = require('fs');
+            var bundleFs = fs.createWriteStream('${__desScriptPath}');
+
+            b.bundle().pipe(bundleFs);
+        })()`;
+        
+        const __browsifyScriptPath = `${__scriptPath}/temp.js`;
+        JavascriptLibrary.WriteStringToFile(Context, __browsifyScriptPath, script);
+        
+        let p = JavascriptProcess.Create(
+            'node',
+            `${__browsifyScriptPath}`,
+            true, false, false, 0, '', true
+        )
+        console.log('browserify', __srcScriptPath, __desScriptPath)
+        function pipe() {
+            let s = p.ReadFromPipe()
+            // @NOTE:
+            // console.log(s)
+        }
+        return new Promise(resolve => {
+            while (p.IsRunning()) {
+                delay(50).then(pipe);
+            }
+
+            JavascriptLibrary.DeleteFile(__browsifyScriptPath);
+            JavascriptLibrary.DeleteFile(__srcScriptPath);
+
+            resolve();
+        })
+    }
+
+    function PostInit() {
+        require(__desScriptPath);
+
+        newModule = global[`cached_${validModuleName}`];
+        delete global[`cached_${validModuleName}`];
+    }
+
+    let newModule = {};
+
+    if (IsNeedPrepare() == false) {
+        Prepare().then(Process).then(PostInit);
+    } else {
+        PostInit();
+    }
+
+    return newModule;
+};
\ No newline at end of file
