Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. const path = require('path');
  2. var Nightmare = require("nightmare");
  3. var Promise = require("bluebird");
  4. Nightmare.Promise = Promise;
  5. var fs = Promise.promisifyAll(require("fs"));
  6. var _ = require("underscore");
  7.  
  8. var conf = JSON.parse(fs.readFileSync(path.join(__dirname, "conf.json"), 'utf8'));
  9. var nightmare_conf = {
  10. show: conf.showBrowser, openDevTools: false, webPreferences: {
  11. partition: 'nopersist'
  12. },
  13. typeInterval: 1
  14. };
  15. var password = conf.password;
  16. var username = conf.username;
  17. var baseURL = conf.url;
  18.  
  19. var rootProjectDir = process.argv[2];
  20.  
  21.  
  22. function readFilePromise(file) {
  23.  
  24. return new Promise((f, r) => {
  25.  
  26. fs.readFile(file, "utf-8", function (err, data) {
  27. if (err) {
  28. console.log("Error reading file");
  29. r(err);
  30. return;
  31. }
  32. f(data);
  33. });
  34.  
  35. });
  36.  
  37. }
  38.  
  39. var names = ["code.html", "style.scss", "client.js", "server.js"];
  40.  
  41. var updater = function () {
  42.  
  43. var currentID;
  44.  
  45. var nightmare = new Nightmare(nightmare_conf);
  46.  
  47.  
  48. function updateNSave(widgetName) {
  49. console.log("Path = " + path.join(rootProjectDir, widgetName));
  50. return Promise.all(names.map((n) => readFilePromise(path.join(rootProjectDir, widgetName, n))))
  51. .then((contents) =>
  52. nightmare.evaluate(function (contents) {
  53. var editors = $("div.CodeMirror");
  54. $(editors[0]).prop("CodeMirror").setValue(contents[0]);
  55. $(editors[1]).prop("CodeMirror").setValue(contents[1]);
  56. $(editors[2]).prop("CodeMirror").setValue(contents[2]);
  57. $(editors[3]).prop("CodeMirror").setValue(contents[3]);
  58. }, contents)
  59. )
  60. .then(() => {
  61.  
  62. return nightmare
  63. .click("button[ng-click='c.save()']")
  64. .wait(function(){return angular.element(document.getElementById("uiNotificationContainer")).scope().c.notifications.filter(n=>n.message === "Saved").length > 0;})
  65.  
  66. });
  67. }
  68.  
  69. return function (name) {
  70.  
  71. var id = conf.pages.find((p) => p.name == name)["id"];
  72.  
  73. if(name === "smartHeader"){ //special case
  74. id = conf.pages.find((p) => p.name !== name)["id"];
  75. }
  76.  
  77. if (id === currentID) {
  78. updateNSave(name)
  79. .then(()=>console.log("Update Done"));
  80. }
  81. else{
  82. nightmare.halt(()=>undefined,()=>{
  83. nightmare = new Nightmare(nightmare_conf);
  84. nightmare.goto(baseURL + id)
  85. .wait("#username")
  86. .type("input#username", username)
  87. .type("input#password", password)
  88. .click("button.btn-block")
  89. .wait("button[ng-click='c.save()']")
  90. .evaluate(function () {
  91. console.log("Evaluating");
  92. $("input.ng-empty[ng-model='section.display']").click()
  93. })
  94. .evaluate(function (header) {
  95. if(!header)return;
  96. else{
  97. $("a.select2-choice").mousedown();
  98. window.setTimeout(()=>$("div.select2-result-label").filter((i,e)=>$(e).text() === '[u]Smart Header-u-smart_header').mouseup(),1000);
  99. }
  100. },name === "smartHeader")
  101. .wait(1100)
  102. .wait(".header-loader.ng-hide")//wait until load symbol is gone
  103. .then(() => updateNSave(name))
  104. .then(() => {
  105. if(name === "smartHeader"){
  106. currentID = undefined;
  107. }
  108. else{
  109. currentID = id;
  110. }
  111. console.log("Update Done");
  112. });
  113. })
  114. }
  115. }
  116.  
  117. }();
  118.  
  119.  
  120. var watchers = conf.pages
  121. .map(e => e.name)
  122. .forEach((dir) => {
  123. try {
  124. fs.watch(path.join(rootProjectDir, dir), (action, filename) => {
  125. console.log("Ac = " + action + " file = " + filename + " in dir " + dir);
  126. updater(dir);
  127. });
  128. }
  129. catch (err) {
  130. console.log("Couldn't find dir " + dir);
  131. }
  132.  
  133. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement