Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. const path = require('path');
  2. var Nightmare = require("nightmare");
  3. var Promise = require("bluebird");
  4. var fs = Promise.promisifyAll(require("fs"));
  5. var _ = require("underscore");
  6.  
  7. var conf = JSON.parse(fs.readFileSync(path.join(__dirname, "conf.json"), 'utf8'));
  8. var nightmare_conf = {
  9. show: true, openDevTools: true, webPreferences: {
  10. partition: 'nopersist'
  11. },
  12. typeInterval: 1
  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.  
  21. function readFilePromise(file) {
  22.  
  23. return new Promise((f, r) => {
  24.  
  25. fs.readFile(file, "utf-8", function (err, data) {
  26. if (err) {
  27. console.log("Error reading file");
  28. r(err);
  29. return;
  30. }
  31. f(data);
  32. });
  33.  
  34. });
  35.  
  36. }
  37.  
  38. var names = ["code.html", "style.scss", "client.js", "server.js"];
  39.  
  40. var updater = function () {
  41.  
  42. var currentID;
  43.  
  44. var nightmare = new Nightmare(nightmare_conf);
  45.  
  46.  
  47. function updateNSave(widgetName) {
  48. console.log("Path = " + path.join(rootProjectDir, widgetName));
  49. Promise.all(names.map((n) => readFilePromise(path.join(rootProjectDir, widgetName, n))))
  50. .then((contents) => {
  51. return nightmare.evaluate(function (contents) {
  52. var editors = $("div.CodeMirror");
  53. $(editors[0]).prop("CodeMirror").setValue(contents[0]);
  54. $(editors[1]).prop("CodeMirror").setValue(contents[1]);
  55. $(editors[2]).prop("CodeMirror").setValue(contents[2]);
  56. $(editors[3]).prop("CodeMirror").setValue(contents[3]);
  57. }, contents);
  58. })
  59. .then(() => nightmare.evaluate(() => $("button[ng-click='c.save()']").click()));
  60.  
  61. }
  62.  
  63. return function (name) {
  64.  
  65. var id = conf.pages.filter((p) => p.name == name)[0]["id"];
  66.  
  67. if (id === currentID) {
  68. updateNSave(name);
  69. }
  70. else {
  71. nightmare.halt(()=>undefined,()=>{
  72. nightmare = new Nightmare(nightmare_conf);
  73. nightmare.goto(baseURL + id)
  74. .wait("#username")
  75. .type("input#username", username)
  76. .type("input#password", password)
  77. .click("button.btn-block")
  78. .wait("input[ng-model='section.display']")
  79. .evaluate(function () {
  80. $("input.ng-empty[ng-model='section.display']").click()
  81. }).then(() => updateNSave(name))
  82. .then(() => {
  83. currentID = id;
  84. });
  85. })
  86. }
  87.  
  88. }
  89.  
  90. }();
  91.  
  92.  
  93. var watchers = conf.pages
  94. .map(e => e.name)
  95. .forEach((dir) => {
  96. try {
  97. fs.watch(path.join(rootProjectDir, dir), (action, filename) => {
  98. console.log("Ac = " + action + " file = " + filename + " in dir " + dir);
  99. updater(dir);
  100. });
  101. }
  102. catch (err) {
  103. console.log("Couldn't find dir " + dir);
  104. }
  105.  
  106. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement