-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
3,418 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.