Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
refs opencomponents#436 Monkey patching..
Browse files Browse the repository at this point in the history
  • Loading branch information
elboletaire committed Apr 7, 2017
1 parent 0fad4df commit a05932c
Show file tree
Hide file tree
Showing 77 changed files with 508 additions and 500 deletions.
14 changes: 7 additions & 7 deletions client/src/components-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const _ = require('./utils/helpers');

module.exports = function(config, renderTemplate){

let cache = new Cache(config.cache),
getComponentsData = new GetComponentsData(config),
renderComponents = new RenderComponents(cache, renderTemplate),
processClientReponses = new ProcessClientResponse(cache, config);
const cache = new Cache(config.cache),
getComponentsData = new GetComponentsData(config),
renderComponents = new RenderComponents(cache, renderTemplate),
processClientReponses = new ProcessClientResponse(cache, config);

return function(components, options, callback){

Expand All @@ -35,9 +35,9 @@ module.exports = function(config, renderTemplate){
getComponentsData(toDo, options, function(){
renderComponents(toDo, options, function(){
processClientReponses(toDo, options, function(){
let errors = [],
results = [],
hasErrors = false;
const errors = [],
results = [];
let hasErrors = false;

_.each(toDo, function(action){
if(action.result.error) {
Expand Down
6 changes: 3 additions & 3 deletions client/src/get-components-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ module.exports = function(config){
};

return function(toDo, options, cb){
let serverRenderingFail = settings.serverSideRenderingFail,
serverRendering = { components: [], positions: [] },
serverRenderingEndpoint = hrefBuilder.server(options);
const serverRenderingFail = settings.serverSideRenderingFail,
serverRendering = { components: [], positions: [] },
serverRenderingEndpoint = hrefBuilder.server(options);

_.each(toDo, function(action){
if(action.render === 'server'){
Expand Down
12 changes: 6 additions & 6 deletions client/src/href-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module.exports = function(config){
throw settings.missingComponentName;
}

let lang = options.headers['accept-language'],
forwardLang = config.forwardAcceptLanguageToClient === true;
const lang = options.headers['accept-language'];
let forwardLang = config.forwardAcceptLanguageToClient === true;

if(!forwardLang && options.forwardAcceptLanguageToClient === true){
forwardLang = true;
Expand All @@ -34,10 +34,10 @@ module.exports = function(config){
component.parameters['__ocAcceptLanguage'] = lang;
}

let versionSegment = component.version ? ('/' + component.version) : '',
registryUrl = clientRenderingEndpoint,
registrySegment = registryUrl.slice(-1) === '/' ? registryUrl : (registryUrl + '/'),
qs = component.parameters ? ('/?' + querystring.stringify(component.parameters)) : '';
const versionSegment = component.version ? ('/' + component.version) : '',
registryUrl = clientRenderingEndpoint,
registrySegment = registryUrl.slice(-1) === '/' ? registryUrl : (registryUrl + '/'),
qs = component.parameters ? ('/?' + querystring.stringify(component.parameters)) : '';

return url.resolve(registrySegment, component.name) + versionSegment + qs;
},
Expand Down
10 changes: 5 additions & 5 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const _ = require('./utils/helpers');

module.exports = function(conf){

let config = sanitiser.sanitiseConfiguration(conf),
validationResult = validator.validateConfiguration(config),
renderTemplate = new TemplateRenderer(),
renderComponents = new ComponentsRenderer(config, renderTemplate),
getComponentsInfo = new GetComponentsInfo(config);
const config = sanitiser.sanitiseConfiguration(conf),
validationResult = validator.validateConfiguration(config),
renderTemplate = new TemplateRenderer(),
renderComponents = new ComponentsRenderer(config, renderTemplate),
getComponentsInfo = new GetComponentsInfo(config);

if(!validationResult.isValid){
throw new Error(validationResult.error);
Expand Down
4 changes: 2 additions & 2 deletions client/src/process-client-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const _ = require('./utils/helpers');

module.exports = function(cache, config){

let getOCClientScript = new GetOCClientScript(cache),
buildHref = new HrefBuilder(config);
const getOCClientScript = new GetOCClientScript(cache),
buildHref = new HrefBuilder(config);

return function(toDo, options, cb){
const toProcess = [];
Expand Down
6 changes: 3 additions & 3 deletions client/src/render-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module.exports = function(cache, renderTemplate){

const fetchTemplateAndRender = function(component, options, cb){

let data = component.data,
isLocal = component.type === 'oc-component-local',
useCache = !isLocal;
const data = component.data,
isLocal = component.type === 'oc-component-local',
useCache = !isLocal;

getCompiledTemplate(component.template, useCache, options.timeout, function(err, template){
if(err){ return cb(err); }
Expand Down
2 changes: 1 addition & 1 deletion client/src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var _ = {
const _ = {
each: function(obj, fn){
if(_.isArray(obj)){
for(let i = 0; i < obj.length; i++){
Expand Down
16 changes: 8 additions & 8 deletions client/src/warmup.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = function(config, renderComponents){
options.timeout = options.timeout || 5;
options.headers = options.headers || {};

let urls = [],
toWarmup = [];
const urls = [],
toWarmup = [];

_.each(config.components, function(version, name){
const versionSegment = version ? (version + '/') : '';
Expand All @@ -48,12 +48,12 @@ module.exports = function(config, renderComponents){
return next(new Error(format(settings.warmupFailed, JSON.stringify(requestDetails), err)));
}

let parameters = componentInfo.oc.parameters,
componentToWarmup = {
name: componentInfo.name,
version: componentInfo.version,
parameters: {}
};
const parameters = componentInfo.oc.parameters,
componentToWarmup = {
name: componentInfo.name,
version: componentInfo.version,
parameters: {}
};

if(parameters){
_.each(parameters, function(value, parameter){
Expand Down
6 changes: 3 additions & 3 deletions src/cli/domain/get-components-by-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module.exports = function(){

const isOcComponent = function(file){

let filePath = path.resolve(componentsDir, file),
packagePath = path.join(filePath, 'package.json'),
content;
const filePath = path.resolve(componentsDir, file),
packagePath = path.join(filePath, 'package.json');
let content;

try {
content = fs.readJsonSync(packagePath);
Expand Down
6 changes: 3 additions & 3 deletions src/cli/domain/get-local-npm-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ module.exports = function(){

return fs.readdirSync(nodeFolder).filter(function(file){

let filePath = path.resolve(nodeFolder, file),
isBin = file === '.bin',
isDir = fs.lstatSync(filePath).isDirectory();
const filePath = path.resolve(nodeFolder, file),
isBin = file === '.bin',
isDir = fs.lstatSync(filePath).isDirectory();

return isDir && !isBin;
});
Expand Down
4 changes: 2 additions & 2 deletions src/cli/domain/get-missing-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = function(dependencies){

_.forEach(dependencies, function(npmModule){

let index = npmModule.indexOf('@'),
moduleName = npmModule;
const index = npmModule.indexOf('@');
let moduleName = npmModule;

if (index > 0) {
moduleName = npmModule.substr(0, index);
Expand Down
20 changes: 10 additions & 10 deletions src/cli/domain/get-mocked-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ const registerDynamicMocks = function(ocJsonLocation, mocks, logger){
}).filter(function(p){ return p; });
};

var findPath = function(pathToResolve, fileName) {
const findPath = function(pathToResolve, fileName) {

let rootDir = fs.realpathSync('.'),
fileToResolve = path.join(pathToResolve, fileName);
const rootDir = fs.realpathSync('.'),
fileToResolve = path.join(pathToResolve, fileName);

if (!fs.existsSync(fileToResolve)) {
if (pathToResolve === rootDir) {
return undefined;
} else {
let getParent = function(x){ return x.split('/').slice(0, -1).join('/'); },
parentDir = pathToResolve ? getParent(pathToResolve) : rootDir;
const getParent = function(x){ return x.split('/').slice(0, -1).join('/'); },
parentDir = pathToResolve ? getParent(pathToResolve) : rootDir;

return findPath(parentDir, fileName);
}
Expand All @@ -76,16 +76,16 @@ var findPath = function(pathToResolve, fileName) {
module.exports = function(logger, componentsDir){
componentsDir = path.resolve(componentsDir || '.');

let plugins = [],
ocJsonFileName = settings.configFile.src.replace('./', ''),
ocJsonPath = findPath(componentsDir, ocJsonFileName);
let plugins = [];
const ocJsonFileName = settings.configFile.src.replace('./', ''),
ocJsonPath = findPath(componentsDir, ocJsonFileName);

if(!ocJsonPath){
return plugins;
}

let content = fs.readJsonSync(ocJsonPath),
ocJsonLocation = ocJsonPath.slice(0, -ocJsonFileName.length);
const content = fs.readJsonSync(ocJsonPath),
ocJsonLocation = ocJsonPath.slice(0, -ocJsonFileName.length);

if(!content.mocks || !content.mocks.plugins){
return plugins;
Expand Down
8 changes: 4 additions & 4 deletions src/cli/domain/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ module.exports = function(){

try {

let pathDir = '../../components/base-component-' + templateType,
baseComponentDir = path.resolve(__dirname, pathDir),
npmIgnorePath = path.resolve(__dirname, pathDir + '/.npmignore');
const pathDir = '../../components/base-component-' + templateType,
baseComponentDir = path.resolve(__dirname, pathDir),
npmIgnorePath = path.resolve(__dirname, pathDir + '/.npmignore');

fs.ensureDirSync(componentName);
fs.copySync(baseComponentDir, componentName);
fs.copySync(npmIgnorePath, componentName + '/.gitignore');

let componentPath = path.resolve(componentName, 'package.json'),
const componentPath = path.resolve(componentName, 'package.json'),
component = _.extend(fs.readJsonSync(componentPath), {
name: componentName
});
Expand Down
12 changes: 6 additions & 6 deletions src/cli/domain/package-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ module.exports = function(){
const componentPath = options.componentPath;
const minify = options.minify === true;

let files = fs.readdirSync(componentPath),
publishPath = path.join(componentPath, '_package');
const files = fs.readdirSync(componentPath),
publishPath = path.join(componentPath, '_package');

if(_.contains(files, '_package')){
fs.removeSync(publishPath);
}

fs.mkdirSync(publishPath);

let componentPackagePath = path.join(componentPath, 'package.json'),
ocPackagePath = path.join(__dirname, '../../../package.json');
const componentPackagePath = path.join(componentPath, 'package.json'),
ocPackagePath = path.join(__dirname, '../../../package.json');

if(!fs.existsSync(componentPackagePath)){
return callback(new Error('component does not contain package.json'));
} else if(!fs.existsSync(ocPackagePath)){
return callback(new Error('error resolving oc internal dependencies'));
}

let component = fs.readJsonSync(componentPackagePath),
ocInfo = fs.readJsonSync(ocPackagePath);
const component = fs.readJsonSync(componentPackagePath),
ocInfo = fs.readJsonSync(ocPackagePath);

if(!validator.validateComponentName(component.name)){
return callback(new Error('name not valid'));
Expand Down
20 changes: 10 additions & 10 deletions src/cli/domain/package-static-files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const _ = require('underscore');
const strings = require('../../../resources');

const copyDir = function(params, cb){
let staticPath = path.join(params.componentPath, params.staticDir),
exists = fs.existsSync(staticPath),
isDir = exists && fs.lstatSync(staticPath).isDirectory();
const staticPath = path.join(params.componentPath, params.staticDir),
exists = fs.existsSync(staticPath),
isDir = exists && fs.lstatSync(staticPath).isDirectory();

if(!exists){
return cb(format(strings.errors.cli.FOLDER_NOT_FOUND, staticPath));
Expand All @@ -24,17 +24,17 @@ const copyDir = function(params, cb){
nodeDir.paths(staticPath, function(err, res){
_.forEach(res.files, function(filePath){

let fileName = path.basename(filePath),
fileExt = path.extname(filePath).toLowerCase(),
fileRelativePath = path.relative(staticPath, path.dirname(filePath)),
fileDestinationPath = path.join(params.publishPath, params.staticDir, fileRelativePath),
fileDestination = path.join(fileDestinationPath, fileName);
const fileName = path.basename(filePath),
fileExt = path.extname(filePath).toLowerCase(),
fileRelativePath = path.relative(staticPath, path.dirname(filePath)),
fileDestinationPath = path.join(params.publishPath, params.staticDir, fileRelativePath),
fileDestination = path.join(fileDestinationPath, fileName);

fs.ensureDirSync(fileDestinationPath);

if(params.minify && params.ocOptions.minify !== false && (fileExt === '.js' || fileExt === '.css')){
let fileContent = fs.readFileSync(filePath).toString(),
minified = minifyFile(fileExt, fileContent);
const fileContent = fs.readFileSync(filePath).toString(),
minified = minifyFile(fileExt, fileContent);

fs.writeFileSync(fileDestination, minified);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/domain/package-static-files/minify-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = function(fileExt, fileContent){
modules: false
};

let babelOptions = { presets: [[babelPresetEnv, presetOptions]] },
es5 = babel.transform(fileContent, babelOptions).code;
const babelOptions = { presets: [[babelPresetEnv, presetOptions]] },
es5 = babel.transform(fileContent, babelOptions).code;

return uglifyJs.minify(es5, { fromString: true }).code;

Expand Down
8 changes: 4 additions & 4 deletions src/cli/domain/package-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const compileView = function(viewPath, type, cb) {
ocTemplate.compile({ template, viewPath }, function(err, compiledView){
if (err) { return cb(err);}

let hashView = hashBuilder.fromString(compiledView.toString()),
javaScriptizedView = javaScriptizeTemplate(hashView, compiledView);
const hashView = hashBuilder.fromString(compiledView.toString()),
javaScriptizedView = javaScriptizeTemplate(hashView, compiledView);

return cb(null, {
hash: hashView,
Expand All @@ -42,8 +42,8 @@ const compileView = function(viewPath, type, cb) {

module.exports = function(params, callback){

let viewSrc = params.ocOptions.files.template.src,
viewPath = path.join(params.componentPath, viewSrc);
const viewSrc = params.ocOptions.files.template.src,
viewPath = path.join(params.componentPath, viewSrc);

if(!fs.existsSync(viewPath)){
return callback(format(strings.errors.cli.TEMPLATE_NOT_FOUND, viewSrc));
Expand Down
4 changes: 2 additions & 2 deletions src/cli/domain/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const urlParser = require('../domain/url-parser');

const getOcVersion = function(){

let ocPackagePath = path.join(__dirname, '../../../package.json'),
ocInfo = fs.readJsonSync(ocPackagePath);
const ocPackagePath = path.join(__dirname, '../../../package.json'),
ocInfo = fs.readJsonSync(ocPackagePath);

return ocInfo.version;
};
Expand Down
14 changes: 7 additions & 7 deletions src/cli/domain/url-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const removeFinalSlashes = function(s){
module.exports = {
parse: function(parsed){

let requestedVersion = parsed.requestVersion,
href = url.parse(parsed.href),
relativePath = removeFinalSlashes(href.pathname),
withoutVersion = removeFinalSlashes(relativePath.replace(requestedVersion, '')),
componentName = withoutVersion.substr(withoutVersion.lastIndexOf('/') + 1),
withoutComponent = removeFinalSlashes(withoutVersion.replace(componentName, '')),
registryUrl = href.protocol + '//' + href.host + withoutComponent + '/';
const requestedVersion = parsed.requestVersion,
href = url.parse(parsed.href),
relativePath = removeFinalSlashes(href.pathname),
withoutVersion = removeFinalSlashes(relativePath.replace(requestedVersion, '')),
componentName = withoutVersion.substr(withoutVersion.lastIndexOf('/') + 1),
withoutComponent = removeFinalSlashes(withoutVersion.replace(componentName, '')),
registryUrl = href.protocol + '//' + href.host + withoutComponent + '/';

return {
clientHref: registryUrl + 'oc-client/client.js',
Expand Down
Loading

0 comments on commit a05932c

Please sign in to comment.