-
-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added node as an option to the generator
- Loading branch information
Showing
8 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "rails/generators" | ||
|
||
module ReactOnRails | ||
module Generators | ||
class NodeGenerator < Rails::Generators::Base | ||
Rails::Generators.hide_namespace(namespace) | ||
source_root(File.expand_path("../templates", __FILE__)) | ||
|
||
def create_node_directory | ||
empty_directory("client/node") | ||
end | ||
|
||
def copy_base_redux_files | ||
base_path = "node/base/" | ||
%w(client/node/server.js | ||
client/node/package.json).each do |file| | ||
copy_file(base_path + file, file) | ||
end | ||
end | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
lib/generators/react_on_rails/templates/node/base/client/node/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "react_on_rails_node", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node ./server.js -s webpack-bundle.js" | ||
}, | ||
"dependencies": { | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
lib/generators/react_on_rails/templates/node/base/client/node/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
var net = require('net'); | ||
var fs = require('fs'); | ||
|
||
var bundlePath = '../../app/assets/webpack/'; | ||
var bundleFileName = 'webpack-bundle.js'; | ||
|
||
var currentArg; | ||
|
||
function Handler() { | ||
this.queue = []; | ||
this.initialized = false; | ||
} | ||
|
||
Handler.prototype.handle = function (connection) { | ||
var callback = function () { | ||
connection.setEncoding('utf8'); | ||
connection.on('data', (data)=> { | ||
console.log('Processing request: ' + data); | ||
var result = eval(data); | ||
connection.write(result); | ||
}); | ||
}; | ||
|
||
if (this.initialized) { | ||
callback(); | ||
} else { | ||
this.queue.push(callback); | ||
} | ||
}; | ||
|
||
Handler.prototype.initialize = function () { | ||
console.log('Processing ' + this.queue.length + ' pending requests'); | ||
var callback; | ||
while (callback = this.queue.pop()) { | ||
callback(); | ||
} | ||
|
||
this.initialized = true; | ||
}; | ||
|
||
var handler = new Handler(); | ||
|
||
process.argv.forEach((val) => { | ||
if (val[0] == '-') { | ||
currentArg = val.slice(1); | ||
return; | ||
} | ||
|
||
if (currentArg == 's') { | ||
bundleFileName = val; | ||
} | ||
}); | ||
|
||
try { | ||
fs.mkdirSync(bundlePath); | ||
} catch (e) { | ||
if (e.code != 'EEXIST') throw e; | ||
} | ||
|
||
fs.watchFile(bundlePath + bundleFileName, (curr) => { | ||
if (curr && curr.blocks && curr.blocks > 0) { | ||
if (handler.initialized) { | ||
console.log('Reloading server bundle must be implemented by restarting the node process!'); | ||
return; | ||
} | ||
|
||
require(bundlePath + bundleFileName); | ||
console.log('Loaded server bundle: ' + bundlePath + bundleFileName); | ||
handler.initialize(); | ||
} | ||
}); | ||
|
||
var unixServer = net.createServer(function (connection) { | ||
handler.handle(connection); | ||
}); | ||
|
||
unixServer.listen('node.sock'); | ||
|
||
process.on('SIGINT', () => { | ||
unixServer.close(); | ||
process.exit(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
spec/react_on_rails/support/shared_examples/node_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
shared_examples "node_generator" do | ||
it "copies base redux files" do | ||
%w(client/node/server.js | ||
client/node/package.json).each { |file| assert_file(file) } | ||
end | ||
end |