Guest User

Untitled

a guest
Feb 8th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 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: false, 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.  
  19.  
  20. function readFilePromise(file) {
  21.  
  22. return new Promise((f, r) => {
  23.  
  24. fs.readFile(file, "utf-8", function (err, data) {
  25. if (err) {
  26. console.log("Error reading file");
  27. r(err);
  28. return;
  29. }
  30. f(data);
  31. });
  32.  
  33. });
  34.  
  35. }
  36.  
  37.  
  38.  
  39. var names = ["code.html", "style.scss", "client.js", "server.js"];
  40.  
  41. var nightmare = new Nightmare(nightmare_conf);
  42.  
  43. var id = conf.pages.filter((p)=>p.name==process.argv[2])[0]["id"];
  44.  
  45. console.log("username = " + username + " password = " + password + " id = " + id);
  46.  
  47. nightmare.goto(baseURL + id)
  48. .wait("#username")
  49. .type("input#username", username)
  50. .type("input#password", password)
  51. .click("button.btn-block")
  52. .wait("input[ng-model='section.display']")
  53. .evaluate(function () {
  54. $("input.ng-empty[ng-model='section.display']").click()
  55. })
  56. .evaluate(function () {
  57. var editors = $("div.CodeMirror");
  58. return [
  59. $(editors[0]).prop("CodeMirror").getValue(),
  60. $(editors[1]).prop("CodeMirror").getValue(),
  61. $(editors[2]).prop("CodeMirror").getValue(),
  62. $(editors[3]).prop("CodeMirror").getValue()
  63. ];
  64. })
  65. .then((srcs) => {
  66. console.log("Loading Content for the first time");
  67. var data = _.zip.apply(null, [names, srcs]);
  68. return Promise.all(data.map((data) => {
  69. return fs.writeFile("./" + data[0], data[1])
  70. }));
  71. })
  72. .then(() => {
  73. console.log("watching");
  74. watcher = fs.watch(process.cwd(), (action, filename) => {
  75.  
  76. if (!_.contains(names, filename)) return;
  77.  
  78. console.log("action = " + action + " filename = " + filename);
  79.  
  80. if(action != 'change'){
  81. console.log("Ignoring action");
  82. return;
  83. }
  84.  
  85. Promise.all(names.map((n) => readFilePromise(path.join(process.cwd(),n))))
  86. .then((contents) => {
  87. return nightmare.evaluate(function (contents) {
  88. var editors = $("div.CodeMirror");
  89. $(editors[0]).prop("CodeMirror").setValue(contents[0]);
  90. $(editors[1]).prop("CodeMirror").setValue(contents[1]);
  91. $(editors[2]).prop("CodeMirror").setValue(contents[2]);
  92. $(editors[3]).prop("CodeMirror").setValue(contents[3]);
  93. }, contents);
  94. })
  95. .then(() => nightmare.evaluate(() => $("button[ng-click='c.save()']").click()));
  96. });
  97. })
  98. .catch((e) => {
  99. console.log(e);
  100. });
Add Comment
Please, Sign In to add comment