#!/usr/bin/env node
const fs = require('fs'),
      util = require('util');

console.log("Generating app startup command");

const DEFAULTAPP = "/opt/startup/default-static-site.js";
const CMDFILE = "/opt/startup/startupCommand";

var httpLoggingEnabled = process.env.HTTP_LOGGING_ENABLED;
httpLoggingEnabled = (typeof httpLoggingEnabled !== 'undefined'
    && httpLoggingEnabled !== null
    && (httpLoggingEnabled.toLowerCase() === 'true' || httpLoggingEnabled.toLowerCase() === '1'))

var roleInstanceId = '';
if (typeof process.env.WEBSITE_ROLE_INSTANCE_ID !== 'undefined'
    && process.env.WEBSITE_ROLE_INSTANCE_ID !== null) {
    roleInstanceId = process.env.WEBSITE_ROLE_INSTANCE_ID;
}

var startupCommand = fs.readFileSync(CMDFILE, 'utf8').trim();

// No user-provided startup command, check for scripts.start
if (!startupCommand) {
    var packageJsonPath = "/home/site/wwwroot/package.json";
    var json = fs.existsSync(packageJsonPath) && JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
    if (typeof json == 'object' && typeof json.scripts == 'object' && typeof json.scripts.start == 'string') {
        console.log("Found scripts.start in package.json")
        startupCommand = 'npm start';
    }
}

// No scripts.start; can we autodetect an app?
if (!startupCommand) {
    var autos = ['bin/www', 'server.js', 'app.js', 'index.js', 'hostingstart.js'];
    for (var i = 0; i < autos.length; i++) {
        var filename = "/home/site/wwwroot/" + autos[i];
        if (fs.existsSync(filename)) {
            console.log("No startup command entered, but found " + filename);
            startupCommand = "pm2 start " + filename+ " --no-daemon";
            break;
        }
    }
}

// Still nothing, run the default static site
if (!startupCommand) {
    console.log("No startup command or autodetected startup script " +
        "found. Running default static site.");
    startupCommand = "pm2 start " + DEFAULTAPP + " --no-daemon";
}

if (startupCommand && fs.existsSync(startupCommand)) {
    // Run with pm2
    startupCommand = "pm2 start " + startupCommand + " --no-daemon";
}

// Write to file
fs.writeFileSync(CMDFILE, startupCommand);
@@ -1215,16 +1215,42 @@
    %7D%0A%7D%0A%0A
+var finalCommand = null;%0A%0A
 // No sc
@@ -1617,39 +1617,37 @@
 e);%0A            
-startup
+final
 Command = %22pm2 s
@@ -1644,47 +1644,16 @@
 d = 
-%22pm2 start %22 + filename+ %22 --no-daemon%22
+filename
 ;%0A  
@@ -1745,32 +1745,49 @@
 (!startupCommand
+ && !finalCommand
 ) %7B%0A    console.
@@ -1901,127 +1901,517 @@
     
-startupCommand = %22pm2 start %22 + DEFAULTAPP + %22 --no-daemon%22;%0A%7D%0A%0Aif (startupCommand && fs.existsSync(startupCommand)) %7B%0A
+finalCommand = DEFAULTAPP;%0A%7D%0A%0Aif (finalCommand && fs.existsSync(finalCommand)) %7B%0A    if (process.env.APPSVC_REMOTE_DEBUGGING == %22TRUE%22)%0A    %7B%0A        if (process.env.APPSVC_REMOTE_DEBUGGING_BREAK == %22TRUE%22)%0A        %7B%0A            startupCommand = %22node --inspect-brk=0.0.0.0:%22 + process.env.APPSVC_TUNNEL_PORT + %22 %22 + finalCommand;%0A        %7D%0A        else%0A        %7B%0A            startupCommand = %22node --inspect=0.0.0.0:%22 + process.env.APPSVC_TUNNEL_PORT + %22 %22 + finalCommand;%0A        %7D%0A    %7D%0A    else%0A    %7B%0A    
     
@@ -2426,16 +2426,20 @@
 ith pm2%0A
+    
     star
@@ -2466,23 +2466,21 @@
 art %22 + 
-startup
+final
 Command 
@@ -2497,19 +2497,26 @@
 aemon%22;%0A
+    %7D%0A
 %7D%0A%0A
+%0A
 // Write
@@ -2566,8 +2566,9 @@
 ommand);
+%0A

Update generateStartupCommand.js