diff --git a/Gruntfile.js.sample b/Gruntfile.js.sample index 44c5e897dd3fa9757263e4081559201e1f51cc20..3bd874be0480d97217d51a68f4b2da2fa98545ab 100644 --- a/Gruntfile.js.sample +++ b/Gruntfile.js.sample @@ -5,14 +5,19 @@ // For performance use one level down: 'name/{,*/}*.js' // If you want to recursively match all subfolders, use: 'name/**/*.js' + module.exports = function (grunt) { 'use strict'; var _ = require('underscore'), path = require('path'), - themes = require('./dev/tools/grunt/configs/themes'), + filesRouter = require('./dev/tools/grunt/tools/files-router'), configDir = './dev/tools/grunt/configs', - tasks = grunt.file.expand('./dev/tools/grunt/tasks/*'); + tasks = grunt.file.expand('./dev/tools/grunt/tasks/*'), + themes; + + filesRouter.set('themes', 'dev/tools/grunt/configs/themes'); + themes = filesRouter.get('themes'); tasks = _.map(tasks, function(task){ return task.replace('.js', '') }); tasks.push('time-grunt'); diff --git a/dev/tools/grunt/configs/clean.js b/dev/tools/grunt/configs/clean.js index 792981a632ce638624a4142327298cb27e48fed8..53bcd8a1d830ebda056e59c6ba592106abcdde72 100644 --- a/dev/tools/grunt/configs/clean.js +++ b/dev/tools/grunt/configs/clean.js @@ -5,7 +5,7 @@ 'use strict'; -var themes = require('./themes'), +var themes = require('../tools/files-router').get('themes'), _ = require('underscore'); var themeOptions = {}; diff --git a/dev/tools/grunt/configs/combo.js b/dev/tools/grunt/configs/combo.js index 930b2fd68715c7efbfe12906c5e291a35ae49e99..6dcbe7e36a667699bb830387aaea27e7932525f3 100644 --- a/dev/tools/grunt/configs/combo.js +++ b/dev/tools/grunt/configs/combo.js @@ -5,7 +5,7 @@ 'use strict'; -var theme = require('./themes'), +var theme = require('../tools/files-router').get('themes'), path = require('./path'); /** diff --git a/dev/tools/grunt/configs/exec.js b/dev/tools/grunt/configs/exec.js index a26a0d95a54c63814d0a3d4f73001354e3d09d8a..3e675ff9b5d81328e94b6e906495c67d43a90597 100644 --- a/dev/tools/grunt/configs/exec.js +++ b/dev/tools/grunt/configs/exec.js @@ -6,7 +6,7 @@ 'use strict'; var combo = require('./combo'), - themes = require('./themes'), + themes = require('../tools/files-router').get('themes'), _ = require('underscore'); var themeOptions = {}; diff --git a/dev/tools/grunt/configs/less.js b/dev/tools/grunt/configs/less.js index 4071b47fa2b7ca347287564d412eb1702b3a5041..7a849577127408fbddeeef91c0d97fa452377fc8 100644 --- a/dev/tools/grunt/configs/less.js +++ b/dev/tools/grunt/configs/less.js @@ -6,7 +6,7 @@ 'use strict'; var combo = require('./combo'), - themes = require('./themes'), + themes = require('../tools/files-router').get('themes'), _ = require('underscore'); var themeOptions = {}; diff --git a/dev/tools/grunt/configs/watch.js b/dev/tools/grunt/configs/watch.js index 014b6b065a96fe3bdfa2d51136f24080fd144884..84657ae7c4d8073ea9a0e8db3b58285c9b1f26cc 100644 --- a/dev/tools/grunt/configs/watch.js +++ b/dev/tools/grunt/configs/watch.js @@ -6,7 +6,7 @@ 'use strict'; var combo = require('./combo'), - themes = require('./themes'), + themes = require('../tools/files-router').get('themes'), _ = require('underscore'); var themeOptions = {}; diff --git a/dev/tools/grunt/tools/files-router.js b/dev/tools/grunt/tools/files-router.js new file mode 100644 index 0000000000000000000000000000000000000000..2bf6f9d8cc2e902dcd5cd9283b158ff2775d85d9 --- /dev/null +++ b/dev/tools/grunt/tools/files-router.js @@ -0,0 +1,83 @@ +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +'use strict'; + +var defaultConfig = {}, + + /** + * Generates full path to file. + * + * @param {String} path - relative path to file. + * + * @returns {String} Full path to file + */ + getFullPath = function (path) { + return process.cwd() + '/' + path; + }, + + /** + * Returns file. + * + * @param {String} path - relative path to file. + * + * @returns {Object|Null} File or NULL + */ + getFile = function (path) { + try { + return require(getFullPath(path)); + } catch (error) { + return null; + } + }, + + /** + * Immediately invoked function. + * Loads user config file. + */ + userConfig = (function () { + try { + return require(process.cwd() + '/grunt-config'); + } catch (error) { + return null; + } + })(); + +module.exports = { + + /** + * Loads file. + * Load priority: + * From user config; + * From default config with ".loc" suffix ; + * From default config; + * + * @param {String} alias + * + * @returns {Object} themes file or error + */ + get: function (alias) { + var tmp; + + if (userConfig && userConfig[alias]) { + return require(getFullPath(userConfig[alias])); + } else if (tmp = getFile(defaultConfig[alias] + '.loc') || getFile(defaultConfig[alias])) { + return tmp; + } else { + throw new Error('Cannot find file. Alias "' + alias + '" not set. ' + + 'Use "filesRouter.set" method to set it.').stack; + } + }, + + /** + * Sets file alias. + * + * @param {String} alias + * @param {String} path + */ + set: function (alias, path) { + defaultConfig[alias] = path; + } +}; diff --git a/grunt-config.json.sample b/grunt-config.json.sample new file mode 100644 index 0000000000000000000000000000000000000000..7ef28a856f92529f71daac39e511a26d853184e2 --- /dev/null +++ b/grunt-config.json.sample @@ -0,0 +1,3 @@ +{ + "themes": "dev/tools/grunt/configs/local-themes" +}