Skip to content

Commit

Permalink
fix: apply various PR fixes
Browse files Browse the repository at this point in the history
- updates JSDoc's that were out-of-date
- removes logic out of Rum() method
- accept required cliOptions oject at earliest stage
  • Loading branch information
NickTolhurst26 committed Sep 24, 2020
1 parent 8a56259 commit c5d964b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 50 deletions.
7 changes: 6 additions & 1 deletion bin/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ if (!versionCheck()) {
const dotStencilFilePath = './.stencil';
const cliOptions = _.pick(Program, ['url', 'token', 'port']);

new StencilInit().run(dotStencilFilePath, cliOptions);
new StencilInit().run(dotStencilFilePath,
{
normalStoreUrl: cliOptions.url,
accessToken: cliOptions.token,
port: cliOptions.port,
});
43 changes: 15 additions & 28 deletions lib/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,23 @@ class StencilInit {
/**
* @param {string} dotStencilFilePath
* @param {object} cliOptions
* @param {string} cliOptions.url
* @param {string} cliOptions.token
* @param {string} cliOptions.normalStoreUrl
* @param {string} cliOptions.accessToken
* @param {number} cliOptions.port
* @returns {Promise<void>}
*/
async run (dotStencilFilePath, cliOptions = {}) {

const cliConfig = this.getCliConfig(cliOptions);
const oldStencilConfig = this.readStencilConfig(dotStencilFilePath);
const defaultAnswers = this.getDefaultAnswers(oldStencilConfig);
const questions = this.getQuestions(defaultAnswers, cliConfig);
const answers = questions ? await this.askQuestions(questions) : {};
const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers, cliConfig);
const questions = this.getQuestions(defaultAnswers, cliOptions);
const answers = await this.askQuestions(questions);
const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers, cliOptions);
this.saveStencilConfig(updatedStencilConfig, dotStencilFilePath);

this.logger.log('You are now ready to go! To start developing, run $ ' + 'stencil start'.cyan);
}

/**
* @param {{port: (number), url: (string), token: (string)}} cliOptions
* @returns {{port: (number), normalStoreUrl: (string), accessToken: (string)}}
*/
getCliConfig (cliOptions) {
return {
normalStoreUrl: cliOptions.url,
accessToken: cliOptions.token,
port: cliOptions.port,
};
}

/**
* @param {string} dotStencilFilePath
* @returns {object}
Expand Down Expand Up @@ -97,13 +84,13 @@ class StencilInit {

/**
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} cliConfig
* @returns {{port: (number), normalStoreUrl: (string), accessToken: (string)}}
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} cliOptions
* @returns {{object[]}}
*/
getQuestions (defaultAnswers, cliConfig) {
getQuestions (defaultAnswers, cliOptions) {
const prompts = [];

if(!cliConfig.normalStoreUrl){
if(!cliOptions.normalStoreUrl){
prompts.push({
type: 'input',
name: 'normalStoreUrl',
Expand All @@ -113,7 +100,7 @@ class StencilInit {
});
}

if(!cliConfig.accessToken){
if(!cliOptions.accessToken){
prompts.push({
type: 'input',
name: 'accessToken',
Expand All @@ -123,7 +110,7 @@ class StencilInit {
});
}

if(!cliConfig.port){
if(!cliOptions.port){
prompts.push({
type: 'input',
name: 'port',
Expand All @@ -141,21 +128,21 @@ class StencilInit {
});
}

return prompts.length > 0 ? prompts : null;
return prompts;
}

/**
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers
* @param {{port: (number), url: (string), token: (string)}} cliOptions
* @param {{object[]}} cliOptions
* @returns {Promise<object>}
*/
async askQuestions (questions) {
return await this.inquirer.prompt(questions);
return questions.length ? this.inquirer.prompt(questions) : {};
}

/**
* @param {object} stencilConfig
* @param {object} answers
* @param {{port: (number), url: (string), token: (string)}} cliOptions
* @returns {object}
*/
applyAnswers (stencilConfig, answers, cliOptions) {
Expand Down
24 changes: 3 additions & 21 deletions lib/stencil-init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const getAnswers = () => ({
accessToken: "accessToken_from_answers",
});
const getCliOptions = () => ({
url: "https://url-from-cli-options.mybigcommerce.com",
normalStoreUrl: "https://url-from-cli-options.mybigcommerce.com",
port: 3002,
token: "accessToken_from_CLI_options",
accessToken: "accessToken_from_CLI_options",
});
const getQuestions = () => ([
{
Expand Down Expand Up @@ -102,12 +102,7 @@ describe('StencilInit integration tests', () => {
it('using cli options, should perform all the actions, save the result and inform the user about the successful finish', async () => {
const dotStencilFilePath = './test/_mocks/bin/dotStencilFile.json';
const cliOptions = getCliOptions();
const cliOptionsAsAnswers = {
"normalStoreUrl": cliOptions.url,
"port": cliOptions.port,
"accessToken": cliOptions.token,
};
const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...cliOptionsAsAnswers }, null, 2);
const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...cliOptions }, null, 2);
const fsWriteFileSyncStub = jest.spyOn(fs, "writeFileSync").mockImplementation(jest.fn());
const inquirerPromptStub = jest.spyOn(inquirer, 'prompt').mockReturnValue({});
const consoleErrorStub = jest.spyOn(console, 'error').mockImplementation(jest.fn());
Expand Down Expand Up @@ -189,19 +184,6 @@ describe('StencilInit unit tests', () => {
});
});

describe('getCliConfig ', () => {
it('should return config object with cli option values', async () => {
const instance = getStencilInitInstance();
const cliOptions = getCliOptions();

const res = instance.getCliConfig(cliOptions);

expect(res.normalStoreUrl).toEqual(cliOptions.url);
expect(res.accessToken).toEqual(cliOptions.token);
expect(res.port).toEqual(cliOptions.port);
});
});

describe('readStencilConfig ', () => {
it('should return an empty config if the file doesn\'t exist', async () => {
const instance = getStencilInitInstance();
Expand Down

0 comments on commit c5d964b

Please sign in to comment.