xaases 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3684fb4858709d60739e6cac8bcd89d268c2ca9f14dc8d764b4c48bc24ff3416
4
- data.tar.gz: 55e17d9d5c5f8ebd3585b073834407426dc923d77d3f6b047ba944dbc83b1517
3
+ metadata.gz: 20c01b0ec0fd646988062e2c636f5f87e9b4d336c6e3a0a5803059c3b7dd0c72
4
+ data.tar.gz: 1f4e2ab166e2e29accc17f637830ae9869837f3d57faa7623af95255d8d1655a
5
5
  SHA512:
6
- metadata.gz: e32dbdd2c7536960700215d6981f4f95c83e14c7107665129e3a5c759f14d277e3faa8942d72191c4072d9fe42ff059def75aeac7d8066ddbc4bc4f020bd461a
7
- data.tar.gz: 0edc73e54643a28aad13f456afb28027a653d3b6ab62a60d812fb5639a76a0445125253985ed6934f9d32ec853f26f99c8e30bc884c2bd6be0e95be2bd970e26
6
+ metadata.gz: ba9b23891348962ca8247bba003f0495f8888bc6b6e4de67f1860ba67cf7eb10c388e88c53b66db1006d2b9b57c76a8d3e604697319933606bbd312b161fbd39
7
+ data.tar.gz: 72b3ee56ac48a105e837176eb280276d63ea3ab3be2eaa7579731530408464e682b81745b5fd5a7da55ec20f69f392f8d32cb0c6fd89f0ad05c129a432622e94
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ serverless.yml
8
8
  xaases.yml
9
9
  .serverless
10
10
  *.gem
