Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const path = require('path');
- var Nightmare = require("nightmare");
- var Promise = require("bluebird");
- Nightmare.Promise = Promise;
- var fs = Promise.promisifyAll(require("fs"));
- var _ = require("underscore");
- var conf = JSON.parse(fs.readFileSync(path.join(__dirname, "conf.json"), 'utf8'));
- var nightmare_conf = {
- show: conf.showBrowser, openDevTools: false, webPreferences: {
- partition: 'nopersist'
- },
- typeInterval: 1
- };
- var password = conf.password;
- var username = conf.username;
- var baseURL = conf.url;
- var rootProjectDir = process.argv[2];
- function readFilePromise(file) {
- return new Promise((f, r) => {
- fs.readFile(file, "utf-8", function (err, data) {
- if (err) {
- console.log("Error reading file");
- r(err);
- return;
- }
- f(data);
- });
- });
- }
- var names = ["code.html", "style.scss", "client.js", "server.js"];
- var updater = function () {
- var currentID;
- var nightmare = new Nightmare(nightmare_conf);
- function updateNSave(widgetName) {
- console.log("Path = " + path.join(rootProjectDir, widgetName));
- return Promise.all(names.map((n) => readFilePromise(path.join(rootProjectDir, widgetName, n))))
- .then((contents) =>
- nightmare.evaluate(function (contents) {
- var editors = $("div.CodeMirror");
- $(editors[0]).prop("CodeMirror").setValue(contents[0]);
- $(editors[1]).prop("CodeMirror").setValue(contents[1]);
- $(editors[2]).prop("CodeMirror").setValue(contents[2]);
- $(editors[3]).prop("CodeMirror").setValue(contents[3]);
- }, contents)
- )
- .then(() => {
- return nightmare
- .click("button[ng-click='c.save()']")
- .wait(function(){return angular.element(document.getElementById("uiNotificationContainer")).scope().c.notifications.filter(n=>n.message === "Saved").length > 0;})
- });
- }
- return function (name) {
- var id = conf.pages.find((p) => p.name == name)["id"];
- if(name === "smartHeader"){ //special case
- id = conf.pages.find((p) => p.name !== name)["id"];
- }
- if (id === currentID) {
- updateNSave(name)
- .then(()=>console.log("Update Done"));
- }
- else{
- nightmare.halt(()=>undefined,()=>{
- nightmare = new Nightmare(nightmare_conf);
- nightmare.goto(baseURL + id)
- .wait("#username")
- .type("input#username", username)
- .type("input#password", password)
- .click("button.btn-block")
- .wait("button[ng-click='c.save()']")
- .evaluate(function () {
- console.log("Evaluating");
- $("input.ng-empty[ng-model='section.display']").click()
- })
- .evaluate(function (header) {
- if(!header)return;
- else{
- $("a.select2-choice").mousedown();
- window.setTimeout(()=>$("div.select2-result-label").filter((i,e)=>$(e).text() === '[u]Smart Header-u-smart_header').mouseup(),1000);
- }
- },name === "smartHeader")
- .wait(1100)
- .wait(".header-loader.ng-hide")//wait until load symbol is gone
- .then(() => updateNSave(name))
- .then(() => {
- if(name === "smartHeader"){
- currentID = undefined;
- }
- else{
- currentID = id;
- }
- console.log("Update Done");
- });
- })
- }
- }
- }();
- var watchers = conf.pages
- .map(e => e.name)
- .forEach((dir) => {
- try {
- fs.watch(path.join(rootProjectDir, dir), (action, filename) => {
- console.log("Ac = " + action + " file = " + filename + " in dir " + dir);
- updater(dir);
- });
- }
- catch (err) {
- console.log("Couldn't find dir " + dir);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement