Skip to content

Commit

Permalink
🎉 Init
Browse files Browse the repository at this point in the history
  • Loading branch information
alexc155 committed Jan 28, 2020
1 parent 47be8d8 commit cf497db
Show file tree
Hide file tree
Showing 8 changed files with 3,418 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js

node_js:
- stable

install:
- npm install

script:
- npm run cover

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"
52 changes: 52 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";

const { existsSync, writeFileSync, readFileSync } = require("fs");
const { log } = require("../utils");

const CONFIG_FILE = "./node-template.config";

function writeConfig(setting, value) {
try {
if (!setting) {
throw "Setting is undefined";
}
if (!existsSync(CONFIG_FILE)) {
writeFileSync(CONFIG_FILE, "{}");
}

let config = JSON.parse(readFileSync(CONFIG_FILE, { encoding: "utf8" }));

config[setting] = value;

writeFileSync(CONFIG_FILE, JSON.stringify(config));
return true;
} catch (error) {
log.error("Error in writeConfig: ");
log.error(error);
return false;
}
}

function readConfig(setting, defaultValue) {
if (!existsSync(CONFIG_FILE)) {
log.error("Config file does not exist");
return;
}

const config = JSON.parse(readFileSync(CONFIG_FILE, { encoding: "utf8" }));

if (!config[setting] && !defaultValue) {
log.error("Config setting does not exist");
return;
} else if (!config[setting]) {
writeConfig(setting, defaultValue);
return defaultValue;
}

return config[setting];
}

module.exports = {
writeConfig,
readConfig
};
114 changes: 114 additions & 0 deletions config/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"use strict";

const { readFileSync, writeFileSync, unlinkSync } = require("fs");

const { expect } = require("chai");
const mockFs = require("mock-fs");
const sinon = require("sinon");

const CONFIG_FILE = "./node-template.config";

const sut = require("./index");

const consoleError = console.error;

beforeEach(function() {
mockFs({
"./": {}
});
console.error = function() {};
sinon.spy(console, "log");
sinon.spy(console, "error");
});

afterEach(function() {
mockFs.restore();
console.log.restore();
console.error.restore();
console.error = consoleError;
});

describe("#config", function() {
it("writes a value to the config file", function() {
sut.writeConfig("setting", "value");
// Run a second time to run thru' if the config file was missing.
sut.writeConfig("setting", "value");

const result = readFileSync(CONFIG_FILE, { encoding: "utf8" });
expect(result).to.equal(
JSON.stringify({
setting: "value"
})
);
});

it("errors writing an invalid setting to the config file", function() {
const result = sut.writeConfig(undefined, "value");

expect(result).to.equal(false);

expect(console.error.calledWith("Error in writeConfig: ")).to.equal(true);
});

it("reads a setting from the config file", function() {
writeFileSync(
CONFIG_FILE,
JSON.stringify({
setting: "value"
})
);

const setting = sut.readConfig("setting");

expect(setting).to.equal("value");
});

it("returns the default value when reading a setting if the setting doesn't exist and there is a default", function() {
writeFileSync(
CONFIG_FILE,
JSON.stringify({
setting: "value"
})
);

const setting = sut.readConfig("missing_setting", "/path/to/files");

expect(setting).to.equal("/path/to/files");
});

it("errors when reading a setting from the config file if the setting doesn't exist and there is no default", function() {
writeFileSync(
CONFIG_FILE,
JSON.stringify({
setting: "value"
})
);

const setting = sut.readConfig("invalid_setting");

expect(setting).to.equal(undefined);

expect(console.error.calledWith("Config setting does not exist")).to.equal(
true
);
});

it("errors when reading a setting from the config file if the config file doesn't exist", function() {
writeFileSync(
CONFIG_FILE,
JSON.stringify({
setting: "value"
})
);

unlinkSync(CONFIG_FILE);

const setting = sut.readConfig("setting");

expect(setting).to.equal(undefined);

expect(console.error.calledWith("Config file does not exist")).to.equal(
true
);
});
});
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#! /usr/bin/env node
"use strict";

const updateNotifier = require("update-notifier");
const pkg = require("./package.json");

function showHelp() {
log.info(`
Description.
Available commands:
--help | -h
Example usage:
$ ...
Notes:
* Note
`);
}

function main() {
updateNotifier({
pkg,
updateCheckInterval: 0
}).notify({
isGlobal: true
});

let action = process.argv[2];
action = action || "";
const args = process.argv.slice(3);

switch (action.toLowerCase()) {
case "-h":
case "--help":
case "":
case undefined:
default:
showHelp();
break;
}
}

main();

0 comments on commit cf497db

Please sign in to comment.