Implement episodic compilation for js code generation
diff --git a/lib/nodejs/test/episodic-code-generation-test/client.js b/lib/nodejs/test/episodic-code-generation-test/client.js
new file mode 100644
index 0000000..55dc702
--- /dev/null
+++ b/lib/nodejs/test/episodic-code-generation-test/client.js
@@ -0,0 +1,77 @@
+#!/usr/bin/env node
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const assert = require("assert");
+const test = require("tape");
+const thrift = require("thrift");
+const program = require("commander");
+
+program
+  .option("--host <host>", "Set the thrift server host to connect", "localhost")
+  .option("--port <port>", "Set the thrift server port number to connect", 9090)
+  .parse(process.argv);
+
+const Service = require("./gen-2/second-episode/gen-nodejs/Service");
+const Types = require("types-package/first-episode/Types_types");
+
+const host = program.host;
+const port = program.port;
+
+const options = {
+  transport: thrift.TBufferedTransport,
+  protocol: thrift.TJSONProtocol
+};
+
+const connection = thrift.createConnection(host, port, options);
+const testDriver = function(client, callback) {
+  test("NodeJS episodic compilation client-server test", function(assert) {
+    const type1Object = new Types.Type1();
+    type1Object.number = 42;
+    type1Object.message = "The answer";
+    client.testEpisode(type1Object, function(err, response) {
+      assert.error(err, "no callback error");
+      assert.equal(response.number, type1Object.number + 1);
+      assert.equal(
+        response.message,
+        type1Object.message + " [Hello from the server]"
+      );
+      assert.end();
+      callback("Server successfully tested");
+    });
+  });
+};
+
+connection.on("error", function(err) {
+  assert(false, err);
+});
+
+const client = thrift.createClient(Service, connection);
+
+runTests();
+
+function runTests() {
+  testDriver(client, function(status) {
+    console.log(status);
+    connection.destroy();
+  });
+}
+
+exports.expressoTest = function() {};
diff --git a/lib/nodejs/test/episodic-code-generation-test/episodic_compilation.package.json b/lib/nodejs/test/episodic-code-generation-test/episodic_compilation.package.json
new file mode 100644
index 0000000..7a78b4b
--- /dev/null
+++ b/lib/nodejs/test/episodic-code-generation-test/episodic_compilation.package.json
@@ -0,0 +1,3 @@
+{
+    "name": "types-package"
+}
diff --git a/lib/nodejs/test/episodic-code-generation-test/server.js b/lib/nodejs/test/episodic-code-generation-test/server.js
new file mode 100644
index 0000000..2917b68
--- /dev/null
+++ b/lib/nodejs/test/episodic-code-generation-test/server.js
@@ -0,0 +1,50 @@
+#!/usr/bin/env node
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License'); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const thrift = require("../../lib/thrift");
+const program = require("commander");
+
+program
+  .option("--port <port>", "Set the thrift server port", 9090)
+  .parse(process.argv);
+
+const Service = require("./gen-2/second-episode/gen-nodejs/Service");
+const Types = require("types-package/first-episode/Types_types");
+
+const port = program.port;
+
+const options = {
+  transport: thrift.TBufferedTransport,
+  protocol: thrift.TJSONProtocol
+};
+
+const ServiceHandler = {
+  testEpisode: function(receivedType1Object) {
+    const type1Object = new Types.Type1();
+    type1Object.number = receivedType1Object.number + 1;
+    type1Object.message =
+      receivedType1Object.message + " [Hello from the server]";
+    return type1Object;
+  }
+};
+
+const server = thrift.createServer(Service, ServiceHandler, options);
+server.listen(port);
diff --git a/lib/nodejs/test/testAll.sh b/lib/nodejs/test/testAll.sh
index e98b198..3ae88b3 100755
--- a/lib/nodejs/test/testAll.sh
+++ b/lib/nodejs/test/testAll.sh
@@ -23,6 +23,12 @@
 
 DIR="$( cd "$( dirname "$0" )" && pwd )"
 
+EPISODIC_DIR=${DIR}/episodic-code-generation-test
+
+THRIFT_FILES_DIR=${DIR}/../../../test
+
+THRIFT_COMPILER=${DIR}/../../../compiler/cpp/thrift
+
 ISTANBUL="$DIR/../../../node_modules/istanbul/lib/cli.js"
 
 REPORT_PREFIX="${DIR}/../coverage/report"
@@ -54,26 +60,68 @@
   return $RET
 }
 
