browserify_rb 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c10b3bb45a0008b233976bef7dbd6ff5af6460c1
4
- data.tar.gz: ee2cee8d353d0dcc479b2dd4f2f4fece5c3ac22c
3
+ metadata.gz: d719f31c38f3e7c258bac2a30640ef9d934b7101
4
+ data.tar.gz: eabc846c29c618278188574cbe7976382032e9dc
5
5
  SHA512:
6
- metadata.gz: 7914c1a319687911748dbdbef9bfc831b0063207a9d26b8be25d5431f8000b6927ad091f04c669211e2fb297bc0a897a3a2b331ac990d609787eba90556a6334
7
- data.tar.gz: 319542416adf75d1278c4ebbc180191a629019ef66034c117bf7b5db93297ef954142316ea87ac8ed2ed60a2558a01ab4d54ca2efa530173de499ce5c5774d5c
6
+ metadata.gz: 471482ceb0aa270e19f6cdb09e6ca3ba95ef16ea7885dbe185a380d7f0a6bdaa7bf6bf6e643cc451eaea60f84487612dc019a602bbee0c34fc3ff5859f0bd462
7
+ data.tar.gz: bba717e1bbb0f8b16dbc0bc306feb52d0794a2f3c66ead75d64eaaa8655a449e61132babcfc8778d3e42f51d4ea4e354671882475d890663d264f89cf1244115
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /.nvm/
11
+ /node_modules/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # BrowserifyRb
2
2
 
3
- browserify wrapper with nvm to install node automatically.
3
+ browserify wrapper for ruby.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,18 +18,45 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install browserify_rb
20
20
 
21
+
21
22
  ## Usage
22
23
 
24
+ BrewserifyRb has 2 implementations: `BrowserifyRb::Browserify`, `BrowserifyRb::Nvm::Browserify`
25
+
26
+
27
+ ### BrowserifyRb::Browserify
28
+
29
+ `browserify` required:
30
+
23
31
  ```ruby
24
32
  require "browserify_rb"
25
33
 
26
- br = BrowserifyRb.new
34
+ # if you install browserify in global
35
+ br = BrowserifyRb::Browserify.new
27
36
  puts br.compile("console.log('foo');")
37
+ # or
38
+ puts BrowserifyRb::Browserify.compile("console.log('bar');")
28
39
 
29
- puts BrowserifyRb.compile("console.log('bar');")
40
+ # if you install browserify in ./node_modules
41
+ br = BrowserifyRb::Browserify.new(command: "$(npm bin)/browserify -")
42
+ puts br.compile("console.log('foo');")
43
+ # or
44
+ puts BrowserifyRb::Browserify.compile("console.log('bar');", command: "$(npm bin)/browserify -")
30
45
  ```
31
46
 
32
- `node` nor `nvm` are not required.
47
+
48
+ ### BrowserifyRb::Nvm::Browserify
49
+
50
+ `browserify`, `node` nor `nvm` are not required. These are installed automatically:
51
+
52
+ ```ruby
53
+ require "browserify_rb"
54
+
55
+ br = BrowserifyRb::Nvm::Browserify.new
56
+ puts br.compile("console.log('foo');")
57
+ # or
58
+ puts BrowserifyRb::Nvm::Browserify.compile("console.log('bar');")
59
+ ```
33
60
 
34
61
  ## Development
35
62
 
@@ -9,8 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Keiichiro Ui"]
10
10
  spec.email = ["keiichiro.ui@gmail.com"]
11
11
 
12
- spec.summary = %q{browserify wrapper}
13
- spec.description = %q{browserify wrapper with nvm to install node automatically}
12
+ spec.summary = %q{browserify wrapper for ruby}
14
13
  spec.homepage = "https://github.com/kui/browserify_rb"
15
14
  spec.license = "MIT"
16
15
 
@@ -1,80 +1,6 @@
1
1
  require "browserify_rb/version"
2
- require "browserify_rb/nvm"
2
+ require "browserify_rb/nvm/browserify"
3
+ require "browserify_rb/browserify"
3
4
 
