Skip to content

Commit

Permalink
feat: add github-workflow install script
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Mar 25, 2021
1 parent 3986b2f commit d923463
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bin/commands/github-workflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

exports.desc = 'wiby Github Workflow maintenance tools'

exports.builder = (yargs) => yargs
.commandDir('./github-workflow')
.demandCommand()
.help()
23 changes: 23 additions & 0 deletions bin/commands/github-workflow/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const fs = require('fs')
const path = require('path')

exports.desc = 'Install the bundled versions of the wiby workflows.'

exports.handler = async (params) => {
const packageRoot = process.env.INIT_CWD || process.cwd()

const workflowsPath = path.join(packageRoot, '.github', 'workflows')
const sourceWibyYaml = path.join(__dirname, '..', '..', '..', '.github', 'workflows', 'wiby.yaml')
const destWibyYaml = path.join(workflowsPath, 'wiby.yaml')

console.log(`Copying ${sourceWibyYaml} to ${workflowsPath}`)

if (!fs.existsSync(workflowsPath)) {
console.error(`${workflowsPath} folder does not exist.`)
process.exit(1)
}

fs.copyFileSync(sourceWibyYaml, destWibyYaml)
}
67 changes: 67 additions & 0 deletions test/cli/github-workflow-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const childProcess = require('child_process')
const fs = require('fs')
const path = require('path')
const tap = require('tap')

const gitFixture = require('../fixtures/git')

const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')

tap.test('github-workflow install command', async (tap) => {
tap.beforeEach(async () => {
gitFixture.init()
})

tap.test('should copy wiby.yaml to the .github/workflows folder', async (tap) => {
const workflowsPath = path.join(process.cwd(), '.github', 'workflows')
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
const contentsBefore = 'should be overwritten with new version'

fs.mkdirSync(workflowsPath, { recursive: true })
fs.writeFileSync(wibyYamlPath, contentsBefore)

childProcess.execSync(`${wibyCommand} github-workflow install`, {
env: {
...process.env,
INIT_CWD: ''
}
}).toString()

tap.notEqual(fs.readFileSync(wibyYamlPath).toString(), contentsBefore)
})

tap.test('should copy wiby.yaml to the ${INIT_CWD}/.github/workflows folder', async (tap) => {
const initCwd = path.join(process.cwd(), 'some-other-place')
const workflowsPath = path.join(initCwd, '.github', 'workflows')
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
const contentsBefore = 'should be overwritten with new version'

fs.mkdirSync(workflowsPath, { recursive: true })
fs.writeFileSync(wibyYamlPath, contentsBefore)

childProcess.execSync(`${wibyCommand} github-workflow install`, {
env: {
...process.env,
INIT_CWD: initCwd
}
}).toString()

tap.notEqual(fs.readFileSync(wibyYamlPath).toString(), contentsBefore)
})

tap.test('should throw when the workflows path does not exist', async (tap) => {
try {
childProcess.execSync(`${wibyCommand} github-workflow install`, {
env: {
...process.env,
INIT_CWD: ''
}
}).toString()
tap.fail('Should fail before reaching here')
} catch (err) {
tap.include(err.message, '/.github/workflows folder does not exist.')
}
})
})

0 comments on commit d923463

Please sign in to comment.