Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const path = require('path');
- const urlJoin = require("url-join");
- var Promise = require("bluebird");
- var fs = Promise.promisifyAll(require("fs"));
- var _ = require("underscore");
- var request = require("request-promise");
- var sass_compiler = require("node-sass");
- // a processor for sass files to css
- var sass_to_css_processor = (data) => sass_compiler.renderSync({"data":data}).css.toString();
- var conf = JSON.parse(fs.readFileSync(path.join(__dirname, "conf.json"), 'utf8'));
- var password = conf.password;
- var username = conf.username;
- var baseURL = conf.url;
- var rootProjectDir = process.argv[2];
- // dictionary containing one entry per type of item included here
- var type_table_map = {
- "widget": {
- tableName: "sp_widget", //SN table to update
- confSection: "pages", //conf.json property where items of this type are stored
- rootFilesDir: "./", //path relative to 'rootProjectDir' where items of this type are stored
- files: [{ //files required for update
- fileName: "client.js", // the name of the file
- column: "client_script" // the SN table column where it is stored
- }, {
- fileName: "code.html",
- column: "template"
- }, {
- fileName: "style.scss",
- column: "css"
- }, {
- fileName: "server.js",
- column: "script"
- }]
- },
- "provider": {
- confSection: "providers",
- tableName: "sp_angular_provider",
- rootFilesDir: "./providers",
- files: [{
- fileName: "script.js",
- column: "script"
- }]
- },
- "themes_css": {
- tableName: "sp_css",
- confSection: "themes_css",
- rootFilesDir: "./themes_css",
- files: [{
- fileName: "style.scss",
- column: "css",
- processor: sass_to_css_processor
- }]
- }
- };
- function getDataFromSN(username, password, type) {
- return request.get(urlJoin(baseURL,"/api/now/table/",type_table_map[type].tableName))
- .auth(username, password, false)
- .then(response => JSON.parse(response).result);
- }
- //saves data to SN.
- //data is a dic where keys are the database columns to be updated and the values are the content
- //sys_id is the id of the entry to be updated
- function saveSN(username, password, data, type, sys_id) {
- return request({
- url: urlJoin(baseURL,"/api/now/table/",type_table_map[type].tableName,sys_id),
- method: "PUT",
- json: data
- }).auth(username, password, false);
- }
- //utility function that returns a promise fulfilled when either the file is read or an error occurs.
- function readFilePromise(file) {
- return new Promise((f, r) => {
- fs.readFile(file, "utf-8", function(err, data) {
- if (err) {
- console.log("Error reading file");
- r(err);
- return;
- }
- f(data);
- });
- });
- }
- //processes a single file by reading it and returning an object with it's update info and contens
- function fileProcessor(basePath, fObj) {
- return readFilePromise(path.join(basePath, fObj.fileName))
- .then(c => ({"file": fObj,"content": ( fObj.processor? fObj.processor(c) : c)}));
- }
- function updateSN(type, itemName) {
- var objConf = type_table_map[type];
- var id = conf[objConf.confSection].find(i => i.name === itemName).id;
- return Promise.all(objConf.files.map(f => fileProcessor(path.join(rootProjectDir, objConf.rootFilesDir, itemName), f)))
- .then(output => {
- var data = {};
- //set props
- output.forEach(o => data[o.file.column] = o.content);
- return saveSN(username, password, data, type, id);
- });
- }
- Object.keys(type_table_map)
- .forEach(objType => {
- //get conf for this type of item
- var objConf = type_table_map[objType];
- //get all items of this type
- getDataFromSN(username, password, objType)
- .then((items) => items.filter(i => conf[objConf.confSection].find(x => x.id === i.sys_id))) // return only the ones we track
- .then((items) => {
- console.log("Found " + items.length + " " + objType);
- return conf[objConf.confSection].forEach((item) => {
- try {
- //catching the dirs in question
- fs.watch(path.join(rootProjectDir, objConf.rootFilesDir, item.name), () => {
- if (objConf.updating) return;//dont allow more than one concurrent update on the same item
- objConf.updating = true;//mark as true
- updateSN(objType, item.name)
- .then(() => console.log("Update done for "+item.name+" ("+objType+")"))
- .catch(() => console.log("Error Updating "+item.name+" ("+objType+")"))
- .finally(() => objConf.updating = false);//release for update whatever happened
- });
- } catch (err) {
- console.log("Could not watch dir " + path.join(rootProjectDir, objConf.rootFilesDir, item.name) + " for " + objType);
- }
- });
- })
- .catch(()=>console.log("Error loading "+objType+"s. Please check your connection."));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement