Skip to content

Instantly share code, notes, and snippets.

@tracker1
Created December 17, 2019 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tracker1/7fd48e818f3b80c8266f04029e989f59 to your computer and use it in GitHub Desktop.
Save tracker1/7fd48e818f3b80c8266f04029e989f59 to your computer and use it in GitHub Desktop.
Application Initialization
/*
The application uses os-service module to register itself in to an
environment based on command line parameters.
*/
const SERVICE_NAME = "my-service-name"; // TODO: changeme
// https://www.npmjs.com/package/os-service - using fork for 12.13 for now
const service = require("os-service");
const startServer = require("./feature/web-server");
const { init, getAppConfig } = require("./feature/environment");
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Install Service
const add = () => {
service.add(SERVICE_NAME, { programArgs: ["--run"] }, error => {
if (error) {
console.error(error);
return;
}
console.log(`${SERVICE_NAME} service added`);
});
};
// Uninstall service
const remove = () => {
service.remove(SERVICE_NAME, error => {
if (error) {
console.error(error);
return;
}
console.log(`${SERVICE_NAME} service removed`);
});
};
// Run Service
const run = async () => {
service.run(() => {
console.log(JSON.stringify({ type: "STOP", time: new Date() }));
service.stop(0);
});
console.log(JSON.stringify({ type: "START", time: new Date() }));
await init();
const config = await getAppConfig();
await startServer(config);
};
// Show Help Text
const help = async () => {
console.log("node index [command]");
console.log(" --add - install service");
console.log(" --remove - uninstall service");
console.log(" --run - run service directly");
await delay(100);
};
const main = async skip => {
// skip/test mode, don't run main
if (skip) return;
if (process.argv.includes("--add")) {
add();
} else if (process.argv.includes("--remove")) {
remove();
} else if (process.argv.includes("--run")) {
await run();
} else {
await help();
}
}
main(process.env.NODE_ENV === "TEST").catch(error => {
console.error(error);
process.exit(1); // exit with non-zero exit code on error.
});
// ./feature/environment/init.js
/*
Some environments will start an application as a service from a directory
other than where the appliation sits, this may be intentional.
In order to facilitate this, the setupWorkingDirectory will search upward
for a .env file, and if it finds one, will change to that directory.
If it doesn't find one, it will then change to the root directory (2 levels up)
and search from there. The reasoning for changing the working directory is so
that relative paths from the .env file will evaluate as expected.
*/
import findUp from "find-up";
import path from "path";
import dotenv from "dotenv";
const setupWorkingDirectory = async () => {
// try from current path first
let dotEnv = await findUp(".env");
if (dotEnv) {
process.chdir(path.dirname(dotEnv));
} else {
// not found, try from project root
process.chdir(`${__dirname}/../../`); // change to project root.
dotEnv = await findUp(".env");
if (dotEnv) {
process.chdir(path.dirname(dotEnv));
}
}
};
module.exports = async () => {
await setupWorkingDirectory();
dotenv.config(); // load .env file (if exists)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment