Skip to content

Commit

Permalink
Merge branch 'feature/cookieless'
Browse files Browse the repository at this point in the history
  • Loading branch information
tamorim committed Oct 27, 2015
2 parents 294af9b + 50b7d08 commit 50eb26a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 23 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
### v0.10.0 (2015-10-27)

Hello, fellow developers!

I'm glad to announce that **THE COOKIE IS DEAD!!11!!**

Ok... maybe not that much. Let me explain:

The cookies for the `sandbox` and `workspace` pretty much still exists, **BUT**, we've created a good and ol' barrel of abstraction on top of it so you don't have to worry anymore.

Now we have a convention for setting those cookies. We will create a workspace with the the name `sb_<your-vtex-developer-email>` and on that workspace you'll have a sandbox with the name `<your-vtex-developer-email>`.

You will access those by simply putting a querystring on the link you use for development, for example: `storename.beta.myvtex.com/[email protected]`.

All we ask in return is that when you log in you inform us the account you wish to be logged (yeah, only one account at a time).

I know you're excited, yeah, gimme a hug homie <3

- [`#62`](https://github.com/vtex/toolbelt/issues/62)
- [`#48`](https://github.com/vtex/toolbelt/issues/48) (closed due to deprecation)

---

### v0.9.4 (2015-10-22)

Update changes log to include warnings from server response.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"grunt-contrib-coffee": "^0.13.0",
"grunt-contrib-copy": "^0.8.1",
"grunt-contrib-watch": "^0.6.1",
"mocha": "^2.3.3"
"mocha": "^2.3.3",
"signalr-client": "0.0.16"
}
}
11 changes: 8 additions & 3 deletions src/lib/auth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ request = require 'request'
prompt = require 'prompt'

class AuthenticationService
constructor: ->
login: =>
@askCredentials()
.then(@saveCredentials)
.then @saveCredentials
.catch (error) ->
throw new Error error

Expand All @@ -22,6 +21,9 @@ class AuthenticationService
deferred = Q.defer()
options =
properties:
account:
message: 'account'
required: true
login:
format: 'email'
message: 'Must be a valid email'
Expand All @@ -41,7 +43,10 @@ class AuthenticationService
console.log 'Login failed. Please try again.' if err
if result.login and result.password
@getAuthenticationToken(result.login, result.password).then (token) ->
deferred.resolve {email: result.login, token: token}
deferred.resolve
email: result.login
token: token
account: result.account
.catch (error) ->
deferred.reject error
else
Expand Down
58 changes: 56 additions & 2 deletions src/lib/watch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fileManager = require './file-manager'
tinylr = require 'tiny-lr'
crypto = require 'crypto'
net = require 'net'
signalR = require 'signalr-client'

class Watcher
ChangeAction: {
Expand All @@ -18,14 +19,17 @@ class Watcher
lastBatch: 0
lrPortInUse: false

constructor: (@app, @vendor, @sandbox, @credentials) ->
constructor: (@app, @vendor, @credentials, @isServerSet) ->
@endpoint = "http://api.beta.vtex.com"
@acceptHeader = "application/vnd.vtex.gallery.v0+json"
@sandbox = @credentials.email
@lrRun(35729)

watch: =>
root = process.cwd()
usePolling = (process.platform is 'win32') ? false
@createWorkspace().then =>
@connectToSignalR()
fileManager.listFiles().then (result) =>
deferred = Q.defer()
@endpoint = result.endpoint if result.endpoint
Expand Down Expand Up @@ -137,13 +141,22 @@ class Watcher

changesSentSuccessfuly: (batchChanges) =>
@logResponse batchChanges

paths = batchChanges.map (change) -> change.path
linkMsg = 'Your URL: '.green

if paths.length > 0
console.log '\n... files uploaded\n'.green
else
console.log '\nEverything is up to date\n'.green

if @isServerSet is 'true'
linkMsg += "http://#{@credentials.account}.local.myvtex.com:3000/".blue.underline
else
linkMsg += "http://#{@credentials.account}.beta.myvtex.com/".blue.underline

linkMsg += "?workspace=sb_#{@sandbox}\n".blue.underline
console.log linkMsg

options =
url: "http://localhost:35729/changed"
method: 'POST'
Expand Down Expand Up @@ -247,4 +260,45 @@ class Watcher
)
.listen(port)


