-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (114 loc) · 3.59 KB
/
index.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
var gulp = require( "gulp" );
var path = require( "path" );
var fs = require( "fs" );
var gutil = require( "gulp-util" );
var crypto = require( "crypto" );
var opts;
module.exports = function ( filename, options ) {
const defOptions = {
keys: [ 'wp_enqueue_style', 'wp_enqueue_script' ],
silent: true
}
opts = { ...defOptions, ...options };
let files = [];
if ( filename instanceof ( Array ) ) {
files = [ ...filename ];
} else if ( typeof ( filename ) === 'string' ) {
files.push( filename );
}
files.forEach( file => {
handleFile( file );
} );
}
function handleFile( filename ) {
try {
var file = fs.statSync( filename );
if ( !file.isFile() ) {
throw new gutil.PluginError(
"gulp-wp-bump-all",
"Requires valid php file"
);
}
} catch ( e ) {
throw new gutil.PluginError( "gulp-wp-bump-all", "Requires valid php file" );
}
fs.readFile( filename, { encoding: "utf-8" }, function ( err, data ) {
if ( err ) {
gutil.log( "gulp-wp-bump-all", err );
} else {
var revisedFile = bumpVersion( data );
if ( revisedFile == data ) {
if ( !opts.silent ) {
gutil.log( `gulp-wp-bump-all: No change made to '${ filename }'` );
}
}
fs.writeFile( filename, revisedFile, function ( err ) {
if ( err ) {
gutil.log( "gulp-wp-bump-all", err );
}
} );
}
} );
function bumpVersion( data ) {
const regex = new RegExp( `(?:(?:${ applyFilterKeys() })\\()(.*)\\)\\\s*;`, 'gm' );
const arrayRegex = new RegExp( 'array\\([^)]*\\)', 'g' );
let m;
//
var token = crypto.randomBytes( 7 ).toString( "hex" );
var original;
while ( ( m = regex.exec( data ) ) !== null ) {
// This is necessary to avoid infinite loops with zero-width matches
if ( m.index === regex.lastIndex ) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach( ( match, groupIndex ) => {
var result;
if ( groupIndex === 1 ) {
original = match;
// try to match the depedency array string part
//
var arrayMatch = match.match( arrayRegex );
if ( arrayMatch ) {
// replace the comma's in the dependency array for | so that we can do the split below on comma
//
var arrayValue = arrayMatch[ 0 ].replace( /,/g, '|' );
match = match.replace( arrayMatch[ 0 ], arrayValue );
}
let parts = match.split( ',' );
if ( parts.length >= 4 ) {
parts[ 3 ] = applyToken( parts[ 3 ], token );
}
if ( parts.length === 3 ) {
parts.push( applyToken( "", token ) );
}
if ( parts.length === 2 ) {
parts.push( "array()" );
parts.push( applyToken( "", token ) );
}
if ( !opts.silent ) {
console.log( `source [${ source }]` );
console.log( `replace [${ parts.join().trim() }]` );
}
result = parts.join().trim();
if ( arrayMatch ) {
result = result.replace( arrayValue, arrayMatch[ 0 ] );
}
data = data.replace( original, ` ${ result } ` );
}
} );
}
return data;
}
function applyToken( part, token ) {
let value = part.trim();
var quotes = value.charAt( 0 ) === '"' ? '"' : "'";
value = quotes + token + quotes;
return value;
}
function applyFilterKeys() {
if ( opts.keys && opts.keys instanceof ( Array ) ) {
return opts.keys.join( '|' );
}
}
}