+testEpisodicCompilation()
+{
+  RET=0
+  if [ -n "${COVER}" ]; then
+    ${ISTANBUL} cover ${EPISODIC_DIR}/server.js --dir ${REPORT_PREFIX}${COUNT} --handle-sigint &
+    COUNT=$((COUNT+1))
+  else
+    node ${EPISODIC_DIR}/server.js &
+  fi
+  SERVERPID=$!
+  sleep 0.1
+  if [ -n "${COVER}" ]; then
+    ${ISTANBUL} cover ${EPISODIC_DIR}/client.js --dir ${REPORT_PREFIX}${COUNT} || RET=1
+    COUNT=$((COUNT+1))
+  else
+    node ${EPISODIC_DIR}/client.js || RET=1
+  fi
+  kill -2 $SERVERPID || RET=1
+  wait $SERVERPID
+  return $RET
+}
+
 
 TESTOK=0
 
-#generating thrift code
+# generating Thrift code
 
-${DIR}/../../../compiler/cpp/thrift -o ${DIR} --gen js:node ${DIR}/../../../test/ThriftTest.thrift
-${DIR}/../../../compiler/cpp/thrift -o ${DIR} --gen js:node ${DIR}/../../../test/JsDeepConstructorTest.thrift
-${DIR}/../../../compiler/cpp/thrift -o ${DIR} --gen js:node ${DIR}/../../../test/Int64Test.thrift
+${THRIFT_COMPILER} -o ${DIR} --gen js:node ${THRIFT_FILES_DIR}/ThriftTest.thrift
+${THRIFT_COMPILER} -o ${DIR} --gen js:node ${THRIFT_FILES_DIR}/JsDeepConstructorTest.thrift
+${THRIFT_COMPILER} -o ${DIR} --gen js:node ${THRIFT_FILES_DIR}/Int64Test.thrift
 mkdir ${DIR}/gen-nodejs-es6
-${DIR}/../../../compiler/cpp/thrift -out ${DIR}/gen-nodejs-es6 --gen js:node,es6 ${DIR}/../../../test/ThriftTest.thrift
-${DIR}/../../../compiler/cpp/thrift -out ${DIR}/gen-nodejs-es6 --gen js:node,es6 ${DIR}/../../../test/JsDeepConstructorTest.thrift
-${DIR}/../../../compiler/cpp/thrift -out ${DIR}/gen-nodejs-es6 --gen js:node,es6 ${DIR}/../../../test/Int64Test.thrift
+${THRIFT_COMPILER} -out ${DIR}/gen-nodejs-es6 --gen js:node,es6 ${THRIFT_FILES_DIR}/ThriftTest.thrift
+${THRIFT_COMPILER} -out ${DIR}/gen-nodejs-es6 --gen js:node,es6 ${THRIFT_FILES_DIR}/JsDeepConstructorTest.thrift
+${THRIFT_COMPILER} -out ${DIR}/gen-nodejs-es6 --gen js:node,es6 ${THRIFT_FILES_DIR}/Int64Test.thrift
 
-#unit tests
+# generate episodic compilation test code
+TYPES_PACKAGE=${EPISODIC_DIR}/node_modules/types-package
+
+# generate the first episode
+mkdir --parents ${EPISODIC_DIR}/gen-1/first-episode
+${THRIFT_COMPILER} -o ${EPISODIC_DIR}/gen-1/first-episode --gen js:node,thrift_package_output_directory=first-episode ${THRIFT_FILES_DIR}/Types.thrift
+
+# create a "package" from the first episode and "install" it, the episode file must be at the module root
+mkdir --parents ${TYPES_PACKAGE}/first-episode
+cp --force ${EPISODIC_DIR}/episodic_compilation.package.json ${TYPES_PACKAGE}/package.json
+cp --force ${EPISODIC_DIR}/gen-1/first-episode/gen-nodejs/Types_types.js ${TYPES_PACKAGE}/first-episode/
+cp --force ${EPISODIC_DIR}/gen-1/first-episode/gen-nodejs/thrift.js.episode ${TYPES_PACKAGE}
+
+# generate the second episode
+mkdir --parents ${EPISODIC_DIR}/gen-2/second-episode
+${THRIFT_COMPILER} -o ${EPISODIC_DIR}/gen-2/second-episode --gen js:node,imports=${TYPES_PACKAGE} ${THRIFT_FILES_DIR}/Service.thrift
+if [ -f ${EPISODIC_DIR}/gen-2/second-episode/Types_types.js ]; then
+  TESTOK=1
+fi
+
+# unit tests
 
 node ${DIR}/binary.test.js || TESTOK=1
 node ${DIR}/int64.test.js || TESTOK=1
 node ${DIR}/deep-constructor.test.js || TESTOK=1
 
-#integration tests
+# integration tests
 
 for type in tcp multiplex websocket http
 do
@@ -91,6 +139,8 @@
   done
 done
 
+# episodic compilation test
+testEpisodicCompilation
 
 if [ -n "${COVER}" ]; then
   ${ISTANBUL} report --dir "${DIR}/../coverage" --include "${DIR}/../coverage/report*/coverage.json" lcov cobertura html