diff --git a/packages/cli/src/cli.js b/packages/cli/src/cli.js index aa4823d5..2ef3c06b 100755 --- a/packages/cli/src/cli.js +++ b/packages/cli/src/cli.js @@ -47,6 +47,7 @@ const setup = async () => { .group('Basics') .description('Attach project to the chosen Instance') .option('-i, --instance ', 'Instance you want to use for your project') + .option('-c, --create-instance ', 'Create instance you want to use for your project') .action(async (...options) => { trackAndDebug(options) session.isAuthenticated() diff --git a/packages/cli/src/commands/attach.js b/packages/cli/src/commands/attach.js index 6959d4eb..55c9755d 100644 --- a/packages/cli/src/commands/attach.js +++ b/packages/cli/src/commands/attach.js @@ -1,6 +1,7 @@ import inquirer from 'inquirer' import logger from '../utils/debug' +import format from 'chalk' import { p, echo } from '../utils/print-tools' import genUniqueInstanceName from '../utils/unique-instance' import { createInstance } from './helpers/create-instance' @@ -30,19 +31,31 @@ class Attach { if (confirm === false) return process.exit() } - const questions = await this.getQuestions() - const { instance } = await inquirer.prompt(questions) || {} + let instanceName + let instance - const respInstanceName = instance && instance !== p(2)('Create a new one...') ? instance.trim() : null - let instanceName = cmd.instance || respInstanceName + if (cmd.createInstance) { + instance = await createInstance(cmd.createInstance) + instanceName = instance.name + } else { + const questions = await this.getQuestions() + const answer = await inquirer.prompt(questions) || {} + + instanceName = answer.instance && + answer.instance !== p(2)('Create a new one...') + ? answer.instance.trim() : null + } if (!instanceName) { - instanceName = await this.createNewInstance() + instance = await this.createNewInstance() + console.log("XXX", instance) + instanceName = instance.name } await this.init.addConfigFiles({ instance: instanceName }, this.session.projectPath) - + echo(4)(`Your project is attached to ${format.green(instanceName)} instance now!`) echo() + return this.session.load() } @@ -57,9 +70,7 @@ class Attach { } ]) - const instance = await createInstance(instanceName) - - return instance.name + return createInstance(instanceName) } async getQuestions () { diff --git a/packages/cli/src/commands/init.js b/packages/cli/src/commands/init.js index b5f24a70..5c641d6b 100644 --- a/packages/cli/src/commands/init.js +++ b/packages/cli/src/commands/init.js @@ -55,6 +55,8 @@ class InitCmd { if (!project && instance) { await this.session.checkConnection(instance) await this.init.addConfigFiles({ instance }) + echo(4)(`Your project is attached to ${format.green(instance.name)} instance now!`) + return this.init.createFilesAndFolders() } @@ -63,6 +65,8 @@ class InitCmd { const newInstance = await createInstance() await this.init.addConfigFiles({ instance: newInstance.name }) + echo(4)(`Your project is attached to ${format.green(newInstance.name)} instance now!`) + this.init.createFilesAndFolders() return this.session.load() } diff --git a/packages/cli/src/commands/socket-deploy.js b/packages/cli/src/commands/socket-deploy.js index 9477bee2..fc92c430 100644 --- a/packages/cli/src/commands/socket-deploy.js +++ b/packages/cli/src/commands/socket-deploy.js @@ -34,6 +34,7 @@ export default class SocketDeployCmd { if (cmd.createInstance) { await createInstance(cmd.createInstance) await this.init.addConfigFiles({ instance: cmd.createInstance }) + echo(4)(`Your project is attached to ${format.green(cmd.createInstance)} instance now!`) } else { // If not, we have to check if we have a project attached to any instance this.session.hasProject() diff --git a/packages/cli/src/utils/init/init.js b/packages/cli/src/utils/init/init.js index 1fe365fe..ab42e859 100644 --- a/packages/cli/src/utils/init/init.js +++ b/packages/cli/src/utils/init/init.js @@ -64,8 +64,6 @@ class Init { this.session.settings.account.addProject(path.join(process.cwd(), 'syncano'), projectParams) } await this.session.load() - - echo(4)(`Your project is attached to ${format.green(projectParams.instance)} instance now!`) } noConfigFiles () { diff --git a/packages/cli/tests/unit/commands-attach.test.js b/packages/cli/tests/unit/commands-attach.test.js index 7aef531b..5bccfb8e 100644 --- a/packages/cli/tests/unit/commands-attach.test.js +++ b/packages/cli/tests/unit/commands-attach.test.js @@ -1,5 +1,6 @@ /* global it describe afterEach beforeEach */ import { expect } from 'chai' +import format from 'chalk' import sinon from 'sinon' import sinonTestFactory from 'sinon-test' import inquirer from 'inquirer' @@ -16,17 +17,23 @@ describe('[commands] Attach', function () { let attach = null let prompt = null let exitProcess = null + let interEcho = null + let echo = null beforeEach(function () { attach = new Attach(context) attach.session.project = { instance: getRandomString('attach_session_project_instance') } prompt = sinon.stub(inquirer, 'prompt') exitProcess = sinon.stub(process, 'exit') + interEcho = sinon.stub() + echo = sinon.stub(printTools, 'echo').callsFake((content) => interEcho) }) afterEach(function () { inquirer.prompt.restore() process.exit.restore() + interEcho.reset() + printTools.echo.restore() }) describe('[run]', function () { @@ -37,7 +44,7 @@ describe('[commands] Attach', function () { addConfigFiles: (configFiles) => configFiles }) sinon.stub(attach.session, 'load') - createNewInstance = sinon.stub(attach, 'createNewInstance') + createNewInstance = sinon.stub(attach, 'createNewInstance').returns({name: 'newInstance'}) sinon.stub(attach, 'getQuestions') }) @@ -84,14 +91,17 @@ describe('[commands] Attach', function () { const newInstanceName = getRandomString('attach_newInstanceName') const init = new attach.Init() const addConfigFiles = sinon.stub(init, 'addConfigFiles') + const response = `Your project is attached to ${format.green(newInstanceName)} instance now!` prompt.returns(Promise.resolve({ instance: printTools.p(2)('Create a new one...') })) - createNewInstance.returns(newInstanceName) + createNewInstance.returns({name: newInstanceName}) await attach.run([]) init.addConfigFiles.restore() + sinon.assert.calledWith(echo, 4) + sinon.assert.calledWith(interEcho, response) sinon.assert.calledWith(addConfigFiles, { instance: newInstanceName }) }) }) diff --git a/packages/cli/tests/unit/utils-init-init.test.js b/packages/cli/tests/unit/utils-init-init.test.js index 51dba5b2..3684422b 100644 --- a/packages/cli/tests/unit/utils-init-init.test.js +++ b/packages/cli/tests/unit/utils-init-init.test.js @@ -2,28 +2,23 @@ import sinon from 'sinon' import sinonTestFactory from 'sinon-test' import path from 'path' -import format from 'chalk' import { getRandomString } from '@syncano/test-tools' import Init from '../../src/utils/init' -import printTools from '../../src/utils/print-tools' sinon.test = sinonTestFactory(sinon) describe('[commands] init', function () { const init = new Init() let interEcho = null - let echo = null beforeEach(function () { interEcho = sinon.stub() - echo = sinon.stub(printTools, 'echo').callsFake((content) => interEcho) }) afterEach(function () { interEcho.reset() - printTools.echo.restore() }) describe('[addConfigFiles]', function () { @@ -45,14 +40,5 @@ describe('[commands] init', function () { sinon.assert.calledWith(addProject, path.join(process.cwd(), 'syncano'), projectParams) }) - - it('should print response about connected instance with proper padding', async function () { - const response = `Your project is attached to ${format.green(projectParams.instance)} instance now!` - - await init.addConfigFiles(projectParams) - - sinon.assert.calledWith(echo, 4) - sinon.assert.calledWith(interEcho, response) - }) }) })