4
- require "stringio"
5
- require "logger"
6
-
7
- class BrowserifyRb
8
- DEFAULT_NVM_DIR = "#{ENV["HOME"]}/.nvm"
9
- LOG = Logger.new(STDERR)
10
- LOG.level = Logger::Severity.const_get(ENV["LOG"] || "INFO")
11
- LOG.progname = BrowserifyRb
12
-
13
- def initialize(
14
- browserify_opts: "", node_ver: nil, env: {},
15
- nvm_dir: DEFAULT_NVM_DIR, suppress_stderr: false,
16
- required_modules: nil)
17
- @nvm = BrowserifyRb::Nvm.new nvm_dir
18
- @node_ver = node_ver
19
- @env = env
20
- @browserify_opts = browserify_opts
21
- @suppress_stderr = suppress_stderr
22
- @prepared = false
23
- @modules = required_modules
24
- end
25
-
26
- def prepare
27
- ms = ["browserify", *@modules].map{|m| %!"#{m}"! }.join(" ")
28
- cmd = "npm install #{ms}"
29
- stdout_handler = proc {|d| }
30
- stderr_handler = @suppress_stderr ?
31
- proc {|d| } :
32
- proc {|d| STDERR.print d}
33
- LOG.debug "run: #{cmd}"
34
- status = @nvm.run(
35
- cmd,
36
- node_ver: @node_ver,
37
- env: @env,
38
- stdout_handler: stdout_handler,
39
- stderr_handler: stderr_handler
40
- ).value
41
- raise "non-zero exit status: #{status.to_i}" unless status.success?
42
- @prepared = true
43
- end
44
-
45
- def compile source
46
- prepare unless @prepared
47
-
48
- out_buf = StringIO.new
49
- cmd = <<-CMD
50
- $(npm bin)/browserify #{@browserify_opts} -- -
51
- CMD
52
- stdout_handler = proc {|d| out_buf << d }
53
- stderr_handler = @suppress_stderr ?
54
- proc {|d| } :
55
- proc {|d| STDERR.print d}
56
- status = @nvm.run(
57
- cmd,
58
- stdin: source,
59
- node_ver: @node_ver,
60
- env: @env,
61
- stdout_handler: stdout_handler,
62
- stderr_handler: stderr_handler
63
- ).value
64
- raise "non-zero exit status: #{status.to_i}" unless status.success?
65
-
66
- return out_buf.string
67
- end
68
-
69
- def self.compile(
70
- source, browserify_opts: "", node_ver: nil, env: {},
71
- nvm_dir: DEFAULT_NVM_DIR, suppress_stderr: false)
72
- new(
73
- browserify_opts: browserify_opts,
74
- node_ver: node_ver,
75
- env: env,
76
- nvm_dir: nvm_dir,
77
- suppress_stderr: suppress_stderr
78
- ).compile(source)
79
- end
5
+ module BrowserifyRb
80
6
  end
@@ -0,0 +1,36 @@
1
+ require "browserify_rb/popen3"
2
+
3
+ require "stringio"
4
+ require "logger"
5
+
6
+ class BrowserifyRb::Browserify
7
+ LOG = Logger.new(STDERR)
8
+ LOG.level = Logger::Severity.const_get(ENV["LOG"] || "INFO")
9
+ LOG.progname = BrowserifyRb::Browserify
10
+
11
+ def initialize command: "browserify -", suppress_stderr: false
12
+ @command = command
13
+ @suppress_stderr = suppress_stderr
14
+ end
15
+
16
+ def compile source
17
+ out_buf = StringIO.new
18
+ stdout_handler = proc {|d| out_buf << d }
19
+ stderr_handler = @suppress_stderr ? proc { } : proc {|d| STDERR.print d }
20
+
21
+ LOG.debug "run: #{@command}"
22
+ status = BrowserifyRb::Popen3.async_exec(
23
+ @command,
24
+ input: source,
25
+ stdout_handler: stdout_handler,
26
+ stderr_handler: stderr_handler,
27
+ ).value
28
+ raise "non-zero exit status: #{status.to_i}" unless status.success?
29
+
30
+ out_buf.string
31
+ end
32
+
33
+ def self.compile source, command: "browserify -", suppress_stderr: false
34
+ new(command: command, suppress_stderr: suppress_stderr).compile(source)
35
+ end
36
+ end
@@ -3,32 +3,31 @@ require "browserify_rb/popen3"
3
3
  require "stringio"
4
4
  require "logger"
5
5
 
6
- class BrowserifyRb
7
- class Nvm
8
- LOG = Logger.new(STDERR)
9
- LOG.level = Logger::Severity.const_get(ENV["LOG"] || "INFO")
10
- LOG.progname = Nvm
6
+ class BrowserifyRb::Nvm
7
+ LOG = Logger.new(STDERR)
8
+ LOG.level = Logger::Severity.const_get(ENV["LOG"] || "INFO")
9
+ LOG.progname = BrowserifyRb::Nvm
11
10
 
