1 |
|
2 |
|
3 |
|
4 |
|
5 | const
|
6 | fs = require('fs'),
|
7 | path = require('path'),
|
8 |
|
9 | defaults = require('../defaults')
|
10 | ;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | module.exports = {
|
17 |
|
18 | getPath: function (file, directory) {
|
19 | let
|
20 | configPath,
|
21 | walk = function (directory) {
|
22 | let
|
23 | nextDirectory = path.resolve(path.join(directory, path.sep, '..')),
|
24 | currentPath = path.normalize(path.join(directory, file))
|
25 | ;
|
26 | if (fs.existsSync(currentPath)) {
|
27 |
|
28 | configPath = path.normalize(directory);
|
29 | } else {
|
30 |
|
31 | if (nextDirectory === directory) {
|
32 | return;
|
33 | }
|
34 |
|
35 | walk(nextDirectory, file);
|
36 | }
|
37 | }
|
38 | ;
|
39 |
|
40 |
|
41 | file = file || defaults.files.config;
|
42 | directory = directory || path.join(__dirname, path.sep, '..');
|
43 | walk(directory);
|
44 |
|
45 | return configPath || '';
|
46 | },
|
47 |
|
48 |
|
49 | addDerivedValues: function (config) {
|
50 | |
51 |
|
52 |
|
53 |
|
54 | let
|
55 | configPath = this.getPath(),
|
56 | sourcePaths = {},
|
57 | outputPaths = {},
|
58 | folder
|
59 | ;
|
60 |
|
61 |
|
62 | for (folder in config.paths.source) {
|
63 | if (Object.prototype.hasOwnProperty.call(config.paths.source, folder)) {
|
64 | sourcePaths[folder] = path.resolve(path.join(configPath, config.base, config.paths.source[folder]));
|
65 | }
|
66 | }
|
67 | for (folder in config.paths.output) {
|
68 | if (Object.prototype.hasOwnProperty.call(config.paths.output, folder)) {
|
69 | outputPaths[folder] = path.resolve(path.join(configPath, config.base, config.paths.output[folder]));
|
70 | }
|
71 | }
|
72 |
|
73 |
|
74 | config.paths.source = sourcePaths;
|
75 | config.paths.output = outputPaths;
|
76 |
|
77 |
|
78 | config.paths.clean = path.resolve(path.join(configPath, config.base, config.paths.clean));
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | config.paths.assets = {
|
88 | source: '../../themes',
|
89 | uncompressed: './' + path.relative(config.paths.output.uncompressed, config.paths.output.themes).replace(/\\/g, '/'),
|
90 | compressed: './' + path.relative(config.paths.output.compressed, config.paths.output.themes).replace(/\\/g, '/'),
|
91 | packaged: './' + path.relative(config.paths.output.packaged, config.paths.output.themes).replace(/\\/g, '/'),
|
92 | };
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 | if (config.permission) {
|
99 | config.hasPermissions = true;
|
100 | config.parsedPermissions = typeof config.permission === 'string' ? parseInt(config.permission, 8) : config.permission;
|
101 | } else {
|
102 |
|
103 | config.permission = {};
|
104 | config.hasPermissions = false;
|
105 | config.parsedPermissions = {};
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 | if (!config.globs) {
|
113 | config.globs = {};
|
114 | }
|
115 |
|
116 |
|
117 | if (Array.isArray(config.components)) {
|
118 | config.components = config.components.filter(function (component, index) {
|
119 | return config.components.indexOf(component) === index;
|
120 | });
|
121 | }
|
122 |
|
123 | const components = Array.isArray(config.components) && config.components.length > 0
|
124 | ? config.components
|
125 | : defaults.components;
|
126 | const individuals = Array.isArray(config.individuals) && config.individuals.length > 0
|
127 | ? config.individuals
|
128 | : [];
|
129 | const componentsExceptIndividuals = components.filter((component) => !individuals.includes(component));
|
130 |
|
131 |
|
132 | config.globs.components = componentsExceptIndividuals.length === 1 ? componentsExceptIndividuals[0] : '{' + componentsExceptIndividuals.join(',') + '}';
|
133 |
|
134 |
|
135 | config.globs.individuals = individuals.length === 1
|
136 | ? individuals[0]
|
137 | : (individuals.length > 1
|
138 | ? '{' + individuals.join(',') + '}'
|
139 | : undefined);
|
140 | },
|
141 |
|
142 | };
|