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");
- 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: true, openDevTools: true, 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));
- Promise.all(names.map((n) => readFilePromise(path.join(rootProjectDir, widgetName, n))))
- .then((contents) => {
- return 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(() => nightmare.evaluate(() => $("button[ng-click='c.save()']").click()));
- }
- return function (name) {
- var id = conf.pages.filter((p) => p.name == name)[0]["id"];
- if (id === currentID) {
- updateNSave(name);
- }
- 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("input[ng-model='section.display']")
- .evaluate(function () {
- $("input.ng-empty[ng-model='section.display']").click()
- }).then(() => updateNSave(name))
- .then(() => {
- currentID = id;
- });
- })
- }
- }
- }();
- 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