Files
PVM/Gruntfile.js

499 lines
18 KiB
JavaScript

// <editor-fold desc="LISTA AZIENDE" defaultstate="collapsed">
var _arr_aziende = [
{n: "agricoper"},
{n: "auricchio", sub: ["auricchio_miki"]},
{n: "biolevante"},
{n: "carelli", sub: ["chiuso", "gfood", "gestfood", "murgia", "panimal", "format", "winact"]},
{n: "cosmapack"},
{n: "didesi"},
{n: "didonna"},
{n: "dolce_bo"},
{n: "dulciar"},
{n: "florapulia"},
{n: "folliesgroup"},
{n: "frudis"},
{n: "giovmaggio", sub: ["maggioSRL"]},
{n: "gramm"},
{n: "ime_te", sub: ["ime_ba"]},
{n: "lamonarca"},
{n: "licor"},
{n: "loredana"},
{n: "maggioSRL"},
{n: "medsol"},
{n: "midel"},
{n: "morgante"},
{n: "mysrl"},
{n: "new_life"},
{n: "produzione"},
{n: "risto_cash"},
{n: "sapori_veri", sub: ["dispensa"]},
{n: "smetar"},
{n: "suit", sub: ["santantonio", "carni_sardegna"]},
{n: "tosca_ce"},
{n: "tosca_mi"},
{n: "tosca_rm"},
{n: "tosca_rg"},
{n: "tosca_vr"},
{n: "twobrothers"},
{n: "vgalimenti", sub: ["salpar"]},
{n: "vinella"}
];
// </editor-fold >
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
clean: {
tmp: {
src: ["exports/tmp/*", "!exports/tmp/assets", "!exports/tmp/vendor"]
},
default: {
src: ["exports"]
},
build: {
src: ["public_html/build", "public_html/build.json", "exports/**/scriptList.json"]
}
},
copy: copy_getValues(grunt),
compress: compress_getValues(),
open: {file: {path: "C:\\wamp\\www\\pvm\\exports\\"}},
prompt: {
newmodule: {
options: {
questions: [
{
config: "moduleName", // arbitrary name or config for any other grunt task
type: "input", // list, checkbox, confirm, input, password
message: "ID modulo:", // Question to ask the user, function needs to return a string,
default: "", // default value if nothing is entered
validate: function (moduleID) {
if (typeof (moduleID) === "undefined" || !moduleID.length) {
return "Valore vuoto non ammesso";
} else {
moduleID = format_id(moduleID);
var moduleList = get_listModuli();
if (moduleID in moduleList) {
return "ID già esistente. Specificarne un altro";
}
//console.log(moduleList);
}
return true;
}
// choices: 'Array|function(answers)',
// validate: function(value), // return true if valid, error message if invalid. works only with type:input
// filter: function(value), // modify the answer
// when: function(answers) // only ask this question when this function returns true
},
{
config: "moduleTitle", // arbitrary name or config for any other grunt task
type: "input", // list, checkbox, confirm, input, password
message: "Titolo modulo:", // Question to ask the user, function needs to return a string,
default: "" // default value if nothing is entered
/*
validate: function(value){
if(typeof(value) === "undefined" || !value.length){
return "Valore vuoto non ammesso";
}
return true;
},*/
// choices: 'Array|function(answers)',
// validate: function(value), // return true if valid, error message if invalid. works only with type:input
// filter: function(value), // modify the answer
// when: function(answers) // only ask this question when this function returns true
}
],
then: function (results) {
var moduleName = format_id(results.moduleName);
var moduleTitle = results.moduleTitle;
if (moduleTitle.length === 0) {
moduleTitle = results.moduleName;
}
moduleTitle = ucwords(moduleTitle.replace("_", " ").toLowerCase());
var moduleNameCC = ucfirst(toCamelCase(moduleName));
// console.log(grunt.config("moduleName"));
grunt.config.set("mkdir.newmodule.options.create", ["public_html/gest-lib/" + moduleName]);
grunt.config.set("copy.newmodule_folder.dest", "public_html/gest-lib/" + moduleName);
grunt.config.set("copy.newmodule_homephp.files", [{
src: "public_html/empty_section.php",
dest: "public_html/" + moduleName + ".php"
}]);
grunt.config.set("rename.newmodule.files", [{
src: "public_html/gest-lib/" + moduleName + "/classes/EmptySection.class.php",
dest: "public_html/gest-lib/" + moduleName + "/classes/" + moduleNameCC + ".class.php"
}]);
var e = {};
e[moduleName] = {
title: moduleTitle,
pages: moduleName + ".php",
position: [],
children: [],
icon: "icon.png",
libs: ["std-pkg"]
};
grunt.config.set("merge-append-json.newmodule.append", e);
}
}
}
},
rename: {
newmodule: {
files: []
}
},
"merge-append-json": {
newmodule: {
src: "public_html/config-menu.json",
append: {},
dest: "public_html/config-menu.json"
}
},
uglify: {
options: {
mangle: false
},
build: {
files: {
"public_html/build/js/application.min.js": get_listScriptFile(),
"public_html/build/js/login.min.js": get_listScriptFileLogin()
}
}
},
cssmin: {
options: {
mergeIntoShorthands: false,
roundingPrecision: -1
},
build: {
files: {
"public_html/build/css/main/main.min.css": ["public_html/css/main/*.css"],
"public_html/build/css/main/fonts.min.css": ["public_html/css/main/fonts.css"],
"public_html/build/css/addons.min.css": ["public_html/css/addons/*.css"],
"public_html/build/css/login.min.css": ["public_html/css/main/layout.css", "public_html/css/addons/bootstrap-hooks.css"]
}
}
},
"file-creator": fileCreator_getValues()
});
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-compress");
grunt.loadNpmTasks("grunt-open");
grunt.loadNpmTasks("grunt-prompt");
grunt.loadNpmTasks("grunt-contrib-rename");
grunt.loadNpmTasks("grunt-merge-append-json");
grunt.loadNpmTasks("grunt-contrib-cssmin");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-file-creator");
grunt.registerTask("build", "Salva file di build", function (arg1) {
var date = new Date();
var build = {
name: cleanName(arg1),
build: {
date: date.toLocaleString(),
timestamp: date.getTime()
}
};
grunt.file.write("public_html/build.json", JSON.stringify(build));
});
var arr_taskBuildAll = [];
arr_taskBuildAll.push("clean:default");
arr_taskBuildAll.push("uglify");
arr_taskBuildAll.push("cssmin");
arr_taskBuildAll.push("copy:build_folder");
arr_taskBuildAll.push("copy:assets");
for (var i in _arr_aziende) {
var nomeAzienda = _arr_aziende[i].n.toLowerCase();
grunt.registerTask(nomeAzienda, [
"clean:default",
"uglify",
"cssmin",
"copy:build_folder",
"build:" + cleanName(nomeAzienda),
"copy:" + cleanName(nomeAzienda),
"file-creator:" + cleanName(nomeAzienda),
"clean:build",
"compress:" + cleanName(nomeAzienda),
"open:file"
]);
arr_taskBuildAll.push("clean:tmp");
arr_taskBuildAll.push("build:" + cleanName(nomeAzienda));
arr_taskBuildAll.push("copy:" + cleanName(nomeAzienda));
arr_taskBuildAll.push("file-creator:" + cleanName(nomeAzienda));
arr_taskBuildAll.push("compress:" + cleanName(nomeAzienda));
}
arr_taskBuildAll.push("clean:build");
arr_taskBuildAll.push("open:file");
grunt.registerTask("BuildAll", arr_taskBuildAll);
grunt.registerTask("Nuovo modulo", ["prompt:newmodule", "copy:newmodule_folder", "copy:newmodule_homephp", "rename:newmodule", "merge-append-json:newmodule"]);
};
function format_id(s) {
return s.replace(/ /g, "_").toLowerCase();
}
function cleanName(s) {
return s.replace(/-/g, "");
}
function fileCreator_getValues() {
var ret = {};
var _getFunc = function (_nomeAzienda) {
return function (fs, fd, done) {
fs.writeSync(fd, _nomeAzienda);
done();
};
};
for (var i in _arr_aziende) {
var nomeAzienda = cleanName(_arr_aziende[i].n).toUpperCase();
var nomeAziendaLower = nomeAzienda.toLowerCase();
var path = "exports/tmp/nome_azienda.txt";
ret[nomeAziendaLower] = {};
ret[nomeAziendaLower][path] = _getFunc(nomeAzienda);
}//log(ret)
return ret;
}
function compress_getValues() {
var ret = {};
for (var i in _arr_aziende) {
var nomeAzienda = cleanName(_arr_aziende[i].n).toUpperCase();
var nomeAziendaLower = nomeAzienda.toLowerCase();
ret[nomeAziendaLower] = {
options: {archive: "exports/" + nomeAziendaLower + ".zip"},
expand: true,
cwd: "exports/tmp", // + nomeAzienda + "/",
src: ["**/*"],
dest: "/"
};
}
return ret;
}
function get_listModuli() {
return require("./public_html/config-menu.json");
}
function log(v) {
console.log(v);
}
function get_listScriptFile() {
var root = "public_html/";
var arr_scripts = require("./" + root + "include/scriptList.json");
for (var i = 0; i < arr_scripts.length; i++) {
arr_scripts[i] = root + arr_scripts[i];
}
return arr_scripts;
}
function get_listScriptFileLogin() {
var root = "public_html/";
var arr_scripts = require("./" + root + "gest-lib/login/" + "include/scriptList.json");
for (var i = 0; i < arr_scripts.length; i++) {
arr_scripts[i] = root + arr_scripts[i];
}
return arr_scripts;
}
// <editor-fold desc="copy_getValues" defaultstate="collapsed">
function copy_getValues(grunt) {
var ret = {};
try {
var arr_systemFile = [
"config.php", "flushcache.php", "functions.php", "image.php", "download.php",
"include_login.php", "index.php", "logout.php", "obj-json-b64-codecs.php", "firebase-messaging-sw.js"
];
var moduleList = get_listModuli();
for (var i in _arr_aziende) {
var item = _arr_aziende[i];
var nomeAzienda = cleanName(item.n).toLowerCase();
var arr_profiles = [nomeAzienda];
if (typeof item.sub != "undefined") {
for (var i in item.sub) {
arr_profiles.push(item.sub[i]);
}
}
var arr_gestAzi = [];
var data = require("./public_html/config_aziende/" + nomeAzienda.toUpperCase() + "/gestioni.json");
for (var i in data) {
var item = data[i];
arr_gestAzi.push(item.name);
}
arr_gestAzi.push("tasks_launcher", "impostazioni", "index", "report_area", "associazione_report", "phpinfo", "login", "emailcheck", "user_settings", "messenger", "logs", "notifiche", "i18n", "weather", "allegati");
var arr_moduleIDToCopy = [];
for (var moduleID in moduleList) {
if (!moduleList.hasOwnProperty(moduleID)) continue;
if (arr_gestAzi.indexOf(moduleID) >= 0) {
arr_moduleIDToCopy.push(moduleID);
var module = moduleList[moduleID];
if ("children" in module && module.children != null) {
for (var j in module.children) {
var childModuleName = module.children[j];
if (arr_moduleIDToCopy.indexOf(childModuleName) < 0 && childModuleName in moduleList) {
arr_moduleIDToCopy.push(childModuleName);
}
}
}
}
}
var arr_gestFileAzi = [];
for (var i = 0; i < arr_moduleIDToCopy.length; i++) {
var moduleID = arr_moduleIDToCopy[i];
var module = moduleList[moduleID];
var basename = moduleID.split("-")[0];
// AGGIUNGE FILE PHP GESTIONE IN ROOT
var page = module.pages.split("?")[0]; // es. "colli_aperti.php?spedizione" -> "colli_aperti.php", "spedizione"
arr_gestFileAzi.push(page);
// AGGIUNGE FILE SFUSI SPECIFICATI IN gestione.include
if (typeof module.include != "undefined") {
arr_gestFileAzi = arr_gestFileAzi.concat(module.include);
}
// AGGIUNGE DIRECTORY GESTIONI SOTTO gest-lib
arr_gestFileAzi.push("gest-lib/" + basename + "/**");
// NON COPIA SCSS IN gest-lib
//arr_gestFileAzi.push("!gest-lib/"+basename+"/scss/**");
// AGGIUNGE FILE CONFIGURAZIONE AZIENDA
for (var j = 0; j < arr_profiles.length; j++) {
var item = arr_profiles[j];
//arr_gestFileAzi.push("!config_aziende/"+item.toUpperCase()+"/login-bg/**"); // non funziona
arr_gestFileAzi.push("config_aziende/" + item.toUpperCase() + "/**");
arr_gestFileAzi.push("config_aziende/" + item.toLowerCase() + ".config.json");
}
}
var arr_copyValues = ["**", "!*.php", "!*.js", "!js/**", "!css/**", "!assets/**", "!vendor/**",
"!gest-lib/**", "!cache/**", "!config_aziende/**"];
arr_copyValues = arr_copyValues.concat(arr_systemFile);
arr_copyValues = arr_copyValues.concat(arr_gestFileAzi);
arr_copyValues.push("!**/old/**");
/*
arr_copyValues = a.filter(function(item, pos){ // rimuove eventuali doppioni
return a.indexOf(item) == pos;
});*/
ret[nomeAzienda] = {
cwd: "public_html",
src: arr_copyValues,
dest: "exports/tmp", // + nomeAzienda.toUpperCase(),
expand: true,
options: {timestamp: true}
};
}
;
ret["assets"] = {
cwd: "public_html",
src: ["assets/**", "vendor/**"],
dest: "exports/tmp", // + nomeAzienda.toUpperCase(),
expand: true,
options: {timestamp: true}
};
ret.build_folder = {
cwd: "public_html/",
src: ["css/**", "!css/*/*.css", "!css/addons"],
dest: "public_html/build/",
expand: true,
options: {timestamp: true}
};
ret.newmodule_folder = {
cwd: "public_html/gest-lib/empty_section",
src: "**",
dest: "",
expand: true,
options: {
timestamp: true,
processContent: function (content, srcpath) { // nelle versioni piu recenti si chiama 'process'
var moduleNameCCLower = toCamelCase(format_id(grunt.config("moduleName")));
var moduleNameCC = ucfirst(moduleNameCCLower);
content = content.replace(/EmptySection/g, moduleNameCC);
content = content.replace(/emptySection/g, moduleNameCCLower);
return content;
}
}
};
ret.newmodule_homephp = {
options: {
expand: true,
flatten: true,
processContent: function (content, srcpath) { // nelle versioni piu recenti si chiama 'process'
var moduleName = format_id(grunt.config("moduleName"));
var moduleNameCC = ucfirst(toCamelCase(moduleName));
//console.log(moduleName+" "+moduleNameCC);
return content.replace(/EmptySection/i, moduleNameCC);
}
},
files: []
};
} catch (e) {
console.log(e.message);
}
return ret;
}
// </editor-fold>
function toCamelCase(s) {
return s.replace(/(\_[a-z])/g, function ($1) {
return $1.toUpperCase().replace("_", "");
});
}
function ucfirst(s) {
return s !== null ? s.charAt(0).toUpperCase() + s.slice(1) : null;
}
function ucwords(s) {
if (s !== null) {
s = s.toLowerCase();
return s.replace(/(^([a-zA-Z\p{M}]))|([ -][a-zA-Z\p{M}])/g,
function (s) {
return s.toUpperCase();
});
}
return null;
}