Skip to content

Commit

Permalink
feat(core): magic_dollar plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 8, 2021
1 parent deab032 commit 77df829
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 148 deletions.
41 changes: 41 additions & 0 deletions packages/core/lib/plugins/magic_dollar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ create = ->
require './plugins/execute'
require './plugins/global'
require './plugins/history'
require './plugins/magic_dollar'
require './plugins/pubsub'
require './plugins/output_logs'
require './plugins/schema'
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/plugins/magic_dollar.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


###
The `magic_dollar` plugin extract all variables starting with a dollar sign.
###

{is_object_literal} = require 'mixme'

module.exports =
name: '@nikitajs/core/src/plugins/magic_dollar'
hooks:
'nikita:normalize':
handler: (action) ->
for k, v of action
continue unless k[0] is '$'
prop = k.substr 1
switch prop
when 'handler'
action.handler = v
when 'parent'
action.parent = v
when 'scheduler'
action.scheduler = v
else
action.metadata[prop] = v
delete action[k]

6 changes: 2 additions & 4 deletions packages/core/test/plugins/argument_to_config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ describe 'plugins.argument', ->

it 'enrich config', ->
nikita
metadata:
argument_to_config: 'my_key'
$argument_to_config: 'my_key'
, 'my value', ({config}) ->
config.should.eql my_key: 'my value'

it 'dont overwrite config', ->
nikita
metadata:
argument_to_config: 'my_key'
$argument_to_config: 'my_key'
my_key: 'my original value'
, 'my new value', ({config}) ->
config.should.eql my_key: 'my original value'
39 changes: 18 additions & 21 deletions packages/core/test/plugins/execute.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,30 @@ describe 'plugins.execute', ->
describe 'usage', ->

it 'supported properties', ->
nikita
metadata:
arch_chroot: true
arch_chroot_rootdir: '/tmp'
bash: true
dry: true
env: key: 'value'
env_export: true
sudo: true
config = await nikita
$arch_chroot: true
$arch_chroot_rootdir: '/tmp'
$bash: true
$dry: true
$env: key: 'value'
$env_export: true
$sudo: true
, ->
@execute 'fake cmd', ({config}) ->
config.arch_chroot.should.eql true
config.arch_chroot_rootdir.should.eql '/tmp'
config.bash.should.eql true
config.dry.should.eql true
config.env.should.containEql key: 'value'
config.env_export.should.eql true
config.sudo.should.eql true
@execute 'fake cmd', ({config}) -> config
config.arch_chroot.should.eql true
config.arch_chroot_rootdir.should.eql '/tmp'
config.bash.should.eql true
config.dry.should.eql true
config.env.should.containEql key: 'value'
config.env_export.should.eql true
config.sudo.should.eql true

describe 'env', ->

they 'merge parent metadata with config', ({ssh}) ->
nikita
ssh: ssh
metadata:
env: 'NIKITA_PROCESS_ENV_1': '1'
$env: 'NIKITA_PROCESS_ENV_1': '1'
, ->
{env} = await @execute
command: 'env'
Expand All @@ -47,8 +45,7 @@ describe 'plugins.execute', ->
they 'process.env disabled if some env are provided', ({ssh}) ->
nikita
ssh: ssh
metadata:
env: 'NIKITA_PROCESS_ENV': '1'
$env: 'NIKITA_PROCESS_ENV': '1'
, ->
process.env['NIKITA_PROCESS_ENV'] = '1'
{stdout} = await @execute
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/plugins/global.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe 'plugins.global', ->
my_key: 'my value'
, ->
@call ->
@call metadata: global: 'my_global', ({config}) ->
@call $global: 'my_global', ({config}) ->
config.should.eql
my_key: 'my value'

Expand All @@ -21,14 +21,14 @@ describe 'plugins.global', ->
my_global:
my_key: 'my value'
, ->
@call metadata: global: 'my_global', ({config}) ->
@call $global: 'my_global', ({config}) ->
config.should.eql
my_key: 'my value'

it 'merge from current', ->
nikita ->
@call
metadata: global: 'my_global'
$global: 'my_global'
my_global:
my_key: 'my value'
, ({config}) ->
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/plugins/magic_dollar.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

{tags} = require '../test'
nikita = require '../../src'

describe 'plugins.magic_dollar', ->
return unless tags.api

it 'extract metadata', ->
metadata = await nikita
$key: 'value'
, ({metadata}) ->
metadata
metadata.should.containEql key: 'value'
8 changes: 4 additions & 4 deletions packages/core/test/plugins/metadata/attempt.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ describe 'plugins.metadata.attempt (plugin.retry)', ->

