From 6b659f8a6a5634a9649ea354cdd92fadd2c37f72 Mon Sep 17 00:00:00 2001 From: Stanimir-Georgiev Date: Mon, 17 Feb 2020 13:32:51 +0200 Subject: [PATCH 1/2] generate theme --- generateTheme.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 generateTheme.js diff --git a/generateTheme.js b/generateTheme.js new file mode 100644 index 0000000..c3c7ee1 --- /dev/null +++ b/generateTheme.js @@ -0,0 +1,79 @@ +const readline = require('readline'); +const path = require('path'); +const replaceInFiles = require('replace-in-file'); + +function generateTheme() { + let fullName = "DevriX Starter"; + let shortName = "DX-Starter"; + let textDomain = "dxstarter"; + let packageName = "DevriX_Starter"; + + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + let recursiveAsyncReadLine = function () { + rl.question('Please enter textDomain (dxstarter) ', function (textDomainInput) { + const pattern = /^[a-z]+$/ + if (!textDomainInput.match(pattern)) { + console.log("Invalid textDomain! The textDomain should be only lower case letters") + recursiveAsyncReadLine(); + } else { + rl.question("Please enter theme fullName (DevriX Starter) ", function (fullNameInput) { + rl.question("Please enter theme shortName (DX-Starter) ", function (shortNameInput) { + rl.question("Please enter packageName (DevriX_Starter) ", function (packageNameInput) { + if (fullNameInput !== "") { + fullName = fullNameInput + } + if (shortNameInput !== "") { + shortName = shortNameInput + } + if (textDomainInput !== "") { + textDomain = textDomainInput + } + if (packageNameInput !== "") { + packageName = packageNameInput + } + rl.close(); + }); + }); + }); + } + }); + + }; + rl.on("close", function () { + const options = { + + files: [ + path.resolve('./*'), + path.resolve('./assets/dist/css/*'), + path.resolve('./assets/src/sass/base/*'), + path.resolve('./inc/*'), + path.resolve('./template-parts/*'), + path.resolve('./templates/*'), + ], + + from: [/DevriX Starter/g, /DX-Starter/g, /dxstarter/g, /DevriX_Starter/g], + to: [fullName, shortName, textDomain, packageName], + + optionsForFiles: { + "ignore": [ + "/node_modules/" + ] + } + + }; + replaceInFiles(options).then(results => { + console.log("Remember to add author name and description"); + console.log("Generated") + console.log("This script is doing search and replace. Be careful when running second time.") + }).catch(error => { + console.log("Errors occured:", error) + }) + }); + recursiveAsyncReadLine(); +} +generateTheme() + diff --git a/package.json b/package.json index 35f759a..61833a5 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "gulp": "./node_modules/.bin/gulp", - "ziptheme": "bash zip.sh" + "ziptheme": "bash zip.sh", + "generate": "node ./generateTheme.js" }, "author": "DevriX", "license": "ISC", From 10ac8a0e039c6ded21bf7a47ea8d3f675fa4e98d Mon Sep 17 00:00:00 2001 From: Stanimir-Georgiev Date: Mon, 17 Feb 2020 13:59:54 +0200 Subject: [PATCH 2/2] add some comments --- generateTheme.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/generateTheme.js b/generateTheme.js index c3c7ee1..5c4a6ec 100644 --- a/generateTheme.js +++ b/generateTheme.js @@ -3,23 +3,26 @@ const path = require('path'); const replaceInFiles = require('replace-in-file'); function generateTheme() { + //default values of the theme let fullName = "DevriX Starter"; let shortName = "DX-Starter"; let textDomain = "dxstarter"; let packageName = "DevriX_Starter"; - const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); - let recursiveAsyncReadLine = function () { + + let recursiveAsyncReadLine = function () { + //textdomain field recursive validation rl.question('Please enter textDomain (dxstarter) ', function (textDomainInput) { const pattern = /^[a-z]+$/ if (!textDomainInput.match(pattern)) { console.log("Invalid textDomain! The textDomain should be only lower case letters") recursiveAsyncReadLine(); } else { + //user input rl.question("Please enter theme fullName (DevriX Starter) ", function (fullNameInput) { rl.question("Please enter theme shortName (DX-Starter) ", function (shortNameInput) { rl.question("Please enter packageName (DevriX_Starter) ", function (packageNameInput) { @@ -41,11 +44,12 @@ function generateTheme() { }); } }); - }; + rl.on("close", function () { + //search and replace options const options = { - + //path to the folders files: [ path.resolve('./*'), path.resolve('./assets/dist/css/*'), @@ -54,7 +58,8 @@ function generateTheme() { path.resolve('./template-parts/*'), path.resolve('./templates/*'), ], - + + //search expressions from: [/DevriX Starter/g, /DX-Starter/g, /dxstarter/g, /DevriX_Starter/g], to: [fullName, shortName, textDomain, packageName], @@ -65,6 +70,8 @@ function generateTheme() { } }; + + //calling the replace function replaceInFiles(options).then(results => { console.log("Remember to add author name and description"); console.log("Generated")