diff --git a/CHANGELOG.md b/CHANGELOG.md index f1a821e98..a88e948e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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_` and on that workspace you'll have a sandbox with the name ``. + +You will access those by simply putting a querystring on the link you use for development, for example: `storename.beta.myvtex.com/?workspace=sb_mydeveloperemail@whut.com`. + +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. diff --git a/package.json b/package.json index eb7192e4a..7b18bbc54 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/lib/auth.coffee b/src/lib/auth.coffee index 9c561e0d4..2aa99a1d3 100644 --- a/src/lib/auth.coffee +++ b/src/lib/auth.coffee @@ -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 @@ -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' @@ -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 diff --git a/src/lib/watch.coffee b/src/lib/watch.coffee index f262f3dc8..3abe197ae 100644 --- a/src/lib/watch.coffee +++ b/src/lib/watch.coffee @@ -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: { @@ -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 @@ -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' @@ -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 + diff --git a/src/vtex-login.coffee b/src/vtex-login.coffee index ebd0a3d27..c085a773c 100644 --- a/src/vtex-login.coffee +++ b/src/vtex-login.coffee @@ -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 + diff --git a/src/vtex-watch.coffee b/src/vtex-watch.coffee index 047d4e31a..359659f40 100644 --- a/src/vtex-watch.coffee +++ b/src/vtex-watch.coffee @@ -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!"), diff --git a/src/vtex.coffee b/src/vtex.coffee index 148eb63e5..5225313aa 100644 --- a/src/vtex.coffee +++ b/src/vtex.coffee @@ -8,7 +8,7 @@ doc = """ vtex login vtex logout vtex publish - vtex watch [--webpack | --server] + vtex watch [--webpack | --server] vtex --help vtex --version @@ -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 @@ -70,8 +67,7 @@ else env = 'HOT' if options['--server'] argv = [ options['--webpack'], - options['--server'], - options[''] + options['--server'] ] run(command, argv, env)