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: false, openDevTools: true, webPreferences: {
- partition: 'nopersist'
- },
- typeInterval: 1
- };
- var password = conf.password;
- var username = conf.username;
- var baseURL = conf.url;
- 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 nightmare = new Nightmare(nightmare_conf);
- var id = conf.pages.filter((p)=>p.name=process.argv[1])[0]["id"];
- console.log("username = " + username + " password = " + password + " id = " + id);
- 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()
- })
- .evaluate(function () {
- var editors = $("div.CodeMirror");
- return [
- $(editors[0]).prop("CodeMirror").getValue(),
- $(editors[1]).prop("CodeMirror").getValue(),
- $(editors[2]).prop("CodeMirror").getValue(),
- $(editors[3]).prop("CodeMirror").getValue()
- ];
- })
- .then((srcs) => {
- console.log("Loading Content for the first time");
- var data = _.zip.apply(null, [names, srcs]);
- return Promise.all(data.map((data) => {
- return fs.writeFile("./" + data[0], data[1])
- }));
- })
- .then(() => {
- console.log("watching");
- watcher = fs.watch(process.cwd(), (action, filename) => {
- if (!_.contains(names, filename)) return;
- console.log("action = " + action + " filename = " + filename);
- if(action != 'change'){
- console.log("Ignoring action");
- return;
- }
- Promise.all(names.map((n) => readFilePromise(path.join(process.cwd(),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()));
- });
- })
- .catch((e) => {
- console.log(e);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement