5
5
6
6
'use strict' ;
7
7
8
- module . exports = {
9
- defaultConfig : {
10
- 'themes' : 'dev/tools/grunt/configs/themes'
8
+ var defaultConfig = { } ,
9
+
10
+ /**
11
+ * Generates full path to file.
12
+ *
13
+ * @param {String } path - relative path to file.
14
+ *
15
+ * @returns {String } Full path to file
16
+ */
17
+ getFullPath = function ( path ) {
18
+ return process . cwd ( ) + '/' + path ;
19
+ } ,
20
+
21
+ /**
22
+ * Returns file.
23
+ *
24
+ * @param {String } path - relative path to file.
25
+ *
26
+ * @returns {Object|Null } File or NULL
27
+ */
28
+ getFile = function ( path ) {
29
+ try {
30
+ return require ( getFullPath ( path ) ) ;
31
+ } catch ( error ) {
32
+ return null ;
33
+ }
11
34
} ,
12
35
13
36
/**
14
37
* Immediately invoked function.
15
38
* Loads user config file.
16
39
*/
17
- userConfig : ( function ( ) {
40
+ userConfig = ( function ( ) {
18
41
try {
19
42
return require ( process . cwd ( ) + '/grunt-config' ) ;
20
43
} catch ( error ) {
21
44
return null ;
22
45
}
23
- } ) ( ) ,
46
+ } ) ( ) ;
47
+
48
+ module . exports = {
24
49
25
50
/**
26
51
* Loads file.
@@ -29,32 +54,30 @@ module.exports = {
29
54
* From default config with ".loc" suffix ;
30
55
* From default config;
31
56
*
32
- * @returns themes file or error
57
+ * @param {String } alias
58
+ *
59
+ * @returns {Object } themes file or error
33
60
*/
34
- get : function ( file ) {
35
- if ( this . userConfig && this . userConfig [ file ] ) {
36
- return require ( this . getFullPath ( this . userConfig [ file ] ) ) ;
61
+ get : function ( alias ) {
62
+ var tmp ;
63
+
64
+ if ( userConfig && userConfig [ alias ] ) {
65
+ return require ( getFullPath ( userConfig [ alias ] ) ) ;
66
+ } else if ( tmp = getFile ( defaultConfig [ alias ] + '.loc' ) || getFile ( defaultConfig [ alias ] ) ) {
67
+ return tmp ;
37
68
} else {
38
- try {
39
- return require ( this . getFullPath ( this . defaultConfig [ file ] + '.loc' ) ) ;
40
- } catch ( error ) {
41
- try {
42
- return require ( this . getFullPath ( this . defaultConfig [ file ] ) ) ;
43
- } catch ( error ) {
44
- throw error ;
45
- }
46
- }
69
+ throw new Error ( 'Cannot find file. Alias "' + alias + '" not set. ' +
70
+ 'Use "filesRouter.set" method to set it.' ) . stack ;
47
71
}
48
72
} ,
49
73
50
74
/**
51
- * Generates full path to file.
75
+ * Sets file alias .
52
76
*
53
- * @param {String } path - relative path to file.
54
- *
55
- * @returns {String } Full path to file
77
+ * @param {String } alias
78
+ * @param {String } path
56
79
*/
57
- getFullPath : function ( path ) {
58
- return process . cwd ( ) + '/' + path ;
80
+ set : function ( alias , path ) {
81
+ defaultConfig [ alias ] = path ;
59
82
}
60
83
} ;
0 commit comments