Skip to content

Commit eb8020d

Browse files
authored
♥️ improve plugin flow and tests (#196)
1 parent d5ed9d4 commit eb8020d

File tree

4 files changed

+123
-5
lines changed

4 files changed

+123
-5
lines changed

__tests__/command_helpers/__snapshots__/createPlugin.ts.snap

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,81 @@
22

33
exports[`check result shape 1`] = `[Function]`;
44

5+
exports[`checking plugin paths Choice 1 - 'I do not want a generated rule file' 1`] = `
6+
Array [
7+
Array [
8+
".gitignore.ejs",
9+
".gitignore",
10+
],
11+
Array [
12+
"README.md.ejs",
13+
"README.md",
14+
],
15+
Array [
16+
"package.json.ejs",
17+
"package.json",
18+
],
19+
Array [
20+
"simple-plugin.js.ejs",
21+
"extensions/solidarity-nachos.js",
22+
],
23+
]
24+
`;
25+
26+
exports[`checking plugin paths Choice 2 - 'Just a simple rule template' 1`] = `
27+
Array [
28+
Array [
29+
".gitignore.ejs",
30+
".gitignore",
31+
],
32+
Array [
33+
"README.md.ejs",
34+
"README.md",
35+
],
36+
Array [
37+
"package.json.ejs",
38+
"package.json",
39+
],
40+
Array [
41+
"rules-template.json.ejs",
42+
"templates/solidarity-nachos-template.json",
43+
],
44+
Array [
45+
"simple-plugin.js.ejs",
46+
"extensions/solidarity-nachos.js",
47+
],
48+
]
49+
`;
50+
51+
exports[`checking plugin paths Choice 3 - 'Template + optional rules' 1`] = `
52+
Array [
53+
Array [
54+
".gitignore.ejs",
55+
".gitignore",
56+
],
57+
Array [
58+
"README.md.ejs",
59+
"README.md",
60+
],
61+
Array [
62+
"package.json.ejs",
63+
"package.json",
64+
],
65+
Array [
66+
"rules-template.json.ejs",
67+
"templates/solidarity-nachos-template.json",
68+
],
69+
Array [
70+
"helpful-plugin.js.ejs",
71+
"extensions/solidarity-nachos.js",
72+
],
73+
Array [
74+
"addOptionalRules.js.ejs",
75+
"extensions/helpers/addOptionalRules.js",
76+
],
77+
]
78+
`;
79+
580
exports[`investigate createPlugin 1`] = `
681
Array [
782
Array [

__tests__/command_helpers/createPlugin.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,43 @@ test('investigate createPlugin', async () => {
1313
expect(context.prompt.confirm).toBeCalled()
1414
expect(context.print.success).toBeCalled()
1515
})
16+
17+
describe('checking plugin paths', () => {
18+
19+
beforeEach(() => {
20+
const mockedPrompt = jest
21+
.fn()
22+
.mockImplementationOnce(() => Promise.resolve({ plugin: 'Nachos' }))
23+
.mockImplementationOnce(() => Promise.resolve({ pluginDesc: 'das nachos plugin' }))
24+
25+
context.prompt.ask = mockedPrompt
26+
})
27+
28+
test(`Choice 1 - 'I do not want a generated rule file'`, async () => {
29+
context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
30+
ruleChoice: 'I do not want a generated rule file'
31+
}))
32+
33+
const result = await createPlugin(context)
34+
expect(result).toMatchSnapshot()
35+
})
36+
37+
test(`Choice 2 - 'Just a simple rule template'`, async () => {
38+
context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
39+
ruleChoice: 'Just a simple rule template'
40+
}))
41+
42+
const result = await createPlugin(context)
43+
expect(result).toMatchSnapshot()
44+
})
45+
46+
test(`Choice 3 - 'Template + optional rules'`, async () => {
47+
context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
48+
ruleChoice: 'Template + optional rules'
49+
}))
50+
51+
const result = await createPlugin(context)
52+
expect(result).toMatchSnapshot()
53+
})
54+
55+
})

src/extensions/functions/createPlugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = async context => {
99
message: 'Plugin name? (we will add the namespacing for you)',
1010
})
1111
if (!answerPluginName.plugin) throw Error('A plugin requires a name')
12-
const pluginName = `solidarity-${answerPluginName.plugin.replace('solidarity-', '')}`
12+
const pluginName = `solidarity-${answerPluginName.plugin.replace('solidarity-', '').toLowerCase()}`
1313

1414
const description = await prompt.ask({
1515
type: 'input',
@@ -29,7 +29,10 @@ module.exports = async context => {
2929
choices: ruleChoices,
3030
})
3131

32-
if (answer.ruleChoice === ruleChoices[1]) {
32+
const noRuleTemplate = answer.ruleChoice === ruleChoices[0]
33+
if (noRuleTemplate) {
34+
files.push(['simple-plugin.js.ejs', `extensions/${pluginName}.js`])
35+
} else if (answer.ruleChoice === ruleChoices[1]) {
3336
files.push(['rules-template.json.ejs', `templates/${pluginName}-template.json`])
3437
files.push(['simple-plugin.js.ejs', `extensions/${pluginName}.js`])
3538
} else if (answer.ruleChoice === ruleChoices[2]) {
@@ -46,7 +49,7 @@ module.exports = async context => {
4649
template.generate({
4750
template: fileSet[0],
4851
target: `${pluginName}/${fileSet[1]}`,
49-
props: { pluginName, customRules, description: description.pluginDesc },
52+
props: { pluginName, customRules, description: description.pluginDesc, noRuleTemplate },
5053
})
5154
})
5255

src/templates/simple-plugin.js.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module.exports = (context) => {
22
// Register this plugin
33
context.addPlugin({
44
name: '<%= props.pluginName %>',
5-
description: '<%= props.description %>',
6-
snapshot: '<%= props.pluginName %>-template.json'<% if(props.customRules){ %>,
5+
description: '<%= props.description %>'<% if(!props.noRuleTemplate){ %>,
6+
snapshot: '<%= props.pluginName %>-template.json'<% } %><% if(props.customRules){ %>,
77
rules: {
88
ruleName: {
99
check: async (rule, context) => {

0 commit comments

Comments
 (0)