Skip to content

Commit fd4b3ce

Browse files
author
Travers McInerney
committed
Merge pull request #68 from zendesk/likun/commonjs_support
APPS-1524 As an App developer I expect CommonJS Support in Apps
2 parents 8499f0d + 36bbdb7 commit fd4b3ce

File tree

17 files changed

+204
-56
lines changed

17 files changed

+204
-56
lines changed
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
(function() {
2-
with( require('apps/framework/app_scope') ) {
2+
with( ZendeskApps.AppScope.create() ) {
3+
<% unless modules.nil? %>
4+
require.modules = {
5+
<% modules.each do |path, content| %>
6+
'<%= path %>': function(exports, require, module) {
7+
<%= content %>
8+
},
9+
<% end %>
10+
eom: undefined
11+
};
12+
<% end %>
313

4-
var source = <%= source %>;
14+
var source = <%= source %>;
515

6-
ZendeskApps[<%= name.to_json %>] = ZendeskApps.defineApp(source)
7-
.reopenClass({ location: <%= location.to_json %> })
8-
.reopen({
9-
assetUrlPrefix: <%= asset_url_prefix.to_json %>,
10-
appClassName: <%= app_class_name.to_json %>,
11-
author: {
12-
name: <%= author[:name].to_json %>,
13-
email: <%= author[:email].to_json %>
14-
},
15-
translations: <%= translations.to_json %>,
16-
templates: <%= templates.to_json %>,
17-
frameworkVersion: <%= framework_version.to_json %>
18-
});
16+
var app = ZendeskApps.defineApp(source)
17+
.reopenClass({ location: <%= location.to_json %> })
18+
.reopen({
19+
assetUrlPrefix: <%= asset_url_prefix.to_json %>,
20+
appClassName: <%= app_class_name.to_json %>,
21+
author: {
22+
name: <%= author[:name].to_json %>,
23+
email: <%= author[:email].to_json %>
24+
},
25+
translations: <%= translations.to_json %>,
26+
templates: <%= templates.to_json %>,
27+
frameworkVersion: <%= framework_version.to_json %>,
28+
});
1929

20-
}
21-
22-
ZendeskApps[<%= name.to_json %>].install({"id": <%= app_id %>, "app_id": <%= app_id %>, "settings": <%= settings.to_json %>});
30+
ZendeskApps[<%= name.to_json %>] = app;
31+
}
2332

33+
ZendeskApps[<%= name.to_json %>].install({"id": <%= app_id %>, "app_id": <%= app_id %>, "settings": <%= settings.to_json %>});
2434
}());
2535

2636
ZendeskApps.trigger && ZendeskApps.trigger('ready');

lib/zendesk_apps_support/package.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ class Package
1010
DEFAULT_SCSS = File.read(File.expand_path('../assets/default_styles.scss', __FILE__))
1111
SRC_TEMPLATE = Erubis::Eruby.new( File.read(File.expand_path('../assets/src.js.erb', __FILE__)) )
1212

13-
attr_reader :root, :warnings
13+
attr_reader :lib_root, :root, :warnings
1414
attr_accessor :requirements_only
1515

1616
def initialize(dir)
1717
@root = Pathname.new(File.expand_path(dir))
18+
@lib_root = Pathname.new(File.join(@root, 'lib'))
1819
@warnings = []
1920
@requirements_only = false
2021
end
@@ -46,10 +47,28 @@ def validate
4647
end
4748
end
4849

50+
def app_js
51+
read_file("app.js")
52+
end
53+
54+
def commonjs_modules
55+
return unless has_lib_js?
56+
57+
lib_files.each_with_object({}) do |file, modules|
58+
name = file.relative_path.gsub!(/^lib\//, '')
59+
content = file.read
60+
modules[name] = content
61+
end
62+
end
63+
4964
def files
5065
non_tmp_files
5166
end
5267

68+
def lib_files
69+
@lib_files ||= files.select { |f| f =~ /^lib\/.*\.js$/ }
70+
end
71+
5372
def template_files
5473
files.select { |f| f =~ /^templates\/.*\.hdbs$/ }
5574
end
@@ -76,7 +95,7 @@ def app_translations
7695

7796
def readified_js(app_name, app_id, asset_url_prefix, settings={})
7897
manifest = manifest_json
79-
source = read_file("app.js")
98+
source = app_js
8099
name = app_name || manifest[:name] || 'Local App'
81100
location = manifest[:location]
82101
app_class_name = "app-#{app_id}"
@@ -97,7 +116,8 @@ def readified_js(app_name, app_id, asset_url_prefix, settings={})
97116
:framework_version => framework_version,
98117
:templates => templates,
99118
:settings => settings,
100-
:app_id => app_id
119+
:app_id => app_id,
120+
:modules => commonjs_modules
101121
)
102122
end
103123

@@ -110,6 +130,10 @@ def has_js?
110130
file_exists?("app.js")
111131
end
112132

133+
def has_lib_js?
134+
lib_files.any?
135+
end
136+
113137
def has_manifest?
114138
file_exists?("manifest.json")
115139
end

lib/zendesk_apps_support/validations/source.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,38 @@ module Source
1515

1616
# predefined globals:
1717
:predef => %w(_ console services helpers alert window document self
18-
JSON Base64 clearInterval clearTimeout setInterval setTimeout)
18+
JSON Base64 clearInterval clearTimeout setInterval setTimeout
19+
require module exports top frames parent)
1920
}.freeze
2021

2122
class <<self
2223
def call(package)
23-
source = package.files.find { |f| f.relative_path == 'app.js' }
24+
app = package.files.find { |file| file.relative_path == 'app.js' }
25+
files = package.lib_files << app
2426

2527
if package.requirements_only
26-
return source ? [ ValidationError.new(:no_app_js_required) ] : []
28+
return app ? [ ValidationError.new(:no_app_js_required) ] : []
2729
end
2830

29-
return [ ValidationError.new(:missing_source) ] unless source
31+
return [ ValidationError.new(:missing_source) ] unless app
3032

31-
jshint_errors = linter.lint(source.read)
32-
if jshint_errors.any?
33-
[ JSHintValidationError.new(source.relative_path, jshint_errors) ]
34-
else
35-
[]
36-
end
33+
jshint_errors(files).flatten!
3734
end
3835

3936
private
4037

38+
def jshint_error(file)
39+
errors = linter.lint(file.read)
40+
[ JSHintValidationError.new(file.relative_path, errors) ] if errors.any?
41+
end
42+
43+
def jshint_errors(files)
44+
files.each_with_object([]) do |file, errors|
45+
error = jshint_error(file)
46+
errors << error unless error.nil?
47+
end
48+
end
49+
4150
def linter
4251
Jshintrb::Lint.new(LINTER_OPTIONS)
4352
end

spec/app/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
(function() {
22

33
return {
4+
a: require('a.js'),
5+
46
events: {
57
'app.activated':'doSomething'
68
},
79

810
doSomething: function() {
11+
console.log(a.name);
912
}
1013
};
1114

spec/app/lib/a.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var a = {
2+
name: 'This is A'
3+
};
4+
5+
module.exports = a;

spec/app/lib/a.txt

Whitespace-only changes.

spec/app/lib/nested/b.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var b = {
2+
name: 'This is B'
3+
};
4+
5+
module.exports = b;

spec/invalid_app/app.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
h1 {
2+
color: red;
3+
span {
4+
color: green;
5+
}
6+
}

spec/invalid_app/app.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(function() {
2+
3+
return {
4+
events: {
5+
'app.activated':'doSomething'
6+
},
7+
8+
doSomething: function() {
9+
console.log("Invalid app");
10+
}
11+
};
12+
13+
}());
18.8 KB
Loading

0 commit comments

Comments
 (0)