11
+ *.js
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xaases (0.1.1)
4
+ xaases (0.2.0)
5
5
  highline (~> 2.0, >= 2.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -22,15 +22,17 @@ $ xaases init
22
22
 
23
23
  is create `xaases.yml`.
24
24
 
25
- ### Add resource
25
+ ### Add table
26
26
 
27
27
  ```
28
- $ xaases add <tablename> <columns...>
28
+ $ xaases table <name> <columns...>
29
29
  ```
30
30
 
31
+ **Memo:** DynamoDB table support columns max of count is 2.
32
+
31
33
  ### Deploy to AWS
32
34
 
33
- Edit `serverless.yml`
35
+ Need edit `serverless.yml` with
34
36
 
35
37
  ```
36
38
  provider:
@@ -38,7 +40,25 @@ provider:
38
40
  region: <region>
39
41
  ```
40
42
 
41
- and run `sls deploy`
43
+ And run `sls deploy` command.
44
+
45
+ ### Add API
46
+
47
+ ```
48
+ $ xaases api GET /
49
+ ```
50
+
51
+ Created API `GET /`, add to `serverless.yml` and generate `handler.js`
52
+
53
+ Generated `handler.js` content is
54
+
55
+ ```js
56
+ module.exports.root = (event, context, callback) => {
57
+ callback(null, { statusCode: 200, body: "OK." });
58
+ };
59
+ ```
60
+
61
+ You can edit anything of it.
42
62
 
43
63
  ## Development
44
64
 
data/lib/xaases/api.rb ADDED
@@ -0,0 +1,14 @@
1
+ module Xaases
2
+ class Api
3
+ def initialize(method, path)
4
+ @method = method.upcase
5
+ @path = path
6
+ end
7
+
8
+ def name
9
+ s = path.sub(/\A\//, '').sub(/\//, '_')
10
+ s = 'root' if method == 'GET' && s == ''
11
+ s
12
+ end
13
+ end
14
+ end
@@ -2,10 +2,13 @@ require 'xaases/js'
2
2
  module Xaases
3
3
  module Aws
4
4
  class LambdaJs
5
- def initialize
6
- @js = Xaases::Js.new
7
- @js.strict!
8
- @js.const(aws: "require('aws-sdk')")
5
+ attr_reader :minify
6
+
7
+ def initialize(minify: true)
8
+ @minify = minify
9
+ @js = Xaases::Js.new(minify: minify)
10
+ #@js.strict!
11
+ #@js.const(aws: "require('aws-sdk')")
9
12
  end
10
13
 
11
14
  def use_dynamo!
@@ -13,27 +16,29 @@ module Xaases
13
16
  end
14
17
 
15
18
  def export(name, content)
16
- f = Xaases::Js::Function.new(:eve, :ctx, :cb)
19
+ #f = Xaases::Js::Function.new(:eve, :ctx, :cb)
20
+ f = Xaases::Js::Function.new(:event, :context, :callback)
21
+ f.content = content
17
22
  @js.export(name, f)
18
23
  end
19
24
 
20
25
  def res(status, content)
21
26
  #err = err ? err : 'null'
22
- body = Xaases::Js.new.hash('statusCode' => status, 'body' => content)
27
+ body = Xaases::Js.new(minify: minify).hash('statusCode' => status, 'body' => content)
23
28
  @js.call :callback, 'null', body
24
29
  end
25
30
 
26
- def put(table, data, handler)
31
+ def dynamo_put(table, data, handler)
27
32
  params = {
28
33
  'TableName' => "'#{table}'",
29
- 'Item' => Xaases::Js.new.hash(data)
34
+ 'Item' => Xaases::Js.new(minify: minify).hash(data)
30
35
  }
31
- params_js = Xaases::Js.new.hash(params)
32
- @js.call :put, params_js, handler
36
+ params_js = Xaases::Js.new(minify: minify).hash(params)
37
+ @js.call 'dyn.put', params_js, handler
33
38
  end
34
39
 
35
40
  def render
36
- @js
41
+ @js.render
37
42
  end
38
43
  end
39
44
  end
@@ -1,5 +1,6 @@
1
1
  puts <<-EOS
2
2
  usage: #{Xaases::NAME} <command> [options]
3
3
  init
4
- add <name> <columns...>
4
+ table <name> <columns...>
5
+ api <method> <path>
5
6
  EOS
data/lib/xaases/cli.rb CHANGED
@@ -1,30 +1,41 @@
1
1
  require 'xaases/cli/color'
2
+ require 'xaases/error'
2
3
  require 'xaases/conf'
3
4
  require 'xaases/serverless'
4
- require 'xaases/aws/dynamodb'
5
5
 
6
6
  cmd = ARGV.first
7
7
  options = ARGV.slice(1..-1)
8
8
 
9
9
  begin
10
10
  case cmd
11
- when 'add'
12
- Xaases.add *options
11
+ when 'table'
12
+ require 'xaases/aws/dynamodb'
13
+ Xaases.table *options
13
14
  sls = Xaases::Serverless::Yaml.new('serverless.yml', 'xaases')
14
- name = options.first + 'DynamoDbTable'
15
- sls.add_resource name, Xaases::Aws::Dynamodb.new(*options).to_hash
15
+ key = options.first + 'DynamoDbTable'
16
+ sls.add_resource key, Xaases::Aws::Dynamodb.new(*options).to_hash
16
17
  sls.write
17
- #`sls deploy`
18
- when 'func'
18
+
19
+ when 'api'
20
+ require 'xaases/aws/lambda_js'
21
+ Xaases.api *options
19
22
  sls = Xaases::Serverless::Yaml.new('serverless.yml', 'xaases')
20
- sls.add_function *options
23
+ name = sls.add_function *options
21
24
  sls.write
22
- #`sls deploy`
25
+
26
+ res = Xaases::Aws::LambdaJs.new(minify: false)
27
+ res.res(200, '"OK."')
28
+
29
+ js = Xaases::Aws::LambdaJs.new(minify: false)
30
+ js.export name, res.render
31
+ f = File.open 'handler.js', 'a'
32
+ f.write js.render
33
+
23
34
  when 'init', 'touch'
24
- require 'xaases/cli/init'
35
+ Xaases.init
25
36
  else
26
37
  require 'xaases/cli/help'
27
38
  end
28
- rescue => e
39
+ rescue Xaases::Error => e
29
40
  Xaases::Color.failure e.message
30
41
  end
data/lib/xaases/conf.rb CHANGED
@@ -3,11 +3,16 @@ require 'yaml'
3
3
  module Xaases
4
4
  CONF_NAME = NAME + '.yml'
5
5
 
6
+ def self.init
7
+ YAML.dump({ 'Tables' => {}, 'APIs' => {} }, File.open(CONF_NAME, 'w'))
8
+ Color.warn "Created #{CONF_NAME}"
9
+ end
10
+
6
11
  def self.load
7
12
  begin
8
13
  @@config = YAML.load_file(CONF_NAME) || {}
9
14
  rescue Errno::ENOENT
10
- raise "Require '#{CONF_NAME}' file, you need run at 'xaases init' command."
15
+ raise Error.new("Require '#{CONF_NAME}' file, you need run at 'xaases init' command.")
11
16
  end
12
17
  end
13
18
 
@@ -21,10 +26,17 @@ module Xaases
21
26
  dump
22
27
  end
23
28
 
24
- def self.add name, *columns
25
- edit do |c|
26
- puts Color.yellow "Add #{name}"
27
- c[name] = columns
29
+ def self.table name, *columns
30
+ edit do |config|
31
+ puts Color.yellow "Add table #{name}"
32
+ config['Tables'][name] = columns
33
+ end
34
+ end
35
+
36
+ def self.api verb, path
37
+ edit do |config|
38
+ puts Color.yellow "Add API: #{verb} #{path}"
39
+ config['APIs'][verb + ' ' + path] = []
28
40
  end
29
41
  end
30
42
  end
@@ -0,0 +1,3 @@
1
+ module Xaases
2
+ class Error < StandardError; end
3
+ end
@@ -8,6 +8,10 @@ module Xaases
8
8
  @s = ''
9
9
  end
10
10
 
11
+ def content=(s)
12
+ @s = s
13
+ end
14
+
11
15
  def render
12
16
  @s
13
17
  end
data/lib/xaases/js.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'xaases/js/function'
2
2
  module Xaases
3
3
  class Js
4
- def initialize
4
+ attr_reader :minify
5
+
6
+ def initialize(minify: true)
7
+ @minify = minify
5
8
  @s = ''
6
9
  end
7
10
 
@@ -16,13 +19,23 @@ module Xaases
16
19
  end
17
20
 
18
21
  def call(name, *params)
19
- @s += "#{name}(#{params.join(',')});"
22
+ if minify
23
+ @s += "#{name}(#{params.join(',')});"
24
+ else
25
+ @s += "#{name}(#{params.join(', ')});"
26
+ end
20
27
  end
21
28
 
22
29
  def hash(data)
23
- @s += '{' + data.map do |key, value|
24
- key.to_s + ':' + value.to_s
25
- end.join(',') + '}'
30
+ if minify
31
+ @s += '{' + data.map do |key, value|
32
+ key.to_s + ':' + value.to_s
33
+ end.join(',') + '}'
34
+ else
35
+ @s += '{ ' + data.map do |key, value|
36
+ key.to_s + ': ' + value.to_s
37
+ end.join(', ') + ' }'
38
+ end
26
39
  end
27
40
 
28
41
  def if(*conditions)
@@ -40,7 +53,12 @@ module Xaases
40
53
  end
41
54
 
42
55
  def export(name, f)
43
- @s += "module.exports.#{name}=(#{f.params.join(',')})=>{#{f.render}};"
56
+ if minify
57
+ @s += "module.exports.#{name}=(#{f.params.join(',')})=>{#{f.render}};"
58
+ else
59
+ content = f.render.split("\n").join("\n ")
60
+ @s += "module.exports.#{name} = (#{f.params.join(', ')}) => {\n #{content}\n};\n"
61
+ end
44
62
  end
45
63
 
46
64
  def render
@@ -27,11 +27,22 @@ module Xaases
27
27
  @data['resources']['Resources'][name] = rs
28
28
  end
29
29
 
30
- def add_function(file, name, path, method, fn)
31
- @data['functions'][file] = {
32
- 'handler' => "#{file}.#{name}",
30
+ def add_function(method, path)
31
+ method = method.downcase
32
+ file = 'index'
33
+ name = path.sub(/\A\//, '').sub(/\//, '_')
34
+ name = nil if name == ''
35
+
36
+ name = ['create', name].compact.join('_') if method == 'post'
37
+ name = ['update', name].compact.join('_') if method == 'put'
38
+ name = ['delete', name].compact.join('_') if method == 'delete'
39
+
40
+ name = 'root' if method == 'get' && !name
41
+ @data['functions'][name] = {
42
+ 'handler' => "handler.#{name}",
33
43
  'events' => [{ 'http' => { 'path' => path, 'method' => method }}]
34
44
  }
45
+ name
35
46
  end
36
47
 
37
48
  def write
@@ -1,3 +1,3 @@
1
1
  module Xaases
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xaases
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - myun2
@@ -76,15 +76,15 @@ files:
76
76
  - bin/setup
77
77
  - exe/xaases
78
78
  - lib/xaases.rb
79
+ - lib/xaases/api.rb
79
80
  - lib/xaases/aws.rb
80
81
  - lib/xaases/aws/dynamodb.rb
81
82
  - lib/xaases/aws/lambda_js.rb
82
83
  - lib/xaases/cli.rb
83
84
  - lib/xaases/cli/color.rb
84
85
  - lib/xaases/cli/help.rb
85
- - lib/xaases/cli/init.rb
86
86
  - lib/xaases/conf.rb
87
- - lib/xaases/function.rb
87
+ - lib/xaases/error.rb
88
88
  - lib/xaases/js.rb
89
89
  - lib/xaases/js/function.rb
90
90
  - lib/xaases/name.rb
@@ -1,5 +0,0 @@
1
- require 'fileutils'
2
- TMPL_NAME = Xaases::NAME + '.yml'
3
- FileUtils.touch(TMPL_NAME)
4
-
5
- attempt "Created #{TMPL_NAME}"
@@ -1,4 +0,0 @@
1
- module Xaases
2
- class Function
3
- end
4
- end