Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const setup = async () => {
.group('Basics')
.description('Attach project to the chosen Instance')
.option('-i, --instance <name>', 'Instance you want to use for your project')
.option('-c, --create-instance <name>', 'Create instance you want to use for your project')
.action(async (...options) => {
trackAndDebug(options)
session.isAuthenticated()
Expand Down
29 changes: 20 additions & 9 deletions packages/cli/src/commands/attach.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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()
}

Expand All @@ -57,9 +70,7 @@ class Attach {
}
])

const instance = await createInstance(instanceName)

return instance.name
return createInstance(instanceName)
}

async getQuestions () {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/socket-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/utils/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
14 changes: 12 additions & 2 deletions packages/cli/tests/unit/commands-attach.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 () {
Expand All @@ -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')
})

Expand Down Expand Up @@ -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 })
})
})
Expand Down
14 changes: 0 additions & 14 deletions packages/cli/tests/unit/utils-init-init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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)
})
})
})