Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env node
const bodyParser = require('body-parser');
const Confirm = require('prompt-confirm');
const { Confirm } = require('enquirer');
const cors = require('cors');
const express = require('express');
const fs = require('fs');
const glob = require('glob');

let files;

let shuttingDown = false;
let writing = false;

const app = express()
app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
Expand All @@ -20,23 +23,38 @@ app.get('/', (req, res) => {
app.post('/save', (req, res) => {
const changes = req.body;

// Prompt to confirm.
console.log([
`\nA-Frame Inspector from ${req.hostname} has requested the following changes:\n`,
`${prettyPrintChanges(changes)}`,
'Do you allow the A-Frame Inspector Watcher to write these updates directly ' +
'within this directory?'
].join('\n'));

const prompt = new Confirm('Y/n');
prompt.run().then(answer => {
// Denied.
if (!answer) { res.sendStatus(403); }

// Accepted.
sync(changes);
res.sendStatus(200);
});
if (!shuttingDown)
{
// Prompt to confirm.
const msg = [
`\nA-Frame Inspector from ${req.hostname} has requested the following changes:\n`,
`${prettyPrintChanges(changes)}`,
'Do you allow the A-Frame Inspector Watcher to write these updates directly ' +
'within this directory?'
].join('\n');

const prompt = new Confirm({
type: 'confirm',
message: msg
});
prompt.run().then(answer => {
// Denied.
if (!answer) { res.sendStatus(403); }

// Accepted.
writing = true;
sync(changes);
res.sendStatus(200);
writing = false;
});
}
else
{
//Shutting down, no writing allowed
res.sendStatus(403);
}


});

function prettyPrintChanges (changes) {
Expand Down Expand Up @@ -202,3 +220,20 @@ if (process.env.NODE_ENV !== 'test') {
'Try passing a directory or wildcard pointing to HTML files (e.g., **/*.html).');
}
}

process.on('SIGINT', function() {
console.log("\nGracefully shutting down from SIGINT (Ctrl+C)");

//Prevents
shuttingDown = true;

//Add whatever needs to be taken care of before shutdown

if (writing)
{
console.log("Warning, shutdown occured during writing. Affected files could have been mangled!")
}

console.log("Exiting...");
process.exit();
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"body-parser": "^1.18.3",
"chai": "^4.2.0",
"cors": "^2.8.5",
"enquirer": "^2.3.6",
"express": "^4.16.4",
"glob": "^7.1.3",
"lodash": "^4.17.11",
"lodash.uniq": "^4.5.0",
"mocha": "^5.2.0",
"nodemon": "^1.18.7",
"prompt": "^1.0.0",
"prompt-confirm": "^2.0.4"
"prompt": "^1.0.0"
},
"bin": {
"aframe-watcher": "./index.js"
Expand Down