12
- NVM_SH = File.join(__dir__, "nvm.sh")
11
+ NVM_SH = File.join(__dir__, "nvm.sh")
13
12
 
14
- def initialize nvm_dir = "#{ENV["HOME"]}/.nvm"
15
- @nvm_dir = nvm_dir
16
- end
13
+ def initialize nvm_dir = "#{ENV["HOME"]}/.nvm"
14
+ @nvm_dir = nvm_dir
15
+ end
17
16
 
18
- def env
19
- { "NVM_DIR" => @nvm_dir }
20
- end
17
+ def env
18
+ { "NVM_DIR" => @nvm_dir }
19
+ end
21
20
 
22
- def run(
23
- cmd,
24
- stdin: "",
25
- stdout_handler: proc{|d| STDOUT.print d },
26
- stderr_handler: proc{|d| STDERR.print d },
27
- node_ver: nil,
28
- env: {})
29
- new_env = (env).merge(self.env)
30
- node_ver = node_ver ? %["#{node_ver}"] : ""
31
- cmd = <<-CMD
21
+ def run(
22
+ cmd,
23
+ stdin: "",
24
+ stdout_handler: proc{|d| STDOUT.print d },
25
+ stderr_handler: proc{|d| STDERR.print d },
26
+ node_ver: nil,
27
+ env: {})
28
+ new_env = (env).merge(self.env)
29
+ node_ver = node_ver ? %["#{node_ver}"] : ""
30
+ cmd = <<-CMD
32
31
  . "#{NVM_SH}"
33
32
  if ! nvm use #{node_ver} >&2; then
34
33
  nvm install #{node_ver} >&2
@@ -37,32 +36,31 @@ class BrowserifyRb
37
36
  #{cmd}
38
37
  CMD
39
38
 
40
- LOG.debug "run: #{cmd}"
41
- BrowserifyRb::Popen3.async_exec(
42
- cmd,
43
- input: stdin, env: new_env,
44
- stdout_handler: stdout_handler,
45
- stderr_handler: stderr_handler
46
- )
47
- end
39
+ LOG.debug "run: #{cmd}"
40
+ BrowserifyRb::Popen3.async_exec(
41
+ cmd,
42
+ input: stdin, env: new_env,
43
+ stdout_handler: stdout_handler,
44
+ stderr_handler: stderr_handler
45
+ )
46
+ end
48
47
 
49
- def self.version
50
- out = StringIO.new
51
- err = StringIO.new
52
- cmd = <<-CMD
48
+ def self.version
49
+ out = StringIO.new
50
+ err = StringIO.new
51
+ cmd = <<-CMD
53
52
  . "#{NVM_SH}"
54
53
  nvm --version
55
54
  CMD
56
55
 
57
- LOG.debug "run: #{cmd}"
58
- st = BrowserifyRb::Popen3.async_exec(
59
- cmd,
60
- stdout_handler: proc {|d| out << d },
61
- stderr_handler: proc {|d| err << d }
62
- ).value
63
- raise "non-zero exit status: #{status.to_i}\n#{err}" unless st.success?
56
+ LOG.debug "run: #{cmd}"
57
+ st = BrowserifyRb::Popen3.async_exec(
58
+ cmd,
59
+ stdout_handler: proc {|d| out << d },
60
+ stderr_handler: proc {|d| err << d }
61
+ ).value
62
+ raise "non-zero exit status: #{status.to_i}\n#{err}" unless st.success?
64
63
 
65
- out.string.strip
66
- end
64
+ out.string.strip
67
65
  end
68
66
  end
