now merges default options
This commit is contained in:
parent
dc53498f22
commit
93bbe75f37
7
dist/index.d.ts
vendored
7
dist/index.d.ts
vendored
@ -1 +1,6 @@
|
||||
export declare let dataFor: (toolNameArg: string, cwdArg?: string) => any;
|
||||
export interface IDataFor {
|
||||
toolName: string;
|
||||
defaultSettings?: any;
|
||||
cwd?: string;
|
||||
}
|
||||
export declare let dataFor: (optionsArg: IDataFor) => any;
|
||||
|
42
dist/index.js
vendored
42
dist/index.js
vendored
@ -1,26 +1,42 @@
|
||||
"use strict";
|
||||
const plugins = require("./npmextra.plugins");
|
||||
const paths = require("./npmextra.paths");
|
||||
let allData;
|
||||
exports.dataFor = (toolNameArg, cwdArg) => {
|
||||
if (typeof allData == "undefined") {
|
||||
exports.dataFor = (optionsArg) => {
|
||||
// handle default settings
|
||||
if (typeof optionsArg.toolName != undefined
|
||||
&& typeof optionsArg.defaultSettings != undefined) {
|
||||
let newDefaultOptions = {};
|
||||
newDefaultOptions[optionsArg.toolName] = optionsArg.defaultSettings;
|
||||
optionsArg.defaultSettings = newDefaultOptions;
|
||||
}
|
||||
;
|
||||
// set lookup path
|
||||
let lookupPath;
|
||||
if (cwdArg) {
|
||||
lookupPath = plugins.path.join(cwdArg, "npmextra.json");
|
||||
if (optionsArg.cwd) {
|
||||
lookupPath = plugins.path.join(optionsArg.cwd, "npmextra.json");
|
||||
}
|
||||
else {
|
||||
lookupPath = paths.configFile;
|
||||
}
|
||||
;
|
||||
allData = plugins.smartfile.fs.toObjectSync(plugins.path.join(lookupPath));
|
||||
}
|
||||
;
|
||||
if (toolNameArg) {
|
||||
if (allData[toolNameArg]) {
|
||||
return allData[toolNameArg];
|
||||
// get allData
|
||||
let allData;
|
||||
if (plugins.smartfile.fs.fileExistsSync(lookupPath)) {
|
||||
allData = plugins.smartfile.fs.toObjectSync(lookupPath);
|
||||
}
|
||||
else {
|
||||
plugins.beautylog.error(`There is no data for ${toolNameArg}`);
|
||||
plugins.beautylog.warn(`${lookupPath} is misssing!`);
|
||||
allData = {};
|
||||
}
|
||||
;
|
||||
//assign all data
|
||||
allData = plugins.lodash.merge({}, optionsArg.defaultSettings, allData);
|
||||
if (optionsArg.toolName) {
|
||||
if (allData[optionsArg.toolName]) {
|
||||
return allData[optionsArg.toolName];
|
||||
}
|
||||
else {
|
||||
plugins.beautylog.error(`There is no data for ${optionsArg.toolName}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@ -28,4 +44,4 @@ exports.dataFor = (toolNameArg, cwdArg) => {
|
||||
return allData;
|
||||
}
|
||||
};
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBWSxPQUFPLFdBQU0sb0JBQ3pCLENBQUMsQ0FENEM7QUFDN0MsTUFBWSxLQUFLLFdBQU0sa0JBQWtCLENBQUMsQ0FBQTtBQUMxQyxJQUFJLE9BQU8sQ0FBQztBQUVELGVBQU8sR0FBRyxDQUFDLFdBQWtCLEVBQUMsTUFBYztJQUNuRCxFQUFFLENBQUEsQ0FBQyxPQUFPLE9BQU8sSUFBSSxXQUFXLENBQUMsQ0FBQSxDQUFDO1FBQzlCLElBQUksVUFBVSxDQUFDO1FBQ2YsRUFBRSxDQUFBLENBQUMsTUFBTSxDQUFDLENBQUEsQ0FBQztZQUNQLFVBQVUsR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxNQUFNLEVBQUMsZUFBZSxDQUFDLENBQUM7UUFDM0QsQ0FBQztRQUFDLElBQUksQ0FBQyxDQUFDO1lBQ0osVUFBVSxHQUFHLEtBQUssQ0FBQyxVQUFVLENBQUM7UUFDbEMsQ0FBQztRQUFBLENBQUM7UUFDRixPQUFPLEdBQUcsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsWUFBW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Q0FBQyxlQUFlLEVBQUMsT0FBTyxDQUFDLENBQUM7SUFDdEUsRUFBRSxDQUFBLENBQUMsVUFBVSxDQUFDLFFBQVEsQ0FBQyxDQUFBLENBQUM7UUFDcEIsRUFBRSxDQUFBLENBQUMsT0FBTyxDQUFDLFVBQVUsQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFBLENBQUM7WUFDN0IsTUFBTSxDQUFDLE9BQU8sQ0FBQyxVQUFVLENBQUMsUUFBUSxDQUFDLENBQUM7UUFDeEMsQ0FBQztRQUFDLElBQUksQ0FBQyxDQUFDO1lBQ0osT0FBTyxDQUFDLFNBQVMsQ0FBQyxLQUFLLENBQUMsd0JBQXdCLFVBQVUsQ0FBQyxRQUFRLEVBQUUsQ0FBQyxDQUFDO1lBQ3ZFLE1BQU0sQ0FBQyxTQUFTLENBQUM7UUFDckIsQ0FBQztJQUNMLENBQUM7SUFBQyxJQUFJLENBQUMsQ0FBQztRQUNKLE1BQU0sQ0FBQyxPQUFPLENBQUM7SUFDbkIsQ0FBQztBQUNMLENBQUMsQ0FBQSJ9
|
1
dist/npmextra.plugins.d.ts
vendored
1
dist/npmextra.plugins.d.ts
vendored
@ -1,5 +1,6 @@
|
||||
import "typings-global";
|
||||
export import beautylog = require("beautylog");
|
||||
export import lodash = require("lodash");
|
||||
export import path = require("path");
|
||||
export import smartfile = require("smartfile");
|
||||
export import q = require("q");
|
||||
|
3
dist/npmextra.plugins.js
vendored
3
dist/npmextra.plugins.js
vendored
@ -1,7 +1,8 @@
|
||||
"use strict";
|
||||
require("typings-global");
|
||||
exports.beautylog = require("beautylog");
|
||||
exports.lodash = require("lodash");
|
||||
exports.path = require("path");
|
||||
exports.smartfile = require("smartfile");
|
||||
exports.q = require("q");
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZXh0cmEucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWV4dHJhLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUNWLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUM7QUFDakMsWUFBSSxXQUFXLE1BQU0sQ0FBQyxDQUFDO0FBQ3ZCLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUM7QUFDakMsU0FBQyxXQUFXLEdBQUcsQ0FBQyxDQUFDIn0=
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZXh0cmEucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWV4dHJhLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUNWLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUM7QUFDakMsY0FBTSxXQUFXLFFBQVEsQ0FBQyxDQUFDO0FBQzNCLFlBQUksV0FBVyxNQUFNLENBQUMsQ0FBQztBQUN2QixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ2pDLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQyJ9
|
@ -18,13 +18,15 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/npmextra#README",
|
||||
"dependencies": {
|
||||
"@types/q": "*",
|
||||
"@types/q": "^0.0.27",
|
||||
"beautylog": "^5.0.14",
|
||||
"lodash": "^4.13.1",
|
||||
"q": "^1.4.1",
|
||||
"smartfile": "^4.0.10",
|
||||
"typings-global": "^1.0.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/should": "^8.1.26",
|
||||
"should": "^9.0.2",
|
||||
"typings-test": "^1.0.1"
|
||||
}
|
||||
|
10
test/test.js
10
test/test.js
@ -5,8 +5,12 @@ require("should");
|
||||
const npmExtra = require("../dist/index");
|
||||
describe("npmextra", function () {
|
||||
it("should read a config file", function () {
|
||||
let testData = npmExtra.dataFor("testTool", path.join(process.cwd(), "test/"));
|
||||
console.log(testData);
|
||||
let testData = npmExtra.dataFor({
|
||||
toolName: "testTool",
|
||||
defaultSettings: { someKey2: "someValue2" },
|
||||
cwd: path.join(process.cwd(), "test/")
|
||||
});
|
||||
testData.should.have.ownProperty("someKey2");
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBTyxJQUFJLFdBQVcsTUFBTSxDQUFDLENBQUM7QUFDOUIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUVoQixNQUFPLFFBQVEsV0FBVyxlQUFlLENBQUMsQ0FBQztBQUUzQyxRQUFRLENBQUMsVUFBVSxFQUFDO0lBQ2hCLEVBQUUsQ0FBQywyQkFBMkIsRUFBQztRQUMzQixJQUFJLFFBQVEsR0FBRyxRQUFRLENBQUMsT0FBTyxDQUFDLFVBQVUsRUFBQyxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxHQUFHLEVBQUUsRUFBQyxPQUFPLENBQUMsQ0FBQyxDQUFDO1FBQzdFLE9BQU8sQ0FBQyxHQUFHLENBQUMsUUFBUSxDQUFDLENBQUM7SUFDMUIsQ0FBQyxDQUFDLENBQUM7QUFDUCxDQUFDLENBQUMsQ0FBQyJ9
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBTyxJQUFJLFdBQVcsTUFBTSxDQUFDLENBQUM7QUFDOUIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUVoQixNQUFPLFFBQVEsV0FBVyxlQUFlLENBQUMsQ0FBQztBQUUzQyxRQUFRLENBQUMsVUFBVSxFQUFDO0lBQ2hCLEVBQUUsQ0FBQywyQkFBMkIsRUFBQztRQUMzQixJQUFJLFFBQVEsR0FBRyxRQUFRLENBQUMsT0FBTyxDQUFDO1lBQzVCLFFBQVEsRUFBQyxVQUFVO1lBQ25CLGVBQWUsRUFBQyxFQUFDLFFBQVEsRUFBQyxZQUFZLEVBQUM7WUFDdkMsR0FBRyxFQUFFLElBQUksQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLEdBQUcsRUFBRSxFQUFDLE9BQU8sQ0FBQztTQUN4QyxDQUFDLENBQUM7UUFDSCxRQUFRLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxXQUFXLENBQUMsVUFBVSxDQUFDLENBQUM7SUFDakQsQ0FBQyxDQUFDLENBQUM7QUFDUCxDQUFDLENBQUMsQ0FBQyJ9
|
@ -6,7 +6,11 @@ import npmExtra = require("../dist/index");
|
||||
|
||||
describe("npmextra",function(){
|
||||
it("should read a config file",function(){
|
||||
let testData = npmExtra.dataFor("testTool",path.join(process.cwd(),"test/"));
|
||||
console.log(testData);
|
||||
let testData = npmExtra.dataFor({
|
||||
toolName:"testTool",
|
||||
defaultSettings:{someKey2:"someValue2"},
|
||||
cwd: path.join(process.cwd(),"test/")
|
||||
});
|
||||
testData.should.have.ownProperty("someKey2");
|
||||
});
|
||||
});
|
52
ts/index.ts
52
ts/index.ts
@ -1,24 +1,48 @@
|
||||
import * as plugins from "./npmextra.plugins"
|
||||
import * as paths from "./npmextra.paths";
|
||||
let allData;
|
||||
|
||||
export let dataFor = (toolNameArg:string,cwdArg?:string) => {
|
||||
if(typeof allData == "undefined"){
|
||||
let lookupPath;
|
||||
if(cwdArg){
|
||||
lookupPath = plugins.path.join(cwdArg,"npmextra.json");
|
||||
export interface IDataFor {
|
||||
toolName:string;
|
||||
defaultSettings?;
|
||||
cwd?:string
|
||||
}
|
||||
|
||||
export let dataFor = (optionsArg:IDataFor) => {
|
||||
|
||||
// handle default settings
|
||||
if(
|
||||
typeof optionsArg.toolName != undefined
|
||||
&& typeof optionsArg.defaultSettings != undefined
|
||||
){
|
||||
let newDefaultOptions = {};
|
||||
newDefaultOptions[optionsArg.toolName] = optionsArg.defaultSettings;
|
||||
optionsArg.defaultSettings = newDefaultOptions;
|
||||
};
|
||||
|
||||
// set lookup path
|
||||
let lookupPath:string;
|
||||
if(optionsArg.cwd){
|
||||
lookupPath = plugins.path.join(optionsArg.cwd,"npmextra.json");
|
||||
} else {
|
||||
lookupPath = paths.configFile;
|
||||
};
|
||||
allData = plugins.smartfile.fs.toObjectSync(
|
||||
plugins.path.join(lookupPath)
|
||||
);
|
||||
};
|
||||
if(toolNameArg){
|
||||
if(allData[toolNameArg]){
|
||||
return allData[toolNameArg];
|
||||
|
||||
// get allData
|
||||
let allData;
|
||||
if(plugins.smartfile.fs.fileExistsSync(lookupPath)){
|
||||
allData = plugins.smartfile.fs.toObjectSync(lookupPath);
|
||||
} else {
|
||||
plugins.beautylog.error(`There is no data for ${toolNameArg}`);
|
||||
plugins.beautylog.warn(`${lookupPath} is misssing!`);
|
||||
allData = {};
|
||||
};
|
||||
|
||||
//assign all data
|
||||
allData = plugins.lodash.merge({},optionsArg.defaultSettings,allData);
|
||||
if(optionsArg.toolName){
|
||||
if(allData[optionsArg.toolName]){
|
||||
return allData[optionsArg.toolName];
|
||||
} else {
|
||||
plugins.beautylog.error(`There is no data for ${optionsArg.toolName}`);
|
||||
return undefined;
|
||||
}
|
||||
} else {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import "typings-global";
|
||||
export import beautylog = require("beautylog");
|
||||
export import lodash = require("lodash");
|
||||
export import path = require("path");
|
||||
export import smartfile = require("smartfile");
|
||||
export import q = require("q");
|
||||
|
Loading…
Reference in New Issue
Block a user