Skip to content

Commit

Permalink
Added ability to set config in package.json (#65)
Browse files Browse the repository at this point in the history
* Added ability to set config in package.json

* Added tests for allowing settings to be in package.json
  • Loading branch information
jtwebb authored and rejas committed Mar 26, 2019
1 parent 3771481 commit 5ae2154
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
15 changes: 15 additions & 0 deletions bin/customizr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ function help() {
return out.join("\n");
}

function configInPackage() {
var config = JSON.parse(fs.readFileSync('package.json'));
if ('customizr' in config) {
return config.customizr;
}

return {};
}

if (opts.help) {
process.stdout.write(help());
return process.exit();
Expand All @@ -46,10 +55,16 @@ if (opts.help) {
if (opts.config) {
if (fs.existsSync(opts.config)) {
config = JSON.parse(fs.readFileSync(opts.config));

if (path.basename(opts.config) === 'package.json') {
config = configInPackage();
}
} else {
process.stderr.write("Path does not exist.");
return process.exit();
}
} else {
config = configInPackage();
}

return customizr(config, process.exit);
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@
"fs-extra": "^7.0.1",
"mocha": "^6.0.2",
"nexpect": "^0.5.0"
},
"customizr": {
"dest": "build/modernizr-package.js",
"files": {
"src": [
"test/{js,css}/*"
]
}
}
}
51 changes: 51 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,54 @@ describe("custom builds", function () {

});
});

describe("settings", function() {
var testsLength = testArray.length,
existingBuild = path.join(cwd, "build", "modernizr-package.js");

it("should honor configs in package.json", function (done) {
process.stdout.write("\n\n");

nexpect.spawn(cli, [
"--config", 'package.json'
], {
stripColors: true
})

.wait("Looking for Modernizr references")

.wait(">> " + testsLength + " matches in")
.expect(">> " + tests)

.expect(">> " + testsLength + " matches in")
.expect(">> " + tests)

.expect(">> " + testsLength + " matches in")
.expect(">> " + tests)

.expect(">> Ready to build using these settings:")

.wait("Building your customized Modernizr").wait("OK")
.expect(">> Success! Saved file to build/modernizr-package.js")

.run(function (err) {
if (!err) {
done();
} else {
throw err;
}
});

describe("should include all tests", function () {
var contents;

testArray.forEach(function (test) {
it(test, function (done) {
contents = contents || fs.readFileSync(existingBuild, "utf8");
expect(contents.indexOf(test)).to.not.equal(-1);
done();
});
});
});
});
});

0 comments on commit 5ae2154

Please sign in to comment.