Skip to content

Commit 3a34f43

Browse files
authored
Merge pull request #86 from pkgjs/workflow-commands
2 parents b7c5ef3 + dba9e00 commit 3a34f43

File tree

8 files changed

+250
-7
lines changed

8 files changed

+250
-7
lines changed

USAGE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ Options:
2222
```
2323

2424

25+
## `wiby github-workflow install`
26+
27+
28+
29+
Install the bundled versions of the wiby workflows. Will overwrite existing
30+
`.github/workflows/wiby.yaml`, if any.
31+
32+
33+
34+
35+
## `wiby github-workflow outdated`
36+
37+
38+
39+
Check if you have the bundled version of wiby Github workflow installed. Will
40+
exit with zero if .github/workflows/wiby.yaml is up to date, and non-zero if it
41+
is outdated.
42+
43+
44+
45+
2546
## `wiby result`
2647

2748

bin/commands/github-workflow.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
exports.desc = 'wiby Github Workflow maintenance tools'
4+
5+
exports.builder = (yargs) => yargs
6+
.commandDir('./github-workflow')
7+
.demandCommand()
8+
.help()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
exports.desc = 'Install the bundled versions of the wiby workflows. Will overwrite existing `.github/workflows/wiby.yaml`, if any.'
7+
8+
exports.handler = async (params) => {
9+
const packageRoot = process.env.INIT_CWD || process.cwd()
10+
11+
const workflowsPath = path.join(packageRoot, '.github', 'workflows')
12+
const sourceWibyYaml = path.join(__dirname, '..', '..', '..', '.github', 'workflows', 'wiby.yaml')
13+
const destWibyYaml = path.join(workflowsPath, 'wiby.yaml')
14+
15+
console.log(`Copying ${sourceWibyYaml} to ${workflowsPath}`)
16+
17+
if (!fs.existsSync(workflowsPath)) {
18+
console.error(`${workflowsPath} folder does not exist.`)
19+
process.exit(1)
20+
}
21+
22+
fs.copyFileSync(sourceWibyYaml, destWibyYaml)
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
exports.desc = 'Check if you have the bundled version of wiby Github workflow installed. Will exit with zero if .github/workflows/wiby.yaml is up to date, and non-zero if it is outdated.'
7+
8+
exports.handler = async (params) => {
9+
const packageRoot = process.env.INIT_CWD || process.cwd()
10+
11+
const workflowsPath = path.join(packageRoot, '.github', 'workflows')
12+
const sourceWibyYaml = path.join(__dirname, '..', '..', '..', '.github', 'workflows', 'wiby.yaml')
13+
const destWibyYaml = path.join(workflowsPath, 'wiby.yaml')
14+
15+
if (!fs.existsSync(destWibyYaml)) {
16+
console.error(`${destWibyYaml} not found. Use \`wiby github-workflow install\` to install it.`)
17+
process.exit(1)
18+
}
19+
20+
const expectedContents = fs.readFileSync(sourceWibyYaml)
21+
const actualContents = fs.readFileSync(destWibyYaml)
22+
23+
if (Buffer.compare(expectedContents, actualContents) !== 0) {
24+
console.error(`${destWibyYaml} is not the same as the bundled version at ${sourceWibyYaml}. Use \`wiby github-workflow install\` to install it.`)
25+
process.exit(1)
26+
}
27+
28+
console.log(`${destWibyYaml} is the same as the bundled version.`)
29+
}