it 'ensure attempt equals or is greater than 0', ->
nikita
.call metadata: attempt: 0, (->)
.call metadata: attempt: 1, (->)
.call metadata: attempt: -1, (->)
.call $attempt: 0, (->)
.call $attempt: 1, (->)
.call $attempt: -1, (->)
.should.be.rejectedWith [
'METADATA_ATTEMPT_INVALID_RANGE:'
'configuration `attempt` expect a number above or equal to 0,'
Expand All @@ -26,7 +26,7 @@ describe 'plugins.metadata.attempt (plugin.retry)', ->

it 'follow the number of retry', ->
count = 0
nikita.call metadata: retry: 5, sleep: 0, ({metadata}) ->
nikita.call $retry: 5, sleep: 0, ({metadata}) ->
metadata.attempt.should.eql count++
throw Error 'Catchme' if metadata.attempt < 4

Expand Down
18 changes: 9 additions & 9 deletions packages/core/test/plugins/metadata/debug.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe 'metadata "debug"', ->
data = []
ws = new stream.Writable()
ws.write = (chunk) -> data.push chunk
await nikita.call metadata: debug: ws, ({tools: {log}}) ->
await nikita.call $debug: ws, ({tools: {log}}) ->
log 'Some message'
data.join().should.eql '\u001b[32m[1.1.INFO call] Some message\u001b[39m\n'

Expand All @@ -24,7 +24,7 @@ describe 'metadata "debug"', ->
command: """
echo to_stdout; echo to_stderr 1>&2
"""
metadata: debug: ws
$debug: ws
data.join().should.eql [
'\u001b[33m[1.1.INFO execute] echo to_stdout; echo to_stderr 1>&2\u001b[39m\n'
'\u001b[36m[1.1.INFO execute] to_stdout\u001b[39m\n'
Expand All @@ -37,7 +37,7 @@ describe 'metadata "debug"', ->
data = []
write = process.stderr.write
process.stderr.write = (chunk) -> data.push chunk
await nikita.call metadata: debug: true, ({tools: {log}}) ->
await nikita.call $debug: true, ({tools: {log}}) ->
log 'Some message'
process.stderr.write = write
data.join().should.eql '\u001b[32m[1.1.INFO call] Some message\u001b[39m\n'
Expand All @@ -46,7 +46,7 @@ describe 'metadata "debug"', ->
data = []
write = process.stdout.write
process.stdout.write = (chunk) -> data.push chunk
await nikita.call metadata: debug: 'stdout', ({tools: {log}}) ->
await nikita.call $debug: 'stdout', ({tools: {log}}) ->
log 'Some message'
process.stdout.write = write
data.join().should.eql '\u001b[32m[1.1.INFO call] Some message\u001b[39m\n'
Expand All @@ -55,7 +55,7 @@ describe 'metadata "debug"', ->
data = []
ws = new stream.Writable()
ws.write = (chunk) -> data.push chunk
await nikita.call metadata: debug: ws, ({tools: {log}}) ->
await nikita.call $debug: ws, ({tools: {log}}) ->
log 'Some message'
data.join().should.eql '\u001b[32m[1.1.INFO call] Some message\u001b[39m\n'

Expand All @@ -65,7 +65,7 @@ describe 'metadata "debug"', ->
data = []
ws = new stream.Writable()
ws.write = (chunk) -> data.push chunk
await nikita.call metadata: debug: ws, ({tools: {log}}) ->
await nikita.call $debug: ws, ({tools: {log}}) ->
log 'Parent message'
@call (->)
@call ({tools: {log}}) ->
Expand All @@ -81,7 +81,7 @@ describe 'metadata "debug"', ->
ws.write = (chunk) -> data.push chunk
await nikita.call ({tools: {log}}) ->
log 'Parent message'
@call metadata: debug: ws, ({tools: {log}}) ->
@call $debug: ws, ({tools: {log}}) ->
log 'Child message'
data.join().should.eql '\u001b[32m[1.1.1.INFO call] Child message\u001b[39m\n'

Expand All @@ -92,7 +92,7 @@ describe 'metadata "debug"', ->
await nikita
.call ->
@call (->)
@call metadata: debug: ws, ({tools: {log}}) ->
@call $debug: ws, ({tools: {log}}) ->
log 'Child message'
.call ({tools: {log}}) ->
log 'Sibling message'
Expand All @@ -102,7 +102,7 @@ describe 'metadata "debug"', ->

it 'invalid string', ->
nikita
.call metadata: debug: 'oh no', (->)
.call $debug: 'oh no', (->)
.should.be.rejectedWith [
'METADATA_DEBUG_INVALID_VALUE:'
'configuration `debug` expect a boolean value,'
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/plugins/metadata/disabled.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ describe 'plugins.metadata.disabled', ->

it 'default', ->
nikita.call
metadata: disabled: false
$disabled: false
, ({metadata: {disabled}}) ->
disabled.should.be.false()

it 'when `true`', ->
nikita.call
metadata: disabled: true
$disabled: true
handler: -> throw Error 'forbidden'

it 'when `false`', ->
nikita.call
metadata: disabled: false
$disabled: false
handler: -> 'called'
.should.be.resolvedWith 'called'

Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/plugins/metadata/raw.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe 'plugins.metadata.raw', ->
describe 'arguments', ->

it 'pass `true` as is', ->
nikita true, metadata: raw_input: true, (action) ->
nikita true, $raw_input: true, (action) ->
config: action.config
argument: action.metadata.argument
.should.be.finally.containEql
Expand All @@ -49,15 +49,15 @@ describe 'plugins.metadata.raw', ->
describe 'output', ->

it 'leave `true` as is', ->
nikita.call metadata: raw_output: true, -> true
nikita.call $raw_output: true, -> true
.should.be.resolvedWith true

it 'leave `false` as is', ->
nikita.call metadata: raw_output: true, -> true
nikita.call $raw_output: true, -> true
.should.be.resolvedWith true

it 'leave `{}` as is', ->
nikita.call metadata: raw_output: true,-> {}
nikita.call $raw_output: true,-> {}
.should.be.resolvedWith {}


Loading

0 comments on commit 77df829

Please sign in to comment.