Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable syntax checking on save and show error on UI #412

Merged
merged 3 commits into from
Jan 16, 2015
Merged
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
139 changes: 89 additions & 50 deletions lib/modules/io.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,131 @@
var fs = require('fs'),
path = require('path'),
Q = require('q'),
writer = require('./variable-writer');
_ = require('lodash'),
parser = require('./variable-parser'),
writer = require('./variable-writer'),
events = {
connection: 'connection',
progress: {
start: 'styleguide progress start',
end: 'styleguide progress end'
},
styles: {
changed: 'styleguide styles changed'
},
variables: {
saved: 'variables saved to server',
toServer: 'variables to server'
},
compile: {
success: 'styleguide compile success',
error: 'styleguide compile error'
}
};

module.exports = function(ioServer, options) {

var io = ioServer,
compileError = false,
fileHashes = options.fileHashes;

function groupVariablesByFileHash(variables) {
return variables.reduce(function(result, v) {
var hash = v.fileHash;
if (!result[hash]) {
result[hash] = [];
function saveVariables(variables) {
return Q.promise(function(resolve, reject) {
try {
_.chain(variables)
.groupBy('fileHash')
.map(asObjectWithFileProperties)
.forEach(readFileContents)
.forEach(updateVariableValues)
.forEach(checkSyntax)
.forEach(writeFileContents);
resolve();
} catch (e) {
reject(e);
}
result[hash].push(v);
return result;
}, {});
});
}

function saveVariables(variables) {
// First group variables by file hash
var groupedVariables = groupVariablesByFileHash(variables),
filePromises = [];

// Go trough every file and update variables defined in that file
Object.keys(groupedVariables).forEach(function(fileHash) {
filePromises.push(Q.promise(function(resolveFile) {
var filePath = fileHashes[fileHash],
fileVariables = groupedVariables[fileHash],
syntax = path.extname(filePath).substring(1);

// Read original file contents to be updated
fs.readFile(filePath, {encoding: 'utf8'}, function(err, originalData) {
// Update variables and store results back to the original file
var data = writer.setVariables(originalData, syntax, fileVariables);
fs.writeFile(filePath, data, function(err) {
if (err) {
console.error(err);
}
resolveFile();
});
});
}));
});
function asObjectWithFileProperties(variables, hash) {
var filePath = fileHashes[hash];
return {
path: filePath,
syntax: path.extname(filePath).substring(1),
variables: variables
};
}

function readFileContents(file) {
file.contents = fs.readFileSync(file.path, { encoding: 'utf8' });
}

function updateVariableValues(file) {
file.contents = writer.setVariables(file.contents, file.syntax, file.variables);
}

return Q.all(filePromises);
function checkSyntax(file) {
parser.parseVariableDeclarations(file.contents, file.syntax);
}

function writeFileContents(file) {
fs.writeFileSync(file.path, file.contents, { encoding: 'utf8' });
}

function emitProgressStart() {
io.sockets.emit('styleguide progress start');
io.sockets.emit(events.progress.start);
}

function emitStylesChanged() {
io.sockets.emit(events.styles.changed);
}

function emitProgressEnd() {
io.sockets.emit('styleguide progress end');
io.sockets.emit(events.progress.end);
}

function emitCompileError(err) {
compileError = true;
io.sockets.emit('styleguide compile error', err);
emitProgressEnd();
function emitCompileError(err, socket) {
compileError = err;
if (socket) {
socket.emit(events.compile.error, err);
} else {
io.sockets.emit(events.compile.error, err);
}
}

function emitCompileSuccess() {
function emitCompileSuccess(socket) {
compileError = false;
io.sockets.emit('styleguide compile success');
emitProgressEnd();
if (socket) {
socket.emit(events.compile.success);
} else {
io.sockets.emit(events.compile.success);
}
}

io.on('connection', function(socket) {
io.on(events.connection, function(socket) {
console.log('Socket connection established (id:', socket.conn.id + ')');
socket.on('variables to server', function(variables) {
saveVariables(variables).then(function() {
socket.emit('variables saved to server');

socket.on(events.variables.toServer, function(variables) {
saveVariables(variables).done(function() {
console.log('EVENT: variables saved to server');
socket.emit(events.variables.saved);
}, function(err) {
console.error('Unable to save variables to server:', err);
emitCompileError(err, socket);
});
});

if (compileError) {
emitCompileError();
emitCompileError(compileError, socket);
} else {
emitCompileSuccess();
emitCompileSuccess(socket);
}
});

return {
saveVariables: saveVariables,
emitProgressStart: emitProgressStart,
emitProgressEnd: emitProgressEnd,
emitStylesChanged: emitStylesChanged,
emitCompileError: emitCompileError,
emitCompileSuccess: emitCompileSuccess
};
Expand Down
Loading