Advertisement
Guest User

Untitled

a guest
May 16th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. const path = require('path');
  2. const urlJoin = require("url-join");
  3. var Promise = require("bluebird");
  4. var fs = Promise.promisifyAll(require("fs"));
  5. var _ = require("underscore");
  6. var request = require("request-promise");
  7. var sass_compiler = require("node-sass");
  8.  
  9. // a processor for sass files to css
  10. var sass_to_css_processor = (data) => sass_compiler.renderSync({"data":data}).css.toString();
  11.  
  12. var conf = JSON.parse(fs.readFileSync(path.join(__dirname, "conf.json"), 'utf8'));
  13.  
  14. var password = conf.password;
  15. var username = conf.username;
  16. var baseURL = conf.url;
  17.  
  18. var rootProjectDir = process.argv[2];
  19.  
  20. // dictionary containing one entry per type of item included here
  21. var type_table_map = {
  22. "widget": {
  23. tableName: "sp_widget", //SN table to update
  24. confSection: "pages", //conf.json property where items of this type are stored
  25. rootFilesDir: "./", //path relative to 'rootProjectDir' where items of this type are stored
  26. files: [{ //files required for update
  27. fileName: "client.js", // the name of the file
  28. column: "client_script" // the SN table column where it is stored
  29. }, {
  30. fileName: "code.html",
  31. column: "template"
  32. }, {
  33. fileName: "style.scss",
  34. column: "css"
  35. }, {
  36. fileName: "server.js",
  37. column: "script"
  38. }]
  39. },
  40. "provider": {
  41. confSection: "providers",
  42. tableName: "sp_angular_provider",
  43. rootFilesDir: "./providers",
  44. files: [{
  45. fileName: "script.js",
  46. column: "script"
  47. }]
  48. },
  49. "themes_css": {
  50. tableName: "sp_css",
  51. confSection: "themes_css",
  52. rootFilesDir: "./themes_css",
  53. files: [{
  54. fileName: "style.scss",
  55. column: "css",
  56. processor: sass_to_css_processor
  57. }]
  58. }
  59. };
  60.  
  61. function getDataFromSN(username, password, type) {
  62. return request.get(urlJoin(baseURL,"/api/now/table/",type_table_map[type].tableName))
  63. .auth(username, password, false)
  64. .then(response => JSON.parse(response).result);
  65. }
  66.  
  67.  
  68. //saves data to SN.
  69. //data is a dic where keys are the database columns to be updated and the values are the content
  70. //sys_id is the id of the entry to be updated
  71. function saveSN(username, password, data, type, sys_id) {
  72. return request({
  73. url: urlJoin(baseURL,"/api/now/table/",type_table_map[type].tableName,sys_id),
  74. method: "PUT",
  75. json: data
  76. }).auth(username, password, false);
  77. }
  78.  
  79. //utility function that returns a promise fulfilled when either the file is read or an error occurs.
  80. function readFilePromise(file) {
  81.  
  82. return new Promise((f, r) => {
  83.  
  84. fs.readFile(file, "utf-8", function(err, data) {
  85. if (err) {
  86. console.log("Error reading file");
  87. r(err);
  88. return;
  89. }
  90. f(data);
  91. });
  92.  
  93. });
  94.  
  95. }
  96.  
  97. //processes a single file by reading it and returning an object with it's update info and contens
  98. function fileProcessor(basePath, fObj) {
  99. return readFilePromise(path.join(basePath, fObj.fileName))
  100. .then(c => ({"file": fObj,"content": ( fObj.processor? fObj.processor(c) : c)}));
  101. }
  102.  
  103. function updateSN(type, itemName) {
  104.  
  105. var objConf = type_table_map[type];
  106.  
  107. var id = conf[objConf.confSection].find(i => i.name === itemName).id;
  108.  
  109.  
  110. return Promise.all(objConf.files.map(f => fileProcessor(path.join(rootProjectDir, objConf.rootFilesDir, itemName), f)))
  111. .then(output => {
  112.  
  113. var data = {};
  114. //set props
  115. output.forEach(o => data[o.file.column] = o.content);
  116. return saveSN(username, password, data, type, id);
  117. });
  118.  
  119.  
  120. }
  121.  
  122.  
  123. Object.keys(type_table_map)
  124. .forEach(objType => {
  125.  
  126. //get conf for this type of item
  127. var objConf = type_table_map[objType];
  128.  
  129. //get all items of this type
  130. getDataFromSN(username, password, objType)
  131. .then((items) => items.filter(i => conf[objConf.confSection].find(x => x.id === i.sys_id))) // return only the ones we track
  132. .then((items) => {
  133. console.log("Found " + items.length + " " + objType);
  134.  
  135. return conf[objConf.confSection].forEach((item) => {
  136. try {
  137. //catching the dirs in question
  138. fs.watch(path.join(rootProjectDir, objConf.rootFilesDir, item.name), () => {
  139. if (objConf.updating) return;//dont allow more than one concurrent update on the same item
  140. objConf.updating = true;//mark as true
  141. updateSN(objType, item.name)
  142. .then(() => console.log("Update done for "+item.name+" ("+objType+")"))
  143. .catch(() => console.log("Error Updating "+item.name+" ("+objType+")"))
  144. .finally(() => objConf.updating = false);//release for update whatever happened
  145. });
  146. } catch (err) {
  147. console.log("Could not watch dir " + path.join(rootProjectDir, objConf.rootFilesDir, item.name) + " for " + objType);
  148. }
  149. });
  150. })
  151. .catch(()=>console.log("Error loading "+objType+"s. Please check your connection."));
  152. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement