Skip to content

Commit 33b34c3

Browse files
committed
simple custom snapshot
1 parent b7ddcd1 commit 33b34c3

File tree

9 files changed

+91
-25
lines changed

9 files changed

+91
-25
lines changed

__tests__/__mocks__/examplePlugin.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
module.exports = (context) => {
1+
module.exports = context => {
22
// Register this plugin
33
context.addPlugin({
44
name: 'Example Plugin',
55
description: 'I help test plugins',
6-
snapshot: async (context) => {
6+
snapshot: async context => {
77
context.addedSnapshot = true
88
},
99
rules: {
1010
checkThing: {
1111
check: async (rule, context) => {
1212
return {
1313
pass: true,
14-
message: 'Yeah good check!'
14+
message: 'Yeah good check!',
1515
}
1616
},
1717
snapshot: async (rule, context) => [
1818
{
1919
prop: 'semver',
20-
value: '12.0.0'
21-
}
20+
value: '12.0.0',
21+
},
2222
],
2323
report: async (rule, context, report) => {
2424
// report.cliRules.push(['android', location, binaryVersion, desired])
2525
report.addCLI({
2626
binary: 'Android SDK',
2727
version: 10,
28-
desired: 12
28+
desired: 12,
2929
})
30-
}
30+
},
3131
},
3232
checkSecondThing: {
3333
check: async (rule, context) => {
3434
return {
3535
pass: false,
36-
message: 'Boooo failed check'
36+
message: 'Boooo failed check',
3737
}
38-
}
39-
}
40-
}
38+
},
39+
},
40+
},
4141
})
4242

4343
return context

__tests__/__mocks__/mockContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const mockContext = {
2424
spin: jest.fn(() => ({
2525
stop: jest.fn(),
2626
fail: jest.fn(),
27-
succeed: jest.fn()
27+
succeed: jest.fn(),
2828
})),
2929
table: jest.fn(),
3030
xmark: jest.fn(),

__tests__/command_helpers/checkRequirement.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,5 @@ describe('checkRequirement', () => {
222222
const result = await checkRequirement(rule, context)
223223
expect(result).toEqual([customError])
224224
})
225-
226225
})
227226
})

__tests__/command_helpers/checkRequirementPlugins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,4 @@ describe('checkRequirement Plugins', () => {
4444
const result = await checkRequirement(rule, mockContext)
4545
expect(result).toEqual([error])
4646
})
47-
4847
})

__tests__/command_helpers/updateRequirement.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { platform } from 'os'
22
import { toPairs } from 'ramda'
3-
import context from 'gluegun'
3+
const examplePlugin = require('examplePlugin')
4+
const context = examplePlugin(require('mockContext'))
45

56
import updateRequirement from '../../src/extensions/functions/updateRequirement'
67

@@ -121,7 +122,19 @@ describe('updateRequirement', () => {
121122
})
122123
})
123124

