From b6fa2a3fc674debb5b52f9314978dc29c4f3edec Mon Sep 17 00:00:00 2001 From: Eugen Zha Date: Fri, 9 Nov 2018 15:13:47 +0300 Subject: [PATCH] Update lint and fix code (#53) --- .eslintrc | 3 +- commands/application.angular.js | 12 +- commands/application.js | 103 +++++------ commands/help.js | 1 - commands/themebuider.js | 311 ++++++++++++++++---------------- index.js | 1 - utility/run-command.js | 6 +- 7 files changed, 217 insertions(+), 220 deletions(-) diff --git a/.eslintrc b/.eslintrc index b6304dd4e..09f5f8907 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,7 +4,7 @@ "node": true }, "parserOptions": { - "ecmaVersion": 6, + "ecmaVersion": 2018, "sourceType": "module", "ecmaFeatures": { "globalReturn": true @@ -40,6 +40,7 @@ } } ], + "linebreak-style": ["error", "unix"], "no-multiple-empty-lines": [ "error", { "max": 2 } ], "no-irregular-whitespace": "error", "no-multi-spaces": "error", diff --git a/commands/application.angular.js b/commands/application.angular.js index 151332a94..0920b7869 100644 --- a/commands/application.angular.js +++ b/commands/application.angular.js @@ -38,7 +38,7 @@ function runSchematicCommand(schematicCommand, options, evaluatingOptions) { } function localPackageExists(packageName) { - const nodeModulesPath = path.join(process.cwd(), `node_modules`); + const nodeModulesPath = path.join(process.cwd(), 'node_modules'); if(!fs.existsSync(nodeModulesPath)) { return; } @@ -49,7 +49,7 @@ function localPackageExists(packageName) { function optimizeNgCommandArguments(args) { return new Promise((resolve, reject) => { - hasSutableNgCli().then(() => resolve(args), () => resolve(['-p', '@angular/cli', ...args])); + hasSutableNgCli().then(() => resolve(args), () => resolve(['-p', '@angular/cli', ...args])); }); } @@ -57,14 +57,14 @@ function hasSutableNgCli() { return new Promise((resolve, reject) => { exec('ng v', (err, stdout, stderr) => { stderr || parseNgCliVersion(stdout).compare(minNgCliVersion) < 0 - ? reject() - : resolve(); + ? reject() + : resolve(); }); }); } function parseNgCliVersion(stdout) { - return new semver(stdout.toString().match(/(?<=angular.cli: )(\S+)/ig)[0]); + return new semver(/angular.cli:\s*(\S+)/ig.exec(stdout.toString())[1]); } const install = (options) => { @@ -84,7 +84,7 @@ const create = (appName, options) => { }; const addTemplate = (appName, options, evaluatingOptions) => { - const schematicOptions = {...(appName && {project: appName}), ...options}; + const schematicOptions = { ...(appName && { project: appName }), ...options }; runSchematicCommand('add-app-template', schematicOptions, evaluatingOptions); }; diff --git a/commands/application.js b/commands/application.js index aa66dadee..ede058fe0 100644 --- a/commands/application.js +++ b/commands/application.js @@ -1,51 +1,52 @@ -const angularApplication = require('./application.angular'); -const printHelp = require('./help').printHelp; - -const isApplicationCommand = (command) => { - return [ 'new', 'add' ].indexOf(command) > -1; -}; - -const run = (commands, options, devextremeConfig) => { - if(!commands[1]) { - console.error('No parameters found.'); - printHelp(commands[0]); - return; - } - - if(commands[0] === 'new') { - if(commands[1] === 'angular-app') { - angularApplication.create(commands[2] || 'my-app', options); - } else { - console.error(`The '${commands[1]}' application type is not valid`); - printHelp(commands[0]); - } - } else { - if(commands[0] === 'add') { - if(commands[1] === 'devextreme-angular') { - angularApplication.install(options); - return; - } - - if(commands[1] === 'angular-template') { - angularApplication.addTemplate(commands[2], options); - return; - } - - if(devextremeConfig.applicationEngine === 'angular') { - if(commands[1] === 'view') { - angularApplication.addView(commands[2], options); - } else { - console.error('Invalid command'); - printHelp(commands[0]); - } - } else { - console.log('The DevExtreme application cannot be found'); - } - } - } -}; - -module.exports = { - isApplicationCommand, - run -}; +const angularApplication = require('./application.angular'); +const printHelp = require('./help').printHelp; + +const isApplicationCommand = (command) => { + return [ 'new', 'add' ].indexOf(command) > -1; +}; + +const run = (commands, options, devextremeConfig) => { + if(!commands[1]) { + console.error('No parameters found.'); + printHelp(commands[0]); + return; + } + + if(commands[0] === 'new') { + if(commands[1] === 'angular-app') { + angularApplication.create(commands[2] || 'my-app', options); + return; + } + + console.error(`The '${commands[1]}' application type is not valid`); + printHelp(commands[0]); + } else { + if(commands[0] === 'add') { + if(commands[1] === 'devextreme-angular') { + angularApplication.install(options); + return; + } + + if(commands[1] === 'angular-template') { + angularApplication.addTemplate(commands[2], options); + return; + } + + if(devextremeConfig.applicationEngine === 'angular') { + if(commands[1] === 'view') { + angularApplication.addView(commands[2], options); + } else { + console.error('Invalid command'); + printHelp(commands[0]); + } + } else { + console.log('The DevExtreme application cannot be found'); + } + } + } +}; + +module.exports = { + isApplicationCommand, + run +}; diff --git a/commands/help.js b/commands/help.js index a0bcebbbe..0855f3c17 100644 --- a/commands/help.js +++ b/commands/help.js @@ -1,5 +1,4 @@ const commands = require('./commands.json').commands; - const textColor = '\x1b[36m%s\x1b[0m'; const findCommand = (commandName) => { diff --git a/commands/themebuider.js b/commands/themebuider.js index a43af2166..396aa6ae2 100644 --- a/commands/themebuider.js +++ b/commands/themebuider.js @@ -1,155 +1,156 @@ -const themeBuilder = require('devextreme-themebuilder'); -const commands = require('devextreme-themebuilder/modules/commands'); -const baseParameters = require('devextreme-themebuilder/modules/base-parameters'); -const fs = require('fs'); -const path = require('path'); - -const scssCompiler = { - render: (scss) => { - return new Promise((resolve, reject) => { - require('node-sass').render({ - data: scss - }, (error, result) => { - if(error) { - reject(error); - } else { - resolve(result.css.toString()); - } - }); - }); - } -}; - -const createPath = filePath => { - const directoryName = path.dirname(filePath); - - directoryName - .split(/\\|\//) - .reduce((currentPath, folder) => { - currentPath += folder + path.sep; - if(!fs.existsSync(currentPath)) { - fs.mkdirSync(currentPath); - } - return currentPath; - }, ''); -}; - -const readFile = fileName => new Promise((resolve, reject) => { - fs.readFile(require.resolve(fileName), 'utf8', (error, data) => { - error ? reject(error) : resolve(data); - }); -}); - -const camelCase = str => str.replace(/[-](\w|$)/g, (_, letter) => { - return letter.toUpperCase(); -}); - -const camelize = (object) => { - let result = {}; - for(let key in object) { - if(object.hasOwnProperty(key)) { - const newKey = camelCase(key); - result[newKey] = object[key]; - } - } - return result; -}; - -const readInput = options => new Promise(resolve => { - const fileName = options.inputFile; - if(!fileName) resolve(); - fs.readFile(fileName, (error, data) => { - if(error) { - console.error(`Unable to read the ${fileName} file.`); - } else { - const extension = path.extname(fileName); - if(extension !== '.json') { - options.data = data; - } else { - const inputObject = JSON.parse(data); - Object.assign(options, inputObject); - } - } - resolve(); - }); -}); - -const getMeta = (fullMeta, base, filter) => { - let result = {}; - - for(const key in fullMeta) { - if(base && baseParameters.indexOf(key) === -1) continue; - if(filter && filter.length > 0 && filter.indexOf(key) === -1) continue; - result[key] = fullMeta[key]; - } - - return result; -}; - -const runThemeBuilder = (rawOptions) => { - const options = camelize(rawOptions); - - readInput(options).then(() => { - options.reader = readFile; - options.sassCompiler = scssCompiler; - options.lessCompiler = require('less/lib/less-node'); - - if(options.assetsBasePath) { - options.lessCompiler.options = options.lessCompiler.options || {}; - options.lessCompiler.options['rootpath'] = options.assetsBasePath; - } - - themeBuilder.buildTheme(options).then((result) => { - let content = ''; - const vars = options.vars || []; - let filter = (vars instanceof Array) ? vars : vars.split(','); - createPath(options.out); - - if(options.command === commands.BUILD_THEME) { - content = result.css; - if(result.swatchSelector) { - console.log(`Add the '${result.swatchSelector}' class to the container to apply swatch styles to its nested elements.`); - } - } else if(options.command === commands.BUILD_VARS) { - const metadata = getMeta(result.compiledMetadata, options.base, filter); - - for(const metadataKey in metadata) { - const formatKey = options.fileFormat === 'scss' ? metadataKey.replace('@', '$') : metadataKey; - content += formatKey + ': ' + metadata[metadataKey] + ';\n'; - } - } else if(options.command === commands.BUILD_META) { - const metadata = getMeta(result.compiledMetadata, options.base, filter); - let exportedMeta = []; - - for(const metadataKey in metadata) { - exportedMeta.push({ key: metadataKey, value: metadata[metadataKey] }); - } - - content = JSON.stringify({ - baseTheme: [ options.themeName, options.colorScheme.replace('-', '.') ].join('.'), - items: exportedMeta - }, ' ', 4); - } - - fs.writeFile(options.out, content, 'utf8', error => { - if(error) { - console.log(`Unable to write the ${options.out} file. ${error.message}`); - } else { - console.log(`The result was written to the ${options.out} file.`); - } - }); - }); - }); -}; - -const isThemeBuilderCommand = command => { - for(const commandKey in commands) { - if(commands[commandKey] === command) return true; - } - return false; -}; - -module.exports = { - run: runThemeBuilder, - isThemeBuilderCommand: isThemeBuilderCommand -}; +const themeBuilder = require('devextreme-themebuilder'); +const commands = require('devextreme-themebuilder/modules/commands'); +const baseParameters = require('devextreme-themebuilder/modules/base-parameters'); +const fs = require('fs'); +const path = require('path'); + +const scssCompiler = { + render: (scss) => { + return new Promise((resolve, reject) => { + require('node-sass').render({ + data: scss + }, (error, result) => { + if(error) { + reject(error); + } else { + resolve(result.css.toString()); + } + }); + }); + } +}; + +const createPath = filePath => { + const directoryName = path.dirname(filePath); + + directoryName + .split(/\\|\//) + .reduce((currentPath, folder) => { + currentPath += folder + path.sep; + if(!fs.existsSync(currentPath)) { + fs.mkdirSync(currentPath); + } + return currentPath; + }, ''); +}; + +const readFile = fileName => new Promise((resolve, reject) => { + fs.readFile(require.resolve(fileName), 'utf8', (error, data) => { + error ? reject(error) : resolve(data); + }); +}); + +const camelCase = str => str.replace(/[-](\w|$)/g, (_, letter) => { + return letter.toUpperCase(); +}); + +const camelize = (object) => { + let result = {}; + for(let key in object) { + if(object.hasOwnProperty(key)) { + const newKey = camelCase(key); + result[newKey] = object[key]; + } + } + return result; +}; + +const readInput = options => new Promise(resolve => { + const fileName = options.inputFile; + if(!fileName) resolve(); + + fs.readFile(fileName, (error, data) => { + if(error) { + console.error(`Unable to read the ${fileName} file.`); + } else { + const extension = path.extname(fileName); + if(extension !== '.json') { + options.data = data; + } else { + const inputObject = JSON.parse(data); + Object.assign(options, inputObject); + } + } + resolve(); + }); +}); + +const getMeta = (fullMeta, base, filter) => { + let result = {}; + + for(const key in fullMeta) { + if(base && baseParameters.indexOf(key) === -1) continue; + if(filter && filter.length > 0 && filter.indexOf(key) === -1) continue; + result[key] = fullMeta[key]; + } + + return result; +}; + +const runThemeBuilder = (rawOptions) => { + const options = camelize(rawOptions); + + readInput(options).then(() => { + options.reader = readFile; + options.sassCompiler = scssCompiler; + options.lessCompiler = require('less/lib/less-node'); + + if(options.assetsBasePath) { + options.lessCompiler.options = options.lessCompiler.options || {}; + options.lessCompiler.options['rootpath'] = options.assetsBasePath; + } + + themeBuilder.buildTheme(options).then((result) => { + let content = ''; + const vars = options.vars || []; + let filter = (vars instanceof Array) ? vars : vars.split(','); + createPath(options.out); + + if(options.command === commands.BUILD_THEME) { + content = result.css; + if(result.swatchSelector) { + console.log(`Add the '${result.swatchSelector}' class to the container to apply swatch styles to its nested elements.`); + } + } else if(options.command === commands.BUILD_VARS) { + const metadata = getMeta(result.compiledMetadata, options.base, filter); + + for(const metadataKey in metadata) { + const formatKey = options.fileFormat === 'scss' ? metadataKey.replace('@', '$') : metadataKey; + content += formatKey + ': ' + metadata[metadataKey] + ';\n'; + } + } else if(options.command === commands.BUILD_META) { + const metadata = getMeta(result.compiledMetadata, options.base, filter); + let exportedMeta = []; + + for(const metadataKey in metadata) { + exportedMeta.push({ key: metadataKey, value: metadata[metadataKey] }); + } + + content = JSON.stringify({ + baseTheme: [ options.themeName, options.colorScheme.replace('-', '.') ].join('.'), + items: exportedMeta + }, ' ', 4); + } + + fs.writeFile(options.out, content, 'utf8', error => { + if(error) { + console.log(`Unable to write the ${options.out} file. ${error.message}`); + } else { + console.log(`The result was written to the ${options.out} file.`); + } + }); + }); + }); +}; + +const isThemeBuilderCommand = command => { + for(const commandKey in commands) { + if(commands[commandKey] === command) return true; + } + return false; +}; + +module.exports = { + run: runThemeBuilder, + isThemeBuilderCommand: isThemeBuilderCommand +}; diff --git a/index.js b/index.js index 348e4aac5..70eba8684 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,6 @@ const themeBuilder = require('./commands/themebuider'); const application = require('./commands/application'); const devextremeConfig = require('./utility/devextreme-config'); const printHelp = require('./commands/help').printHelp; - const packageJson = require('./package.json'); if(!commands.length) { diff --git a/utility/run-command.js b/utility/run-command.js index 713133468..e16d3506c 100644 --- a/utility/run-command.js +++ b/utility/run-command.js @@ -15,11 +15,7 @@ module.exports = function(commandName, args, customConfig) { return new Promise((resolve, reject) => { spawn(command, args, config).on('exit', (code) => { - if(code) { - reject(code); - } else { - resolve(); - } + code ? reject(code) : resolve(); }); }); };