Skip to content

Commit

Permalink
feat: (strf-8671) replace "wreck" with "node-fetch"
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Sep 21, 2020
1 parent c51d6dd commit 6dcdd9b
Show file tree
Hide file tree
Showing 13 changed files with 517 additions and 374 deletions.
6 changes: 4 additions & 2 deletions bin/stencil-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ async function run (opts) {
try {
await promisify(stencilDownload)(opts);
} catch (err) {
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`);
themeApiClient.printErrorMessages(err.messages);
console.log('\n\n' + 'not ok'.red + ` -- ` + err);
if (err.messages) {
themeApiClient.printErrorMessages(err.messages);
}
console.log('If this error persists, please visit https://github.com/bigcommerce/stencil-cli/issues and submit an issue.');
return;
}
Expand Down
6 changes: 4 additions & 2 deletions bin/stencil-pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const options = {

stencilPull(options, (err, result) => {
if (err) {
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`);
themeApiClient.printErrorMessages(err.messages);
console.log('\n\n' + 'not ok'.red + ` -- ` + err);
if (err.messages) {
themeApiClient.printErrorMessages(err.messages);
}
console.log('If this error persists, please visit https://github.com/bigcommerce/stencil-cli/issues and submit an issue.');
} else {
console.log('ok'.green + ` -- Pulled active theme config to ${result.saveConfigName}`);
Expand Down
6 changes: 4 additions & 2 deletions bin/stencil-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ const options = {
};
stencilPush(options, (err, result) => {
if (err) {
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`);
themeApiClient.printErrorMessages(err.messages);
console.log('\n\n' + 'not ok'.red + ` -- ` + err);
if (err.messages) {
themeApiClient.printErrorMessages(err.messages);
}
console.log('If this error persists, please visit https://github.com/bigcommerce/stencil-cli/issues and submit an issue.');
} else {
console.log('ok'.green + ` -- ${result}`);
Expand Down
97 changes: 59 additions & 38 deletions bin/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('colors');
const Bs = require('browser-sync').create();
const recursiveRead = require('recursive-readdir');
const Async = require('async');
const Wreck = require('wreck');
const fetch = require('node-fetch');
const Fs = require('fs');
const Path = require('path');
const Url = require('url');
Expand Down Expand Up @@ -84,45 +84,66 @@ if (!(dotStencilFile.normalStoreUrl) || !(dotStencilFile.customLayouts)) {
process.exit(2);
}

let staplerUrl;
const headers = {
'stencil-cli': PACKAGE_INFO.version,
};
if (dotStencilFile.staplerUrl) {
staplerUrl = dotStencilFile.staplerUrl;
headers['stencil-store-url'] = dotStencilFile.normalStoreUrl;
} else {
staplerUrl = dotStencilFile.normalStoreUrl;
}
runAPICheck(dotStencilFile, PACKAGE_INFO.version)
.then(storeInfoFromAPI => {
dotStencilFile.storeUrl = storeInfoFromAPI.sslUrl;
dotStencilFile.normalStoreUrl = storeInfoFromAPI.baseUrl;
dotStencilFile.stencilServerPort = stencilServerPort;
})
.then(() => {
startServer();
})
.catch(err => console.error(err.message));

Wreck.get(
Url.resolve(staplerUrl, '/stencil-version-check?v=' + PACKAGE_INFO.version),
{
headers: headers,
json: true,
rejectUnauthorized: false,
},
function (err, res, payload) {
if (err || !payload) {
console.error(
'The BigCommerce Store you are pointing to either does not exist or is not available at this time.'.red,
);
} else if (payload.error) {
return console.error(payload.error.red);
} else if (payload.status !== 'ok') {
console.error(
'Error: You are using an outdated version of stencil-cli, please run '.red +
'$ npm install -g @bigcommerce/stencil-cli'.cyan,
);
} else {
dotStencilFile.storeUrl = payload.sslUrl;
dotStencilFile.normalStoreUrl = payload.baseUrl;
dotStencilFile.stencilServerPort = stencilServerPort;
/**
*
* @param {object} stencilConfig
* @param {string} currentCliVersion
* @returns {Promise<object>}
*/
async function runAPICheck (stencilConfig, currentCliVersion) {
const staplerUrl = stencilConfig.staplerUrl
? stencilConfig.staplerUrl
: stencilConfig.normalStoreUrl;
const reqUrl = Url.resolve(staplerUrl, '/stencil-version-check?v=' + currentCliVersion);
let payload;

const headers = {
'stencil-cli': currentCliVersion,
};
if (stencilConfig.staplerUrl) {
headers['stencil-store-url'] = stencilConfig.normalStoreUrl;
}

return startServer();
try {
const response = await fetch(reqUrl, { headers });
if (!response.ok) {
throw new Error(response.statusText);
}
},
);
payload = await response.json();
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;
}

/**
* Starts up the local Stencil Server as well as starts up BrowserSync and sets some watch options.
Expand Down Expand Up @@ -262,7 +283,7 @@ function getStartUpInfo() {
information += 'Store URL: ' + dotStencilFile.normalStoreUrl.cyan + '\n';

if (dotStencilFile.staplerUrl) {
information += 'Stapler URL: ' + staplerUrl.cyan + '\n';
information += 'Stapler URL: ' + dotStencilFile.staplerUrl.cyan + '\n';
}

information += 'SSL Store URL: ' + dotStencilFile.storeUrl.cyan + '\n';
Expand Down
18 changes: 7 additions & 11 deletions lib/stencil-push.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Wreck = require('wreck');
const fetchMock = require('node-fetch');
const { promisify } = require('util');

const StencilPush = require('./stencil-push');
Expand All @@ -9,11 +9,12 @@ const { MockDB } = require('../test/_mocks/MockDB');

const mockDb = new MockDB();

jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox());

describe('stencil push', () => {
beforeEach(() => {
jest.spyOn(Wreck, 'get').mockImplementation(requestStub);
jest.spyOn(Wreck, 'post').mockImplementation(requestStub);
jest.spyOn(Wreck, 'put').mockImplementation(requestStub);
fetchMock.mock('*', mockDb.data);

jest.spyOn(utils, 'generateBundle').mockImplementation(utilStub({
bundleZipPath: 'bundleZipPath',
}));
Expand All @@ -24,19 +25,14 @@ describe('stencil push', () => {
variationId: 'bold',
}));

function requestStub(url, options, callback) {
process.nextTick(() => {
callback(null, { statusCode: 200 }, mockDb.data);
});
}

function utilStub(data) {
return async options => ({...options, ...data});
}
});

afterEach(() => {
jest.restoreAllMocks();
fetchMock.mockReset();
mockDb.data = {};
});

Expand All @@ -59,6 +55,6 @@ describe('stencil push', () => {

await expect(
promisify(StencilPush)({ dotStencilFilePath }),
).rejects.toThrow('Failed to retrieve store hash');
).rejects.toThrow('Received empty store_hash value in the server response');
});
});
Loading

0 comments on commit 6dcdd9b

Please sign in to comment.