124-
describe('rule: !cli', () => {
125+
describe('rule is custom', () => {
126+
it('returns stuff', async () => {
127+
const requirement = toPairs({
128+
TestRequirement: [{ rule: 'custom', plugin: 'Example Plugin', name: 'checkThing' }],
129+
})[0]
130+
131+
const result = await updateRequirement(requirement, settings, context)
132+
expect(result).toEqual([["Setting checkThing 'semver' to '12.0.0'"]])
133+
expect(spinner.stop.mock.calls.length).toEqual(1)
134+
})
135+
})
136+
137+
describe('rule: !== cli || custom', () => {
125138
it('returns an empty array', async () => {
126139
const requirement = toPairs({
127140
Yarn: [

src/extensions/functions/checkRequirement.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ const checkDir = require('./checkDir')
1111
const checkFile = require('./checkFile')
1212
const checkShell = require('./checkShell')
1313
const skipRule = require('./skipRule')
14+
const findPluginInfo = require('./findPluginInfo')
1415

1516
module.exports = async (
1617
requirement: SolidarityRequirementChunk,
1718
context: SolidarityRunContext
1819
): Promise<void | object[]> => {
19-
const { head, tail, pipe, flatten, map, filter } = require('ramda')
20+
const { head, tail, pipe, flatten, map } = require('ramda')
2021

2122
const { print } = context
2223
const requirementName: string = head(requirement)
@@ -116,12 +117,9 @@ module.exports = async (
116117
return addFailure(rule.error || `'${rule.command}' output did not match '${rule.match}'`)
117118
}
118119
case 'custom':
119-
// find correct rule function
120-
const correctPlugin = head(filter(plugin => plugin.name === rule.plugin, context._pluginsList))
121-
if (correctPlugin === undefined) return addFailure(`Plugin not found '${rule.plugin}'`)
122-
const customChecker = correctPlugin.rules && correctPlugin.rules[rule.name] && correctPlugin.rules[rule.name].check
123-
if (correctPlugin && customChecker) {
124-
const customResult = await customChecker(rule, context)
120+
const customPluginRule = findPluginInfo(rule, context)
121+
if (customPluginRule.success) {
122+
const customResult = await customPluginRule.plugin.check(rule, context)
125123
if (customResult && customResult.pass) {
126124
return addSuccess(customResult.message)
127125
} else {
@@ -132,7 +130,7 @@ module.exports = async (
132130
return addFailure(rule.error || failMessage)
133131
}
134132
} else {
135-
return addFailure(`NOT FOUND: Custom rule from '${rule.plugin}' plugin with check function '${rule.name}'`)
133+
return addFailure(customPluginRule.message)
136134
}
137135
default:
138136
return addFailure('Encountered unknown rule')
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CustomRule, SolidarityRunContext, PluginFind } from '../../types'
2+
3+
const { head, filter } = require('ramda')
4+
5+
module.exports = (rule: CustomRule, context: SolidarityRunContext): PluginFind => {
6+
// find correct rule function
7+
const correctPlugin = head(filter(plugin => plugin.name === rule.plugin, context._pluginsList))
8+
if (correctPlugin === undefined) {
9+
return {
10+
success: false,
11+
message: `Plugin not found '${rule.plugin}'`,
12+
}
13+
} else {
14+
const customRule = correctPlugin.rules && correctPlugin.rules[rule.name]
15+
if (customRule) {
16+
return {
17+
success: true,
18+
plugin: customRule,
19+
}
20+
} else {
21+
return {
22+
success: false,
23+
message: `NOT FOUND: Custom rule from '${rule.plugin}' plugin with check function '${rule.name}'`,
24+
}
25+
}
26+
}
27+
}

src/extensions/functions/updateRequirement.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { SolidarityRequirement, SolidarityRunContext } from '../../types'
2+
const findPluginInfo = require('./findPluginInfo')
3+
24
module.exports = async (
35
requirement: SolidarityRequirement,
46
settings: object,
@@ -18,10 +20,11 @@ module.exports = async (
1820
// check each rule for requirement
1921
const ruleChecks = await map(async rule => {
2022
// skip if we can't update
21-
if (skipRule(rule.platform) || !rule.semver) return []
23+
if (skipRule(rule.platform)) return []
2224
switch (rule.rule) {
2325
// Handle CLI rule update
2426
case 'cli':
27+
if (!rule.semver) return []
2528
const updateResult = await checkCLIForUpdates(rule, context)
2629
ruleString = `Keep ${rule.binary} ${rule.semver}`
2730
if (updateResult) {
@@ -31,6 +34,27 @@ module.exports = async (
3134
spinner.succeed(ruleString)
3235
return []
3336
}
37+
case 'custom':
38+
const customPluginRule = findPluginInfo(rule, context)
39+
if (customPluginRule.success) {
40+
const customResult = await customPluginRule.plugin.snapshot(rule, context)
41+
const changes = customResult.map(patch => {
42+
rule[patch.prop] = patch.value
43+
return `'${patch.prop}' to '${patch.value}'`
44+
})
45+
46+
// report changes
47+
if (changes.length > 0) {
48+
const message = print.colors.green(`Setting ${rule.name} ${changes.join(', ')}`)
49+
spinner.succeed(message)
50+
return message
51+
} else {
52+
return []
53+
}
54+
} else {
55+
spinner.fail(customPluginRule.message)
56+
return customPluginRule.message
57+
}
3458
default:
3559
return []
3660
}

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ export interface CustomRule {
102102
// discriminated union for rule sets
103103
export type SolidarityRule = CLIRule | ENVRule | FSRule | ShellRule | CustomRule
104104

105+
export interface PluginFind {
106+
success: boolean
107+
message?: string
108+
plugin?: SolidarityRule
109+
}
110+
105111
export enum SolidarityOutputMode {
106112
MODERATE,
107113
VERBOSE,

0 commit comments

Comments
 (0)