Skip to content

Commit

Permalink
fix: strf-8574, remove redundant dependency "hoek"
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Aug 10, 2020
1 parent c535b9b commit 9aeb3c1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 56 deletions.
23 changes: 10 additions & 13 deletions lib/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ const Fs = require('fs');
const { promisify } = require("util");
const Path = require('path');
const Inquirer = require('inquirer');
const hoek = require('hoek');

const jsonLint = require('./json-lint');
const themePath = process.cwd();

async function performAnswers(JspmAssembler, ThemeConfig, stencilConfig, dotStencilFilePath, answers) {
// Check for custom layout configurations
// If already set, do nothing otherwise write the empty configurations
if (!stencilConfig || stencilConfig && !stencilConfig.customLayouts) {
answers.customLayouts = {
const performedStencilConfig = {
customLayouts: {
'brand': {},
'category': {},
'page': {},
'product': {},
};
}

const performedStencilConfig = stencilConfig ? hoek.applyToDefaults(stencilConfig, answers) : answers;
},
...stencilConfig,
...answers,
};

Fs.writeFileSync(dotStencilFilePath, JSON.stringify(performedStencilConfig, null, 2));

Expand All @@ -44,7 +41,7 @@ async function performAnswers(JspmAssembler, ThemeConfig, stencilConfig, dotSten
}

async function run(JspmAssembler, ThemeConfig, dotStencilFilePath, url, token, port) {
let stencilConfig;
let stencilConfig = {};

if (Fs.existsSync(dotStencilFilePath)) {
const dotStencilFile = Fs.readFileSync(dotStencilFilePath, { encoding: 'utf-8' });
Expand All @@ -71,13 +68,13 @@ async function run(JspmAssembler, ThemeConfig, dotStencilFilePath, url, token, p
return 'You must enter a URL';
}
},
default: url || stencilConfig && stencilConfig.normalStoreUrl || undefined,
default: url || stencilConfig.normalStoreUrl,
},
{
type: 'input',
name: 'accessToken',
message: 'What is your Stencil OAuth Access Token?',
default: token || stencilConfig && stencilConfig.accessToken,
default: token || stencilConfig.accessToken,
filter: function(val) {
return val.trim();
},
Expand All @@ -86,7 +83,7 @@ async function run(JspmAssembler, ThemeConfig, dotStencilFilePath, url, token, p
type: 'input',
name: 'port',
message: 'What port would you like to run the server on?',
default: port || stencilConfig && stencilConfig.port || 3000,
default: port || stencilConfig.port || 3000,
validate: function (val) {
if (isNaN(val)) {
return 'You must enter an integer';
Expand Down
55 changes: 27 additions & 28 deletions lib/theme-config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var _ = require('lodash');
var Fs = require('fs');
var Hoek = require('hoek');
var Path = require('path');
var Glob = require('glob');
var Async = require('async');
var jsonLint = require('./json-lint');
const _ = require('lodash');
const Fs = require('fs');
const Path = require('path');
const Glob = require('glob');
const Async = require('async');
const jsonLint = require('./json-lint');

var themeConfigInstance;
let themeConfigInstance;

module.exports.getInstance = getInstance;

Expand Down Expand Up @@ -54,10 +53,10 @@ function ThemeConfig(themePath) {
* @returns {object}
*/
ThemeConfig.prototype.getConfig = function () {
var config = this.getRawConfig();
var variation = getVariation(config, this.variationIndex);
const config = this.getRawConfig();
const variation = getVariation(config, this.variationIndex);

this.globalSettings = Hoek.clone(config.settings);
this.globalSettings = _.cloneDeep(config.settings);

if (!this.currentVariationSettings) {
this.currentVariationSettings = variation.settings;
Expand All @@ -69,8 +68,8 @@ ThemeConfig.prototype.getConfig = function () {
config.autoprefixer_browsers = config.autoprefixer_browsers || ['> 1%', 'last 2 versions', 'Firefox ESR'];

// Merge in the variation settings and images objects
config.settings = Hoek.applyToDefaults(config.settings || {}, this.currentVariationSettings);
config.images = Hoek.applyToDefaults(config.images || {}, variation.images);
config.settings = _.defaultsDeep(this.currentVariationSettings, config.settings || {});
config.images = _.defaultsDeep(variation.images, config.images || {});
config.variationName = variation.name;

return config;
Expand All @@ -84,8 +83,8 @@ ThemeConfig.prototype.getConfig = function () {
* @returns {ThemeConfig}
*/
ThemeConfig.prototype.updateConfig = function (newSettings, saveToFile) {
var rawConfig;
var variation;
let rawConfig;
let variation;

// Remove all variation settings that match theme's settings
this.currentVariationSettings = _.omit(newSettings, function (value, key) {
Expand Down Expand Up @@ -166,8 +165,8 @@ ThemeConfig.prototype.getVariation = function (variationIndex) {
* @returns {ThemeConfig}
*/
ThemeConfig.prototype.setVariationByName = function (variationName) {
var config = this.getRawConfig();
var variationIndex = 0;
const config = this.getRawConfig();
let variationIndex = 0;

if (variationName) {
variationIndex = _.findIndex(config.variations, function (variation) {
Expand Down Expand Up @@ -234,7 +233,7 @@ ThemeConfig.prototype.getVariationName = function () {
* @return {Number}
*/
ThemeConfig.prototype.getVariationCount = function () {
var variations = this.getConfig().variations;
const variations = this.getConfig().variations;

if (!_.isArray(variations)) {
return 0;
Expand Down Expand Up @@ -305,8 +304,8 @@ ThemeConfig.prototype.schemaTranslationsExists = function () {
* @param {Function} callback
*/
ThemeConfig.prototype.getSchema = function (callback) {
var themeSchemaContent;
var themeSchema;
let themeSchemaContent;
let themeSchema;

try {
themeSchemaContent = Fs.readFileSync(this.schemaPath, {encoding: 'utf-8'});
Expand All @@ -332,7 +331,7 @@ ThemeConfig.prototype.getSchema = function (callback) {
}

Async.map(files, fetchThemeSettings, function (err, themeSettings) {
var forceReloadIds = {};
let forceReloadIds = {};

if (err) {
return callback(err);
Expand Down Expand Up @@ -361,11 +360,11 @@ ThemeConfig.prototype.getSchema = function (callback) {
* @param {Function} callback
*/
function fetchThemeSettings(path, callback) {
var themeSettingsRegexPattern = /\Wtheme_settings\.(.+?)\W/g;
var themeSettings = {};
const themeSettingsRegexPattern = /\Wtheme_settings\.(.+?)\W/g;
const themeSettings = {};

Fs.readFile(path, 'utf8', function (err, content) {
var match;
let match;

if (err) {
return callback(err);
Expand All @@ -377,16 +376,16 @@ function fetchThemeSettings(path, callback) {

return callback(null, themeSettings);
});
};
}

/**
* Get Schema Translations
*
* @param {Function} callback
*/
ThemeConfig.prototype.getSchemaTranslations = function(callback) {
var schemaTranslationsContent;
var schemaTranslations;
let schemaTranslationsContent;
let schemaTranslations;

try {
schemaTranslationsContent = Fs.readFileSync(this.schemaTranslationsPath, {encoding: 'utf-8'});
Expand Down Expand Up @@ -444,7 +443,7 @@ ThemeConfig.prototype.getRawSchemaTranslations = function() {
* @returns {object}
*/
function getVariation(config, variationIndex) {
var variation;
let variation;

if (! _.isArray(config.variations) || config.variations.length === 0) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions lib/theme-config.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Code = require('code');
const Hoek = require('hoek');
const _ = require('lodash');
const Lab = require('@hapi/lab');
const Sinon = require('sinon');
const Path = require('path');
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('ThemeConfig', () => {
lab.beforeEach(async () => {
themeConfig = ThemeConfig.getInstance();
config = themeConfig.getConfig();
originalSettings = Hoek.clone(config.settings);
originalSettings = _.cloneDeep(config.settings);
newSettings = {
color: '#000000',
font: 'Sans Something',
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"good-console": "^4.1.0",
"graceful-fs": "^4.1.11",
"hapi": "^8.4.0",
"hoek": "^2.12.0",
"image-size": "^0.4.0",
"inquirer": "^7.3.3",
"jsonlint": "^1.6.2",
Expand Down
4 changes: 0 additions & 4 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const Glue = require('glue');
const Hoek = require('hoek');
const Url = require('url');
const manifest = require('./manifest');
const logo = require('./lib/show-logo');
Expand All @@ -13,8 +12,6 @@ module.exports = (options, callback) => {
const parsedSecureUrl = Url.parse(options.dotStencilFile.storeUrl); //The url to a secure page (prompted as login page)
const parsedNormalUrl = Url.parse(options.dotStencilFile.normalStoreUrl); //The host url of the homepage;

callback = Hoek.nextTick(callback);

config.connections[0].port = options.dotStencilFile.port;
config.plugins['./plugins/router/router.module'].storeUrl = parsedSecureUrl.protocol + '//' + parsedSecureUrl.host;
config.plugins['./plugins/router/router.module'].normalStoreUrl = parsedNormalUrl.protocol + '//' + parsedNormalUrl.host;
Expand All @@ -38,6 +35,5 @@ module.exports = (options, callback) => {
console.log(logo);
return callback(null, server);
});

});
};
7 changes: 3 additions & 4 deletions server/plugins/renderer/renderer.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const Boom = require('@hapi/boom');
const Cache = require('memory-cache');
const Crypto = require('crypto');
const Frontmatter = require('front-matter');
const Hoek = require('hoek');
const Path = require('path');
const LangAssembler = require('../../../lib/lang-assembler');
const Pkg = require('../../../package.json');
Expand All @@ -21,7 +20,7 @@ const internals = {
};

module.exports.register = function (server, options, next) {
internals.options = Hoek.applyToDefaults(internals.options, options);
internals.options = _.defaultsDeep(options, internals.options);

server.expose('implementation', internals.implementation);

Expand Down Expand Up @@ -440,7 +439,7 @@ internals.getHeaders = function (request, options, config) {
headers = {
'stencil-cli': Pkg.version,
'stencil-version': Pkg.config.stencil_version,
'stencil-options': JSON.stringify(Hoek.applyToDefaults(options, currentOptions)),
'stencil-options': JSON.stringify(_.defaultsDeep(currentOptions, options)),
'accept-encoding': 'identity',
};

Expand All @@ -454,7 +453,7 @@ internals.getHeaders = function (request, options, config) {
headers['stencil-store-url'] = request.app.storeUrl;
}

return Hoek.applyToDefaults(request.headers, headers);
return { ...request.headers, ...headers };
};

/**
Expand Down
4 changes: 2 additions & 2 deletions server/plugins/router/router.module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hoek = require('hoek');
const _ = require('lodash');
const ThemeConfig = require('../../../lib/theme-config');
const internals = {
options: {
Expand All @@ -20,7 +20,7 @@ const internals = {
};

module.exports.register = function(server, options, next) {
internals.options = Hoek.applyToDefaults(internals.options, options);
internals.options = _.defaultsDeep(options, internals.options);

server.ext('onRequest', function(request, reply) {
request.app.storeUrl = internals.options.storeUrl;
Expand Down
4 changes: 2 additions & 2 deletions server/plugins/theme-assets/theme-assets.module.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const _ = require('lodash');
const Boom = require('@hapi/boom');
const CssAssembler = require('../../../lib/css-assembler');
const Utils = require('../../lib/utils');
const Hoek = require('hoek');
const Path = require('path');
const StencilStyles = require('@bigcommerce/stencil-styles');
const internals = {
options: {},
};

module.exports.register = function (server, options, next) {
internals.options = Hoek.applyToDefaults(internals.options, options);
internals.options = _.defaultsDeep(options, internals.options);

server.expose('cssHandler', internals.cssHandler);
server.expose('assetHandler', internals.assetHandler);
Expand Down

0 comments on commit 9aeb3c1

Please sign in to comment.