Skip to content

Commit

Permalink
[INTERNAL] Refactored a few modules
Browse files Browse the repository at this point in the history
  • Loading branch information
romaniam committed Jan 3, 2019
1 parent 29fc902 commit c2b125f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const initCommand = {

initCommand.lazyRequireDependencies = async function() {
return {
validate: require("../../utils/validate"),
fsHelper: require("../../utils/fsHelper"),
init: require("../../init/init"),
promisify: require("util").promisify,
path: require("path"),
Expand All @@ -17,11 +17,11 @@ initCommand.lazyRequireDependencies = async function() {
};

initCommand.handler = async function() {
const {validate, init, promisify, path, fs, jsYaml} = initCommand.lazyRequireDependencies();
const {fsHelper, init, promisify, path, fs, jsYaml} = initCommand.lazyRequireDependencies();
const writeFile = promisify(fs.writeFile);
try {
const yamlPath = path.resolve("./ui5.yaml");
if (await validate.exists(yamlPath)) {
if (await fsHelper.exists(yamlPath)) {
throw new Error("Initialization not possible: ui5.yaml already exists");
}

Expand Down
9 changes: 6 additions & 3 deletions lib/cli/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ serve.handler = function(argv = {}) {
cert: argv.h2 ? argv.cert : undefined,
key: argv.h2 ? argv.key : undefined
};
return !serverConfig.h2
? {serverConfig, tree}
: ui5Server.sslUtil.getSslCertificate(serverConfig.key, serverConfig.cert).then(({key, cert}) => {

if (!serverConfig.h2) {
return {serverConfig, tree};
} else {
return ui5Server.sslUtil.getSslCertificate(serverConfig.key, serverConfig.cert).then(({key, cert}) => {
serverConfig.key = key;
serverConfig.cert = cert;
return {serverConfig, tree};
});
}
}).then(({serverConfig, tree}) => {
return server.serve(tree, serverConfig).then(function({h2, port}) {
const protocol = h2 ? "https" : "http";
Expand Down
4 changes: 2 additions & 2 deletions lib/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {promisify} = require("util");
const path = require("path");
const fs = require("fs");
const readFile = promisify(fs.readFile);
const validate = require("../utils/validate");
const fsHelper = require("../utils/fsHelper");

/**
* Reads the package.json file and returns its content
Expand Down Expand Up @@ -87,7 +87,7 @@ async function init({cwd = "./"} = {}) {
throw new Error("Initialization not possible: Missing 'name' in package.json");
}

const [hasWebapp, hasSrc, hasTest] = await validate.pathsExist(["webapp", "src", "test"], cwd);
const [hasWebapp, hasSrc, hasTest] = await fsHelper.pathsExist(["webapp", "src", "test"], cwd);
projectConfig.type = getProjectType(hasWebapp, hasSrc, hasTest);

return projectConfig;
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions test/lib/cli/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {test} = require("ava");
const sinon = require("sinon");
const initCommand = require("../../../../lib/cli/commands/init");
const validate = require("../../../../lib/utils/validate");
const fsHelper = require("../../../../lib/utils/fsHelper");
const path = require("path");
const promisify = require("util").promisify;
const init = require("../../../../lib/init/init");
Expand All @@ -17,12 +17,12 @@ test.serial("Writes ui5.yaml to fs", async (t) => {
type: application`;

const pathStub = sinon.stub(path, "resolve").returns(ui5YamlPath);
const validateStub = sinon.stub(validate, "exists").resolves(false);
const fsHelperStub = sinon.stub(fsHelper, "exists").resolves(false);
const initStub = sinon.stub(init, "init").resolves({});
const safeDumpYamlStub = sinon.stub(jsYaml, "safeDump").returns(ui5Yaml);
const fsWriteFileStub = sinon.stub(fs, "writeFile").callsArgWith(2);
const lazyRequireStub = sinon.stub(initCommand, "lazyRequireDependencies").returns({
validate: validate,
fsHelper: fsHelper,
init: init,
promisify: promisify,
path: path,
Expand All @@ -35,7 +35,7 @@ test.serial("Writes ui5.yaml to fs", async (t) => {
t.is(fsWriteFileStub.getCall(0).args[0], ui5YamlPath, "Passes yaml path to write the yaml file to");
t.is(fsWriteFileStub.getCall(0).args[1], ui5Yaml, "Passes yaml content to write to fs");

validateStub.restore();
fsHelperStub.restore();
pathStub.restore();
initStub.restore();
safeDumpYamlStub.restore();
Expand All @@ -45,7 +45,7 @@ test.serial("Writes ui5.yaml to fs", async (t) => {

// FIXME: stubbing process leads to errors, need to be fixed
// test.serial("Error: throws if ui5.yaml already exists", async (t) => {
// const fileExistsStub = sinon.stub(validate, "exists").resolves(true);
// const fileExistsStub = sinon.stub(fsHelper, "exists").resolves(true);
// const processExitStub = sinon.stub(process, "exit");

// await initCommand.handler();
Expand Down
6 changes: 3 additions & 3 deletions test/lib/utils/validate.js → test/lib/utils/fsHelper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const {test} = require("ava");
const {exists, pathsExist} = require("../../../lib/utils/validate");
const {exists, pathsExist} = require("../../../lib/utils/fsHelper");

test("validates if directory or file exists", async (t) => {
test("check if directory or file exists", async (t) => {
t.is(await exists("./test/fixtures/init/application/ui5.yaml"), true, "ui5.yaml found in path");
t.is(await exists("./test/fixtures/init"), true, "directory exists in path");
});

test("validate if list of paths exist", async (t) => {
test("check if list of paths exist", async (t) => {
t.deepEqual(await pathsExist(["src", "test"], "./test/fixtures/init/library"), [true, true], "paths do exist");
});

Expand Down

0 comments on commit c2b125f

Please sign in to comment.