UNPKG

5.12 kBJavaScriptView Raw
1/*******************************
2 Set-up
3*******************************/
4
5const
6 fs = require('fs'),
7 path = require('path'),
8
9 defaults = require('../defaults')
10;
11
12/*******************************
13 Exports
14*******************************/
15
16module.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 // found file
28 configPath = path.normalize(directory);
29 } else {
30 // reached file system root, let's stop
31 if (nextDirectory === directory) {
32 return;
33 }
34 // otherwise recurse
35 walk(nextDirectory, file);
36 }
37 }
38 ;
39
40 // start walk from outside require-dot-files directory
41 file = file || defaults.files.config;
42 directory = directory || path.join(__dirname, path.sep, '..');
43 walk(directory);
44
45 return configPath || '';
46 },
47
48 // adds additional derived values to a config object
49 addDerivedValues: function (config) {
50 /* --------------
51 File Paths
52 --------------- */
53
54 let
55 configPath = this.getPath(),
56 sourcePaths = {},
57 outputPaths = {},
58 folder
59 ;
60
61 // resolve paths (config location + base + path)
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 // set config paths to full paths
74 config.paths.source = sourcePaths;
75 config.paths.output = outputPaths;
76
77 // resolve "clean" command path
78 config.paths.clean = path.resolve(path.join(configPath, config.base, config.paths.clean));
79
80 /* --------------
81 CSS URLs
82 --------------- */
83
84 // determine asset paths in css by finding relative path between themes and output
85 // force forward slashes
86
87 config.paths.assets = {
88 source: '../../themes', // source asset path is always the same
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 Permission
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 // pass blank object to avoid causing errors
103 config.permission = {};
104 config.hasPermissions = false;
105 config.parsedPermissions = {};
106 }
107
108 /* --------------
109 Globs
110 --------------- */
111
112 if (!config.globs) {
113 config.globs = {};
114 }
115
116 // remove duplicates from component array
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 // takes component object and creates file glob matching selected components
132 config.globs.components = componentsExceptIndividuals.length === 1 ? componentsExceptIndividuals[0] : '{' + componentsExceptIndividuals.join(',') + '}';
133
134 // components that should be built, but excluded from main .css/.js files
135 config.globs.individuals = individuals.length === 1
136 ? individuals[0]
137 : (individuals.length > 1
138 ? '{' + individuals.join(',') + '}'
139 : undefined);
140 },
141
142};