mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-17 13:20:57 +00:00
Convert to ES5, add web server
* No compilation step * Beginnings of web interface * Simple express server; React-based frontend * CommonJS style across codebase; auto-converts to RequireJS for browser * Using diffsync for realtime UI * "Provider" -> "Plugin" * Plugins expose one or more Providers
This commit is contained in:
30
lib/util/autoamd.js
Normal file
30
lib/util/autoamd.js
Normal file
@@ -0,0 +1,30 @@
|
||||
var interceptor = require('express-interceptor');
|
||||
|
||||
/**
|
||||
* Express middleware that converts CommonJS-style code to RequireJS style for the browser (assuming require.js is loaded).
|
||||
*/
|
||||
|
||||
module.exports = function(urlPrefix) {
|
||||
return interceptor(function(req, res){
|
||||
return {
|
||||
// Only URLs with the given prefix will be converted to require.js style
|
||||
isInterceptable: function(){
|
||||
return req.originalUrl.indexOf(urlPrefix) == 0;
|
||||
},
|
||||
intercept: function(body, send) {
|
||||
send(toRequireJS(body));
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// From https://github.com/shovon/connect-commonjs-amd/blob/master/src/middleware.coffee
|
||||
function toRequireJS(str) {
|
||||
var requireCalls = str.match(/require\((\s+)?('[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*")(\s+)?\)/g) || [];
|
||||
requireCalls = requireCalls.map(function(str) {
|
||||
return (str.match(/('[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*")/))[0];
|
||||
});
|
||||
requireCalls.unshift("'require'");
|
||||
str = "define([" + (requireCalls.join(', ')) + "], function (require) {\nvar module = { exports: {} }\n , exports = module.exports;\n\n(function () {\n\n" + str + "\n\n})();\n\nreturn module.exports;\n});";
|
||||
return str;
|
||||
};
|
||||
13
lib/util/camelcase.js
Normal file
13
lib/util/camelcase.js
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
module.exports = {
|
||||
camelCaseToRegularForm: camelCaseToRegularForm
|
||||
}
|
||||
|
||||
// Converts "accessToken" to "Access Token"
|
||||
function camelCaseToRegularForm(camelCase) {
|
||||
return camelCase
|
||||
// insert a space before all caps
|
||||
.replace(/([A-Z])/g, ' $1')
|
||||
// uppercase the first character
|
||||
.replace(/^./, function(str){ return str.toUpperCase(); })
|
||||
}
|
||||
Reference in New Issue
Block a user