-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
158 lines (132 loc) · 4.35 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
/**
* External dependencies
*/
import gulpPlumber from 'gulp-plumber';
import importFresh from 'import-fresh';
import notify from 'gulp-notify';
/**
* Internal dependencies
*/
import { rootPath } from './constants';
/**
* Gets default configuration.
*
* @return {JSON} The config as json.
*/
export const getDefaultConfig = () => require( `${ rootPath }/config/config.default.json` );
/**
* Gets final configuration merged from default and custom.
*
* @param {boolean} uncached Whether to get an uncached version of the configuration.
* Defaults to false.
* @return {Object} Main configuration data.
*/
export const getMainConfig = ( uncached = false ) => {
const configPath = `${ process.cwd() }/config/config.js`,
customConfig = uncached ? importFresh( configPath ) : require( configPath );
// Define slug value for the current project.
if ( ! customConfig.core.slug ) {
customConfig.core.slug = customConfig.core.name
.toLowerCase()
.replace( /[\s_]+/g, '-' ).replace( /[^a-z0-9-]+/g, '' );
}
// Define underscore value for the current project.
if ( ! customConfig.core.underscoreCase ) {
customConfig.core.underscoreCase = customConfig.core.slug
.replace( /-/g, '_' );
}
// Define constant value for the current project.
if ( ! customConfig.core.constant ) {
customConfig.core.constant = customConfig.core.underscoreCase.toUpperCase();
}
// Define camel case value for the current project.
if ( ! customConfig.core.camelCase ) {
customConfig.core.camelCase = customConfig.core.slug
.split( '-' )
.map( ( part ) => part[ 0 ].toUpperCase() + part.substring( 1 ) )
.join( '' );
}
if ( ! customConfig.core.camelCaseVar ) {
customConfig.core.camelCaseVar = customConfig.core.camelCase[ 0 ].toLowerCase() + customConfig.core.camelCase.substring( 1 );
}
// Define whether current project is a theme (not a plugin).
if ( ! customConfig.export.isTheme ) {
customConfig.export.isTheme = false;
}
return customConfig;
};
export const logError = ( errorTitle = 'gulp' ) => {
return gulpPlumber( {
errorHandler: notify.onError( {
title: errorTitle,
message: '<%= error.message %>',
} ),
} );
};
/**
* Makes backslashes to forward slashes.
*
* @param {string} path The path to be converted.
* @return {string} The converted path.
*/
export const backslashToForwardSlash = ( path ) => {
const replaceFn = ( ( p ) => p.replace( /\\/g, '/' ) );
if ( Array.isArray( path ) ) {
const paths = [];
path.forEach( ( p ) => paths.push( replaceFn( p ) ) );
return paths;
}
return replaceFn( path );
};
/**
* Determines if a config value is defined.
*
* @param {string} configValueLocation a config value path to search for, e.g. 'config.core.slug'
* @return {boolean} Whether the config value is defined
*/
export const configValueDefined = ( configValueLocation ) => {
// We won't find anything if the location to search is empty.
if ( 0 === configValueLocation.length ) {
return false;
}
// Get a copy of the config
let config = getMainConfig();
// Turn the value location given into an array
const configValueLocationArray = configValueLocation.split( '.' );
// Remove config from the array if present
if ( 'config' === configValueLocationArray[ 0 ] ) {
configValueLocationArray.shift();
}
// Loop through the config value paths passed
for ( const currentValueLocation of configValueLocationArray ) {
// Check if there is a match in the current object level.
if ( ! Object.prototype.hasOwnProperty.call( config, currentValueLocation ) ) {
// Return false if no match
return false;
}
// Move the config object to the next level
config = config[ currentValueLocation ];
}
// If we've made it this far there is a match for the given config value path.
return true;
};
/**
* Appends a base file path to a list of files.
*
* @param {string|Array} filePaths the file or files to append the base path to
* @param {string} basePath the base path to append
* @return {string|Array} file paths with base path appended
*/
export function appendBaseToFilePathArray( filePaths, basePath ) {
if ( ! Array.isArray( filePaths ) ) {
return `${ basePath }/${ filePaths }`;
}
const output = [];
// Loop through all file paths
for ( const filePath of filePaths ) {
// And push them into output with the base added
output.push( `${ basePath }/${ filePath }` );
}
return output;
}