func main()

in src/startupscriptgenerator/src/node/main.go [18:126]


func main() {
	versionCommand := flag.NewFlagSet(consts.VersionCommandName, flag.ExitOnError)

	setupEnvCommand := flag.NewFlagSet(consts.SetupEnvCommandName, flag.ExitOnError)
	setupEnvAppPathPtr := setupEnvCommand.String(
		"appPath",
		".",
		"The path to the application folder, e.g. '/home/site/wwwroot/'.")

	scriptCommand := flag.NewFlagSet(consts.CreateScriptCommandName, flag.ExitOnError)
	appPathPtr := scriptCommand.String(
		"appPath",
		".",
		"The path to the application folder, e.g. '/home/site/wwwroot/'.")
	manifestDirPtr := scriptCommand.String(
		"manifestDir",
		"",
		"[Optional] Path to the directory where build manifest file can be found. If no value is provided, then "+
			"it is assumed to be under the directory specified by 'appPath'.")
	userStartupCommandPtr := scriptCommand.String(
		"userStartupCommand",
		"",
		"[Optional] Command that will be executed to start the application up.")
	defaultAppFilePathPtr := scriptCommand.String(
		"defaultApp",
		"",
		"[Optional] Path to a default file that will be executed if the entrypoint is not found. "+
			"Ex: '/opt/startup/default-static-site.js'")
	bindPortPtr := scriptCommand.String(
		"bindPort",
		"",
		"[Optional] Port where the application will bind to. Default is 8080")
	usePm2Ptr := scriptCommand.Bool("usePM2", false, "If enabled, application will run using PM2.")
	remoteDebugEnabledPtr := scriptCommand.Bool("remoteDebug", false, "Application will run in debug mode.")
	remoteDebugBrkEnabledPtr := scriptCommand.Bool(
		"remoteDebugBrk",
		false,
		"Application will run in debug mode, and will debugger will break before the user code starts.")
	remoteDebugPort := scriptCommand.String("debugPort", "", "The port the debugger will listen to.")
	outputPathPtr := scriptCommand.String("output", "run.sh", "Path to the script to be generated.")
	skipNodeModulesExtraction := scriptCommand.Bool(
		"skipNodeModulesExtraction",
		false,
		"Disables the extraction of node_modules file. If used, some external tool will have to extract it - "+
			"otherwise the application might not work.")
	flag.Parse()

	logger := common.GetLogger("node.main")
	defer logger.Shutdown()
	logger.StartupScriptRequested()

	commands := []*flag.FlagSet{versionCommand, scriptCommand, setupEnvCommand}
	common.ValidateCommands(commands)

	if scriptCommand.Parsed() {
		fullAppPath := common.GetValidatedFullPath(*appPathPtr)
		defaultAppFullPAth := common.GetValidatedFullPath(*defaultAppFilePathPtr)

		buildManifest := common.GetBuildManifest(manifestDirPtr, fullAppPath)
		common.SetGlobalOperationID(buildManifest)

		var configuration Configuration
		viperConfig := common.GetViperConfiguration(fullAppPath)
		configuration.NodeVersion = viperConfig.GetString("NODE_VERSION")
		configuration.EnableDynamicInstall = viperConfig.GetBool(consts.EnableDynamicInstallKey)
		configuration.AppInsightsAgentExtensionVersion = getAppInsightsAgentVersion(configuration)
		configuration.PreRunCommand = viperConfig.GetString(consts.PreRunCommandEnvVarName)

		useLegacyDebugger := isLegacyDebuggerNeeded(configuration.NodeVersion)

		gen := NodeStartupScriptGenerator{
			SourcePath:                      fullAppPath,
			UserStartupCommand:              *userStartupCommandPtr,
			DefaultAppJsFilePath:            defaultAppFullPAth,
			UsePm2:                          *usePm2Ptr,
			BindPort:                        *bindPortPtr,
			RemoteDebugging:                 *remoteDebugEnabledPtr,
			RemoteDebuggingBreakBeforeStart: *remoteDebugBrkEnabledPtr,
			RemoteDebuggingPort:             *remoteDebugPort,
			UseLegacyDebugger:               useLegacyDebugger,
			SkipNodeModulesExtraction:       *skipNodeModulesExtraction,
			Manifest:                        buildManifest,
			Configuration:                   configuration,
		}
		script := gen.GenerateEntrypointScript()
		common.WriteScript(*outputPathPtr, script)
	}

	if setupEnvCommand.Parsed() {
		fullAppPath := common.GetValidatedFullPath(*setupEnvAppPathPtr)
		buildManifest := common.GetBuildManifest(manifestDirPtr, fullAppPath)

		nodeInstallationRoot := "/usr/local"
		script := common.GetSetupScript(
			"nodejs",
			buildManifest.NodeVersion,
			nodeInstallationRoot)
		scriptBuilder := strings.Builder{}
		scriptBuilder.WriteString(script)
		scriptBuilder.WriteString("echo Installing dependencies...\n")
		scriptBuilder.WriteString("/opt/node/installDependencies.sh\n")
		scriptBuilder.WriteString("echo Done installing dependencies.\n")
		finalScript := scriptBuilder.String()
		fmt.Println(fmt.Sprintf(
			"Setting up the environment with 'NodeJS' version '%s'...\n",
			buildManifest.NodeVersion))
		common.SetupEnv(finalScript)
	}
}