Skip to content

Commit

Permalink
refactor: move version check request from StencilStart to themeApiClient
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Mar 30, 2021
1 parent 768c815 commit 06f8d61
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bin/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ program
)
.parse(process.argv);

new StencilStart().run(program.opts(), PACKAGE_INFO.version).catch(printCliResultErrorAndExit);
new StencilStart().run(program.opts()).catch(printCliResultErrorAndExit);
56 changes: 8 additions & 48 deletions lib/stencil-start.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('colors');
const BrowserSync = require('browser-sync');
const { promisify } = require('util');
const fsModule = require('fs');
const path = require('path');

const Cycles = require('./Cycles');
Expand All @@ -13,13 +12,12 @@ const ThemeConfig = require('./theme-config');
const BuildConfigManager = require('./BuildConfigManager');
const fsUtilsModule = require('./utils/fsUtils');
const cliCommonModule = require('./cliCommon');
const NetworkUtils = require('./utils/NetworkUtils');
const themeApiClientModule = require('./theme-api-client');

class StencilStart {
constructor({
browserSync = BrowserSync.create(),
networkUtils = new NetworkUtils(),
fs = fsModule,
themeApiClient = themeApiClientModule,
fsUtils = fsUtilsModule,
cliCommon = cliCommonModule,
stencilConfigManager = new StencilConfigManager(),
Expand All @@ -30,8 +28,7 @@ class StencilStart {
logger = console,
} = {}) {
this._browserSync = browserSync;
this._networkUtils = networkUtils;
this._fs = fs;
this._themeApiClient = themeApiClient;
this._fsUtils = fsUtils;
this._cliCommon = cliCommon;
this._stencilConfigManager = stencilConfigManager;
Expand All @@ -42,7 +39,7 @@ class StencilStart {
this._logger = logger;
}

async run(cliOptions, stencilCliVersion) {
async run(cliOptions) {
this.runBasicChecks(cliOptions);

if (cliOptions.variation) {
Expand All @@ -53,10 +50,9 @@ class StencilStart {
// Use initial (before updates) port for BrowserSync
const browserSyncPort = initialStencilConfig.port;

const storeInfoFromAPI = await this.runAPICheck(
initialStencilConfig.normalStoreUrl,
stencilCliVersion,
);
const storeInfoFromAPI = await this._themeApiClient.checkCliVersion({
storeUrl: initialStencilConfig.normalStoreUrl,
});
const updatedStencilConfig = this.updateStencilConfig(
initialStencilConfig,
storeInfoFromAPI,
Expand All @@ -75,7 +71,7 @@ class StencilStart {
runBasicChecks(cliOptions) {
this._cliCommon.checkNodeVersion();

if (!this._fs.existsSync(this._themeConfigManager.configPath)) {
if (!this._fsUtils.existsSync(this._themeConfigManager.configPath)) {
throw new Error(
'You must have a '.red +
' config.json '.cyan +
Expand All @@ -89,42 +85,6 @@ class StencilStart {
}
}

/**
*
* @param {string} storeUrl
* @param {string} currentCliVersion
* @returns {Promise<object>}
*/
async runAPICheck(storeUrl, currentCliVersion) {
const url = new URL(`/stencil-version-check?v=${currentCliVersion}`, storeUrl).toString();
let payload;

try {
const response = await this._networkUtils.sendApiRequest({ url });
payload = response.data;
if (!payload) {
throw new Error('Empty payload in the server response');
}
} catch (err) {
throw new Error(
'The BigCommerce Store you are pointing to either does not exist or is not available at this time.'
.red +
'\nError details:\n' +
err.message,
);
}
if (payload.error) {
throw new Error(payload.error.red);
}
if (payload.status !== 'ok') {
throw new Error(
'Error: You are using an outdated version of stencil-cli, please run '.red +
'$ npm install -g @bigcommerce/stencil-cli'.cyan,
);
}
return payload;
}

updateStencilConfig(stencilConfig, storeInfoFromAPI) {
return {
...stencilConfig,
Expand Down
14 changes: 5 additions & 9 deletions lib/stencil-start.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ describe('StencilStart unit tests', () => {
watch: jest.fn(),
init: jest.fn(),
});
const getNetworkUtilsStub = () => ({
sendApiRequest: jest.fn(),
});
const getFsStub = () => ({
existsSync: jest.fn(),
const getThemeApiClientStub = () => ({
checkCliVersion: jest.fn(),
});
const getFsUtilsStub = () => ({
existsSync: jest.fn(),
parseJsonFile: jest.fn(),
recursiveReadDir: jest.fn(),
});
Expand All @@ -34,9 +32,8 @@ describe('StencilStart unit tests', () => {

const createStencilStartInstance = ({
browserSync,
fs,
fsUtils,
networkUtils,
themeApiClient,
cliCommon,
stencilConfigManager,
themeConfigManager,
Expand All @@ -47,9 +44,8 @@ describe('StencilStart unit tests', () => {
} = {}) => {
const passedArgs = {
browserSync: browserSync || getBrowserSyncStub(),
fs: fs || getFsStub(),
fsUtils: fsUtils || getFsUtilsStub(),
networkUtils: networkUtils || getNetworkUtilsStub(),
themeApiClient: themeApiClient || getThemeApiClientStub(),
cliCommon: cliCommon || getCliCommonStub(),
stencilConfigManager: stencilConfigManager || getStencilConfigManagerStub(),
themeConfigManager: themeConfigManager || getThemeConfigManagerStub(),
Expand Down
38 changes: 38 additions & 0 deletions lib/theme-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs');
const path = require('path');
const FormData = require('form-data');
const NetworkUtils = require('./utils/NetworkUtils');
const { PACKAGE_INFO } = require('../constants');

const networkUtils = new NetworkUtils();

Expand All @@ -25,6 +26,42 @@ async function getStoreHash({ storeUrl }) {
}
}

/**
* @param {object} options
* @param {string} options.storeUrl
* @param {string} [options.currentCliVersion]
* @returns {Promise<object>}
*/
async function checkCliVersion({ storeUrl, currentCliVersion = PACKAGE_INFO.version }) {
const url = new URL(`/stencil-version-check?v=${currentCliVersion}`, storeUrl).toString();
let payload;

try {
const response = await networkUtils.sendApiRequest({ url });
payload = response.data;
if (!payload) {
throw new Error('Empty payload in the server response');
}
} catch (err) {
throw new Error(
'The BigCommerce Store you are pointing to either does not exist or is not available at this time.'
.red +
'\nError details:\n' +
err.message,
);
}
if (payload.error) {
throw new Error(payload.error.red);
}
if (payload.status !== 'ok') {
throw new Error(
'Error: You are using an outdated version of stencil-cli, please run '.red +
'$ npm install -g @bigcommerce/stencil-cli'.cyan,
);
}
return payload;
}

/**
* @param {object} options
* @param {string} options.variationId
Expand Down Expand Up @@ -318,6 +355,7 @@ async function downloadTheme({ accessToken, apiHost, storeHash, themeId }) {

module.exports = {
getStoreHash,
checkCliVersion,
activateThemeByVariationId,
deleteThemeById,
getStoreChannels,
Expand Down
1 change: 1 addition & 0 deletions lib/utils/fsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function parseJsonFile(filePath) {
}

module.exports = {
...fs,
parseJsonFile,
recursiveReadDir,
};

0 comments on commit 06f8d61

Please sign in to comment.