Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d543117

Browse files
author
KAWACHI Takashi
committed
Initial commit
0 parents  commit d543117

11 files changed

+198
-0
lines changed

.gitignore

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp
18+
.rvmrc
19+
.idea

Gemfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
source 'https://rubygems.org'
2+
3+
group :development do
4+
gem "rake"
5+
gem "rspec"
6+
end
7+
8+
# Specify your gem's dependencies in typescript-node-ruby.gemspec
9+
gemspec

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2012 KAWACHI Takashi
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Typescript::Node::Ruby
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'typescript-node-ruby'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install typescript-node-ruby
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Added some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env rake
2+
require "bundler/gem_tasks"

lib/typescript-node.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require "tmpdir"
2+
require "tempfile"
3+
require "typescript-src"
4+
require "typescript-node/version"
5+
require "typescript-node/compile_result"
6+
7+
module TypeScript
8+
module Node
9+
10+
class << self
11+
def compile_file(source_file)
12+
Dir.mktmpdir do |output_dir|
13+
output_file = File.join(output_dir, "out.js")
14+
cmd = ["node", Src.tsc_path, "--out", output_file, source_file]
15+
Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
16+
exit_status = wait_thr.value
17+
output_js = File.exists?(output_file) ? File.read(output_file) : nil
18+
CompileResult.new(
19+
output_js,
20+
exit_status,
21+
stdout.read,
22+
stderr.read
23+
)
24+
end
25+
end
26+
end
27+
28+
def compile(source)
29+
js_file = Tempfile.new(["typescript-node", ".ts"])
30+
begin
31+
js_file.write(source)
32+
js_file.close
33+
result = compile_file(js_file.path)
34+
if result.success?
35+
result.js
36+
else
37+
raise result.stderr
38+
end
39+
ensure
40+
js_file.unlink
41+
end
42+
end
43+
end
44+
end
45+
end

lib/typescript-node/compile_result.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# coding: utf-8
2+
3+
module TypeScript
4+
module Node
5+
class CompileResult
6+
7+
# Constructor
8+
#
9+
# @param [String] js compiled JavaScript
10+
# @param [Fixnum] exit_status return code of tsc command
11+
# @param [String] stdout Standard out
12+
# @return [String] stderr Standard err
13+
def initialize(js, exit_status, stdout, stderr)
14+
@js = js
15+
@exit_status = exit_status
16+
@stdout = stdout
17+
@stderr = stderr
18+
end
19+
20+
attr_reader :js, :source_map, :exit_status, :stdout, :stderr
21+
22+
def success?
23+
@exit_status == 0
24+
end
25+
26+
end
27+
end
28+
end
29+

lib/typescript-node/version.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module TypeScript
2+
module Node
3+
VERSION = "0.0.1"
4+
end
5+
end

spec/data/hello.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello TypeScript")

spec/typescript-node_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "typescript-node"
2+
3+
module TypeScript
4+
module Node
5+
describe "#compile_file" do
6+
subject do
7+
hello_ts = File.expand_path("data/hello.ts", File.dirname(__FILE__))
8+
Node.compile_file(hello_ts)
9+
end
10+
11+
its(:exit_status) { should == 0 }
12+
it { should be_success }
13+
its(:js) { should == "console.log(\"Hello TypeScript\");\r\n" }
14+
its(:stdout) { should == "" }
15+
its(:stderr) { should == "" }
16+
end
17+
end
18+
end

typescript-node.gemspec

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- encoding: utf-8 -*-
2+
require File.expand_path('../lib/typescript-node/version', __FILE__)
3+
4+
Gem::Specification.new do |gem|
5+
gem.authors = ["KAWACHI Takashi"]
6+
gem.email = ["[email protected]"]
7+
gem.description = %q{TypeScript ruby interface using Node.js}
8+
gem.summary = %q{TypeScript ruby interface using Node.js}
9+
gem.homepage = ""
10+
11+
gem.files = `git ls-files`.split($\)
12+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14+
gem.name = "typescript-node"
15+
gem.require_paths = ["lib"]
16+
gem.version = TypeScript::Node::VERSION
17+
18+
gem.add_dependency 'typescript-src', '0.8.1.1'
19+
end

0 commit comments

Comments
 (0)