bin/generate-usage.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const fs = require('fs')
1515
/**
1616
* Parses wiby --help output and returns block with commands list
1717
*/
18-
const getGeneralHelpOutput = () => {
19-
const helpOutput = execSync(`${wibyCommand} --help`, { cwd: cwd }).toString()
18+
const getGeneralHelpOutput = (commandWithSubCommands = '') => {
19+
const helpOutput = execSync(`${wibyCommand} ${commandWithSubCommands}--help`, { cwd: cwd }).toString()
2020
const helpParseResult = /(Commands:\n)([\S\s]+)(Options:)/.exec(helpOutput)
2121

2222
if (helpParseResult !== null) {
@@ -29,13 +29,20 @@ const getGeneralHelpOutput = () => {
2929
/**
3030
* Parses commands list block ands returns array of commands names
3131
*/
32-
const parseCommandsListOutput = (commandsOutput) => {
32+
const parseCommandsListOutput = (commandsOutput, commandWithSubCommands = '') => {
3333
// parse commands list
34-
const re = /^ {2}wiby ([\w]+)/gm
34+
const re = new RegExp(`^ {2}wiby ${commandWithSubCommands}([\\w-]+)`, 'gm')
3535
const commandsList = []
3636
let commandsParseResult
3737
while ((commandsParseResult = re.exec(commandsOutput)) !== null) {
38-
commandsList.push(commandsParseResult[1])
38+
const [, command] = commandsParseResult
39+
if (command === 'github-workflow') {
40+
const subCommandsOutput = getGeneralHelpOutput(`${command} `)
41+
const subCommandList = parseCommandsListOutput(subCommandsOutput, `${command} `)
42+
commandsList.push(...subCommandList.map((subCommand) => `${command} ${subCommand}`))
43+
} else {
44+
commandsList.push(command)
45+
}
3946
}
4047

4148
return commandsList
@@ -66,7 +73,7 @@ const renderCommand = (command) => {
6673
return `
6774
## \`wiby ${command.get('commandName')}\`
6875
69-
${command.get('help').replace(/^wiby.*$/m, '').replace(/Options:(.+)$/s, '```\n$&\n```')}
76+
${command.get('help').replace(/^wiby.*$/m, '').replace(/Options:\s+$/, '').replace(/Options:(.+)$/s, '```\n$&\n```')}
7077
`
7178
}
7279

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
const childProcess = require('child_process')
4+
const fs = require('fs')
5+
const path = require('path')
6+
const tap = require('tap')
7+
8+
const gitFixture = require('../fixtures/git')
9+
10+
const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')
11+
12+
tap.test('github-workflow install command', async (tap) => {
13+
tap.beforeEach(async () => {
14+
gitFixture.init()
15+
})
16+
17+
tap.test('should copy wiby.yaml to the .github/workflows folder', async (tap) => {
18+
const workflowsPath = path.join(process.cwd(), '.github', 'workflows')
19+
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
20+
const contentsBefore = 'should be overwritten with new version'
21+
22+
fs.mkdirSync(workflowsPath, { recursive: true })
23+
fs.writeFileSync(wibyYamlPath, contentsBefore)
24+
25+
childProcess.execSync(`${wibyCommand} github-workflow install`, {
26+
env: {
27+
...process.env,
28+
INIT_CWD: ''
29+
}
30+
})
31+
32+
tap.notEqual(fs.readFileSync(wibyYamlPath).toString(), contentsBefore)
33+
})
34+
35+
tap.test('should copy wiby.yaml to the $INIT_CWD/.github/workflows folder', async (tap) => {
36+
const initCwd = path.join(process.cwd(), 'some-other-place')
37+
const workflowsPath = path.join(initCwd, '.github', 'workflows')
38+
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
39+
const contentsBefore = 'should be overwritten with new version'
40+
41+
fs.mkdirSync(workflowsPath, { recursive: true })
42+
fs.writeFileSync(wibyYamlPath, contentsBefore)
43+
44+
childProcess.execSync(`${wibyCommand} github-workflow install`, {
45+
env: {
46+
...process.env,
47+
INIT_CWD: initCwd
48+
}
49+
})
50+
51+
tap.notEqual(fs.readFileSync(wibyYamlPath).toString(), contentsBefore)
52+
})
53+
54+
tap.test('should throw when the workflows path does not exist', async (tap) => {
55+
try {
56+
childProcess.execSync(`${wibyCommand} github-workflow install`, {
57+
env: {
58+
...process.env,
59+
INIT_CWD: ''
60+
}
61+
})
62+
tap.fail('Should fail before reaching here')
63+
} catch (err) {
64+
tap.include(err.message, '/.github/workflows folder does not exist.')
65+
}
66+
})
67+
})
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict'
2+
3+
const childProcess = require('child_process')
4+
const fs = require('fs')
5+
const path = require('path')
6+
const tap = require('tap')
7+
8+
const gitFixture = require('../fixtures/git')
9+
10+
const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')
11+
12+
tap.test('github-workflow outdated command', async (tap) => {
13+
tap.beforeEach(async () => {
14+
gitFixture.init()
15+
})
16+
17+
tap.test('should fail when wiby.yaml is missing', async (tap) => {
18+
try {
19+
childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
20+
env: {
21+
...process.env,
22+
INIT_CWD: ''
23+
}
24+
})
25+
tap.fail('Should fail before reaching here')
26+
} catch (err) {
27+
tap.include(err.message, '/.github/workflows/wiby.yaml not found. Use `wiby github-workflow install` to install it.')
28+
}
29+
})
30+
31+
tap.test('should fail when wiby.yaml has the wrong contents', async (tap) => {
32+
const workflowsPath = path.join(process.cwd(), '.github', 'workflows')
33+
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
34+
const contentsBefore = 'should be overwritten with new version'
35+
36+
fs.mkdirSync(workflowsPath, { recursive: true })
37+
fs.writeFileSync(wibyYamlPath, contentsBefore)
38+
39+
try {
40+
childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
41+
env: {
42+
...process.env,
43+
INIT_CWD: ''
44+
}
45+
})
46+
tap.fail('Should fail before reaching here')
47+
} catch (err) {
48+
tap.include(err.message, '/.github/workflows/wiby.yaml is not the same as the bundled version')
49+
}
50+
})
51+
52+
tap.test('should pass when wiby.yaml has the same contents', async (tap) => {
53+
const originalContents = fs.readFileSync(path.join(__dirname, '..', '..', '.github', 'workflows', 'wiby.yaml'))
54+
const workflowsPath = path.join(process.cwd(), '.github', 'workflows')
55+
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
56+
57+
fs.mkdirSync(workflowsPath, { recursive: true })
58+
fs.writeFileSync(wibyYamlPath, originalContents)
59+
60+
const result = childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
61+
env: {
62+
...process.env,
63+
INIT_CWD: ''
64+
}
65+
}).toString()
66+
67+
tap.include(result, 'wiby.yaml is the same as the bundled version.')
68+
})
69+
70+
tap.test('should pass when wiby.yaml has the same contents in $INIT_CWD', async (tap) => {
71+
const originalContents = fs.readFileSync(path.join(__dirname, '..', '..', '.github', 'workflows', 'wiby.yaml'))
72+
const initCwd = path.join(process.cwd(), 'some-other-place')
73+
const workflowsPath = path.join(initCwd, '.github', 'workflows')
74+
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
75+
76+
fs.mkdirSync(workflowsPath, { recursive: true })
77+
fs.writeFileSync(wibyYamlPath, originalContents)
78+
79+
const result = childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
80+
env: {
81+
...process.env,
82+
INIT_CWD: initCwd
83+
}
84+
}).toString()
85+
86+
tap.include(result, 'wiby.yaml is the same as the bundled version.')
87+
})
88+
})

test/cli/result.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tap.test('result command', async (tap) => {
1919

2020
tap.test('result command should fail when config and dependent provided', async (tap) => {
2121
try {
22-
childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`).toString()
22+
childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`)
2323
tap.fail()
2424
} catch (err) {
2525
tap.equal(true, err.message.includes('Arguments dependent and config are mutually exclusive'))

0 commit comments

Comments
 (0)