Skip to content

Commit b5991ae

Browse files
authored
Merge pull request #4 from tomyitav/deploy-command
Deploy command
2 parents 3a5948f + 15131fa commit b5991ae

File tree

7 files changed

+122
-2
lines changed

7 files changed

+122
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ To generate a matching resolver file for type file, execute the command:
3838
This will create a matching file, with all Query, Mutation and Subscription
3939
definitions.
4040

41+
### Deploy server to production
42+
43+
Inside the project directory, type:
44+
45+
```gga d```
46+
47+
This will run the server *deploy* script, and will move the server to production! :rocket:
48+
4149
### Extended documentation
4250

4351
Please check out the [extended documentation](https://tomyitav.github.io/generate-graphql-app) for more information

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/all-commands.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { AbstractCommand } from './abstract-command'
22
import { Init } from './init'
33
import { Resolver } from './resolver/resolver'
4+
import { Deploy } from './deploy'
45

5-
const allCommands: AbstractCommand[] = [new Init(), new Resolver()]
6+
const allCommands: AbstractCommand[] = [new Init(), new Resolver(), new Deploy()]
67

78
export default allCommands

src/commands/deploy.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { AbstractCommand } from './abstract-command'
2+
import { shell } from '../utils/shell'
3+
import { fileExists } from '../utils/file-operations'
4+
import * as path from 'path'
5+
6+
export class Deploy extends AbstractCommand {
7+
public getName(): string {
8+
return 'deploy [project-path]'
9+
}
10+
11+
public getDescription(): string {
12+
return 'Deploy project to production'
13+
}
14+
15+
public getAlias(): string {
16+
return 'd'
17+
}
18+
19+
public getAction(): (...args: any[]) => void {
20+
return async (projectPath: string) => {
21+
try {
22+
const pathToAppend = projectPath ? projectPath : ''
23+
const pkgJsonFileExist = await fileExists(path.join(pathToAppend, 'package.json'))
24+
if (!pkgJsonFileExist) {
25+
console.error('No package.json file found, exiting...')
26+
return
27+
}
28+
console.log('Deploying server to production...')
29+
if (pathToAppend !== '') {
30+
process.chdir(pathToAppend)
31+
}
32+
await shell(this.getDeployCommand())
33+
console.log('Project successfully deployed!')
34+
} catch (err) {
35+
console.error('Got error in deploy command- ', err)
36+
}
37+
}
38+
}
39+
40+
private getDeployCommand(): string {
41+
return `npm run deploy`
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { AbstractCommand } from '../../../src/commands/abstract-command'
2+
import { Deploy } from '../../../src/commands/deploy'
3+
import * as path from 'path'
4+
5+
describe('test for deploy command', () => {
6+
let deploy: AbstractCommand
7+
const pathToProjectDir = './test/commands/deploy/inner-dir'
8+
const pathToProjectNoPackage = './test/commands/deploy/inner-dir-no-package'
9+
const absoluteProcessDir = path.join(process.cwd(), pathToProjectDir)
10+
const absoluteProcessDirNoPackage = path.join(process.cwd(), pathToProjectNoPackage)
11+
12+
beforeAll(() => {
13+
deploy = new Deploy()
14+
})
15+
16+
it('works if action returns a function', () => {
17+
const act = deploy.getAction()
18+
expect(act).toBeInstanceOf(Function)
19+
})
20+
21+
it('works if deploy command is executed in directory', async () => {
22+
const act = deploy.getAction()
23+
const spy = jest.spyOn(deploy as any, 'getDeployCommand')
24+
await act(undefined)
25+
expect(spy).toHaveBeenCalled()
26+
spy.mockRestore()
27+
})
28+
29+
it('works if deploy command is executed with directory path', async () => {
30+
const act = deploy.getAction()
31+
const spy = jest.spyOn(deploy as any, 'getDeployCommand')
32+
await act(absoluteProcessDir)
33+
expect(spy).toHaveBeenCalled()
34+
spy.mockRestore()
35+
})
36+
37+
it('works if deploy command is not executed when no package.json', async () => {
38+
const act = deploy.getAction()
39+
const spy = jest.spyOn(deploy as any, 'getDeployCommand')
40+
await act(absoluteProcessDirNoPackage)
41+
expect(spy).not.toHaveBeenCalled()
42+
spy.mockRestore()
43+
})
44+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "packag-json-example",
3+
"version": "1.0.0",
4+
"description": "simple package json example",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"deploy": "echo \"Deploy command will be executed\" "
9+
},
10+
"author": "",
11+
"license": "ISC"
12+
}

test/commands/deploy/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "packag-json-example",
3+
"version": "1.0.0",
4+
"description": "simple package json example",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"deploy": "echo \"Deploy command will be executed\" "
9+
},
10+
"author": "",
11+
"license": "ISC"
12+
}

0 commit comments

Comments
 (0)