@@ -0,0 +1,79 @@
1
+ require "browserify_rb/nvm"
2
+
3
+ require "stringio"
4
+ require "logger"
5
+
6
+ class BrowserifyRb::Nvm::Browserify
7
+ DEFAULT_NVM_DIR = "#{ENV["HOME"]}/.nvm"
8
+ LOG = Logger.new(STDERR)
9
+ LOG.level = Logger::Severity.const_get(ENV["LOG"] || "INFO")
10
+ LOG.progname = BrowserifyRb::Nvm::Browserify
11
+
12
+ def initialize(
13
+ browserify_opts: "", node_ver: nil, env: {},
14
+ nvm_dir: DEFAULT_NVM_DIR, suppress_stderr: false,
15
+ required_modules: nil)
16
+ @nvm = BrowserifyRb::Nvm.new nvm_dir
17
+ @node_ver = node_ver
18
+ @env = env
19
+ @browserify_opts = browserify_opts
20
+ @suppress_stderr = suppress_stderr
21
+ @prepared = false
22
+ @modules = required_modules
23
+ end
24
+
25
+ def prepare
26
+ ms = ["browserify", *@modules].map{|m| %!"#{m}"! }.join(" ")
27
+ cmd = "npm install #{ms}"
28
+ stdout_handler = proc { }
29
+ stderr_handler = @suppress_stderr ?
30
+ proc { } :
31
+ proc {|d| STDERR.print d}
32
+ LOG.debug "run: #{cmd}"
33
+ status = @nvm.run(
34
+ cmd,
35
+ node_ver: @node_ver,
36
+ env: @env,
37
+ stdout_handler: stdout_handler,
38
+ stderr_handler: stderr_handler
39
+ ).value
40
+ raise "non-zero exit status: #{status.to_i}" unless status.success?
41
+ @prepared = true
42
+ end
43
+
44
+ def compile source
45
+ prepare unless @prepared
46
+
47
+ out_buf = StringIO.new
48
+ cmd = <<-CMD
49
+ $(npm bin)/browserify #{@browserify_opts} -- -
50
+ CMD
51
+ stdout_handler = proc {|d| out_buf << d }
52
+ stderr_handler = @suppress_stderr ?
53
+ proc {|d| } :
54
+ proc {|d| STDERR.print d}
55
+ status = @nvm.run(
56
+ cmd,
57
+ stdin: source,
58
+ node_ver: @node_ver,
59
+ env: @env,
60
+ stdout_handler: stdout_handler,
61
+ stderr_handler: stderr_handler
62
+ ).value
63
+ raise "non-zero exit status: #{status.to_i}" unless status.success?
64
+
65
+ return out_buf.string
66
+ end
67
+
68
+ def self.compile(
69
+ source, browserify_opts: "", node_ver: nil, env: {},
70
+ nvm_dir: DEFAULT_NVM_DIR, suppress_stderr: false)
71
+ new(
72
+ browserify_opts: browserify_opts,
73
+ node_ver: node_ver,
74
+ env: env,
75
+ nvm_dir: nvm_dir,
76
+ suppress_stderr: suppress_stderr
77
+ ).compile(source)
78
+ end
79
+ end
@@ -2,78 +2,76 @@ require "open3"
2
2
  require "stringio"
3
3
  require "logger"
4
4
 
5
- class BrowserifyRb
6
- module Popen3
7
- LOG = Logger.new(STDERR)
8
- LOG.level = Logger::Severity.const_get(ENV["LOG"] || "INFO")
9
- LOG.progname = Popen3
5
+ module BrowserifyRb::Popen3
6
+ LOG = Logger.new(STDERR)
7
+ LOG.level = Logger::Severity.const_get(ENV["LOG"] || "INFO")
8
+ LOG.progname = BrowserifyRb::Popen3
10
9
 
11
- CHUNK_SIZE = 2000
12
- DEFAULT_STDOUT_HANDLER = proc {|data| STDOUT.write data }
13
- DEFAULT_STDERR_HANDLER = proc {|data| STDERR.write data }
10
+ CHUNK_SIZE = 2000
11
+ DEFAULT_STDOUT_HANDLER = proc {|data| STDOUT.write data }
12
+ DEFAULT_STDERR_HANDLER = proc {|data| STDERR.write data }
14
13
 
15
- def self.async_exec(
16
- cmd,
17
- input: "",
18
- env: {},
19
- stdout_handler: DEFAULT_STDOUT_HANDLER,
20
- stderr_handler: DEFAULT_STDERR_HANDLER,
21
- spawn_opts: {},
22
- chunk_size: CHUNK_SIZE)
23
- Thread.fork do
24
- begin
25
- stdin, stdout, stderr, wait_thr = Open3.popen3 env, cmd, spawn_opts
14
+ def self.async_exec(
15
+ cmd,
16
+ input: "",
17
+ env: {},
18
+ stdout_handler: DEFAULT_STDOUT_HANDLER,
19
+ stderr_handler: DEFAULT_STDERR_HANDLER,
20
+ spawn_opts: {},
21
+ chunk_size: CHUNK_SIZE)
22
+ Thread.fork do
23
+ begin
24
+ stdin, stdout, stderr, wait_thr = Open3.popen3 env, cmd, spawn_opts
26
25
 
