js-preflight 0.0.1

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.
data/bin/jsmin ADDED
Binary file
data/bin/preflight ADDED
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/ruby
2
+ require 'pathname'
3
+ require 'optparse'
4
+ require 'stringio'
5
+ require 'fileutils'
6
+ require 'lib/js-preflight'
7
+
8
+ module Js
9
+ module Preflight
10
+ module Instructions
11
+ extend self
12
+ def executable
13
+ @executable ||= Pathname.new(__FILE__).basename.to_s
14
+ end
15
+
16
+ def print_usage
17
+
18
+ STDERR.puts <<USAGE
19
+
20
+ #{executable}
21
+ #{"=" * executable.length}
22
+ Provide one or more html files as arguments. #{executable} will
23
+ find files ending in .js in the src property of each script tag,
24
+ and process them in order of appearance.
25
+
26
+ For help with options and usage:
27
+ #{executable} --help
28
+
29
+ USAGE
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ options = {}
36
+ js_output = StringIO.new
37
+ regexp = Js::Preflight::JsRegexp
38
+ protected_paths = %w{/bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin /var /var/log /var/spool /etc}
39
+
40
+ optparse = OptionParser.new do |opts|
41
+ opts.banner = "Usage: #{Js::Preflight::Instructions.executable} [options] index.html [index2.html, ...]"
42
+
43
+ options[:lint] = true
44
+ opts.on("-l", "--[no-]lint", "Run JSLint on every .js file (default: true)") do |j|
45
+ options[:lint] = j
46
+ end
47
+
48
+ options[:strict] = true
49
+ opts.on("-s", "--[no-]strict", "Don't proceed unless JSLint passes (default: true)") do |s|
50
+ options[:strict] = s
51
+ end
52
+
53
+ options[:pack] = true
54
+ opts.on("-p", "--[no-]pack", "Pack every .js file, in order of apperance, using JSMin (default: true)") do |p|
55
+ options[:pack] = p
56
+ end
57
+
58
+ options[:exclude_paths] = []
59
+ opts.on("--exclude path1,path2,path3", Array, "Exclude .js files if they are in this list of comma-separated paths") do |paths|
60
+ options[:exclude_paths] = paths
61
+ end
62
+
63
+ options[:quiet] = false
64
+ opts.on("-q", "--quiet", "Don't report errors (default: false)") do |q|
65
+ options[:quiet] = q
66
+ end
67
+
68
+ options[:js_file] = false
69
+ opts.on("-j [FILE]", "--js-file [FILE]", "Output to file <FILE> instead of STDOUT (FILE default: assets.js)") do |f|
70
+ options[:js_file] = true
71
+ f = f.nil? ? "assets.js" : f
72
+ js_output = File.new(f, "w")
73
+ end
74
+
75
+ options[:html_path] = false
76
+ opts.on("--html-path PATH", "Output the provided HTML file(s), with affected script tags", " replaced by the packaged version, at <PATH>/[JS_FILE].", " Protected paths are silently ignored; ask an adult for help.", " Depends on -j, which provides the basename for JS_FILE") do |path|
77
+ unless options[:js_file]
78
+ STDERR.puts "--html-path PATH requires --js-file JS_FILE"
79
+ exit
80
+ end
81
+ unless protected_paths.include?(path)
82
+ options[:html_path] = path
83
+ end
84
+ end
85
+
86
+ opts.on("-h", "--help", "Show options and usage") do
87
+ Js::Preflight::Instructions.print_usage
88
+ puts opts
89
+ exit
90
+ end
91
+ end
92
+
93
+ optparse.parse!
94
+
95
+
96
+
97
+ if ARGV.length > 0
98
+
99
+ ARGV.each do |filename|
100
+
101
+ js_files = []
102
+ path = Pathname.new(filename)
103
+ file = File.new(path)
104
+
105
+ ::Js::Preflight::Scanner.scan(file, options) do |js_file|
106
+ js_files << js_file
107
+ end
108
+
109
+ relative_js_files = js_files.map{|js_file| File.join(path.dirname, js_file)}
110
+
111
+ lint_errors = []
112
+ if options[:lint]
113
+ ::Js::Preflight::Lint.build(relative_js_files) do |errors|
114
+ lint_errors << errors
115
+ end
116
+ end
117
+
118
+ if lint_errors.any?
119
+ unless options[:quiet]
120
+ STDERR.puts("#{path}:")
121
+ STDERR.puts("=" * path.to_s.length)
122
+ lint_errors.each {|error| STDERR.puts(error)}
123
+ end
124
+
125
+ next if options[:strict]
126
+ end
127
+
128
+ if options[:pack]
129
+ ::Js::Preflight::Pack.build(relative_js_files) do |packed_file|
130
+ js_output << packed_file
131
+ end
132
+ end
133
+
134
+ if options[:html_path]
135
+ file.rewind
136
+ out_filename = Pathname.new(File.join(options[:html_path], path.basename))
137
+ FileUtils.mkdir_p(out_filename.dirname)
138
+ File.open(out_filename, "w+") do |outfile|
139
+ while line = file.gets do
140
+ outfile << line unless js_files.include?(line[regexp, 1])
141
+ outfile << %Q{<script src="#{js_output.path}" type="text/javascript" charset="utf-8"></script>} if line[regexp, 1] == js_files.last
142
+ end
143
+ end
144
+ end
145
+
146
+ end
147
+
148
+ if options[:js_file]
149
+ js_output.close
150
+ else
151
+ js_output.rewind
152
+ STDOUT.puts js_output.read
153
+ end
154
+
155
+ else
156
+ Js::Preflight::Instructions.print_usage
157
+ exit
158
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "js-preflight/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "js-preflight"
7
+ s.version = Js::Preflight::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Scott Burton", "Doug Crockford", "Andy Walker"]
10
+ s.email = ["scottburton11@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Easy preflight command-line script for jslint and jsmin}
13
+ s.description = %q{Easy preflight command-line script for jslint and jsmin. Provides a packed, minified javascript file on STDOUT, or writes .js and provided .html files to a provided directory.}
14
+
15
+ s.rubyforge_project = "js-preflight"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.bindir = "bin"
22
+ end
@@ -0,0 +1,32 @@
1
+ module Js
2
+ module Preflight
3
+ class Lint
4
+ attr_reader :filename, :executable_path
5
+ def initialize(filename, options = {})
6
+ @filename = filename
7
+ @executable_path = options[:executable_path] || File.join(::Js::Preflight::Basedir, "bin")
8
+ end
9
+
10
+ def errors
11
+ @errors ||= lint
12
+ end
13
+
14
+ def clean?
15
+ errors == "jslint: No problems found."
16
+ end
17
+
18
+ def self.build(files)
19
+ files.each do |file|
20
+ lint = Lint.new(file)
21
+ yield lint.errors unless lint.clean?
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def lint
28
+ `js -f #{executable_path}/jslint < #{filename}`
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ module Js
2
+ module Preflight
3
+ class Pack
4
+ attr_reader :filename, :executable_path
5
+ def initialize(filename, options = {})
6
+ @filename = filename
7
+ @executable_path = options[:executable_path] || File.join(::Js::Preflight::Basedir, "bin")
8
+ end
9
+
10
+ def packed
11
+ @pack ||= pack
12
+ end
13
+
14
+ def self.build(files)
15
+ files.each do |file|
16
+ yield Pack.new(file).packed
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def pack
23
+ `#{executable_path}/jsmin < #{filename}`
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module Js
2
+ module Preflight
3
+ class Scanner
4
+ class << self
5
+ def scan(file, options = {})
6
+ exclude_paths = options[:exclude_paths] || []
7
+ file = File.open(file, "r") unless file.kind_of?(File)
8
+ while line = file.gets do
9
+ line.scan(::Js::Preflight::JsRegexp).each do |js_file|
10
+ yield js_file[0] unless exclude_paths.any? { |p| js_file[0].match(p) }
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Js
2
+ module Preflight
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ $:.unshift("./lib")
2
+ require 'js-preflight/lint'
3
+ require 'js-preflight/pack'
4
+ require 'js-preflight/scanner'
5
+ require 'js-preflight/version'
6
+
7
+ module Js
8
+ module Preflight
9
+ JsRegexp = %r|src=\"(.+\.js)\"|
10
+ Basedir = File.expand_path(File.join(__FILE__, "..", ".."))
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js-preflight
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Scott Burton
14
+ - Doug Crockford
15
+ - Andy Walker
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-04-26 00:00:00 -07:00
21
+ default_executable:
22
+ dependencies: []
23
+
24
+ description: Easy preflight command-line script for jslint and jsmin. Provides a packed, minified javascript file on STDOUT, or writes .js and provided .html files to a provided directory.
25
+ email:
26
+ - scottburton11@gmail.com
27
+ executables:
28
+ - jslint
29
+ - jsmin
30
+ - preflight
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - README.md
39
+ - Rakefile
40
+ - bin/jslint
41
+ - bin/jsmin
42
+ - bin/preflight
43
+ - js-preflight.gemspec
44
+ - lib/js-preflight.rb
45
+ - lib/js-preflight/lint.rb
46
+ - lib/js-preflight/pack.rb
47
+ - lib/js-preflight/scanner.rb
48
+ - lib/js-preflight/version.rb
49
+ has_rdoc: true
50
+ homepage: ""
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project: js-preflight
79
+ rubygems_version: 1.4.2
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Easy preflight command-line script for jslint and jsmin
83
+ test_files: []
84
+