Skip to content

Commit

Permalink
Merge pull request #507 from opentable/prettier-eslint
Browse files Browse the repository at this point in the history
Pretty printed code
  • Loading branch information
nickbalestra authored Jun 8, 2017
2 parents 8825c98 + 8a1dd2a commit cc22411
Show file tree
Hide file tree
Showing 98 changed files with 2,844 additions and 1,904 deletions.
19 changes: 11 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

const _ = require('lodash');

module.exports = function(grunt){

module.exports = function(grunt) {
const taskObject = { pkg: grunt.file.readJSON('package.json') };

grunt.file.expand('tasks/*.js', '!tasks/_*.js').forEach((file) => {
grunt.file.expand('tasks/*.js', '!tasks/_*.js').forEach(file => {
let name = file.split('/');
name = name[name.length - 1].replace('.js', '');
const task = require('./' + file);

if(_.isFunction(task)) {
if (_.isFunction(task)) {
task(grunt);
} else {
taskObject[name] = task;
Expand All @@ -22,10 +21,14 @@ module.exports = function(grunt){
require('load-grunt-tasks')(grunt);

grunt.registerTask('default', ['test-local', 'build']);
grunt.registerTask('sauce', ['karma:sauce-linux', 'karma:sauce-osx', 'karma:sauce-windows']);
grunt.registerTask('test-local', ['eslint', 'mochaTest:all', 'karma:local']);
grunt.registerTask('test-local-silent', ['eslint', 'mochaTest:silent', 'karma:local']);
grunt.registerTask('test', ['eslint', 'mochaTest:all']);
grunt.registerTask('sauce', [
'karma:sauce-linux',
'karma:sauce-osx',
'karma:sauce-windows'
]);
grunt.registerTask('test-local', ['mochaTest:all', 'karma:local']);
grunt.registerTask('test-local-silent', ['mochaTest:silent', 'karma:local']);
grunt.registerTask('test', ['mochaTest:all']);
grunt.registerTask('git-stage', [
'gitadd:versionFiles',
'gitcommit:version',
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
"oc": "./src/oc-cli.js"
},
"scripts": {
"precommit": "lint-staged",
"test": "grunt test"
},
"lint-staged": {
"*.js": [
"prettier-eslint --single-quote --write",
"git add"
]
},
"engines": {
"node": ">=4"
},
Expand All @@ -31,20 +38,22 @@
"chai": "3.5.0",
"cheerio": "0.22.0",
"grunt": "^1.0.1",
"grunt-eslint": "^19.0.0",
"grunt-git": "^1.0.0",
"grunt-karma": "^2.0.0",
"grunt-mocha-test": "^0.12.7",
"husky": "^0.13.4",
"injectr": "0.5.1",
"jasmine-core": "^2.5.1",
"karma": "^1.3.0",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.2",
"karma-sauce-launcher": "^1.0.0",
"karma-sinon": "^1.0.5",
"lint-staged": "^3.6.0",
"load-grunt-tasks": "^3.5.2",
"mocha": "^3.0.2",
"phantomjs-prebuilt": "^2.1.12",
"prettier-eslint-cli": "^4.0.4",
"semver-sort": "0.0.4",
"sinon": "^1.17.5"
},
Expand Down
31 changes: 20 additions & 11 deletions src/cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
module.exports = {
usage: 'Usage: $0 <command> [options]',
commands: {

dev: {
cmd: 'dev <dirPath> [port] [baseUrl]',
example: {
cmd: '$0 dev ../all-components 3001 127.0.0.1:3001 --fallbackRegistryUrl=http://anotherhost:anotherport/'
cmd:
'$0 dev ../all-components 3001 127.0.0.1:3001 --fallbackRegistryUrl=http://anotherhost:anotherport/'
},
description: 'Runs a local oc test registry in order to develop and test components',
description:
'Runs a local oc test registry in order to develop and test components',
options: {
fallbackRegistryUrl: {
description: 'Url to another registry which will be used by dev registry when component cannot be found in local registry',
description:
'Url to another registry which will be used by dev registry when component cannot be found in local registry'
},
hotReloading: {
boolean: true,
description: 'Enables hot reloading. Note: when hot reloading is set to true, each request to the component will make the registry to create a new instance for the javascript closures to be loaded, while when false the instance will be recycled between components executions',
description:
'Enables hot reloading. Note: when hot reloading is set to true, each request to the component will make the registry to create a new instance for the javascript closures to be loaded, while when false the instance will be recycled between components executions',
default: true
},
verbose: {
Expand All @@ -34,24 +37,28 @@ module.exports = {
example: {
cmd: '$0 init test-component jade'
},
description: 'Creates a new empty component [of either jade or handlebars template type] in the current folder',
description:
'Creates a new empty component [of either jade or handlebars template type] in the current folder',
usage: 'Usage: $0 init <componentName> [templateType]'
},

mock: {
cmd: 'mock <targetType> <targetName> <targetValue>',
example: {
cmd: '$0 mock plugin hash "always-returned-value"',
description: 'Creates static mock for a "hash" plugin which always returns "always-returned-value" value'
description:
'Creates static mock for a "hash" plugin which always returns "always-returned-value" value'
},
description: 'Allows to mock configuration in order to facilitate local development',
description:
'Allows to mock configuration in order to facilitate local development',
usage: 'Usage: $0 mock <targetType> <targetName> <targetValue>'
},

preview: {
cmd: 'preview <componentHref>',
example: {
cmd: '$0 preview "http://localhost:3000/my-new-component/1.0.0/?param1=hello&name=Arthur"'
cmd:
'$0 preview "http://localhost:3000/my-new-component/1.0.0/?param1=hello&name=Arthur"'
},
description: 'Runs a test page consuming a component',
usage: 'Usage: $0 preview <componentHref>'
Expand Down Expand Up @@ -80,10 +87,12 @@ module.exports = {
},
options: {
password: {
description: 'password used to authenticate when publishing to registry'
description:
'password used to authenticate when publishing to registry'
},
username: {
description: 'username used to authenticate when publishing to registry'
description:
'username used to authenticate when publishing to registry'
}
},
description: 'Publish a component',
Expand Down
19 changes: 8 additions & 11 deletions src/cli/domain/get-components-by-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ const fs = require('fs-extra');
const path = require('path');
const _ = require('lodash');

module.exports = function(){

return function(componentsDir, callback){

const isOcComponent = function(file){

module.exports = function() {
return function(componentsDir, callback) {
const isOcComponent = function(file) {
const filePath = path.resolve(componentsDir, file),
packagePath = path.join(filePath, 'package.json');
let content;

try {
content = fs.readJsonSync(packagePath);
}
catch(err)
{
} catch (err) {
return false;
}

Expand All @@ -35,11 +30,13 @@ module.exports = function(){

try {
dirContent = fs.readdirSync(componentsDir);
} catch(err){
} catch (err) {
return callback(null, []);
}

const components = dirContent.filter(isOcComponent).map((component) => path.resolve(componentsDir, component));
const components = dirContent
.filter(isOcComponent)
.map(component => path.resolve(componentsDir, component));

callback(null, components);
};
Expand Down
12 changes: 6 additions & 6 deletions src/cli/domain/get-components-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ const coreModules = require('builtin-modules');
const format = require('stringformat');
const fs = require('fs-extra');
const path = require('path');
const _ = require('lodash');
const _ = require('lodash');

const settings = require('../../resources');

module.exports = function(components){
module.exports = function(components) {
const deps = { modules: {}, withVersions: {}, templates: {} };

const legacyTemplates = {
'jade': true,
'handlebars': true
jade: true,
handlebars: true
};

components.forEach((c) => {
components.forEach(c => {
const pkg = fs.readJsonSync(path.join(c, 'package.json'));
const type = pkg.oc.files.template.type;
const dependencies = pkg.dependencies || {};
Expand All @@ -28,7 +28,7 @@ module.exports = function(components){
deps.templates[type] = true;
}

_.keys(dependencies).forEach((name) => {
_.keys(dependencies).forEach(name => {
const version = dependencies[name];
const depToInstall = version.length > 0 ? `${name}@${version}` : name;

Expand Down
10 changes: 4 additions & 6 deletions src/cli/domain/get-local-npm-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
const fs = require('fs-extra');
const path = require('path');

module.exports = function(){
return function(componentsDir){

module.exports = function() {
return function(componentsDir) {
const nodeFolder = path.join(componentsDir, 'node_modules');

if(!fs.existsSync(nodeFolder)){
if (!fs.existsSync(nodeFolder)) {
return [];
}

return fs.readdirSync(nodeFolder).filter((file) => {

return fs.readdirSync(nodeFolder).filter(file => {
const filePath = path.resolve(nodeFolder, file),
isBin = file === '.bin',
isDir = fs.lstatSync(filePath).isDirectory();
Expand Down
8 changes: 3 additions & 5 deletions src/cli/domain/get-missing-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
const path = require('path');
const _ = require('lodash');

module.exports = (dependencies) => {

module.exports = dependencies => {
const missing = [];

_.forEach(dependencies, (npmModule) => {

_.forEach(dependencies, npmModule => {
const index = npmModule.indexOf('@');
let moduleName = npmModule;

Expand All @@ -19,7 +17,7 @@ module.exports = (dependencies) => {
const pathToModule = path.resolve('node_modules/', moduleName);

try {
if(require.cache[pathToModule]){
if (require.cache[pathToModule]) {
delete require.cache[pathToModule];
}

Expand Down
36 changes: 20 additions & 16 deletions src/cli/domain/get-mocked-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,34 @@ const _ = require('lodash');
const settings = require('../../resources/settings');
const strings = require('../../resources/');

const registerStaticMocks = function(mocks, logger){
const registerStaticMocks = function(mocks, logger) {
return _.map(mocks, (mockedValue, pluginName) => {
logger.log(colors.green('├── ' + pluginName + ' () => ' + mockedValue));
return {
name: pluginName,
register: {
register: function(options, dependencies, next){
register: function(options, dependencies, next) {
return next();
},
execute: function(){
execute: function() {
return mockedValue;
}
}
};
});
};

const registerDynamicMocks = function(ocJsonLocation, mocks, logger){
const registerDynamicMocks = function(ocJsonLocation, mocks, logger) {
return _.map(mocks, (source, pluginName) => {

let p;
try {
p = require(path.resolve(ocJsonLocation, source));
} catch(er) {
} catch (er) {
logger.err(er.toString());
return;
}

if(!_.isFunction(p)){
if (!_.isFunction(p)) {
logger.err(strings.errors.cli.MOCK_PLUGIN_IS_NOT_A_FUNCTION);
return;
}
Expand All @@ -45,25 +44,26 @@ const registerDynamicMocks = function(ocJsonLocation, mocks, logger){
return {
name: pluginName,
register: {
register: function(options, dependencies, next){
register: function(options, dependencies, next) {
return next();
},
execute: p
}
};
}).filter((p) => p);
}).filter(p => p);
};

const findPath = function(pathToResolve, fileName) {

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

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

return findPath(parentDir, fileName);
Expand All @@ -73,28 +73,32 @@ const findPath = function(pathToResolve, fileName) {
return fileToResolve;
};

module.exports = function(logger, componentsDir){
module.exports = function(logger, componentsDir) {
componentsDir = path.resolve(componentsDir || '.');

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

if(!ocJsonPath){
if (!ocJsonPath) {
return plugins;
}

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

if(!content.mocks || !content.mocks.plugins){
if (!content.mocks || !content.mocks.plugins) {
return plugins;
}

logger.warn(strings.messages.cli.REGISTERING_MOCKED_PLUGINS);

plugins = plugins.concat(registerStaticMocks(content.mocks.plugins.static, logger));
plugins = plugins.concat(registerDynamicMocks(ocJsonLocation, content.mocks.plugins.dynamic, logger));
plugins = plugins.concat(
registerStaticMocks(content.mocks.plugins.static, logger)
);
plugins = plugins.concat(
registerDynamicMocks(ocJsonLocation, content.mocks.plugins.dynamic, logger)
);

return plugins;
};
Loading

0 comments on commit cc22411

Please sign in to comment.