diff --git a/README.md b/README.md index 601f0710..55087626 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 5f2d4055..0ba9a7eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "generate-graphql-app", - "version": "1.3.3", + "version": "1.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/commands/all-commands.ts b/src/commands/all-commands.ts index 59b8af74..82756fd3 100644 --- a/src/commands/all-commands.ts +++ b/src/commands/all-commands.ts @@ -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 diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts new file mode 100644 index 00000000..60b3f867 --- /dev/null +++ b/src/commands/deploy.ts @@ -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` + } +} diff --git a/test/commands/deploy/deploy.test.ts b/test/commands/deploy/deploy.test.ts new file mode 100644 index 00000000..d67eea80 --- /dev/null +++ b/test/commands/deploy/deploy.test.ts @@ -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() + }) +}) diff --git a/test/commands/deploy/inner-dir/package.json b/test/commands/deploy/inner-dir/package.json new file mode 100644 index 00000000..b9550740 --- /dev/null +++ b/test/commands/deploy/inner-dir/package.json @@ -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" +} diff --git a/test/commands/deploy/package.json b/test/commands/deploy/package.json new file mode 100644 index 00000000..b9550740 --- /dev/null +++ b/test/commands/deploy/package.json @@ -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" +}