typescript-monkey 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.editorconfig +27 -0
- data/.gitignore +52 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +27 -0
- data/.vscode/tasks.json +40 -0
- data/CHANGES.md +48 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +22 -0
- data/README.md +351 -0
- data/Rakefile +13 -0
- data/contrib/example_package.json +10 -0
- data/contrib/example_typescript.rb +14 -0
- data/gulpfile.js +67 -0
- data/lib/assets/javascripts/transpiler.ts.erb +1 -0
- data/lib/assets/javascripts/transpiler_pkg.js.erb +2 -0
- data/lib/assets/javascripts/typescript.js.erb +1 -0
- data/lib/assets/typescripts/transpile_once.ts +13 -0
- data/lib/assets/typescripts/transpiler.ts +203 -0
- data/lib/rails/generators/typescript/assets/assets_generator.rb +13 -0
- data/lib/rails/generators/typescript/assets/templates/javascript.ts +3 -0
- data/lib/typescript-monkey.rb +8 -0
- data/lib/typescript/monkey.rb +9 -0
- data/lib/typescript/monkey/cli.rb +22 -0
- data/lib/typescript/monkey/compiler.rb +177 -0
- data/lib/typescript/monkey/configuration.rb +49 -0
- data/lib/typescript/monkey/engine.rb +18 -0
- data/lib/typescript/monkey/js_hook.rb +15 -0
- data/lib/typescript/monkey/package.rb +239 -0
- data/lib/typescript/monkey/railtie.rb +20 -0
- data/lib/typescript/monkey/template.rb +32 -0
- data/lib/typescript/monkey/template_handler.rb +24 -0
- data/lib/typescript/monkey/transformer.rb +20 -0
- data/lib/typescript/monkey/transpiler.rb +256 -0
- data/lib/typescript/monkey/version.rb +3 -0
- data/package.json +32 -0
- data/test/assets_generator_test.rb +15 -0
- data/test/assets_test.rb +41 -0
- data/test/controller_generator_test.rb +19 -0
- data/test/fixtures/assets/javascripts/hello.js.ts +3 -0
- data/test/fixtures/assets/javascripts/included.ts +4 -0
- data/test/fixtures/assets/javascripts/reference.ts +2 -0
- data/test/fixtures/references/ref1_1.js.ts +3 -0
- data/test/fixtures/references/ref1_2.js.ts +3 -0
- data/test/fixtures/references/ref2_1.d.ts +1 -0
- data/test/fixtures/references/ref2_2.js.ts +3 -0
- data/test/fixtures/references/ref3_1.js.ts +5 -0
- data/test/fixtures/references/ref3_2.ts +3 -0
- data/test/fixtures/references/ref3_3.ts +3 -0
- data/test/fixtures/routes.rb +0 -0
- data/test/fixtures/site/es5.js.ts +7 -0
- data/test/fixtures/site/index.js.ts +1 -0
- data/test/fixtures/site/script_tags.html.erb +23 -0
- data/test/fixtures/sprockets/ref1_1.js.ts +3 -0
- data/test/fixtures/sprockets/ref1_2.js.ts +3 -0
- data/test/fixtures/sprockets/ref1_manifest.js.ts +2 -0
- data/test/references_test.rb +48 -0
- data/test/scaffold_generator_test.rb +19 -0
- data/test/sprockets_test.rb +36 -0
- data/test/support/routes.rb +1 -0
- data/test/template_handler_test.rb +35 -0
- data/test/test_helper.rb +111 -0
- data/tsconfig.json +23 -0
- data/typescript-monkey.gemspec +34 -0
- metadata +175 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'typescript/monkey/package'
|
2
|
+
|
3
|
+
module Typescript::Monkey
|
4
|
+
class << self
|
5
|
+
attr_accessor :configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.configure(&block)
|
9
|
+
self.configuration ||= Configuration.new
|
10
|
+
block.call(configuration) if block_given?
|
11
|
+
end
|
12
|
+
|
13
|
+
class Configuration
|
14
|
+
|
15
|
+
attr_accessor :options
|
16
|
+
attr_accessor :logger
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@_default_options = [
|
20
|
+
"--target es5",
|
21
|
+
"--outFile /dev/stdout",
|
22
|
+
"--noResolve",
|
23
|
+
"--removeComments",
|
24
|
+
"--typeRoots ['#{File.expand_path("../lib", Typescript::Monkey::Package.metadata_path())}']"
|
25
|
+
]
|
26
|
+
@options = @_default_options.to_set
|
27
|
+
@compile = false;
|
28
|
+
@logger = nil;
|
29
|
+
end
|
30
|
+
|
31
|
+
def default_options
|
32
|
+
@_default_options.to_enum
|
33
|
+
end
|
34
|
+
|
35
|
+
def compile=(value)
|
36
|
+
unless (!!value == value)
|
37
|
+
raise TypeError, "#{method(__method__).owner}.#{__method__}: value parameter must be type Bool"
|
38
|
+
end
|
39
|
+
|
40
|
+
if value == true
|
41
|
+
@options.delete("--noResolve")
|
42
|
+
@compile = true
|
43
|
+
else
|
44
|
+
@options.add("--noResolve")
|
45
|
+
@compile = false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/engine'
|
2
|
+
require 'rails/generators'
|
3
|
+
require 'typescript/monkey/js_hook'
|
4
|
+
|
5
|
+
class Typescript::Monkey::Engine < Rails::Engine
|
6
|
+
# To become the default generator...
|
7
|
+
# config.app_generators.javascript_engine :typescript
|
8
|
+
|
9
|
+
if config.respond_to?(:annotations)
|
10
|
+
config.annotations.register_extensions(".ts") { |annotation| /#\s*(#{annotation}):?\s*(.*)$/ }
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer 'override js_template hook' do |app|
|
14
|
+
if app.config.generators.rails[:javascript_engine] == :typescript
|
15
|
+
::Rails::Generators::NamedBase.send :include, Typescript::Monkey::JsHook
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Typescript
|
2
|
+
module Monkey
|
3
|
+
module JsHook
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
no_tasks do
|
8
|
+
redefine_method :js_template do |source, destination|
|
9
|
+
template(source + '.ts', destination + '.ts')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,239 @@
|
|
1
|
+
module Typescript::Monkey
|
2
|
+
require 'pathname'
|
3
|
+
require_relative 'cli'
|
4
|
+
|
5
|
+
#
|
6
|
+
# The Package class.
|
7
|
+
#
|
8
|
+
# A class that implements an interface to the Typescript node installation.
|
9
|
+
#
|
10
|
+
class Package
|
11
|
+
# Returns path to Typescript compiler executable
|
12
|
+
#
|
13
|
+
# The executable compiler can be used to transform Typescript to Javascript
|
14
|
+
# and is used during the asset pipeline compilation stage.
|
15
|
+
#
|
16
|
+
# @return [Pathname] path to Typescript compiler executable
|
17
|
+
#
|
18
|
+
def self.compiler_bin_path
|
19
|
+
compiler_bin_path = npm_bin_path()
|
20
|
+
|
21
|
+
if compiler_bin_path
|
22
|
+
compiler_bin_path = compiler_bin_path.join("tsc")
|
23
|
+
unless (compiler_bin_path && compiler_bin_path.file? && compiler_bin_path.executable?)
|
24
|
+
compiler_bin_path = nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
compiler_bin_path
|
29
|
+
end
|
30
|
+
|
31
|
+
# @TODO: REMOVE compiler_js_path()
|
32
|
+
# Returns path to Typescript compiler source
|
33
|
+
#
|
34
|
+
# The compiler can be included with web content to transform embedded
|
35
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
36
|
+
#
|
37
|
+
# @return [Pathname] path to Typescript javascript compiler source
|
38
|
+
#
|
39
|
+
def self.compiler_js_path
|
40
|
+
compiler_js_path = npm_root_path()
|
41
|
+
|
42
|
+
if compiler_js_path
|
43
|
+
compiler_js_path = compiler_js_path.join("typescript/lib/typescriptServices.js")
|
44
|
+
unless compiler_js_path.file? && compiler_js_path.readable?
|
45
|
+
compiler_js_path = nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
compiler_js_path
|
50
|
+
end
|
51
|
+
|
52
|
+
# @TODO: REMOVE compiler_js()
|
53
|
+
# Returns content for Typescript compiler source
|
54
|
+
#
|
55
|
+
# The compiler can be included with web content to transform embedded
|
56
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
57
|
+
#
|
58
|
+
# @return [String] javascript Typescript compiler source
|
59
|
+
#
|
60
|
+
def self.compiler_js
|
61
|
+
compiler_js = ""
|
62
|
+
compiler_js_path = self.compiler_js_path()
|
63
|
+
|
64
|
+
unless compiler_js_path.nil?
|
65
|
+
compiler_js = compiler_js_path.read()
|
66
|
+
end
|
67
|
+
|
68
|
+
compiler_js
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns package version for Typescript installation
|
72
|
+
#
|
73
|
+
# @return [String] version information
|
74
|
+
#
|
75
|
+
def self.compiler_version
|
76
|
+
compiler_version = "unknown"
|
77
|
+
metadata = self.metadata()
|
78
|
+
|
79
|
+
unless metadata.empty? || !metadata.has_key?('version')
|
80
|
+
compiler_version = metadata['version']
|
81
|
+
end
|
82
|
+
|
83
|
+
compiler_version
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns path to package metadata file for Typescript installation
|
87
|
+
#
|
88
|
+
# The package metadata file is the package.json file.
|
89
|
+
#
|
90
|
+
# @return [Pathname] path to Typepackage information file
|
91
|
+
#
|
92
|
+
def self.metadata_path
|
93
|
+
metadata_path = npm_root_path()
|
94
|
+
|
95
|
+
if metadata_path
|
96
|
+
metadata_path = metadata_path.join("typescript/package.json")
|
97
|
+
unless metadata_path.file? && metadata_path.readable?
|
98
|
+
metadata_path = nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
metadata_path
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
# Returns package metadata contents for Typescript installation
|
107
|
+
#
|
108
|
+
# @return [Hash] hash representation of package metadata contents
|
109
|
+
#
|
110
|
+
def self.metadata
|
111
|
+
metadata = {}
|
112
|
+
metadata_path = self.metadata_path()
|
113
|
+
|
114
|
+
unless metadata_path.nil?
|
115
|
+
metadata = JSON.parse(metadata_path.read())
|
116
|
+
metadata ||= {}
|
117
|
+
end
|
118
|
+
|
119
|
+
metadata
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns path to Typescript services javascript source
|
123
|
+
#
|
124
|
+
# The Typescript services can be included with web content to provide
|
125
|
+
# embedded Typescript functionality. This Typescript::Monkey::Transpiler
|
126
|
+
# leverages services to transpile <script type="text/typescript"> tags
|
127
|
+
# at runtime.
|
128
|
+
#
|
129
|
+
# @return [Pathname] path to Typescript service javascript source
|
130
|
+
#
|
131
|
+
def self.services_js_path
|
132
|
+
services_js_path = npm_root_path()
|
133
|
+
|
134
|
+
if services_js_path
|
135
|
+
services_js_path = services_js_path.join("typescript/lib/typescriptServices.js")
|
136
|
+
unless services_js_path.file? && services_js_path.readable?
|
137
|
+
services_js_path = nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
services_js_path
|
142
|
+
end
|
143
|
+
|
144
|
+
# Returns content for Typescript services javascript source
|
145
|
+
#
|
146
|
+
# The Typescript services can be included with web content to provide
|
147
|
+
# embedded Typescript functionality. This Typescript::Monkey::Transpiler
|
148
|
+
# leverages services to transpile <script type="text/typescript"> tags
|
149
|
+
# at runtime.
|
150
|
+
#
|
151
|
+
# @return [String] Typescript services javascript source
|
152
|
+
#
|
153
|
+
def self.services_js
|
154
|
+
services_js = ""
|
155
|
+
services_js_path = self.services_js_path()
|
156
|
+
|
157
|
+
unless services_js_path.nil?
|
158
|
+
services_js = services_js_path.read()
|
159
|
+
end
|
160
|
+
|
161
|
+
services_js
|
162
|
+
end
|
163
|
+
|
164
|
+
class << self
|
165
|
+
# Returns path to Typescript compiler executable
|
166
|
+
#
|
167
|
+
alias compiler_bin compiler_bin_path
|
168
|
+
|
169
|
+
private
|
170
|
+
|
171
|
+
# Returns closest path for npm directory
|
172
|
+
#
|
173
|
+
# The directory is the npm directory to resolve. Must be a value of:
|
174
|
+
# + root - effective node_modules root directory
|
175
|
+
# + bin - effective node_modules bin directory
|
176
|
+
#
|
177
|
+
# The resolution process favors a local node_modules directory over a
|
178
|
+
# global installation.
|
179
|
+
#
|
180
|
+
# @param directory [String] directory to discover
|
181
|
+
#
|
182
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
183
|
+
#
|
184
|
+
# @raise [ArgumentError] raises this exception if directory has not been
|
185
|
+
# supplied or is invalid.
|
186
|
+
#
|
187
|
+
def npm_path_for(directory)
|
188
|
+
if directory.empty? || directory.nil?
|
189
|
+
raise ArgumentError, "directory parameter required but not supplied"
|
190
|
+
end
|
191
|
+
if !["bin", "root"].include?(directory)
|
192
|
+
raise ArgumentError, "invalid directory specified: #{directory}"
|
193
|
+
end
|
194
|
+
|
195
|
+
npm_path, stderr, status = Typescript::Monkey::CLI.run_command("npm", [directory])
|
196
|
+
unless status.success? && File.directory?(npm_path.chomp!)
|
197
|
+
# try again with global resolution
|
198
|
+
npm_path, stderr, status = Typescript::Monkey::CLI.run_command("npm", ["--global", directory])
|
199
|
+
unless status.success? && File.directory?(npm_path.chomp!)
|
200
|
+
npm_path = ""
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
((npm_path_obj = Pathname.new(npm_path)).to_s.empty?) ? nil : npm_path_obj
|
205
|
+
end
|
206
|
+
|
207
|
+
# Returns closest path for npm root directory
|
208
|
+
#
|
209
|
+
# The resolution process favors a local node_modules directory over a
|
210
|
+
# global installation.
|
211
|
+
#
|
212
|
+
# Results are memoized.
|
213
|
+
#
|
214
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
215
|
+
#
|
216
|
+
# @see Typescript::Monkey::Package.npm_path_for
|
217
|
+
#
|
218
|
+
def npm_root_path
|
219
|
+
@npm_root_path ||= npm_path_for("root")
|
220
|
+
end
|
221
|
+
|
222
|
+
# Returns closest path for npm bin directory
|
223
|
+
#
|
224
|
+
# The resolution process favors a local node_modules directory over a
|
225
|
+
# global installation.
|
226
|
+
#
|
227
|
+
# Results are memoized.
|
228
|
+
#
|
229
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
230
|
+
#
|
231
|
+
# @see Typescript::Monkey::Packags.npm_path_for
|
232
|
+
#
|
233
|
+
def npm_bin_path
|
234
|
+
@npm_bin_path ||= npm_path_for("bin")
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
end
|
239
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'typescript/monkey'
|
2
|
+
|
3
|
+
class Typescript::Monkey::Railtie < ::Rails::Railtie
|
4
|
+
config.before_initialize do |app|
|
5
|
+
if ::Rails::VERSION::MAJOR >= 4 || app.config.assets.enabled
|
6
|
+
require 'typescript/monkey/template'
|
7
|
+
require 'typescript/monkey/transformer'
|
8
|
+
require 'sprockets'
|
9
|
+
|
10
|
+
if Sprockets.respond_to?(:register_engine)
|
11
|
+
Sprockets.register_engine '.ts', Typescript::Monkey::Template, silence_deprecation: true
|
12
|
+
end
|
13
|
+
|
14
|
+
if Sprockets.respond_to?(:register_transformer)
|
15
|
+
Sprockets.register_mime_type 'text/typescript', extensions: ['.ts']
|
16
|
+
Sprockets.register_transformer 'text/typescript', 'application/javascript', Typescript::Monkey::Transformer
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'typescript/monkey'
|
2
|
+
require 'tilt/template'
|
3
|
+
|
4
|
+
class Typescript::Monkey::Template < ::Tilt::Template
|
5
|
+
self.default_mime_type = 'application/javascript'
|
6
|
+
|
7
|
+
# @!scope class
|
8
|
+
class_attribute :default_bare
|
9
|
+
|
10
|
+
def self.engine_initialized?
|
11
|
+
defined? ::Typescript::Monkey::Compiler
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize_engine
|
15
|
+
require_template_library 'typescript/monkey/compiler'
|
16
|
+
end
|
17
|
+
|
18
|
+
def prepare
|
19
|
+
if !options.key?(:bare) and !options.key?(:no_wrap)
|
20
|
+
options[:bare] = self.class.default_bare
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def evaluate(context, locals, &block)
|
25
|
+
@output ||= ::Typescript::Monkey::Compiler.compile(file, data, context)
|
26
|
+
end
|
27
|
+
|
28
|
+
# @override
|
29
|
+
def allows_script?
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'typescript/monkey/compiler'
|
2
|
+
|
3
|
+
class Typescript::Monkey::TemplateHandler
|
4
|
+
class << self
|
5
|
+
def erb_handler
|
6
|
+
@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(template)
|
10
|
+
compiled_source = erb_handler.call(template)
|
11
|
+
path = template.identifier.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\'
|
12
|
+
<<-EOS
|
13
|
+
::Typescript::Monkey::Compiler.compile('#{path}', (begin;#{compiled_source};end))
|
14
|
+
EOS
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Register template handler for .ts files, enable digest for .ts files
|
20
|
+
ActiveSupport.on_load(:action_view) do
|
21
|
+
ActionView::Template.register_template_handler :ts, Typescript::Monkey::TemplateHandler
|
22
|
+
require 'action_view/dependency_tracker'
|
23
|
+
ActionView::DependencyTracker.register_tracker :ts, ActionView::DependencyTracker::ERBTracker
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'typescript/monkey'
|
2
|
+
|
3
|
+
class Typescript::Monkey::Transformer
|
4
|
+
def self.instance
|
5
|
+
@instance ||= new
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.call(input)
|
9
|
+
instance.call(input)
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(input)
|
13
|
+
filename = input[:filename]
|
14
|
+
source = input[:data]
|
15
|
+
context = input[:environment].context_class.new(input)
|
16
|
+
|
17
|
+
result = ::Typescript::Monkey::Compiler.compile(filename, source, context)
|
18
|
+
{ data: result }
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,256 @@
|
|
1
|
+
module Typescript::Monkey
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
#
|
5
|
+
# The Transpiler class.
|
6
|
+
#
|
7
|
+
# A class that implements an interface to a dynamic runtime Typescript to
|
8
|
+
# javascript transpiler.
|
9
|
+
#
|
10
|
+
# The scripts that are returned by this class are dependent upon Typescript
|
11
|
+
# Services to be available.
|
12
|
+
#
|
13
|
+
# @see Typescript::Monkey::Package.services_js
|
14
|
+
#
|
15
|
+
class Transpiler
|
16
|
+
|
17
|
+
# Returns path to dynamic runtime transpiler
|
18
|
+
#
|
19
|
+
# The transpiler can be included with web content to transform embedded
|
20
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
21
|
+
#
|
22
|
+
# @return [Pathname] path to dyrt source
|
23
|
+
#
|
24
|
+
def self.dyrt_js_path
|
25
|
+
transpiler_js_path = gem_javascripts_path()
|
26
|
+
|
27
|
+
if transpiler_js_path
|
28
|
+
transpiler_js_path = transpiler_js_path.join("dyrt.js")
|
29
|
+
unless transpiler_js_path.file? && transpiler_js_path.readable?
|
30
|
+
transpiler_js_path = nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
transpiler_js_path
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns content for dynamic runtime transpiler
|
38
|
+
#
|
39
|
+
# The transpiler can be included with web content to transform embedded
|
40
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
41
|
+
#
|
42
|
+
# @return [String] dyrt source
|
43
|
+
#
|
44
|
+
def self.dyrt_js
|
45
|
+
transpiler_js = ""
|
46
|
+
transpiler_js_path = self.dyrt_js_path()
|
47
|
+
|
48
|
+
unless transpiler_js_path.nil?
|
49
|
+
transpiler_js = transpiler_js_path.read()
|
50
|
+
end
|
51
|
+
|
52
|
+
transpiler_js
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns path to dynamic runtime transpiler
|
56
|
+
#
|
57
|
+
# The transpiler can be included with web content to transform embedded
|
58
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
59
|
+
#
|
60
|
+
# The "once" compilers include an immediately invoked function expression
|
61
|
+
# (IIFE) that triggers transpile exactly once. Add this script to the
|
62
|
+
# bottom of the body in an HTML page; ideally, this should run last. The
|
63
|
+
# script does not, and probably should not, be run after DOM ready; however
|
64
|
+
# the Typescript objects in your page can wait for DOM ready.
|
65
|
+
#
|
66
|
+
# @return [Pathname] path to dyrt source
|
67
|
+
#
|
68
|
+
def self.dyrt_once_js_path
|
69
|
+
transpiler_js_path = gem_javascripts_path()
|
70
|
+
|
71
|
+
if transpiler_js_path
|
72
|
+
transpiler_js_path = transpiler_js_path.join("dyrt_once.js")
|
73
|
+
unless transpiler_js_path.file? && transpiler_js_path.readable?
|
74
|
+
transpiler_js_path = nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
transpiler_js_path
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns content for dynamic runtime transpiler
|
82
|
+
#
|
83
|
+
# The transpiler can be included with web content to transform embedded
|
84
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
85
|
+
#
|
86
|
+
# The "once" compilers include an immediately invoked function expression
|
87
|
+
# (IIFE) that triggers transpile exactly once. Add this script to the
|
88
|
+
# bottom of the body in an HTML page; ideally, this should run last. The
|
89
|
+
# script does not, and probably should not, be run after DOM ready; however
|
90
|
+
# the Typescript objects in your page can wait for DOM ready.
|
91
|
+
#
|
92
|
+
# @return [String] dyrt source
|
93
|
+
#
|
94
|
+
def self.dyrt_once_js
|
95
|
+
transpiler_js = ""
|
96
|
+
transpiler_js_path = self.dyrt_once_js_path()
|
97
|
+
|
98
|
+
unless transpiler_js_path.nil?
|
99
|
+
transpiler_js = transpiler_js_path.read()
|
100
|
+
end
|
101
|
+
|
102
|
+
transpiler_js
|
103
|
+
end
|
104
|
+
|
105
|
+
# Returns path to dynamic runtime transpiler (pre-transpiled version)
|
106
|
+
#
|
107
|
+
# The transpiler can be included with web content to transform embedded
|
108
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
109
|
+
#
|
110
|
+
# @return [Pathname] path to dyrt source (pre-transpiled version)
|
111
|
+
#
|
112
|
+
def self.dyrt_ts_path
|
113
|
+
transpiler_ts_path = gem_typescripts_path()
|
114
|
+
|
115
|
+
if transpiler_ts_path
|
116
|
+
transpiler_ts_path = transpiler_ts_path.join("transpiler.ts")
|
117
|
+
unless transpiler_ts_path.file? && transpiler_ts_path.readable?
|
118
|
+
transpiler_ts_path = nil
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
transpiler_ts_path
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns content for dynamic runtime transpiler (pre-transpiled version)
|
126
|
+
#
|
127
|
+
# The transpiler can be included with web content to transform embedded
|
128
|
+
# Typescript wrapped in <script type="text/typescript"></script> tags.
|
129
|
+
#
|
130
|
+
# @return [String] typescript transpiler source (pre-transpiled version)
|
131
|
+
#
|
132
|
+
def self.dyrt_ts
|
133
|
+
transpiler_ts = ""
|
134
|
+
transpiler_ts_path = self.dyrt_ts_path()
|
135
|
+
|
136
|
+
unless transpiler_ts_path.nil?
|
137
|
+
transpiler_ts = transpiler_ts_path.read()
|
138
|
+
end
|
139
|
+
|
140
|
+
transpiler_ts
|
141
|
+
end
|
142
|
+
|
143
|
+
class << self
|
144
|
+
|
145
|
+
# Returns path to transpiler source
|
146
|
+
#
|
147
|
+
alias runner_js dyrt_js
|
148
|
+
alias runner_ts dyrt_ts
|
149
|
+
|
150
|
+
# private
|
151
|
+
|
152
|
+
# Returns path for gem directory
|
153
|
+
#
|
154
|
+
# The directory is the gem directory to resolve. Must be a value of:
|
155
|
+
# + root - gem root directory
|
156
|
+
# + lib - gem lib directory
|
157
|
+
# + assets - gem lib/assets directory
|
158
|
+
# + javascripts - gem lib/assets/javascripts directory
|
159
|
+
# + typescripts - gem lib/assets/typescripts directory
|
160
|
+
#
|
161
|
+
# Results are memoized.
|
162
|
+
#
|
163
|
+
# @param directory [String] directory to discover
|
164
|
+
#
|
165
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
166
|
+
#
|
167
|
+
# @raise [ArgumentError] raises this exception if directory has not been
|
168
|
+
# supplied or is invalid.
|
169
|
+
#
|
170
|
+
def gem_path_for(directory)
|
171
|
+
if directory.empty? || directory.nil?
|
172
|
+
raise ArgumentError, "directory parameter required but not supplied"
|
173
|
+
end
|
174
|
+
if !["root", "lib", "assets", "javascripts", "typescripts"].include?(directory)
|
175
|
+
raise ArgumentError, "invalid directory specified: #{directory}"
|
176
|
+
end
|
177
|
+
|
178
|
+
directory_sym = directory.downcase.to_sym
|
179
|
+
@gem_paths ||= {}
|
180
|
+
return @gem_paths[directory_sym] if @gem_paths.has_key?(directory_sym)
|
181
|
+
|
182
|
+
# resolve gem root (top directory of this gem)
|
183
|
+
gem_root = Pathname.new(File.expand_path("../../../../", __FILE__))
|
184
|
+
|
185
|
+
# process shortcuts
|
186
|
+
case directory_sym
|
187
|
+
when :lib
|
188
|
+
gem_path = gem_root.join("lib")
|
189
|
+
when :assets
|
190
|
+
gem_path = gem_root.join("lib/assets")
|
191
|
+
when :javascripts
|
192
|
+
gem_path = gem_root.join("lib/assets/javascripts")
|
193
|
+
when :typescripts
|
194
|
+
gem_path = gem_root.join("lib/assets/typescripts")
|
195
|
+
when :root
|
196
|
+
gem_path = gem_root
|
197
|
+
else
|
198
|
+
gem_path = Pathname.new("")
|
199
|
+
end
|
200
|
+
|
201
|
+
@gem_paths[directory_sym] = ((gem_path.directory?) ? gem_path : nil)
|
202
|
+
end
|
203
|
+
|
204
|
+
# Returns top directory for this gem
|
205
|
+
#
|
206
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
207
|
+
#
|
208
|
+
# @see Typescript::Monkey::Transpiler.gem_path_for
|
209
|
+
#
|
210
|
+
def gem_root_path
|
211
|
+
gem_path_for("root")
|
212
|
+
end
|
213
|
+
|
214
|
+
# Returns lib directory for this gem
|
215
|
+
#
|
216
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
217
|
+
#
|
218
|
+
# @see Typescript::Monkey::Transpiler.gem_path_for
|
219
|
+
#
|
220
|
+
def gem_lib_path
|
221
|
+
gem_path_for("lib")
|
222
|
+
end
|
223
|
+
|
224
|
+
# Returns assets directory for this gem
|
225
|
+
#
|
226
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
227
|
+
#
|
228
|
+
# @see Typescript::Monkey::Transpiler.gem_path_for
|
229
|
+
#
|
230
|
+
def gem_assets_path
|
231
|
+
gem_path_for("assets")
|
232
|
+
end
|
233
|
+
|
234
|
+
# Returns javascripts directory for this gem
|
235
|
+
#
|
236
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
237
|
+
#
|
238
|
+
# @see Typescript::Monkey::Transpiler.gem_path_for
|
239
|
+
#
|
240
|
+
def gem_javascripts_path
|
241
|
+
gem_path_for("javascripts")
|
242
|
+
end
|
243
|
+
|
244
|
+
# Returns typescripts directory for this gem
|
245
|
+
#
|
246
|
+
# @return [Pathname] path to directory on success, otherwise nil
|
247
|
+
#
|
248
|
+
# @see Typescript::Monkey::Transpiler.gem_path_for
|
249
|
+
#
|
250
|
+
def gem_typescripts_path
|
251
|
+
gem_path_for("typescripts")
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
256
|
+
end
|