-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(core): fix an issue where "no command specified" error was shown erroneously * feat(core): add `serve` command for serving project matching branch * chore(tests): add initial tests for `serve` and deploy actions
- Loading branch information
1 parent
5fd3ca7
commit d1ee3dd
Showing
9 changed files
with
198 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* deploy commander component | ||
* To use add require('../cmds/deploy.js')(program) to your commander.js based node executable before program.parse | ||
*/ | ||
'use strict' | ||
const serve = require('../lib/actions/serve').default | ||
|
||
/** | ||
* @name serve | ||
* Serve project using project matching associated branch | ||
* @param {object} program - Commander program object | ||
* @example <caption>Basic</caption> | ||
* # make sure FIREBASE_TOKEN env variable is set | ||
* firebase-ci deploy | ||
*/ | ||
module.exports = function(program) { | ||
program | ||
.command('serve') | ||
.description( | ||
'Use firebase serve to serve a project matching branch name settings in .firebaserc' | ||
) | ||
.option('-d --debug', 'Enable extra logging') // taken by autocmdr | ||
.option( | ||
'-o --only <targets>', | ||
'Only serve specified targets, comma-seperated (e.g "hosting, storage")' | ||
) | ||
.action(opts => { | ||
return serve(opts) | ||
.then(() => process.exit(0)) | ||
.catch(() => process.exit(1)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { get } from 'lodash' | ||
import chalk from 'chalk' | ||
import { error, info, warn } from '../utils/logger' | ||
import { getFile } from '../utils/files' | ||
import { to } from '../utils/async' | ||
import { runCommand } from '../utils/commands' | ||
import { getProjectKey, getFallbackProjectKey } from '../utils/ci' | ||
|
||
const skipPrefix = 'Skipping firebase-ci serve' | ||
|
||
/** | ||
* Serve specific project | ||
* @param {object} opts - Settings for serving | ||
* @returns {Promise} Resolves after serve is called | ||
*/ | ||
export default async function serve(opts) { | ||
// Load settings from .firebaserc | ||
const settings = getFile('.firebaserc') | ||
|
||
// Get project from passed options, falling back to branch name | ||
const fallbackProjectName = getFallbackProjectKey() | ||
const projectKey = getProjectKey(opts) | ||
|
||
// Get project setting from settings file based on branchName falling back | ||
// to fallbackProjectName | ||
const projectName = get(settings, `projects.${projectKey}`) | ||
const fallbackProjectSetting = get( | ||
settings, | ||
`projects.${fallbackProjectName}` | ||
) | ||
|
||
// Handle project option | ||
if (!projectName) { | ||
const nonProjectBranch = `${skipPrefix} - Project ${chalk.cyan( | ||
projectKey | ||
)} is not an alias, checking for fallback...` | ||
warn(nonProjectBranch) | ||
if (!fallbackProjectSetting) { | ||
const nonFallbackBranch = `${skipPrefix} - Fallback Project: ${chalk.cyan( | ||
fallbackProjectName | ||
)} is a not an alias, exiting...` | ||
warn(nonFallbackBranch) | ||
return nonProjectBranch | ||
} | ||
return null | ||
} | ||
|
||
info( | ||
`Calling serve for project ${chalk.cyan(projectName)} (alias ${chalk.cyan( | ||
projectKey | ||
)})` | ||
) | ||
|
||
const serveArgs = ['serve', '-P', projectKey] | ||
const onlyString = opts && opts.only ? `--only ${opts.only}` : '' | ||
if (onlyString) { | ||
serveArgs.push(onlyString) | ||
} | ||
|
||
// Run command to set functions config | ||
const [serveErr] = await to( | ||
runCommand({ | ||
command: 'firebase', | ||
args: serveArgs | ||
}) | ||
) | ||
|
||
// Handle errors running functions config | ||
if (serveErr) { | ||
const errMsg = `Error calling serve for ${chalk.cyan( | ||
projectName | ||
)} (alias ${chalk.cyan(projectKey)}) :` | ||
error(errMsg, serveErr) | ||
throw new Error(errMsg) | ||
} | ||
|
||
info( | ||
`Successfully called serve for project ${chalk.cyan( | ||
projectName | ||
)} (alias ${chalk.cyan(projectKey)})` | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import deploy from 'actions/deploy' | ||
|
||
describe('deploy action', () => { | ||
let logSpy | ||
beforeEach(() => { | ||
logSpy = sinon.spy(console, 'log') | ||
}) | ||
|
||
afterEach(() => { | ||
logSpy && logSpy.restore() | ||
}) | ||
|
||
it('exports a function to be used as a command', () => { | ||
expect(deploy).to.be.an('function') | ||
}) | ||
|
||
it('Logs error saying no config found for provided alias', async () => { | ||
await deploy({ project: 'asdf' }) | ||
// first time with: 'ℹ Skipping Firebase Deploy - Project asdf is not an alias, checking for fallback...' | ||
// second time with: 'ℹ Skipping Firebase Deploy - Fallback Project: undefined is a not an alias, exiting...' | ||
expect(logSpy).to.have.been.calledTwice | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import serve from 'actions/serve' | ||
|
||
describe('serve action', () => { | ||
let logSpy | ||
beforeEach(() => { | ||
logSpy = sinon.spy(console, 'log') | ||
}) | ||
|
||
afterEach(() => { | ||
logSpy && logSpy.restore() | ||
}) | ||
|
||
it('exports a function to be used as a command', () => { | ||
expect(serve).to.be.an('function') | ||
}) | ||
|
||
it('Logs error saying no config found for provided alias', async () => { | ||
await serve({ project: 'asdf' }) | ||
// first time with: '⚠ Warning: Skipping firebase-ci serve - Project asdf is not an alias, checking for fallback...' | ||
// second time with: '⚠ Warning: Skipping firebase-ci serve - Fallback Project: undefined is a not an alias, exiting...' | ||
expect(logSpy).to.have.been.calledTwice | ||
}) | ||
}) |