9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/middleman-typescript/extension.rb', line 9
def initialize(app, options_hash={}, &block)
super
app.set :typescript_dir, options.typescript_dir
compile_options = ['--target', options.target]
compile_options << '--noImplicitAny' if options.no_implicit_any
app.set :typescript_compile_options, compile_options
app.set :typescript_js_lib_dir, options.js_lib_dir
app.ready do
files.changed do |file|
next if File.extname(file) != '.ts'
file_path = "#{Dir.pwd}/#{file}"
result = TypeScript::Node.compile_file(file_path, *app.typescript_compile_options)
if result.success?
export_dir = source_dir + File.dirname(file_path).sub(source_dir, '').sub(app.typescript_dir, app.js_dir)
FileUtils.mkdir_p export_dir unless Dir.exist? export_dir
file_name = File.basename(file_path).gsub(/\.ts/, '.js')
File.open "#{export_dir}/#{file_name}", "w" do |f|
f.write result.js
end
else
logger.info "TypeScript: #{result.stderr}"
end
end
files.deleted do |file|
next if File.extname(file) != '.ts'
file_path = "#{Dir.pwd}/#{file}"
file_name = File.basename(file_path).gsub(/\.ts/, '.js')
unlink_dir = source_dir + File.dirname(file_path).sub(source_dir, '').sub(app.typescript_dir, app.js_dir)
unlink_path = "#{unlink_dir}/#{file_name}"
File.unlink unlink_path
end
end
end
|