Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. var Promise = require("bluebird");
  2. var _ = require("underscore");
  3. var fs = Promise.promisifyAll(require("fs"));
  4. var Nightmare = require("nightmare");
  5. var iframe = require('nightmare-iframe');
  6.  
  7. var nightmare = new Nightmare({
  8. show: false, openDevTools: true, webPreferences: {
  9. partition: 'nopersist'
  10. },
  11. typeInterval:1
  12. });
  13.  
  14. var names = ["code.html", "style.css", "client.js", "server.js"];
  15. var args = process.argv;
  16.  
  17. var baseURL = "https://smartdev.service-now.com/sp_config?id=widget_editor&sys_id=";
  18.  
  19. var id = args[2];
  20. var username = args[3];
  21. var password = args[4];
  22.  
  23. var readFilePromise = (file) => {
  24.  
  25. return new Promise((f,r)=>{
  26.  
  27. fs.readFile(file,"utf-8",function(err,data){
  28. if(err){
  29. console.log("Error reading file");
  30. r(err);
  31. return;
  32. }
  33. //console.log("Success reading file");
  34. f(data);
  35. });
  36.  
  37. });
  38.  
  39. }
  40.  
  41.  
  42. nightmare.goto(baseURL + id)
  43. .wait("#username")
  44. .type("input#username", username)
  45. .type("input#password", password)
  46. .click("button.btn-block")
  47. .wait("input[ng-model='section.display']")
  48. .evaluate(function () {
  49. $("input.ng-empty[ng-model='section.display']").click()
  50. })
  51. .evaluate(function () {
  52.  
  53. var editors = $("div.CodeMirror");
  54.  
  55. return [
  56. $(editors[0]).prop("CodeMirror").getValue(),
  57. $(editors[1]).prop("CodeMirror").getValue(),
  58. $(editors[2]).prop("CodeMirror").getValue(),
  59. $(editors[3]).prop("CodeMirror").getValue()
  60. ];
  61.  
  62.  
  63. })
  64. .then((srcs) => {
  65. console.log("Loading Content for the first time");
  66.  
  67. var data = _.zip.apply(null, [names, srcs]);
  68.  
  69. return Promise.all(data.map((data) => {
  70. return fs.writeFile("./" + data[0], data[1])
  71. }));
  72.  
  73. })
  74. .then(() => {
  75.  
  76. console.log("watching");
  77.  
  78. fs.watch("./", (action, filename) => {
  79.  
  80. if (!_.contains(names,filename))return;
  81.  
  82. console.log("action = " + action + " filename = " + filename);
  83.  
  84. Promise.all(names.map((n)=>readFilePromise("./"+n)))
  85. .then((contents)=>{
  86. return nightmare.evaluate(function(contents){
  87. var editors = $("div.CodeMirror");
  88.  
  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.  
  94. },contents);
  95. })
  96. .then(()=>nightmare.evaluate(()=>$("button[ng-click='c.save()']").click()));
  97. });
  98. })
  99. .catch((e) => {
  100. console.log(e);
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement