Skip to content

Commit

Permalink
Merge pull request #4 from tomyitav/deploy-command
Browse files Browse the repository at this point in the history
Deploy command
  • Loading branch information
tomyitav authored Dec 7, 2018
2 parents 3a5948f + 15131fa commit b5991ae
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ To generate a matching resolver file for type file, execute the command:
This will create a matching file, with all Query, Mutation and Subscription
definitions.

### Deploy server to production

Inside the project directory, type:

```gga d```

This will run the server *deploy* script, and will move the server to production! :rocket:

### Extended documentation

Please check out the [extended documentation](https://tomyitav.github.io/generate-graphql-app) for more information
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/commands/all-commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AbstractCommand } from './abstract-command'
import { Init } from './init'
import { Resolver } from './resolver/resolver'
import { Deploy } from './deploy'

const allCommands: AbstractCommand[] = [new Init(), new Resolver()]
const allCommands: AbstractCommand[] = [new Init(), new Resolver(), new Deploy()]

export default allCommands
43 changes: 43 additions & 0 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { AbstractCommand } from './abstract-command'
import { shell } from '../utils/shell'
import { fileExists } from '../utils/file-operations'
import * as path from 'path'

export class Deploy extends AbstractCommand {
public getName(): string {
return 'deploy [project-path]'
}

public getDescription(): string {
return 'Deploy project to production'
}

public getAlias(): string {
return 'd'
}

public getAction(): (...args: any[]) => void {
return async (projectPath: string) => {
try {
const pathToAppend = projectPath ? projectPath : ''
const pkgJsonFileExist = await fileExists(path.join(pathToAppend, 'package.json'))
if (!pkgJsonFileExist) {
console.error('No package.json file found, exiting...')
return
}
console.log('Deploying server to production...')
if (pathToAppend !== '') {
process.chdir(pathToAppend)
}
await shell(this.getDeployCommand())
console.log('Project successfully deployed!')
} catch (err) {
console.error('Got error in deploy command- ', err)
}
}
}

private getDeployCommand(): string {
return `npm run deploy`
}
}
44 changes: 44 additions & 0 deletions test/commands/deploy/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AbstractCommand } from '../../../src/commands/abstract-command'
import { Deploy } from '../../../src/commands/deploy'
import * as path from 'path'

describe('test for deploy command', () => {
let deploy: AbstractCommand
const pathToProjectDir = './test/commands/deploy/inner-dir'
const pathToProjectNoPackage = './test/commands/deploy/inner-dir-no-package'
const absoluteProcessDir = path.join(process.cwd(), pathToProjectDir)
const absoluteProcessDirNoPackage = path.join(process.cwd(), pathToProjectNoPackage)

beforeAll(() => {
deploy = new Deploy()
})

it('works if action returns a function', () => {
const act = deploy.getAction()
expect(act).toBeInstanceOf(Function)
})

it('works if deploy command is executed in directory', async () => {
const act = deploy.getAction()
const spy = jest.spyOn(deploy as any, 'getDeployCommand')
await act(undefined)
expect(spy).toHaveBeenCalled()
spy.mockRestore()
})

it('works if deploy command is executed with directory path', async () => {
const act = deploy.getAction()
const spy = jest.spyOn(deploy as any, 'getDeployCommand')
await act(absoluteProcessDir)
expect(spy).toHaveBeenCalled()
spy.mockRestore()
})

it('works if deploy command is not executed when no package.json', async () => {
const act = deploy.getAction()
const spy = jest.spyOn(deploy as any, 'getDeployCommand')
await act(absoluteProcessDirNoPackage)
expect(spy).not.toHaveBeenCalled()
spy.mockRestore()
})
})
12 changes: 12 additions & 0 deletions test/commands/deploy/inner-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "packag-json-example",
"version": "1.0.0",
"description": "simple package json example",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": "echo \"Deploy command will be executed\" "
},
"author": "",
"license": "ISC"
}
12 changes: 12 additions & 0 deletions test/commands/deploy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "packag-json-example",
"version": "1.0.0",
"description": "simple package json example",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": "echo \"Deploy command will be executed\" "
},
"author": "",
"license": "ISC"
}

0 comments on commit b5991ae

Please sign in to comment.