-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathassets-version.js
46 lines (35 loc) · 1.63 KB
/
assets-version.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
/**
* Currently this script will generate a new version
* number for the theme's asset files. It writes in inc/theme-version.php
*/
// The two packages required to write in the theme-version.php file
var fs = require('fs');
var moment = require('moment');
// Generate version for theme-version.php for the assets
let someFile = './inc/theme-version.php';
fs.readFile(someFile, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
// The REGEX to find the assets version
let versionRegex = /define\( 'DX_ASSETS_VERSION', '*(.*?)' \);/g;
// Find the line where the version is at.
let found = data.match(versionRegex);
// This will get and increment the version of the assets file.
// 39 must be replaced with automated find
let verIndexOf = found[0].indexOf(', \'') + 3 + 9; // 3 for the ", '" bit and 9 for the date + dash
currentVersion = parseInt(found[0].substring(verIndexOf, found[0].length - 4));
currentVersion++;
// Create the date format. Of course you can change that to anything you like
// or even remove it so that you have only the version number and nothing else.
let time = moment().format("YYYYMMDD");
// Create the string that we will replace with the existing one.
let themeVersionString = "define( 'DX_ASSETS_VERSION', '" + time + '-' + currentVersion + "' );";
// Once again find the string to replace and set the new version
let result = data.replace(versionRegex, themeVersionString);
// End it all by writing in the file (or throw error :)
fs.writeFile(someFile, result, 'utf8', function (err) {
if (err) return console.log(err);
});
console.log("Assets version has been updated!");
});