// const _arr_aziende = [ {n: "agricoper"}, {n: "auricchio", sub: ["auricchio_miki"]}, {n: "biolevante"}, {n: "carelli", sub: ["chiuso", "gestfood", "murgia", "panimal", "format", "winact"]}, {n: "cosmapack"}, {n: "didesi"}, {n: "didonna"}, {n: "dolce_bonta"}, {n: "dulciar"}, {n: "florapulia"}, {n: "folliesgroup"}, {n: "frudis"}, {n: "giovmaggio", sub: ["maggioSRL"]}, {n: "gramm"}, {n: "ime_te"}, {n: "ime_ba"}, {n: "integry"}, {n: "lamonarca"}, {n: "levanplast"}, {n: "licor"}, {n: "loredana"}, {n: "maggioSRL"}, {n: "medsol"}, {n: "midel"}, {n: "morgante"}, {n: "mysrl"}, {n: "new_life"}, {n: "of_demo"}, {n: "produzione"}, {n: "risto_cash"}, {n: "rossogargano", sub :["rossogarganocont"]}, {n: "sapori_veri", sub: ["dispensa"]}, {n: "smetar"}, {n: "suit", sub: ["santantonio", "carni_sardegna", "valpietro", "suit_puglia"]}, {n: "tosca_cloud"}, {n: "tosca_ce"}, {n: "tosca_ba"}, {n: "tosca_mi"}, {n: "tosca_rm"}, {n: "tosca_rg"}, {n: "tosca_vr"}, {n: "tosca_cz"}, {n: "tosca_fc"}, {n: "twobrothers"}, {n: "vgalimenti", sub: ["salpar"]}, {n: "vinella"} ]; // 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(), rename: rename_export_folder_script(), 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); const 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) { const moduleName = format_id(results.moduleName); let moduleTitle = results.moduleTitle; if (moduleTitle.length === 0) { moduleTitle = results.moduleName; } moduleTitle = ucwords(moduleTitle.replace("_", " ").toLowerCase()); const 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" }, { src: "public_html/gest-lib/" + moduleName + "/classes/EmptyAjax.php", dest: "public_html/gest-lib/" + moduleName + "/classes/" + moduleNameCC + "Ajax.php" }, { src: "public_html/gest-lib/" + moduleName + "/ts/EmptySection.ts", dest: "public_html/gest-lib/" + moduleName + "/ts/" + moduleNameCC + ".ts" }, ]); const e = {}; e[moduleName] = { title: moduleTitle, pages: moduleName + ".php", position: [], children: [], icon: "icon.png", libs: ["std-pkg"], requirePermission: true }; grunt.config.set("merge-append-json.newmodule.append", e); } } } }, "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/dist/build/js/application.min.js": get_listScriptFile(), "public_html/dist/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/fonts.css", "public_html/css/main/*.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) { const date = new Date(); const build = { name: cleanName(arg1), build: { date: date.toLocaleString(), timestamp: date.getTime() } }; grunt.file.write("exports/tmp/build.json", JSON.stringify(build)); }); _arr_aziende.forEach(azienda => { let nomeAzienda = azienda.n.toLowerCase(); grunt.registerTask(nomeAzienda, [ //"uglify", //"cssmin", //"copy:build_folder", //"copy:assets", "build:" + cleanName(nomeAzienda), "copy:" + cleanName(nomeAzienda), "file-creator:" + cleanName(nomeAzienda), //"clean:build", "compress:" + cleanName(nomeAzienda), //"rename:" + cleanName(nomeAzienda), "clean:tmp" ]); }); const 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"); arr_taskBuildAll.push("compress:assets"); _arr_aziende.forEach(item => { const nomeAzienda = item.n.toLowerCase(); arr_taskBuildAll.push(nomeAzienda); // 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("rename:" + cleanName(nomeAzienda)); }); // arr_taskBuildAll.push("clean:build"); 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() { const ret = {}; const _getFunc = function (_nomeAzienda) { return function (fs, fd, done) { fs.writeSync(fd, _nomeAzienda); done(); }; }; _arr_aziende.forEach(item => { const nomeAzienda = cleanName(item.n).toUpperCase(); const nomeAziendaLower = nomeAzienda.toLowerCase(); const path = "exports/tmp/nome_azienda.txt"; ret[nomeAziendaLower] = {}; ret[nomeAziendaLower][path] = _getFunc(nomeAzienda); });//log(ret) return ret; } function compress_getValues() { const ret = {}; _arr_aziende.forEach(azienda => { const nomeAzienda = cleanName(azienda.n).toUpperCase(); const nomeAziendaLower = nomeAzienda.toLowerCase(); ret[nomeAziendaLower] = { options: {archive: "exports/" + nomeAziendaLower + ".zip"}, expand: true, cwd: "exports/tmp", // + nomeAzienda + "/", src: ["**/*"], dest: "/", dot: true }; }); ret["assets"] = { options: {archive: "exports/_base.zip"}, expand: true, cwd: "exports/_base", src: ["**/*"], dest: "/", dot: true } return ret; } function rename_export_folder_script() { const ret = {}; _arr_aziende.forEach(azienda => { const nomeAzienda = cleanName(azienda.n).toUpperCase(); const nomeAziendaLower = nomeAzienda.toLowerCase(); ret[nomeAziendaLower] = { src: "exports/tmp", dest: "exports/" + nomeAziendaLower + "/" }; }); return ret; } function get_listModuli() { return require("./public_html/config-menu.json"); } function log(v) { console.log(v); } function get_listScriptFile() { const root = "public_html/"; const arr_scripts = require("./" + root + "include/scriptList.json"); for (let i = 0; i < arr_scripts.length; i++) { arr_scripts[i] = root + "dist/" + arr_scripts[i]; } return arr_scripts; } function get_listScriptFileLogin() { const root = "public_html/"; const arr_scripts = require("./" + root + "gest-lib/login/" + "include/scriptList.json"); for (let i = 0; i < arr_scripts.length; i++) { arr_scripts[i] = root + "dist/" + arr_scripts[i]; } return arr_scripts; } // function copy_getValues(grunt) { const ret = {}; try { const 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" ]; //const moduleList = get_listModuli(); _arr_aziende.forEach(azienda => { const nomeAzienda = cleanName(azienda.n).toLowerCase(); const arr_profiles = [nomeAzienda]; if (typeof azienda.sub != "undefined") { azienda.sub.forEach(profile => arr_profiles.push(profile)); } //const arr_gestAzi = []; //const data = require("./public_html/config_aziende/" + nomeAzienda.toUpperCase() + "/gestioni.json"); //data.forEach(gestione => arr_gestAzi.push(gestione.name)); //arr_gestAzi.push("tasks_launcher", "impostazioni", "index", "report_area", "associazione_report", "phpinfo", "login", "emailcheck", "user_settings", "messenger", "logs", "notifiche", "i18n", "weather", "allegati"); // Metto in build tutti i moduli // const arr_moduleIDToCopy = []; // for (const moduleID in moduleList) { // if (!moduleList.hasOwnProperty(moduleID)) continue; // if (arr_gestAzi.indexOf(moduleID) >= 0) { // arr_moduleIDToCopy.push(moduleID); // // const module = moduleList[moduleID]; // if ("children" in module && module.children != null) { // module.children.forEach(childModuleName => { // if (arr_moduleIDToCopy.indexOf(childModuleName) < 0 && childModuleName in moduleList) { // arr_moduleIDToCopy.push(childModuleName); // } // }) // } // } // } let arr_gestFileAzi = []; // Object.entries(moduleList).forEach(([moduleID]) => { // const module = moduleList[moduleID]; // const basename = moduleID.split("-")[0]; // // // AGGIUNGE FILE PHP GESTIONE IN ROOT // const 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 arr_profiles.forEach(profile => { //arr_gestFileAzi.push("!config_aziende/"+item.toUpperCase()+"/login-bg/**"); // non funziona arr_gestFileAzi.push("config_aziende/" + profile.toUpperCase() + "/**"); arr_gestFileAzi.push("config_aziende/" + profile.toLowerCase() + ".config.json"); }); // }); arr_gestFileAzi.push("config_aziende/.htaccess"); //let arr_copyValues = []; //["**", "!*.php", "!*.js", "!js/**", "!css/**", "!scss/**", "!assets/**", "!vendor/**", "!gest-lib/**", "!cache/**", "!config_aziende/**"]; //arr_copyValues.push("!**/old/**"); /* arr_copyValues = a.filter(function(item, pos){ // rimuove eventuali doppioni return a.indexOf(item) == pos; });*/ ret[nomeAzienda] = { files: [ { cwd: "public_html", src: arr_gestFileAzi,//.concat("!**/*.js", "!**/*.ts"), dest: "exports/tmp", // + nomeAzienda.toUpperCase(), expand: true, options: {timestamp: true} }, { cwd: "public_html", src: arr_profiles.map(profile => `dist/config_aziende/${profile.toUpperCase()}/**`), dest: "exports/tmp", expand: true, options: {timestamp: true} } ] }; }); ret["assets"] = { cwd: "public_html", src: [ "**", "!config_aziende/**", "!js/**", "!scss/**", "!css/**", "!gest-lib/**/*.js", "!gest-lib/**/*.ts", "!dist/config_aziende/**" ], dest: "exports/_base", // + nomeAzienda.toUpperCase(), expand: true, options: {timestamp: true}, dot: true }; ret.build_folder = { cwd: "public_html/", src: ["css/**", "!css/*/*.css", "!css/addons/**", "!**/*.css.map"], dest: "public_html/build/", expand: true, options: {timestamp: true} }; ret.newmodule_folder = { cwd: "public_html/gest-lib/empty_section", src: ["**", "../../empty_section.php"], dest: "", expand: true, options: { timestamp: true, processContent: function (content, srcpath) { // nelle versioni piu recenti si chiama 'process' const moduleNameCCLower = toCamelCase(format_id(grunt.config("moduleName"))); const moduleNameCC = ucfirst(moduleNameCCLower); content = content.replace(/EmptySection/g, moduleNameCC); content = content.replace(/EmptyAjax/g, moduleNameCC + "Ajax"); content = content.replace(/emptySection/g, moduleNameCCLower); content = content.replace(/empty_section/g, format_id(grunt.config("moduleName"))); return content; } } }; ret.newmodule_homephp = { options: { expand: true, flatten: true, processContent: function (content, srcpath) { // nelle versioni piu recenti si chiama 'process' const moduleName = format_id(grunt.config("moduleName")); const moduleNameCC = ucfirst(toCamelCase(moduleName)); //console.log(moduleName+" "+moduleNameCC); return content.replace(/EmptySection/i, moduleNameCC); } }, files: [] }; } catch (e) { console.log(e.message); } return ret; } // 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; }