createWorkspace: =>
deferred = Q.defer()
options =
url: "#{@endpoint}/#{@credentials.account}/workspaces"
method: 'POST'
headers:
Authorization: 'token ' + @credentials.token
Accept: @acceptHeader
'Content-Type': 'application/json'
json:
name: "sb_#{@credentials.email}"

request options, (error, response) ->
if error or response.statusCode not in [200, 409]
deferred.reject()
console.log error
process.exit 1

deferred.resolve()

deferred.promise

connectToSignalR: =>
client = new signalR.client 'http://workspaces.beta.vtex.com/signalr', ['SandboxStateHub'], 2, true

client.headers['Authorization'] = "token #{@credentials.token}"
client.headers['x-vtex-app'] = "#{@vendor}.#{@app}"
client.headers['x-vtex-account'] = @credentials.account
client.headers['x-vtex-user'] = @credentials.email

client.on 'SandboxStateHub', 'Abort', (msg) ->
console.log msg
client.end()

client.serviceHandlers.connected = (conn) ->
return true

client.start()

module.exports = Watcher

6 changes: 4 additions & 2 deletions src/vtex-login.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pkg = require '../package.json'
auth = require './lib/auth'

auth.login().then((data) ->
auth.login()
.then (data) ->
console.log "\n", "Logged in as #{data.email}".green
).catch (error) ->
.catch (error) ->
console.log error.message

13 changes: 4 additions & 9 deletions src/vtex-watch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@ WebpackRunner = require './lib/webpack'
chalk = require 'chalk'
vtexsay = require 'vtexsay'

SANDBOX_INDEX = process.argv.length - 1
SERVER_INDEX = process.argv.length - 2
WEBPACK_INDEX = process.argv.length - 3
SERVER_INDEX = process.argv.length - 1
WEBPACK_INDEX = process.argv.length - 2

sandbox = process.argv[SANDBOX_INDEX]
serverFlag = process.argv[SERVER_INDEX]
webpackFlag = process.argv[WEBPACK_INDEX]
isFlagActive = webpackFlag is 'true' or serverFlag is 'true'

unless sandbox.match /^[\w_-]+$/
throw Error 'Sandbox may contain only letters, numbers, underscores and hyphens'.red

promise = Q.all [auth.getValidCredentials(), metadata.getAppMetadata()]
Q.all [auth.getValidCredentials(), metadata.getAppMetadata()]
.spread (credentials, meta) ->
name = meta.name
vendor = meta.vendor

watcher = new Watcher name, vendor, sandbox, credentials
watcher = new Watcher name, vendor, credentials, serverFlag
watcher.watch()
.then (app) ->
console.log vtexsay("Welcome to the VTEX Toolbelt!"),
Expand Down
8 changes: 2 additions & 6 deletions src/vtex.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ doc = """
vtex login
vtex logout
vtex publish
vtex watch [--webpack | --server] <sandbox>
vtex watch [--webpack | --server]
vtex --help
vtex --version
Expand All @@ -24,9 +24,6 @@ doc = """
-w --webpack Start with webpack
-s --server Start with dev server
Arguments:
sandbox The name of the sandbox you wish to work on
"""

options = docopt doc, version: pkg.version
Expand Down Expand Up @@ -70,8 +67,7 @@ else
env = 'HOT' if options['--server']
argv = [
options['--webpack'],
options['--server'],
options['<sandbox>']
options['--server']
]

run(command, argv, env)
Expand Down

0 comments on commit 50eb26a

Please sign in to comment.