27
- input = input.is_a?(String) ?
28
- StringIO.new(input) :
29
- iput
30
- input_buff = ""
31
- opened_ins = [stdin]
32
- opened_outs = [stdout, stderr]
33
- handlers = {
34
- stdout => stdout_handler,
35
- stderr => stderr_handler
36
- }
37
- while not opened_outs.empty?
38
- ios = IO.select opened_outs, opened_ins, nil, 1
39
- if ios.nil? and Process.waitpid(wait_thr.pid, Process::WNOHANG)
40
- break
41
- end
42
-
43
- outs, ins, = ios
26
+ input = input.is_a?(String) ?
27
+ StringIO.new(input) :
28
+ iput
29
+ input_buff = ""
30
+ opened_ins = [stdin]
31
+ opened_outs = [stdout, stderr]
32
+ handlers = {
33
+ stdout => stdout_handler,
34
+ stderr => stderr_handler
35
+ }
36
+ while not opened_outs.empty?
37
+ ios = IO.select opened_outs, opened_ins, nil, 1
38
+ if ios.nil? and Process.waitpid(wait_thr.pid, Process::WNOHANG)
39
+ break
40
+ end
44
41
 
45
- if not outs.nil?
46
- outs.each do |out|
47
- if out.eof?
48
- out.close
49
- opened_outs.delete out
50
- else
51
- d = out.readpartial chunk_size
52
- handlers[out].call d
53
- end
54
- end
55
- end
42
+ outs, ins, = ios
56
43
 
57
- if not ins.nil? and ins.include? stdin
58
- if input.eof?
59
- stdin.close
60
- opened_ins.delete stdin
61
- elsif input_buff.empty?
62
- d = input.readpartial(chunk_size)
63
- bytes = stdin.write_nonblock(d)
64
- input_buff = d[bytes .. -1]
44
+ if not outs.nil?
45
+ outs.each do |out|
46
+ if out.eof?
47
+ out.close
48
+ opened_outs.delete out
65
49
  else
66
- bytes = stdin.write_nonblock(input_buff)
67
- input_buff = input_buff[bytes .. -1]
50
+ d = out.readpartial chunk_size
51
+ handlers[out].call d
68
52
  end
69
53
  end
70
54
  end
71
55
 
72
- wait_thr.value
73
- rescue => e
74
- LOG.error e
75
- nil
56
+ if not ins.nil? and ins.include? stdin
57
+ if input.eof?
58
+ stdin.close
59
+ opened_ins.delete stdin
60
+ elsif input_buff.empty?
61
+ d = input.readpartial(chunk_size)
62
+ bytes = stdin.write_nonblock(d)
63
+ input_buff = d[bytes .. -1]
64
+ else
65
+ bytes = stdin.write_nonblock(input_buff)
66
+ input_buff = input_buff[bytes .. -1]
67
+ end
68
+ end
76
69
  end
70
+
71
+ wait_thr.value
72
+ rescue => e
73
+ LOG.error e
74
+ nil
77
75
  end
78
76
  end
79
77
  end
@@ -1,3 +1,3 @@
1
- class BrowserifyRb
2
- VERSION = "0.3.0"
1
+ module BrowserifyRb
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "browserify_rb",
3
+ "private": true,
4
+ "devDependencies": {
5
+ "babel-preset-es2015": "^6.3.13",
6
+ "babelify": "^7.2.0",
7
+ "browserify": "^12.0.1"
8
+ }
9
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browserify_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keiichiro Ui
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
- description: browserify wrapper with nvm to install node automatically
55
+ description:
56
56
  email:
57
57
  - keiichiro.ui@gmail.com
58
58
  executables: []
@@ -69,10 +69,13 @@ files:
69
69
  - bin/setup
70
70
  - browserify_rb.gemspec
71
71
  - lib/browserify_rb.rb
72
+ - lib/browserify_rb/browserify.rb
72
73
  - lib/browserify_rb/nvm.rb
73
74
  - lib/browserify_rb/nvm.sh
75
+ - lib/browserify_rb/nvm/browserify.rb
74
76
  - lib/browserify_rb/popen3.rb
75
77
  - lib/browserify_rb/version.rb
78
+ - package.json
76
79
  homepage: https://github.com/kui/browserify_rb
77
80
  licenses:
78
81
  - MIT
@@ -97,5 +100,5 @@ rubyforge_project:
97
100
  rubygems_version: 2.4.5.1
98
101
  signing_key:
99
102
  specification_version: 4
100
- summary: browserify wrapper
103
+ summary: browserify wrapper for ruby
101
104
  test_files: []