├── .gitignore ├── LICENSE ├── README.md ├── bin ├── .gitkeep └── optcli.js ├── contributing.md ├── index.js ├── lib ├── assets.js ├── commands │ ├── create-experiment.js │ ├── create-variation.js │ ├── host.js │ ├── init-project.js │ ├── push-experiment.js │ ├── push-variation.js │ └── set-token.js ├── experiment.js ├── file-util.js ├── files.js ├── logger.js ├── optcli-base.js ├── project.js ├── read-config.js ├── server │ ├── assets │ │ └── jquery.min.js │ └── controller.js ├── set-token.js ├── variation.js └── write-config.js ├── package.json ├── ssl ├── server.crt └── server.key ├── templates ├── index.ejs ├── install.user.js.ejs ├── script.ejs └── server.ejs └── test ├── commands ├── create-experiment.test.js ├── create-variation.test.js ├── host.test.js ├── init-project.test.js ├── push-experiment.test.js ├── push-variation.test.js └── set-token.test.js ├── experiment.test.js ├── mocha.opts ├── project.test.js └── utils.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac crap 2 | .DS_Store 3 | 4 | # Log Files 5 | *.log 6 | 7 | # Node 8 | node_modules/ 9 | .npmrc 10 | 11 | tmp/ 12 | coverage/ 13 | .coveralls.yml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Optimizely Command Line Interface 2 | 3 | [![Codeship Status for FunnelEnvy/optimizely-cli](https://codeship.com/projects/3b6cfc10-040d-0133-0e10-62fced7320b0/status?branch=master)](https://codeship.com/projects/89330) 4 | [![Code Climate](https://codeclimate.com/github/FunnelEnvy/optimizely-cli/badges/gpa.svg)](https://codeclimate.com/github/FunnelEnvy/optimizely-cli) [![Test Coverage](https://codeclimate.com/github/FunnelEnvy/optimizely-cli/badges/coverage.svg)](https://codeclimate.com/github/FunnelEnvy/optimizely-cli/coverage) 5 | 6 | Working with Optimizely X? Check out the [Optimizely X CLI](https://github.com/teamroboboogie/x-optimizely-cli) from @teamroboboogie! 7 | 8 | Optimizely-CLI (optcli) is a command line tool that lets developers build experiments faster by using the sofware tools you already love and publish to Optimizely when ready. We build a lot of tests at [FunnelEnvy](http://www.funnelenvy.com) and found that (being stubborn engineers) we were more comfortable using our source editors and Git to develop locally - and this had a *significant* positive impact on our test velocity. 9 | 10 | Optimizely-cli includes a command line executable that also integrates with either the[Tampermonkey](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en) (Google Chrome) or [Greasemonkey](https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/) (Firefox) extensions for local development / preview and the Optimizely API for publishing tests. 11 | 12 | Brief intro below - for more usage details check out our [Optimizely-CLI page](http://www.funnelenvy.com/optimizely-cli/). 13 | 14 | ## Installation 15 | 16 | ``` 17 | npm install -g optimizely-cli 18 | ``` 19 | This will install the __optcli__ executable on your system. 20 | 21 | ### Dependencies 22 | 23 | 24 | You'll need to have [node.js](http://nodejs.org/) installed locally to run `optcli` and either the Tampermonkey or Greasemonkey extensions to view variations locally. 25 | 26 | ## Quickstart 27 | 28 | ``` 29 | optcli 30 | ``` 31 | 32 | View available commands 33 | 34 | ``` 35 | optcli init [options] [project_id] 36 | ``` 37 | Initializes a new Optimizely project locally (use `-r` for remote). 38 | 39 | ``` 40 | optcli experiment 41 | ``` 42 | Create a local experiment 43 | 44 | ``` 45 | optcli variation 46 | ``` 47 | Create a local variation 48 | 49 | ``` 50 | optcli host [options] [port] 51 | ``` 52 | Host a variation locally. Point your browser at http(s)://localhost:8080 (default port) for usage info. 53 | 54 | ``` 55 | optcli push-experiment 56 | ``` 57 | Push a local experiment to Optimizely. 58 | 59 | ``` 60 | optcli push-variation 61 | ``` 62 | Push a local variation to Optimizely 63 | 64 | ## Known Issues 65 | * Tests - We have some. We're adding more. 66 | 67 | 68 | ## Release History 69 | * 0.15.0 Iteration option on push-experiment 70 | * 0.14.3 Added push-experiment, push-variation tests 71 | * 0.14.2 Show help when no arguments passed 72 | * 0.14.1 Bugfixes 73 | * 0.14.0 Move node client into [separate module](https://github.com/FunnelEnvy/optimizely-node) 74 | * 0.12.0 Bugfixes, more compliant with semver 75 | * 0.0.11 Separated create from push operations 76 | * 0.0.10 Refactored and cleanup 77 | * 0.0.7 Push 78 | * 0.0.2 Clone bug fix 79 | * 0.0.1 Initial release 80 | 81 | ## Contributing 82 | 83 | Please see [CONTRIBUTING.md](contributing.md). 84 | 85 | ## Copyright and license 86 | 87 | Code copyright 2015 Celerius Group Inc. Released under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). 88 | -------------------------------------------------------------------------------- /bin/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FunnelEnvy/optimizely-cli/5716446558c04805e9260c4e5575e79e496a36cd/bin/.gitkeep -------------------------------------------------------------------------------- /bin/optcli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var program = require("commander"); 4 | var path = require("path"); 5 | var optcliPackage = require(path.join(__dirname, "../", "package.json")); 6 | var logger = require("../lib/logger.js"); 7 | 8 | /* commands */ 9 | var loadCommand = function(cmd) { 10 | var self = this; 11 | return function() { 12 | require("../lib/commands/" + cmd) 13 | .apply(self, arguments); 14 | } 15 | } 16 | 17 | //default log level 18 | logger.debugLevel = 'info'; 19 | 20 | function increaseVerbosity(v) { 21 | logger.debugLevel = 'debug'; 22 | } 23 | 24 | program 25 | .version(optcliPackage.version) 26 | .usage(" - " + optcliPackage.description) 27 | .description(optcliPackage.description) 28 | .option("-v --verbose", "show debug output", increaseVerbosity) 29 | 30 | program 31 | .command("init [project_id]") 32 | .description("Initialize an optimizely project.") 33 | .option("-r --remote", "initialize from remote project") 34 | .option("-j --jquery", "include jquery (local project only)") 35 | .action(loadCommand("init-project")); 36 | 37 | program 38 | .command("experiment ") 39 | .description("Create Local Experiment") 40 | .action(loadCommand("create-experiment")); 41 | 42 | program 43 | .command("variation ") 44 | .description("Create Local Variation") 45 | .action(loadCommand("create-variation")); 46 | 47 | program 48 | .command("host [port]") 49 | .option("-s --ssl", "SSL") 50 | .option("-o --open", "Open the localhost index page") 51 | .description("Host variation locally") 52 | .action(loadCommand("host")); 53 | 54 | program 55 | .command("push-experiment ") 56 | .option("-i --iterate", "Push experiment and iterate through all variations") 57 | .description("Push an experiment to Optimizely") 58 | .action(loadCommand("push-experiment")); 59 | 60 | program 61 | .command("push-variation ") 62 | .description( 63 | "Push a variation to Optimizely (experiment must be pushed first)") 64 | .action(loadCommand("push-variation")); 65 | 66 | program 67 | .command("set-token [token]") 68 | .description("Set the optimizely API token in a project folder") 69 | .action(loadCommand("set-token")); 70 | 71 | //Show help if no arguments are passed 72 | if (!process.argv.slice(2).length) { 73 | program._name = process.argv[1]; 74 | program._name = program._name.substr(program._name.lastIndexOf("/") + 1); 75 | program.outputHelp(); 76 | } 77 | 78 | program.parse(process.argv); 79 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We love pull requests. Here's a quick guide. 4 | 5 | Fork, then clone the repo: 6 | 7 | git clone git@github.com:your-username/optimizely-cli.git 8 | 9 | Make sure the tests pass: 10 | 11 | npm test 12 | 13 | Make your change. Add tests for your change. Make the tests pass: 14 | 15 | npm test 16 | 17 | Push to your fork and [submit a pull request][pr]. 18 | 19 | [pr]: https://github.com/funnelenvy/optimizely-cli/compare/ 20 | 21 | At this point you're waiting on us. We like to at least comment on pull requests 22 | within three business days (and, typically, one business day). We may suggest 23 | some changes or improvements or alternatives. 24 | 25 | Some things that will increase the chance that your pull request is accepted: 26 | 27 | * Write tests. 28 | * Follow our [style guide][style]. 29 | * Write a [good commit message][commit]. 30 | 31 | [style]: https://github.com/RisingStack/node-style-guide 32 | [commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html 33 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require("./lib/optin.js"); 2 | -------------------------------------------------------------------------------- /lib/assets.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | var OptCLIBase = require("./optcli-base"); 4 | 5 | function Assets(attributes, baseDir) { 6 | Assets.super_.call(this, attributes, baseDir); 7 | } 8 | 9 | Assets.JSON_FILE_NAME = "assets.json"; 10 | 11 | util.inherits(Assets, OptCLIBase); 12 | 13 | module.exports = Assets; 14 | -------------------------------------------------------------------------------- /lib/commands/create-experiment.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies 3 | */ 4 | var Experiment = require("../experiment"); 5 | var logger = require("../logger"); 6 | 7 | 8 | /** 9 | * Create local experiment 10 | * 11 | * @param description the experiment description 12 | * @param edit_url experiment editor url 13 | */ 14 | var createLocalExperiment = function(folder, description, edit_url) { 15 | var success = Experiment.create({ 16 | description: description, 17 | edit_url: edit_url 18 | }, folder); 19 | if (success) { 20 | logger.log("info", "created experiment \"" + description + "\" in folder " + folder); 21 | } else { 22 | logger.log("error", "failed to create experiment"); 23 | } 24 | }; 25 | 26 | /** 27 | * The export. Create a remote or local experiment 28 | * 29 | * @param description the experiment descrition 30 | * @param edit_url experiment editor url 31 | */ 32 | module.exports = function(folder, description, edit_url) { 33 | createLocalExperiment(folder, description, edit_url); 34 | }; 35 | -------------------------------------------------------------------------------- /lib/commands/create-variation.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var path = require("path"); 3 | 4 | var Experiment = require("../experiment"); 5 | var Variation = require("../variation"); 6 | var logger = require("../logger"); 7 | 8 | var createLocalVariation = function(experiment, folder, description) { 9 | var success = Variation.create({ 10 | description: description, 11 | }, path.join(experiment.baseDir, folder)); 12 | if (success) { 13 | logger.log("info", "created variation " + description + "in folder " + folder); 14 | } else { 15 | logger.log("error", "failed to create variation"); 16 | } 17 | }; 18 | 19 | module.exports = function(identifier, folder, description) { 20 | var experiment = Experiment.locateAndLoad(identifier); 21 | if (experiment) { 22 | createLocalVariation(experiment, folder, description); 23 | } else { 24 | console.log("no local experiment found by: " + identifier + ". Please specify experiment folder, id or description"); 25 | } 26 | 27 | }; 28 | -------------------------------------------------------------------------------- /lib/commands/host.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies 3 | */ 4 | var express = require("express"); 5 | var path = require('path'); 6 | var fs = require('fs'); 7 | var open = require('open'); 8 | var logger = require('../logger'); 9 | 10 | var LocalController = require('../server/controller'); 11 | var Variation = require('../variation'); 12 | 13 | var getSSLCredentials = function() { 14 | var originalPath = path.join( 15 | path.dirname(fs.realpathSync(__filename)), 16 | '../../'); 17 | var credentials = { 18 | key: fs.readFileSync(originalPath + "ssl/server.key", 'utf8'), 19 | cert: fs.readFileSync(originalPath + "ssl/server.crt", 'utf8') 20 | }; 21 | return credentials; 22 | }; 23 | 24 | var handleStartupError = function(error){ 25 | if(error.errno === 'EADDRINUSE') { 26 | logger.log('error', 'Port already in use. Kill the other process or use another port.'); 27 | } else { 28 | logger.log("error", error.message); 29 | } 30 | }; 31 | 32 | 33 | module.exports = function(varPath, port, program) { 34 | //Start Server 35 | var app = express(); 36 | var localController; 37 | 38 | app.set('view engine', 'ejs'); 39 | 40 | //configure the controller 41 | varPath = path.resolve(process.cwd(), varPath); 42 | var variation = new Variation({}, varPath); 43 | variation.loadFromFile(); 44 | 45 | if (!variation) { 46 | return logger.log("error", "No variation at path: \"" + varPath + 47 | "\" found"); 48 | } 49 | 50 | //local controller for the requests 51 | try { 52 | localController = new LocalController(variation, port); 53 | } catch(error) { 54 | logger.log("error", error.message); 55 | return; 56 | } 57 | 58 | //set the routes 59 | app.get("/", localController.installUserScript.bind(localController)); 60 | app.get("/install.user.js", localController.userScript.bind(localController)); 61 | app.get("/variation.js", localController.variationJS.bind(localController)); 62 | app.get("/variation.css", localController.variationCSS.bind(localController)); 63 | 64 | //start the server 65 | var onStartup = function(){ 66 | if(!program.silence){ 67 | console.log("Serving variation " + varPath + " at port " + localController.port); 68 | console.log("point your browser to http" + (program.ssl ? "s" : "") + "://localhost:" + localController.port); 69 | console.log("Ctrl-c to quit"); 70 | } 71 | if(program.open){ 72 | open("http" + (program.ssl ? "s" : "") + "://localhost:" + localController.port); 73 | } 74 | }; 75 | 76 | if (program.ssl) { 77 | return require('https').createServer(getSSLCredentials(), app) 78 | .listen(localController.port, onStartup) 79 | .on('error', handleStartupError); 80 | } else { 81 | return app 82 | .listen(localController.port, onStartup) 83 | .on('error', handleStartupError); 84 | } 85 | }; 86 | -------------------------------------------------------------------------------- /lib/commands/init-project.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module depencies 3 | */ 4 | var logger = require("../logger"); 5 | var OptimizelyClient = require('optimizely-node-client'); 6 | var readConfig = require("../read-config"); 7 | var Project = require("../project"); 8 | 9 | 10 | /** 11 | * pull a remote project down via the api 12 | * optionally retrieve experiments and variations 13 | * and write project.json 14 | * 15 | * @param id 16 | * @param program 17 | * @param project 18 | */ 19 | var pullRemoteProject = function(id, program) { 20 | var optClient; 21 | readConfig("token") 22 | .then(function(token) { 23 | optClient = new OptimizelyClient(token); 24 | return optClient.getProject(id) 25 | .then(function(project) { 26 | var theProject = new Project(project); 27 | return theProject.save(); 28 | }); 29 | }) 30 | .catch(function (e) { 31 | logger.log("error", "unable to pull project: " + e.message); 32 | console.error(e.stack); 33 | }); 34 | }; 35 | 36 | var createLocalProject = function(id, program) { 37 | var defaultAttrs = { 38 | id: id, 39 | include_jquery: !!program.jquery, 40 | }; 41 | var theProject = new Project(defaultAttrs, "./"); 42 | return theProject.save(); 43 | }; 44 | 45 | /** 46 | * Initialize a remote, pulled or local (default) project 47 | * 48 | * @param id 49 | * @param program 50 | */ 51 | module.exports = function(id, program) { 52 | logger.log("info", "initializing project " + id); 53 | if (program.remote) { 54 | pullRemoteProject(id, program); 55 | } else { 56 | createLocalProject(id, program); 57 | } 58 | logger.log("info", "initialized project " + id); 59 | }; 60 | -------------------------------------------------------------------------------- /lib/commands/push-experiment.js: -------------------------------------------------------------------------------- 1 | var readConfig = require("../read-config"); 2 | var Experiment = require("../experiment"); 3 | var Variation = require("../variation"); 4 | var pushVariation = require("./push-variation"); 5 | var logger = require("../logger"); 6 | var OptimizelyClient = require('optimizely-node-client'); 7 | 8 | module.exports = function(folder, program) { 9 | //find the experiment 10 | var experiment = Experiment.locateAndLoad(folder); 11 | 12 | if (!experiment) { 13 | logger.log("error", "could not find experiment at " + folder); 14 | return; 15 | } else { 16 | logger.log("info", "pushing experiment at " + folder); 17 | } 18 | 19 | readConfig("token").then(function(token) { 20 | var client = new OptimizelyClient(token); 21 | 22 | //if we already have an id, then update 23 | if (experiment.attributes.id) { 24 | 25 | experiment.updateRemote(client); 26 | } else { 27 | experiment.createRemote(client); 28 | } 29 | }).then(function() { 30 | if(program.iterate){ 31 | experiment.getVariations().forEach(function(variationPath) { 32 | pushVariation(variationPath.slice(0,- Variation.JSON_FILE_NAME.length),program); 33 | }); 34 | } 35 | }).catch(function(error) { 36 | // Handle any error from all above steps 37 | logger.log("error", error.stack); 38 | }) 39 | .done(); 40 | }; 41 | -------------------------------------------------------------------------------- /lib/commands/push-variation.js: -------------------------------------------------------------------------------- 1 | var path = require("path"); 2 | 3 | var readConfig = require("../read-config"); 4 | var Variation = require("../variation"); 5 | var Experiment = require("../experiment"); 6 | var logger = require("../logger"); 7 | var OptimizelyClient = require('optimizely-node-client'); 8 | 9 | module.exports = function(folder) { 10 | //find the variation 11 | var varPath = path.resolve(process.cwd(), folder); 12 | var variation = new Variation({}, varPath); 13 | variation.loadFromFile(); 14 | 15 | if (!variation) { 16 | logger.log("error", "could not find variation at " + folder); 17 | return; 18 | } 19 | logger.log("info", "pushing variation at " + folder); 20 | readConfig("token").then(function(token) { 21 | var client = new OptimizelyClient(token); 22 | //if we already have an id, then update 23 | if (variation.attributes.id) { 24 | variation.updateRemote(client); 25 | } else { 26 | //find the experiment 27 | this.experiment = new Experiment({}, path.normalize(variation.baseDir + 28 | "/..")); 29 | if(!this.experiment.loadFromFile()){ 30 | logger.log("error", "no experiment.json found."); 31 | return; 32 | } else if (!this.experiment.attributes.id) { 33 | logger.log("error", 34 | "no id found for experiment. Please run push-experiment first" 35 | ); 36 | return; 37 | } 38 | variation.createRemote(client, this.experiment); 39 | } 40 | }).catch(function(error) { 41 | // Handle any error from all above steps 42 | logger.log("error", error.stack); 43 | }) 44 | .done(); 45 | }; 46 | -------------------------------------------------------------------------------- /lib/commands/set-token.js: -------------------------------------------------------------------------------- 1 | var setToken = require('../set-token.js'); 2 | var logger = require('../logger.js'); 3 | 4 | /** 5 | * Sets the optimizely API token in the 6 | * current project folder. 7 | * @param {String} token Optimizely API token 8 | */ 9 | module.exports = function(token){ 10 | setToken(token).then(function(){ 11 | logger.log('info', 'token successfully set'); 12 | }).catch(function(err){ 13 | logger.log('error', err.message); 14 | }); 15 | }; -------------------------------------------------------------------------------- /lib/experiment.js: -------------------------------------------------------------------------------- 1 | var glob = require('glob'); 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var util = require('util'); 5 | var _ = require("lodash"); 6 | 7 | var fileUtil = require("./file-util"); 8 | var logger = require("./logger"); 9 | var Variation = require("./variation"); 10 | var OptCLIBase = require("./optcli-base"); 11 | var Project = require("./project"); 12 | var Variation = require("./variation"); 13 | 14 | function Experiment(attributes, baseDir) { 15 | Experiment.super_.call(this, attributes, baseDir); 16 | } 17 | 18 | Experiment.JSON_FILE_NAME = "experiment.json"; 19 | Experiment.JS_FILE_NAME = "global.js"; 20 | Experiment.CSS_FILE_NAME = "global.css"; 21 | 22 | util.inherits(Experiment, OptCLIBase); 23 | 24 | Experiment.create = function(attrs, baseDir) { 25 | //create directory 26 | fileUtil.writeDir(baseDir); 27 | fileUtil.writeText(path.join(baseDir, Experiment.CSS_FILE_NAME)); 28 | fileUtil.writeText(path.join(baseDir, Experiment.JS_FILE_NAME)); 29 | fileUtil.writeJSON(path.join(baseDir, Experiment.JSON_FILE_NAME), attrs); 30 | return new Experiment(attrs, baseDir); 31 | } 32 | 33 | Experiment.locateAndLoad = function(identifier) { 34 | var experiment = null; 35 | if (fs.existsSync(identifier) && fs.lstatSync(identifier).isDirectory()) { 36 | //it's a directory 37 | experiment = new Experiment({}, identifier); 38 | if(!experiment.loadFromFile()) return false; 39 | } else { 40 | var attrs = {}; 41 | glob.sync("**/" + Experiment.JSON_FILE_NAME).forEach(function(jsonFile) { 42 | if (experiment) return; 43 | try { 44 | var attrs = JSON.parse(fs.readFileSync(jsonFile), { 45 | encoding: "utf-8" 46 | }); 47 | if (identifier === String(attrs.id) || identifier === attrs.description) { 48 | experiment = new Experiment(attrs, path.dirName(jsonFile)); 49 | return experiment; 50 | } 51 | } catch (e) { 52 | logger.log("warn", "could not parse " + jsonFile); 53 | return false; 54 | } 55 | }) 56 | } 57 | return experiment; 58 | } 59 | 60 | Experiment.prototype.getJSPath = function() { 61 | return this.getFilePath(Experiment.JS_FILE_NAME); 62 | } 63 | 64 | Experiment.prototype.getCSSPath = function() { 65 | return this.getFilePath(Experiment.CSS_FILE_NAME); 66 | } 67 | 68 | Experiment.prototype.getCSS = function() { 69 | return fileUtil.loadFile(this.getCSSPath()) || ""; 70 | } 71 | 72 | Experiment.prototype.getJS = function() { 73 | return fileUtil.loadFile(this.getJSPath()) || ""; 74 | } 75 | 76 | Experiment.prototype.getVariations = function() { 77 | return glob.sync(this.baseDir+'/**/'+Variation.JSON_FILE_NAME); 78 | } 79 | 80 | Experiment.prototype.createRemote = function(client) { 81 | //find the project - assume it's one directory above 82 | var project = new Project({}, path.normalize(this.baseDir + "/..")); 83 | project.loadFromFile(); 84 | //create new experiment 85 | var expArgs = _.clone(this.attributes); 86 | expArgs['custom_css'] = this.getCSS(); 87 | expArgs['custom_js'] = this.getJS(); 88 | expArgs['project_id'] = project.attributes.id; 89 | 90 | var self = this; 91 | return client.createExperiment(expArgs).then(function(experimentAttrs) { 92 | //update the id 93 | self.attributes.id = experimentAttrs.id; 94 | self.saveAttributes(); 95 | logger.log("info", "created remote experiment: " + experimentAttrs.id); 96 | }, function(error) { 97 | logger.log("error", error); 98 | }) 99 | .catch(function(e) { 100 | logger.log("error", "unable to create remote experiment: " + e.message); 101 | console.error(e.stack); 102 | }); 103 | } 104 | 105 | Experiment.prototype.updateRemote = function(client) { 106 | //create new experiment 107 | var expArgs = _.clone(this.attributes); 108 | expArgs['custom_css'] = this.getCSS(); 109 | expArgs['custom_js'] = this.getJS(); 110 | 111 | var self = this; 112 | return client.updateExperiment(expArgs).then(function(experimentAttrs) { 113 | logger.log("info", "updated remote experiment: " + experimentAttrs.id); 114 | }, function(error) { 115 | logger.log("error", error); 116 | }).catch(function(e) { 117 | logger.log("error", "unable to update remote experiment: " + e.message); 118 | console.error(e.stack); 119 | }); 120 | } 121 | 122 | Experiment.prototype.saveAttributes = function() { 123 | fileUtil.writeJSON(path.join(this.baseDir, Experiment.JSON_FILE_NAME), this 124 | .attributes); 125 | } 126 | 127 | Experiment.prototype.getOptcliURL = function() { 128 | var optcliURL; 129 | var appendToURL; 130 | optcliURL = this.attributes.edit_url; 131 | optcliURL.indexOf('?') === -1 ? 132 | appendToURL = '?optcli=activate' : 133 | appendToURL = '&optcli=activate'; 134 | optcliURL.indexOf('#') === -1 ? 135 | optcliURL += appendToURL : 136 | optcliURL = optcliURL.replace('#', appendToURL + '#'); 137 | 138 | return optcliURL; 139 | } 140 | 141 | module.exports = Experiment; 142 | -------------------------------------------------------------------------------- /lib/file-util.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var logger = require("./logger"); 3 | 4 | module.exports = { 5 | writeDir: function(name) { 6 | fs.mkdirSync(name); 7 | }, 8 | writeJSON: function(name, data) { 9 | fs.writeFileSync( 10 | name, 11 | data ? JSON.stringify(data, null, " ") : "" 12 | ); 13 | return data; 14 | }, 15 | writeText: function(name, data) { 16 | fs.writeFileSync( 17 | name, 18 | data || "" 19 | ); 20 | return data; 21 | }, 22 | loadConfigItem: function(fileName) { 23 | var configItem; 24 | if (fs.existsSync(fileName)){ 25 | configItem = JSON.parse( 26 | fs.readFileSync(fileName, { 27 | encoding: "utf-8" 28 | })); 29 | return configItem; 30 | } else { 31 | return false; 32 | } 33 | 34 | }, 35 | loadFile: function(fileName) { 36 | var theFile; 37 | if (fs.existsSync(fileName)){ 38 | theFile = fs.readFileSync(fileName, { 39 | encoding: "utf-8" 40 | }); 41 | return theFile; 42 | } else { 43 | return false; 44 | } 45 | } 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /lib/files.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | var fs = require('fs'); 3 | var logger = require("./logger"); 4 | var ejs = require("ejs"); 5 | 6 | var OptCLIBase = require("./optcli-base"); 7 | 8 | function Files(attributes, baseDir, assets) { 9 | Files.super_.call(this, attributes, baseDir); 10 | //get HTML files in the experiment folder 11 | this.filenames = fs.readdirSync(baseDir); 12 | 13 | this.data = {}; 14 | 15 | //check for HTML or EJS files 16 | if(this.filenames.length) { 17 | for(var i = this.filenames.length - 1; i >= 0; i--) { 18 | if(this.filenames[i].indexOf('.html') === -1 && this.filenames[i].indexOf('.ejs') === -1) { 19 | this.filenames.splice(i, 1); 20 | } 21 | } 22 | 23 | for(i = 0; i < this.filenames.length; i++) { 24 | var filename = this.filenames[i]; 25 | var filekey = filename.split(/(\.html|\.ejs)/)[0]; 26 | var filedata = fs.readFileSync(this.baseDir + '/' + filename, 'utf8'); 27 | 28 | // Run the file data through EJS if assets were passed. 29 | if(assets) { 30 | filedata = String(ejs.render(filedata, { 31 | locals: { 32 | assets: assets.attributes 33 | } 34 | })); 35 | } 36 | 37 | // Minify by removing line breaks, tabs, and excess white space 38 | filedata = filedata.replace(/(\t|\r\n|\r|\n)/gm,' '); 39 | filedata = filedata.replace(/ {2,}/g, ' '); 40 | filedata = filedata.replace(/> /g, '>'); 41 | filedata = filedata.replace(/ = levels.indexOf(logger.debugLevel) ) { 9 | if (typeof message !== 'string') { 10 | message = JSON.stringify(message); 11 | }; 12 | console.log(level+': '+message); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /lib/optcli-base.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var fs = require('fs'); 3 | 4 | var fileUtil = require('./file-util'); 5 | var logger = require('./logger'); 6 | 7 | function OptCLIBase(attributes, baseDir) { 8 | if (attributes) { 9 | this.attributes = attributes; 10 | } else { 11 | this.attributes = {}; 12 | } 13 | this.baseDir = (baseDir) ? baseDir : null; 14 | } 15 | 16 | OptCLIBase.prototype.setBaseDir = function(baseDir) { 17 | this.baseDir = baseDir; 18 | }; 19 | 20 | OptCLIBase.prototype.getJSONPath = function() { 21 | if (!this.baseDir) { 22 | logger.log("warn", "no base directory set"); 23 | return null; 24 | } 25 | return path.join(this.baseDir, this.constructor.JSON_FILE_NAME); 26 | }; 27 | 28 | OptCLIBase.prototype.loadFromFile = function() { 29 | if (this.JSONFileExists()){ 30 | this.attributes = fileUtil.loadConfigItem(this.getJSONPath()) || {}; 31 | return this.attributes; 32 | } else { 33 | return false; 34 | } 35 | }; 36 | 37 | OptCLIBase.prototype.JSONFileExists = function() { 38 | return fs.existsSync(this.getJSONPath()); 39 | }; 40 | 41 | OptCLIBase.prototype.getFilePath = function(filename){ 42 | if (!this.baseDir) { 43 | logger.log("warn", "no base directory set"); 44 | return null; 45 | } 46 | return path.join(this.baseDir, filename); 47 | }; 48 | 49 | module.exports = OptCLIBase; 50 | -------------------------------------------------------------------------------- /lib/project.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | var _ = require('lodash'); 3 | 4 | var fileUtil = require("./file-util"); 5 | var OptCLIBase = require("./optcli-base"); 6 | 7 | 8 | function Project(attributes, baseDir) { 9 | Project.super_.call(this, attributes, baseDir); 10 | } 11 | 12 | Project.JSON_FILE_NAME = "project.json"; 13 | 14 | util.inherits(Project, OptCLIBase); 15 | 16 | Project.createFromFile = function() { 17 | var project = new Project({}, "./"); 18 | if(!project.loadFromFile()) return false; 19 | return project; 20 | 21 | } 22 | 23 | Project.prototype.save = function() { 24 | var attrsToSave = _.pick(this.attributes, ['id', 'project_name', 'include_jquery']); 25 | 26 | fileUtil.writeJSON(Project.JSON_FILE_NAME, attrsToSave); 27 | } 28 | 29 | module.exports = Project; 30 | -------------------------------------------------------------------------------- /lib/read-config.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var q = require("q"); 3 | 4 | module.exports = function(name){ 5 | var d = q.defer(); 6 | try{ 7 | var result = fs.readFileSync(".optcli/" + name, {encoding:"utf-8"}); 8 | d.resolve(result); 9 | }catch(e){ 10 | //console.log(e.stack); 11 | switch(name){ 12 | case "token": 13 | require("./set-" + name + "")() 14 | .then(function(token){ 15 | d.resolve(token); 16 | }) 17 | .fail(function(reason){ 18 | console.log(name," ","not set") 19 | d.reject(reason); 20 | }) 21 | break; 22 | case "project": 23 | require("./set-" + name + "")() 24 | .then(function(token){ 25 | d.resolve(token); 26 | }) 27 | .fail(function(reason){ 28 | console.log(name," ","not set") 29 | d.reject(reason); 30 | }) 31 | break; 32 | default: 33 | console.log(name," ","not set") 34 | d.reject(e); 35 | } 36 | } 37 | return d.promise; 38 | } 39 | -------------------------------------------------------------------------------- /lib/server/assets/jquery.min.js: -------------------------------------------------------------------------------- 1 | /*! jQuery v1.6.4 http://jquery.com/ | http://jquery.org/license */ 2 | module.exports = function(a,b){function cu(a){return f.isWindow(a)?a:a.nodeType===9?a.defaultView||a.parentWindow:!1}function cr(a){if(!cg[a]){var b=c.body,d=f("<"+a+">").appendTo(b),e=d.css("display");d.remove();if(e==="none"||e===""){ch||(ch=c.createElement("iframe"),ch.frameBorder=ch.width=ch.height=0),b.appendChild(ch);if(!ci||!ch.createElement)ci=(ch.contentWindow||ch.contentDocument).document,ci.write((c.compatMode==="CSS1Compat"?"":"")+""),ci.close();d=ci.createElement(a),ci.body.appendChild(d),e=f.css(d,"display"),b.removeChild(ch)}cg[a]=e}return cg[a]}function cq(a,b){var c={};f.each(cm.concat.apply([],cm.slice(0,b)),function(){c[this]=a});return c}function cp(){cn=b}function co(){setTimeout(cp,0);return cn=f.now()}function cf(){try{return new a.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}}function ce(){try{return new a.XMLHttpRequest}catch(b){}}function b$(a,c){a.dataFilter&&(c=a.dataFilter(c,a.dataType));var d=a.dataTypes,e={},g,h,i=d.length,j,k=d[0],l,m,n,o,p;for(g=1;g0){c!=="border"&&f.each(e,function(){c||(d-=parseFloat(f.css(a,"padding"+this))||0),c==="margin"?d+=parseFloat(f.css(a,c+this))||0:d-=parseFloat(f.css(a,"border"+this+"Width"))||0});return d+"px"}d=bv(a,b,b);if(d<0||d==null)d=a.style[b]||0;d=parseFloat(d)||0,c&&f.each(e,function(){d+=parseFloat(f.css(a,"padding"+this))||0,c!=="padding"&&(d+=parseFloat(f.css(a,"border"+this+"Width"))||0),c==="margin"&&(d+=parseFloat(f.css(a,c+this))||0)});return d+"px"}function bl(a,b){b.src?f.ajax({url:b.src,async:!1,dataType:"script"}):f.globalEval((b.text||b.textContent||b.innerHTML||"").replace(bd,"/*$0*/")),b.parentNode&&b.parentNode.removeChild(b)}function bk(a){f.nodeName(a,"input")?bj(a):"getElementsByTagName"in a&&f.grep(a.getElementsByTagName("input"),bj)}function bj(a){if(a.type==="checkbox"||a.type==="radio")a.defaultChecked=a.checked}function bi(a){return"getElementsByTagName"in a?a.getElementsByTagName("*"):"querySelectorAll"in a?a.querySelectorAll("*"):[]}function bh(a,b){var c;if(b.nodeType===1){b.clearAttributes&&b.clearAttributes(),b.mergeAttributes&&b.mergeAttributes(a),c=b.nodeName.toLowerCase();if(c==="object")b.outerHTML=a.outerHTML;else if(c!=="input"||a.type!=="checkbox"&&a.type!=="radio"){if(c==="option")b.selected=a.defaultSelected;else if(c==="input"||c==="textarea")b.defaultValue=a.defaultValue}else a.checked&&(b.defaultChecked=b.checked=a.checked),b.value!==a.value&&(b.value=a.value);b.removeAttribute(f.expando)}}function bg(a,b){if(b.nodeType===1&&!!f.hasData(a)){var c=f.expando,d=f.data(a),e=f.data(b,d);if(d=d[c]){var g=d.events;e=e[c]=f.extend({},d);if(g){delete e.handle,e.events={};for(var h in g)for(var i=0,j=g[h].length;i=0===c})}function U(a){return!a||!a.parentNode||a.parentNode.nodeType===11}function M(a,b){return(a&&a!=="*"?a+".":"")+b.replace(y,"`").replace(z,"&")}function L(a){var b,c,d,e,g,h,i,j,k,l,m,n,o,p=[],q=[],r=f._data(this,"events");if(!(a.liveFired===this||!r||!r.live||a.target.disabled||a.button&&a.type==="click")){a.namespace&&(n=new RegExp("(^|\\.)"+a.namespace.split(".").join("\\.(?:.*\\.)?")+"(\\.|$)")),a.liveFired=this;var s=r.live.slice(0);for(i=0;ic)break;a.currentTarget=e.elem,a.data=e.handleObj.data,a.handleObj=e.handleObj,o=e.handleObj.origHandler.apply(e.elem,arguments);if(o===!1||a.isPropagationStopped()){c=e.level,o===!1&&(b=!1);if(a.isImmediatePropagationStopped())break}}return b}}function J(a,c,d){var e=f.extend({},d[0]);e.type=a,e.originalEvent={},e.liveFired=b,f.event.handle.call(c,e),e.isDefaultPrevented()&&d[0].preventDefault()}function D(){return!0}function C(){return!1}function m(a,c,d){var e=c+"defer",g=c+"queue",h=c+"mark",i=f.data(a,e,b,!0);i&&(d==="queue"||!f.data(a,g,b,!0))&&(d==="mark"||!f.data(a,h,b,!0))&&setTimeout(function(){!f.data(a,g,b,!0)&&!f.data(a,h,b,!0)&&(f.removeData(a,e,!0),i.resolve())},0)}function l(a){for(var b in a)if(b!=="toJSON")return!1;return!0}function k(a,c,d){if(d===b&&a.nodeType===1){var e="data-"+c.replace(j,"-$1").toLowerCase();d=a.getAttribute(e);if(typeof d=="string"){try{d=d==="true"?!0:d==="false"?!1:d==="null"?null:f.isNaN(d)?i.test(d)?f.parseJSON(d):d:parseFloat(d)}catch(g){}f.data(a,c,d)}else d=b}return d}var c=a.document,d=a.navigator,e=a.location,f=function(){function K(){if(!e.isReady){try{c.documentElement.doScroll("left")}catch(a){setTimeout(K,1);return}e.ready()}}var e=function(a,b){return new e.fn.init(a,b,h)},f=a.jQuery,g=a.$,h,i=/^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,j=/\S/,k=/^\s+/,l=/\s+$/,m=/\d/,n=/^<(\w+)\s*\/?>(?:<\/\1>)?$/,o=/^[\],:{}\s]*$/,p=/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,q=/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,r=/(?:^|:|,)(?:\s*\[)+/g,s=/(webkit)[ \/]([\w.]+)/,t=/(opera)(?:.*version)?[ \/]([\w.]+)/,u=/(msie) ([\w.]+)/,v=/(mozilla)(?:.*? rv:([\w.]+))?/,w=/-([a-z]|[0-9])/ig,x=/^-ms-/,y=function(a,b){return(b+"").toUpperCase()},z=d.userAgent,A,B,C,D=Object.prototype.toString,E=Object.prototype.hasOwnProperty,F=Array.prototype.push,G=Array.prototype.slice,H=String.prototype.trim,I=Array.prototype.indexOf,J={};e.fn=e.prototype={constructor:e,init:function(a,d,f){var g,h,j,k;if(!a)return this;if(a.nodeType){this.context=this[0]=a,this.length=1;return this}if(a==="body"&&!d&&c.body){this.context=c,this[0]=c.body,this.selector=a,this.length=1;return this}if(typeof a=="string"){a.charAt(0)!=="<"||a.charAt(a.length-1)!==">"||a.length<3?g=i.exec(a):g=[null,a,null];if(g&&(g[1]||!d)){if(g[1]){d=d instanceof e?d[0]:d,k=d?d.ownerDocument||d:c,j=n.exec(a),j?e.isPlainObject(d)?(a=[c.createElement(j[1])],e.fn.attr.call(a,d,!0)):a=[k.createElement(j[1])]:(j=e.buildFragment([g[1]],[k]),a=(j.cacheable?e.clone(j.fragment):j.fragment).childNodes);return e.merge(this,a)}h=c.getElementById(g[2]);if(h&&h.parentNode){if(h.id!==g[2])return f.find(a);this.length=1,this[0]=h}this.context=c,this.selector=a;return this}return!d||d.jquery?(d||f).find(a):this.constructor(d).find(a)}if(e.isFunction(a))return f.ready(a);a.selector!==b&&(this.selector=a.selector,this.context=a.context);return e.makeArray(a,this)},selector:"",jquery:"1.6.4",length:0,size:function(){return this.length},toArray:function(){return G.call(this,0)},get:function(a){return a==null?this.toArray():a<0?this[this.length+a]:this[a]},pushStack:function(a,b,c){var d=this.constructor();e.isArray(a)?F.apply(d,a):e.merge(d,a),d.prevObject=this,d.context=this.context,b==="find"?d.selector=this.selector+(this.selector?" ":"")+c:b&&(d.selector=this.selector+"."+b+"("+c+")");return d},each:function(a,b){return e.each(this,a,b)},ready:function(a){e.bindReady(),B.done(a);return this},eq:function(a){return a===-1?this.slice(a):this.slice(a,+a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(G.apply(this,arguments),"slice",G.call(arguments).join(","))},map:function(a){return this.pushStack(e.map(this,function(b,c){return a.call(b,c,b)}))},end:function(){return this.prevObject||this.constructor(null)},push:F,sort:[].sort,splice:[].splice},e.fn.init.prototype=e.fn,e.extend=e.fn.extend=function(){var a,c,d,f,g,h,i=arguments[0]||{},j=1,k=arguments.length,l=!1;typeof i=="boolean"&&(l=i,i=arguments[1]||{},j=2),typeof i!="object"&&!e.isFunction(i)&&(i={}),k===j&&(i=this,--j);for(;j0)return;B.resolveWith(c,[e]),e.fn.trigger&&e(c).trigger("ready").unbind("ready")}},bindReady:function(){if(!B){B=e._Deferred();if(c.readyState==="complete")return setTimeout(e.ready,1);if(c.addEventListener)c.addEventListener("DOMContentLoaded",C,!1),a.addEventListener("load",e.ready,!1);else if(c.attachEvent){c.attachEvent("onreadystatechange",C),a.attachEvent("onload",e.ready);var b=!1;try{b=a.frameElement==null}catch(d){}c.documentElement.doScroll&&b&&K()}}},isFunction:function(a){return e.type(a)==="function"},isArray:Array.isArray||function(a){return e.type(a)==="array"},isWindow:function(a){return a&&typeof a=="object"&&"setInterval"in a},isNaN:function(a){return a==null||!m.test(a)||isNaN(a)},type:function(a){return a==null?String(a):J[D.call(a)]||"object"},isPlainObject:function(a){if(!a||e.type(a)!=="object"||a.nodeType||e.isWindow(a))return!1;try{if(a.constructor&&!E.call(a,"constructor")&&!E.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}var d;for(d in a);return d===b||E.call(a,d)},isEmptyObject:function(a){for(var b in a)return!1;return!0},error:function(a){throw a},parseJSON:function(b){if(typeof b!="string"||!b)return null;b=e.trim(b);if(a.JSON&&a.JSON.parse)return a.JSON.parse(b);if(o.test(b.replace(p,"@").replace(q,"]").replace(r,"")))return(new Function("return "+b))();e.error("Invalid JSON: "+b)},parseXML:function(c){var d,f;try{a.DOMParser?(f=new DOMParser,d=f.parseFromString(c,"text/xml")):(d=new ActiveXObject("Microsoft.XMLDOM"),d.async="false",d.loadXML(c))}catch(g){d=b}(!d||!d.documentElement||d.getElementsByTagName("parsererror").length)&&e.error("Invalid XML: "+c);return d},noop:function(){},globalEval:function(b){b&&j.test(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(x,"ms-").replace(w,y)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toUpperCase()===b.toUpperCase()},each:function(a,c,d){var f,g=0,h=a.length,i=h===b||e.isFunction(a);if(d){if(i){for(f in a)if(c.apply(a[f],d)===!1)break}else for(;g0&&a[0]&&a[j-1]||j===0||e.isArray(a));if(k)for(;i1?h.call(arguments,0):c,--e||g.resolveWith(g,h.call(b,0))}}var b=arguments,c=0,d=b.length,e=d,g=d<=1&&a&&f.isFunction(a.promise)?a:f.Deferred();if(d>1){for(;c
a",d=a.getElementsByTagName("*"),e=a.getElementsByTagName("a")[0];if(!d||!d.length||!e)return{};g=c.createElement("select"),h=g.appendChild(c.createElement("option")),i=a.getElementsByTagName("input")[0],k={leadingWhitespace:a.firstChild.nodeType===3,tbody:!a.getElementsByTagName("tbody").length,htmlSerialize:!!a.getElementsByTagName("link").length,style:/top/.test(e.getAttribute("style")),hrefNormalized:e.getAttribute("href")==="/a",opacity:/^0.55$/.test(e.style.opacity),cssFloat:!!e.style.cssFloat,checkOn:i.value==="on",optSelected:h.selected,getSetAttribute:a.className!=="t",submitBubbles:!0,changeBubbles:!0,focusinBubbles:!1,deleteExpando:!0,noCloneEvent:!0,inlineBlockNeedsLayout:!1,shrinkWrapBlocks:!1,reliableMarginRight:!0},i.checked=!0,k.noCloneChecked=i.cloneNode(!0).checked,g.disabled=!0,k.optDisabled=!h.disabled;try{delete a.test}catch(v){k.deleteExpando=!1}!a.addEventListener&&a.attachEvent&&a.fireEvent&&(a.attachEvent("onclick",function(){k.noCloneEvent=!1}),a.cloneNode(!0).fireEvent("onclick")),i=c.createElement("input"),i.value="t",i.setAttribute("type","radio"),k.radioValue=i.value==="t",i.setAttribute("checked","checked"),a.appendChild(i),l=c.createDocumentFragment(),l.appendChild(a.firstChild),k.checkClone=l.cloneNode(!0).cloneNode(!0).lastChild.checked,a.innerHTML="",a.style.width=a.style.paddingLeft="1px",m=c.getElementsByTagName("body")[0],o=c.createElement(m?"div":"body"),p={visibility:"hidden",width:0,height:0,border:0,margin:0,background:"none"},m&&f.extend(p,{position:"absolute",left:"-1000px",top:"-1000px"});for(t in p)o.style[t]=p[t];o.appendChild(a),n=m||b,n.insertBefore(o,n.firstChild),k.appendChecked=i.checked,k.boxModel=a.offsetWidth===2,"zoom"in a.style&&(a.style.display="inline",a.style.zoom=1,k.inlineBlockNeedsLayout=a.offsetWidth===2,a.style.display="",a.innerHTML="
",k.shrinkWrapBlocks=a.offsetWidth!==2),a.innerHTML="
t
",q=a.getElementsByTagName("td"),u=q[0].offsetHeight===0,q[0].style.display="",q[1].style.display="none",k.reliableHiddenOffsets=u&&q[0].offsetHeight===0,a.innerHTML="",c.defaultView&&c.defaultView.getComputedStyle&&(j=c.createElement("div"),j.style.width="0",j.style.marginRight="0",a.appendChild(j),k.reliableMarginRight=(parseInt((c.defaultView.getComputedStyle(j,null)||{marginRight:0}).marginRight,10)||0)===0),o.innerHTML="",n.removeChild(o);if(a.attachEvent)for(t in{submit:1,change:1,focusin:1})s="on"+t,u=s in a,u||(a.setAttribute(s,"return;"),u=typeof a[s]=="function"),k[t+"Bubbles"]=u;o=l=g=h=m=j=a=i=null;return k}(),f.boxModel=f.support.boxModel;var i=/^(?:\{.*\}|\[.*\])$/,j=/([A-Z])/g;f.extend({cache:{},uuid:0,expando:"jQuery"+(f.fn.jquery+Math.random()).replace(/\D/g,""),noData:{embed:!0,object:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",applet:!0},hasData:function(a){a=a.nodeType?f.cache[a[f.expando]]:a[f.expando];return!!a&&!l(a)},data:function(a,c,d,e){if(!!f.acceptData(a)){var g,h,i=f.expando,j=typeof c=="string",k=a.nodeType,l=k?f.cache:a,m=k?a[f.expando]:a[f.expando]&&f.expando;if((!m||e&&m&&l[m]&&!l[m][i])&&j&&d===b)return;m||(k?a[f.expando]=m=++f.uuid:m=f.expando),l[m]||(l[m]={},k||(l[m].toJSON=f.noop));if(typeof c=="object"||typeof c=="function")e?l[m][i]=f.extend(l[m][i],c):l[m]=f.extend(l[m],c);g=l[m],e&&(g[i]||(g[i]={}),g=g[i]),d!==b&&(g[f.camelCase(c)]=d);if(c==="events"&&!g[c])return g[i]&&g[i].events;j?(h=g[c],h==null&&(h=g[f.camelCase(c)])):h=g;return h}},removeData:function(a,b,c){if(!!f.acceptData(a)){var d,e=f.expando,g=a.nodeType,h=g?f.cache:a,i=g?a[f.expando]:f.expando;if(!h[i])return;if(b){d=c?h[i][e]:h[i];if(d){d[b]||(b=f.camelCase(b)),delete d[b];if(!l(d))return}}if(c){delete h[i][e];if(!l(h[i]))return}var j=h[i][e];f.support.deleteExpando||!h.setInterval?delete h[i]:h[i]=null,j?(h[i]={},g||(h[i].toJSON=f.noop),h[i][e]=j):g&&(f.support.deleteExpando?delete a[f.expando]:a.removeAttribute?a.removeAttribute(f.expando):a[f.expando]=null)}},_data:function(a,b,c){return f.data(a,b,c,!0)},acceptData:function(a){if(a.nodeName){var b=f.noData[a.nodeName.toLowerCase()];if(b)return b!==!0&&a.getAttribute("classid")===b}return!0}}),f.fn.extend({data:function(a,c){var d=null;if(typeof a=="undefined"){if(this.length){d=f.data(this[0]);if(this[0].nodeType===1){var e=this[0].attributes,g;for(var h=0,i=e.length;h-1)return!0;return!1},val:function(a){var c,d,e=this[0];if(!arguments.length){if(e){c=f.valHooks[e.nodeName.toLowerCase()]||f.valHooks[e.type];if(c&&"get"in c&&(d=c.get(e,"value"))!==b)return d;d=e.value;return typeof d=="string"?d.replace(p,""):d==null?"":d}return b}var g=f.isFunction(a);return this.each(function(d){var e=f(this),h;if(this.nodeType===1){g?h=a.call(this,d,e.val()):h=a,h==null?h="":typeof h=="number"?h+="":f.isArray(h)&&(h=f.map(h,function(a){return a==null?"":a+""})),c=f.valHooks[this.nodeName.toLowerCase()]||f.valHooks[this.type];if(!c||!("set"in c)||c.set(this,h,"value")===b)this.value=h}})}}),f.extend({valHooks:{option:{get:function(a){var b=a.attributes.value;return!b||b.specified?a.value:a.text}},select:{get:function(a){var b,c=a.selectedIndex,d=[],e=a.options,g=a.type==="select-one";if(c<0)return null;for(var h=g?c:0,i=g?c+1:e.length;h=0}),c.length||(a.selectedIndex=-1);return c}}},attrFn:{val:!0,css:!0,html:!0,text:!0,data:!0,width:!0,height:!0,offset:!0},attrFix:{tabindex:"tabIndex"},attr:function(a,c,d,e){var g=a.nodeType;if(!a||g===3||g===8||g===2)return b;if(e&&c in f.attrFn)return f(a)[c](d);if(!("getAttribute"in a))return f.prop(a,c,d);var h,i,j=g!==1||!f.isXMLDoc(a);j&&(c=f.attrFix[c]||c,i=f.attrHooks[c],i||(t.test(c)?i=v:u&&(i=u)));if(d!==b){if(d===null){f.removeAttr(a,c);return b}if(i&&"set"in i&&j&&(h=i.set(a,d,c))!==b)return h;a.setAttribute(c,""+d);return d}if(i&&"get"in i&&j&&(h=i.get(a,c))!==null)return h;h=a.getAttribute(c);return h===null?b:h},removeAttr:function(a,b){var c;a.nodeType===1&&(b=f.attrFix[b]||b,f.attr(a,b,""),a.removeAttribute(b),t.test(b)&&(c=f.propFix[b]||b)in a&&(a[c]=!1))},attrHooks:{type:{set:function(a,b){if(q.test(a.nodeName)&&a.parentNode)f.error("type property can't be changed");else if(!f.support.radioValue&&b==="radio"&&f.nodeName(a,"input")){var c=a.value;a.setAttribute("type",b),c&&(a.value=c);return b}}},value:{get:function(a,b){if(u&&f.nodeName(a,"button"))return u.get(a,b);return b in a?a.value:null},set:function(a,b,c){if(u&&f.nodeName(a,"button"))return u.set(a,b,c);a.value=b}}},propFix:{tabindex:"tabIndex",readonly:"readOnly","for":"htmlFor","class":"className",maxlength:"maxLength",cellspacing:"cellSpacing",cellpadding:"cellPadding",rowspan:"rowSpan",colspan:"colSpan",usemap:"useMap",frameborder:"frameBorder",contenteditable:"contentEditable"},prop:function(a,c,d){var e=a.nodeType;if(!a||e===3||e===8||e===2)return b;var g,h,i=e!==1||!f.isXMLDoc(a);i&&(c=f.propFix[c]||c,h=f.propHooks[c]);return d!==b?h&&"set"in h&&(g=h.set(a,d,c))!==b?g:a[c]=d:h&&"get"in h&&(g=h.get(a,c))!==null?g:a[c]},propHooks:{tabIndex:{get:function(a){var c=a.getAttributeNode("tabindex");return c&&c.specified?parseInt(c.value,10):r.test(a.nodeName)||s.test(a.nodeName)&&a.href?0:b}}}}),f.attrHooks.tabIndex=f.propHooks.tabIndex,v={get:function(a,c){var d;return f.prop(a,c)===!0||(d=a.getAttributeNode(c))&&d.nodeValue!==!1?c.toLowerCase():b},set:function(a,b,c){var d;b===!1?f.removeAttr(a,c):(d=f.propFix[c]||c,d in a&&(a[d]=!0),a.setAttribute(c,c.toLowerCase()));return c}},f.support.getSetAttribute||(u=f.valHooks.button={get:function(a,c){var d;d=a.getAttributeNode(c);return d&&d.nodeValue!==""?d.nodeValue:b},set:function(a,b,d){var e=a.getAttributeNode(d);e||(e=c.createAttribute(d),a.setAttributeNode(e));return e.nodeValue=b+""}},f.each(["width","height"],function(a,b){f.attrHooks[b]=f.extend(f.attrHooks[b],{set:function(a,c){if(c===""){a.setAttribute(b,"auto");return c}}})})),f.support.hrefNormalized||f.each(["href","src","width","height"],function(a,c){f.attrHooks[c]=f.extend(f.attrHooks[c],{get:function(a){var d=a.getAttribute(c,2);return d===null?b:d}})}),f.support.style||(f.attrHooks.style={get:function(a){return a.style.cssText.toLowerCase()||b},set:function(a,b){return a.style.cssText=""+b}}),f.support.optSelected||(f.propHooks.selected=f.extend(f.propHooks.selected,{get:function(a){var b=a.parentNode;b&&(b.selectedIndex,b.parentNode&&b.parentNode.selectedIndex);return null}})),f.support.checkOn||f.each(["radio","checkbox"],function(){f.valHooks[this]={get:function(a){return a.getAttribute("value")===null?"on":a.value}}}),f.each(["radio","checkbox"],function(){f.valHooks[this]=f.extend(f.valHooks[this],{set:function(a,b){if(f.isArray(b))return a.checked=f.inArray(f(a).val(),b)>=0}})});var w=/\.(.*)$/,x=/^(?:textarea|input|select)$/i,y=/\./g,z=/ /g,A=/[^\w\s.|`]/g,B=function(a){return a.replace(A,"\\$&")};f.event={add:function(a,c,d,e){if(a.nodeType!==3&&a.nodeType!==8){if(d===!1)d=C;else if(!d)return;var g,h;d.handler&&(g=d,d=g.handler),d.guid||(d.guid=f.guid++);var i=f._data(a);if(!i)return;var j=i.events,k=i.handle;j||(i.events=j={}),k||(i.handle=k=function(a){return typeof f!="undefined"&&(!a||f.event.triggered!==a.type)?f.event.handle.apply(k.elem,arguments):b}),k.elem=a,c=c.split(" ");var l,m=0,n;while(l=c[m++]){h=g?f.extend({},g):{handler:d,data:e},l.indexOf(".")>-1?(n=l.split("."),l=n.shift(),h.namespace=n.slice(0).sort().join(".")):(n=[],h.namespace=""),h.type=l,h.guid||(h.guid=d.guid);var o=j[l],p=f.event.special[l]||{};if(!o){o=j[l]=[];if(!p.setup||p.setup.call(a,e,n,k)===!1)a.addEventListener?a.addEventListener(l,k,!1):a.attachEvent&&a.attachEvent("on"+l,k)}p.add&&(p.add.call(a,h),h.handler.guid||(h.handler.guid=d.guid)),o.push(h),f.event.global[l]=!0}a=null}},global:{},remove:function(a,c,d,e){if(a.nodeType!==3&&a.nodeType!==8){d===!1&&(d=C);var g,h,i,j,k=0,l,m,n,o,p,q,r,s=f.hasData(a)&&f._data(a),t=s&&s.events;if(!s||!t)return;c&&c.type&&(d=c.handler,c=c.type);if(!c||typeof c=="string"&&c.charAt(0)==="."){c=c||"";for(h in t)f.event.remove(a,h+c);return}c=c.split(" ");while(h=c[k++]){r=h,q=null,l=h.indexOf(".")<0,m=[],l||(m=h.split("."),h=m.shift(),n=new RegExp("(^|\\.)"+f.map(m.slice(0).sort(),B).join("\\.(?:.*\\.)?")+"(\\.|$)")),p=t[h];if(!p)continue;if(!d){for(j=0;j=0&&(h=h.slice(0,-1),j=!0),h.indexOf(".")>=0&&(i=h.split("."),h=i.shift(),i.sort());if(!!e&&!f.event.customEvent[h]||!!f.event.global[h]){c=typeof c=="object"?c[f.expando]?c:new f.Event(h,c):new f.Event(h),c.type=h,c.exclusive=j,c.namespace=i.join("."),c.namespace_re=new RegExp("(^|\\.)"+i.join("\\.(?:.*\\.)?")+"(\\.|$)");if(g||!e)c.preventDefault(),c.stopPropagation();if(!e){f.each(f.cache,function(){var a=f.expando,b=this[a];b&&b.events&&b.events[h]&&f.event.trigger(c,d,b.handle.elem)});return}if(e.nodeType===3||e.nodeType===8)return;c.result=b,c.target=e,d=d!=null?f.makeArray(d):[],d.unshift(c);var k=e,l=h.indexOf(":")<0?"on"+h:"";do{var m=f._data(k,"handle");c.currentTarget=k,m&&m.apply(k,d),l&&f.acceptData(k)&&k[l]&&k[l].apply(k,d)===!1&&(c.result=!1,c.preventDefault()),k=k.parentNode||k.ownerDocument||k===c.target.ownerDocument&&a}while(k&&!c.isPropagationStopped());if(!c.isDefaultPrevented()){var n,o=f.event.special[h]||{};if((!o._default||o._default.call(e.ownerDocument,c)===!1)&&(h!=="click"||!f.nodeName(e,"a"))&&f.acceptData(e)){try{l&&e[h]&&(n=e[l],n&&(e[l]=null),f.event.triggered=h,e[h]())}catch(p){}n&&(e[l]=n),f.event.triggered=b}}return c.result}},handle:function(c){c=f.event.fix(c||a.event);var d=((f._data(this,"events")||{})[c.type]||[]).slice(0),e=!c.exclusive&&!c.namespace,g=Array.prototype.slice.call(arguments,0);g[0]=c,c.currentTarget=this;for(var h=0,i=d.length;h-1?f.map(a.options,function(a){return a.selected}).join("-"):"":f.nodeName(a,"select")&&(c=a.selectedIndex);return c},I=function(c){var d=c.target,e,g;if(!!x.test(d.nodeName)&&!d.readOnly){e=f._data(d,"_change_data"),g=H(d),(c.type!=="focusout"||d.type!=="radio")&&f._data(d,"_change_data",g);if(e===b||g===e)return;if(e!=null||g)c.type="change",c.liveFired=b,f.event.trigger(c,arguments[1],d)}};f.event.special.change={filters:{focusout:I,beforedeactivate:I,click:function(a){var b=a.target,c=f.nodeName(b,"input")?b.type:"";(c==="radio"||c==="checkbox"||f.nodeName(b,"select"))&&I.call(this,a)},keydown:function(a){var b=a.target,c=f.nodeName(b,"input")?b.type:"";(a.keyCode===13&&!f.nodeName(b,"textarea")||a.keyCode===32&&(c==="checkbox"||c==="radio")||c==="select-multiple")&&I.call(this,a)},beforeactivate:function(a){var b=a.target;f._data(b,"_change_data",H(b))}},setup:function(a,b){if(this.type==="file")return!1;for(var c in G)f.event.add(this,c+".specialChange",G[c]);return x.test(this.nodeName)},teardown:function(a){f.event.remove(this,".specialChange");return x.test(this.nodeName)}},G=f.event.special.change.filters,G.focus=G.beforeactivate}f.support.focusinBubbles||f.each({focus:"focusin",blur:"focusout"},function(a,b){function e(a){var c=f.event.fix(a);c.type=b,c.originalEvent={},f.event.trigger(c,null,c.target),c.isDefaultPrevented()&&a.preventDefault()}var d=0;f.event.special[b]={setup:function(){d++===0&&c.addEventListener(a,e,!0)},teardown:function(){--d===0&&c.removeEventListener(a,e,!0)}}}),f.each(["bind","one"],function(a,c){f.fn[c]=function(a,d,e){var g;if(typeof a=="object"){for(var h in a)this[c](h,d,a[h],e);return this}if(arguments.length===2||d===!1)e=d,d=b;c==="one"?(g=function(a){f(this).unbind(a,g);return e.apply(this,arguments)},g.guid=e.guid||f.guid++):g=e;if(a==="unload"&&c!=="one")this.one(a,d,e);else for(var i=0,j=this.length;i0?this.bind(b,a,c):this.trigger(b)},f.attrFn&&(f.attrFn[b]=!0)}),function(){function u(a,b,c,d,e,f){for(var g=0,h=d.length;g0){j=i;break}}i=i[a]}d[g]=j}}}function t(a,b,c,d,e,f){for(var g=0,h=d.length;g+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,d=0,e=Object.prototype.toString,g=!1,h=!0,i=/\\/g,j=/\W/;[0,0].sort(function(){h=!1;return 0});var k=function(b,d,f,g){f=f||[],d=d||c;var h=d;if(d.nodeType!==1&&d.nodeType!==9)return[];if(!b||typeof b!="string")return f;var i,j,n,o,q,r,s,t,u=!0,w=k.isXML(d),x=[],y=b;do{a.exec(""),i=a.exec(y);if(i){y=i[3],x.push(i[1]);if(i[2]){o=i[3];break}}}while(i);if(x.length>1&&m.exec(b))if(x.length===2&&l.relative[x[0]])j=v(x[0]+x[1],d);else{j=l.relative[x[0]]?[d]:k(x.shift(),d);while(x.length)b=x.shift(),l.relative[b]&&(b+=x.shift()),j=v(b,j)}else{!g&&x.length>1&&d.nodeType===9&&!w&&l.match.ID.test(x[0])&&!l.match.ID.test(x[x.length-1])&&(q=k.find(x.shift(),d,w),d=q.expr?k.filter(q.expr,q.set)[0]:q.set[0]);if(d){q=g?{expr:x.pop(),set:p(g)}:k.find(x.pop(),x.length===1&&(x[0]==="~"||x[0]==="+")&&d.parentNode?d.parentNode:d,w),j=q.expr?k.filter(q.expr,q.set):q.set,x.length>0?n=p(j):u=!1;while(x.length)r=x.pop(),s=r,l.relative[r]?s=x.pop():r="",s==null&&(s=d),l.relative[r](n,s,w)}else n=x=[]}n||(n=j),n||k.error(r||b);if(e.call(n)==="[object Array]")if(!u)f.push.apply(f,n);else if(d&&d.nodeType===1)for(t=0;n[t]!=null;t++)n[t]&&(n[t]===!0||n[t].nodeType===1&&k.contains(d,n[t]))&&f.push(j[t]);else for(t=0;n[t]!=null;t++)n[t]&&n[t].nodeType===1&&f.push(j[t]);else p(n,f);o&&(k(o,h,f,g),k.uniqueSort(f));return f};k.uniqueSort=function(a){if(r){g=h,a.sort(r);if(g)for(var b=1;b0},k.find=function(a,b,c){var d;if(!a)return[];for(var e=0,f=l.order.length;e":function(a,b){var c,d=typeof b=="string",e=0,f=a.length;if(d&&!j.test(b)){b=b.toLowerCase();for(;e=0)?c||d.push(h):c&&(b[g]=!1));return!1},ID:function(a){return a[1].replace(i,"")},TAG:function(a,b){return a[1].replace(i,"").toLowerCase()},CHILD:function(a){if(a[1]==="nth"){a[2]||k.error(a[0]),a[2]=a[2].replace(/^\+|\s*/g,"");var b=/(-?)(\d*)(?:n([+\-]?\d*))?/.exec(a[2]==="even"&&"2n"||a[2]==="odd"&&"2n+1"||!/\D/.test(a[2])&&"0n+"+a[2]||a[2]);a[2]=b[1]+(b[2]||1)-0,a[3]=b[3]-0}else a[2]&&k.error(a[0]);a[0]=d++;return a},ATTR:function(a,b,c,d,e,f){var g=a[1]=a[1].replace(i,"");!f&&l.attrMap[g]&&(a[1]=l.attrMap[g]),a[4]=(a[4]||a[5]||"").replace(i,""),a[2]==="~="&&(a[4]=" "+a[4]+" ");return a},PSEUDO:function(b,c,d,e,f){if(b[1]==="not")if((a.exec(b[3])||"").length>1||/^\w/.test(b[3]))b[3]=k(b[3],null,null,c);else{var g=k.filter(b[3],c,d,!0^f);d||e.push.apply(e,g);return!1}else if(l.match.POS.test(b[0])||l.match.CHILD.test(b[0]))return!0;return b},POS:function(a){a.unshift(!0);return a}},filters:{enabled:function(a){return a.disabled===!1&&a.type!=="hidden"},disabled:function(a){return a.disabled===!0},checked:function(a){return a.checked===!0},selected:function(a){a.parentNode&&a.parentNode.selectedIndex;return a.selected===!0},parent:function(a){return!!a.firstChild},empty:function(a){return!a.firstChild},has:function(a,b,c){return!!k(c[3],a).length},header:function(a){return/h\d/i.test(a.nodeName)},text:function(a){var b=a.getAttribute("type"),c=a.type;return a.nodeName.toLowerCase()==="input"&&"text"===c&&(b===c||b===null)},radio:function(a){return a.nodeName.toLowerCase()==="input"&&"radio"===a.type},checkbox:function(a){return a.nodeName.toLowerCase()==="input"&&"checkbox"===a.type},file:function(a){return a.nodeName.toLowerCase()==="input"&&"file"===a.type},password:function(a){return a.nodeName.toLowerCase()==="input"&&"password"===a.type},submit:function(a){var b=a.nodeName.toLowerCase();return(b==="input"||b==="button")&&"submit"===a.type},image:function(a){return a.nodeName.toLowerCase()==="input"&&"image"===a.type},reset:function(a){var b=a.nodeName.toLowerCase();return(b==="input"||b==="button")&&"reset"===a.type},button:function(a){var b=a.nodeName.toLowerCase();return b==="input"&&"button"===a.type||b==="button"},input:function(a){return/input|select|textarea|button/i.test(a.nodeName)},focus:function(a){return a===a.ownerDocument.activeElement}},setFilters:{first:function(a,b){return b===0},last:function(a,b,c,d){return b===d.length-1},even:function(a,b){return b%2===0},odd:function(a,b){return b%2===1},lt:function(a,b,c){return bc[3]-0},nth:function(a,b,c){return c[3]-0===b},eq:function(a,b,c){return c[3]-0===b}},filter:{PSEUDO:function(a,b,c,d){var e=b[1],f=l.filters[e];if(f)return f(a,c,b,d);if(e==="contains")return(a.textContent||a.innerText||k.getText([a])||"").indexOf(b[3])>=0;if(e==="not"){var g=b[3];for(var h=0,i=g.length;h=0}},ID:function(a,b){return a.nodeType===1&&a.getAttribute("id")===b},TAG:function(a,b){return b==="*"&&a.nodeType===1||a.nodeName.toLowerCase()===b},CLASS:function(a,b){return(" "+(a.className||a.getAttribute("class"))+" ").indexOf(b)>-1},ATTR:function(a,b){var c=b[1],d=l.attrHandle[c]?l.attrHandle[c](a):a[c]!=null?a[c]:a.getAttribute(c),e=d+"",f=b[2],g=b[4];return d==null?f==="!=":f==="="?e===g:f==="*="?e.indexOf(g)>=0:f==="~="?(" "+e+" ").indexOf(g)>=0:g?f==="!="?e!==g:f==="^="?e.indexOf(g)===0:f==="$="?e.substr(e.length-g.length)===g:f==="|="?e===g||e.substr(0,g.length+1)===g+"-":!1:e&&d!==!1},POS:function(a,b,c,d){var e=b[2],f=l.setFilters[e];if(f)return f(a,c,b,d)}}},m=l.match.POS,n=function(a,b){return"\\"+(b-0+1)};for(var o in l.match)l.match[o]=new RegExp(l.match[o].source+/(?![^\[]*\])(?![^\(]*\))/.source),l.leftMatch[o]=new RegExp(/(^(?:.|\r|\n)*?)/.source+l.match[o].source.replace(/\\(\d+)/g,n));var p=function(a,b){a=Array.prototype.slice.call(a,0);if(b){b.push.apply(b,a);return b}return a};try{Array.prototype.slice.call(c.documentElement.childNodes,0)[0].nodeType}catch(q){p=function(a,b){var c=0,d=b||[];if(e.call(a)==="[object Array]")Array.prototype.push.apply(d,a);else if(typeof a.length=="number")for(var f=a.length;c",e.insertBefore(a,e.firstChild),c.getElementById(d)&&(l.find.ID=function(a,c,d){if(typeof c.getElementById!="undefined"&&!d){var e=c.getElementById(a[1]);return e?e.id===a[1]||typeof e.getAttributeNode!="undefined"&&e.getAttributeNode("id").nodeValue===a[1]?[e]:b:[]}},l.filter.ID=function(a,b){var c=typeof a.getAttributeNode!="undefined"&&a.getAttributeNode("id");return a.nodeType===1&&c&&c.nodeValue===b}),e.removeChild(a),e=a=null}(),function(){var a=c.createElement("div");a.appendChild(c.createComment("")),a.getElementsByTagName("*").length>0&&(l.find.TAG=function(a,b){var c=b.getElementsByTagName(a[1]);if(a[1]==="*"){var d=[];for(var e=0;c[e];e++)c[e].nodeType===1&&d.push(c[e]);c=d}return c}),a.innerHTML="",a.firstChild&&typeof a.firstChild.getAttribute!="undefined"&&a.firstChild.getAttribute("href")!=="#"&&(l.attrHandle.href=function(a){return a.getAttribute("href",2)}),a=null}(),c.querySelectorAll&&function(){var a=k,b=c.createElement("div"),d="__sizzle__";b.innerHTML="

";if(!b.querySelectorAll||b.querySelectorAll(".TEST").length!==0){k=function(b,e,f,g){e=e||c;if(!g&&!k.isXML(e)){var h=/^(\w+$)|^\.([\w\-]+$)|^#([\w\-]+$)/.exec(b);if(h&&(e.nodeType===1||e.nodeType===9)){if(h[1])return p(e.getElementsByTagName(b),f);if(h[2]&&l.find.CLASS&&e.getElementsByClassName)return p(e.getElementsByClassName(h[2]),f)}if(e.nodeType===9){if(b==="body"&&e.body)return p([e.body],f);if(h&&h[3]){var i=e.getElementById(h[3]);if(!i||!i.parentNode)return p([],f);if(i.id===h[3])return p([i],f)}try{return p(e.querySelectorAll(b),f)}catch(j){}}else if(e.nodeType===1&&e.nodeName.toLowerCase()!=="object"){var m=e,n=e.getAttribute("id"),o=n||d,q=e.parentNode,r=/^\s*[+~]/.test(b);n?o=o.replace(/'/g,"\\$&"):e.setAttribute("id",o),r&&q&&(e=e.parentNode);try{if(!r||q)return p(e.querySelectorAll("[id='"+o+"'] "+b),f)}catch(s){}finally{n||m.removeAttribute("id")}}}return a(b,e,f,g)};for(var e in a)k[e]=a[e];b=null}}(),function(){var a=c.documentElement,b=a.matchesSelector||a.mozMatchesSelector||a.webkitMatchesSelector||a.msMatchesSelector;if(b){var d=!b.call(c.createElement("div"),"div"),e=!1;try{b.call(c.documentElement,"[test!='']:sizzle")}catch(f){e=!0}k.matchesSelector=function(a,c){c=c.replace(/\=\s*([^'"\]]*)\s*\]/g,"='$1']");if(!k.isXML(a))try{if(e||!l.match.PSEUDO.test(c)&&!/!=/.test(c)){var f=b.call(a,c);if(f||!d||a.document&&a.document.nodeType!==11)return f}}catch(g){}return k(c,null,null,[a]).length>0}}}(),function(){var a=c.createElement("div");a.innerHTML="
";if(!!a.getElementsByClassName&&a.getElementsByClassName("e").length!==0){a.lastChild.className="e";if(a.getElementsByClassName("e").length===1)return;l.order.splice(1,0,"CLASS"),l.find.CLASS=function(a,b,c){if(typeof b.getElementsByClassName!="undefined"&&!c)return b.getElementsByClassName(a[1])},a=null}}(),c.documentElement.contains?k.contains=function(a,b){return a!==b&&(a.contains?a.contains(b):!0)}:c.documentElement.compareDocumentPosition?k.contains=function(a,b){return!!(a.compareDocumentPosition(b)&16)}:k.contains=function(){return!1},k.isXML=function(a){var b=(a?a.ownerDocument||a:0).documentElement;return b?b.nodeName!=="HTML":!1};var v=function(a,b){var c,d=[],e="",f=b.nodeType?[b]:b;while(c=l.match.PSEUDO.exec(a))e+=c[0],a=a.replace(l.match.PSEUDO,"");a=l.relative[a]?a+"*":a;for(var g=0,h=f.length;g0)for(h=g;h0:this.filter(a).length>0)},closest:function(a,b){var c=[],d,e,g=this[0];if(f.isArray(a)){var h,i,j={},k=1;if(g&&a.length){for(d=0,e=a.length;d-1:f(g).is(h))&&c.push({selector:i,elem:g,level:k});g=g.parentNode,k++}}return c}var l=S.test(a)||typeof a!="string"?f(a,b||this.context):0;for(d=0,e=this.length;d-1:f.find.matchesSelector(g,a)){c.push(g);break}g=g.parentNode;if(!g||!g.ownerDocument||g===b||g.nodeType===11)break}}c=c.length>1?f.unique(c):c;return this.pushStack(c,"closest",a)},index:function(a){if(!a)return this[0]&&this[0].parentNode?this.prevAll().length:-1;if(typeof a=="string")return f.inArray(this[0],f(a));return f.inArray(a.jquery?a[0]:a,this)},add:function(a,b){var c=typeof a=="string"?f(a,b):f.makeArray(a&&a.nodeType?[a]:a),d=f.merge(this.get(),c);return this.pushStack(U(c[0])||U(d[0])?d:f.unique(d))},andSelf:function(){return this.add(this.prevObject)}}),f.each({parent:function(a){var b=a.parentNode;return b&&b.nodeType!==11?b:null},parents:function(a){return f.dir(a,"parentNode")},parentsUntil:function(a,b,c){return f.dir(a,"parentNode",c)},next:function(a){return f.nth(a,2,"nextSibling")},prev:function(a){return f.nth(a,2,"previousSibling")},nextAll:function(a){return f.dir(a,"nextSibling")},prevAll:function(a){return f.dir(a,"previousSibling")},nextUntil:function(a,b,c){return f.dir(a,"nextSibling",c)},prevUntil:function(a,b,c){return f.dir(a,"previousSibling",c)},siblings:function(a){return f.sibling(a.parentNode.firstChild,a)},children:function(a){return f.sibling(a.firstChild)},contents:function(a){return f.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:f.makeArray(a.childNodes)}},function(a,b){f.fn[a]=function(c,d){var e=f.map(this,b,c),g=R.call(arguments);N.test(a)||(d=c),d&&typeof d=="string"&&(e=f.filter(d,e)),e=this.length>1&&!T[a]?f.unique(e):e,(this.length>1||P.test(d))&&O.test(a)&&(e=e.reverse());return this.pushStack(e,a,g.join(","))}}),f.extend({filter:function(a,b,c){c&&(a=":not("+a+")");return b.length===1?f.find.matchesSelector(b[0],a)?[b[0]]:[]:f.find.matches(a,b)},dir:function(a,c,d){var e=[],g=a[c];while(g&&g.nodeType!==9&&(d===b||g.nodeType!==1||!f(g).is(d)))g.nodeType===1&&e.push(g),g=g[c];return e},nth:function(a,b,c,d){b=b||1;var e=0;for(;a;a=a[c])if(a.nodeType===1&&++e===b)break;return a},sibling:function(a,b){var c=[];for(;a;a=a.nextSibling)a.nodeType===1&&a!==b&&c.push(a);return c}});var W=/ jQuery\d+="(?:\d+|null)"/g,X=/^\s+/,Y=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,Z=/<([\w:]+)/,$=/",""],legend:[1,"
","
"],thead:[1,"","
"],tr:[2,"","
"],td:[3,"","
"],col:[2,"","
"],area:[1,"",""],_default:[0,"",""]};be.optgroup=be.option,be.tbody=be.tfoot=be.colgroup=be.caption=be.thead,be.th=be.td,f.support.htmlSerialize||(be._default=[1,"div
","
"]),f.fn.extend({text:function(a){if(f.isFunction(a))return this.each(function(b){var c=f(this);c.text(a.call(this,b,c.text()))});if(typeof a!="object"&&a!==b)return this.empty().append((this[0]&&this[0].ownerDocument||c).createTextNode(a));return f.text(this)},wrapAll:function(a){if(f.isFunction(a))return this.each(function(b){f(this).wrapAll(a.call(this,b))});if(this[0]){var b=f(a,this[0].ownerDocument).eq(0).clone(!0);this[0].parentNode&&b.insertBefore(this[0]),b.map(function(){var a=this;while(a.firstChild&&a.firstChild.nodeType===1)a=a.firstChild;return a}).append(this)}return this},wrapInner:function(a){if(f.isFunction(a))return this.each(function(b){f(this).wrapInner(a.call(this,b))});return this.each(function(){var b=f(this),c=b.contents();c.length?c.wrapAll(a):b.append(a)})},wrap:function(a){return this.each(function(){f(this).wrapAll(a)})},unwrap:function(){return this.parent().each(function(){f.nodeName(this,"body")||f(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.insertBefore(a,this.firstChild)})},before:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this)});if(arguments.length){var a=f(arguments[0]);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}},after:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this.nextSibling)});if(arguments.length){var a=this.pushStack(this,"after",arguments);a.push.apply(a,f(arguments[0]).toArray());return a}},remove:function(a,b){for(var c=0,d;(d=this[c])!=null;c++)if(!a||f.filter(a,[d]).length)!b&&d.nodeType===1&&(f.cleanData(d.getElementsByTagName("*")),f.cleanData([d])),d.parentNode&&d.parentNode.removeChild(d);return this},empty:function(){for(var a=0,b;(b=this[a])!=null;a++){b.nodeType===1&&f.cleanData(b.getElementsByTagName("*"));while(b.firstChild)b.removeChild(b.firstChild)}return this},clone:function(a,b){a=a==null?!1:a,b=b==null?a:b;return this.map(function(){return f.clone(this,a,b)})},html:function(a){if(a===b)return this[0]&&this[0].nodeType===1?this[0].innerHTML.replace(W,""):null;if(typeof a=="string"&&!ba.test(a)&&(f.support.leadingWhitespace||!X.test(a))&&!be[(Z.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(Y,"<$1>");try{for(var c=0,d=this.length;c1&&l0?this.clone(!0):this).get();f(e[h])[b](j),d=d.concat(j)}return this.pushStack(d,a,e.selector)}}),f.extend({clone:function(a,b,c){var d=a.cloneNode(!0),e,g,h;if((!f.support.noCloneEvent||!f.support.noCloneChecked)&&(a.nodeType===1||a.nodeType===11)&&!f.isXMLDoc(a)){bh(a,d),e=bi(a),g=bi(d);for(h=0;e[h];++h)g[h]&&bh(e[h],g[h])}if(b){bg(a,d);if(c){e=bi(a),g=bi(d);for(h=0;e[h];++h)bg(e[h],g[h])}}e=g=null;return d},clean:function(a,b,d,e){var g;b=b||c,typeof b.createElement=="undefined"&&(b=b.ownerDocument||b[0]&&b[0].ownerDocument||c);var h=[],i;for(var j=0,k;(k=a[j])!=null;j++){typeof k=="number"&&(k+="");if(!k)continue;if(typeof k=="string")if(!_.test(k))k=b.createTextNode(k);else{k=k.replace(Y,"<$1>");var l=(Z.exec(k)||["",""])[1].toLowerCase(),m=be[l]||be._default,n=m[0],o=b.createElement("div");o.innerHTML=m[1]+k+m[2];while(n--)o=o.lastChild;if(!f.support.tbody){var p=$.test(k),q=l==="table"&&!p?o.firstChild&&o.firstChild.childNodes:m[1]===""&&!p?o.childNodes:[];for(i=q.length-1;i>=0;--i)f.nodeName(q[i],"tbody")&&!q[i].childNodes.length&&q[i].parentNode.removeChild(q[i])}!f.support.leadingWhitespace&&X.test(k)&&o.insertBefore(b.createTextNode(X.exec(k)[0]),o.firstChild),k=o.childNodes}var r;if(!f.support.appendChecked)if(k[0]&&typeof (r=k.length)=="number")for(i=0;i=0)return b+"px"}}}),f.support.opacity||(f.cssHooks.opacity={get:function(a,b){return bn.test((b&&a.currentStyle?a.currentStyle.filter:a.style.filter)||"")?parseFloat(RegExp.$1)/100+"":b?"1":""},set:function(a,b){var c=a.style,d=a.currentStyle,e=f.isNaN(b)?"":"alpha(opacity="+b*100+")",g=d&&d.filter||c.filter||"";c.zoom=1;if(b>=1&&f.trim(g.replace(bm,""))===""){c.removeAttribute("filter");if(d&&!d.filter)return}c.filter=bm.test(g)?g.replace(bm,e):g+" "+e}}),f(function(){f.support.reliableMarginRight||(f.cssHooks.marginRight={get:function(a,b){var c;f.swap(a,{display:"inline-block"},function(){b?c=bv(a,"margin-right","marginRight"):c=a.style.marginRight});return c}})}),c.defaultView&&c.defaultView.getComputedStyle&&(bw=function(a,c){var d,e,g;c=c.replace(bo,"-$1").toLowerCase();if(!(e=a.ownerDocument.defaultView))return b;if(g=e.getComputedStyle(a,null))d=g.getPropertyValue(c),d===""&&!f.contains(a.ownerDocument.documentElement,a)&&(d=f.style(a,c));return d}),c.documentElement.currentStyle&&(bx=function(a,b){var c,d=a.currentStyle&&a.currentStyle[b],e=a.runtimeStyle&&a.runtimeStyle[b],f=a.style;!bp.test(d)&&bq.test(d)&&(c=f.left,e&&(a.runtimeStyle.left=a.currentStyle.left),f.left=b==="fontSize"?"1em":d||0,d=f.pixelLeft+"px",f.left=c,e&&(a.runtimeStyle.left=e));return d===""?"auto":d}),bv=bw||bx,f.expr&&f.expr.filters&&(f.expr.filters.hidden=function(a){var b=a.offsetWidth,c=a.offsetHeight;return b===0&&c===0||!f.support.reliableHiddenOffsets&&(a.style.display||f.css(a,"display"))==="none"},f.expr.filters.visible=function(a){return!f.expr.filters.hidden(a)});var bz=/%20/g,bA=/\[\]$/,bB=/\r?\n/g,bC=/#.*$/,bD=/^(.*?):[ \t]*([^\r\n]*)\r?$/mg,bE=/^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,bF=/^(?:about|app|app\-storage|.+\-extension|file|res|widget):$/,bG=/^(?:GET|HEAD)$/,bH=/^\/\//,bI=/\?/,bJ=/)<[^<]*)*<\/script>/gi,bK=/^(?:select|textarea)/i,bL=/\s+/,bM=/([?&])_=[^&]*/,bN=/^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/,bO=f.fn.load,bP={},bQ={},bR,bS,bT=["*/"]+["*"];try{bR=e.href}catch(bU){bR=c.createElement("a"),bR.href="",bR=bR.href}bS=bN.exec(bR.toLowerCase())||[],f.fn.extend({load:function(a,c,d){if(typeof a!="string"&&bO)return bO.apply(this,arguments);if(!this.length)return this;var e=a.indexOf(" ");if(e>=0){var g=a.slice(e,a.length);a=a.slice(0,e)}var h="GET";c&&(f.isFunction(c)?(d=c,c=b):typeof c=="object"&&(c=f.param(c,f.ajaxSettings.traditional),h="POST"));var i=this;f.ajax({url:a,type:h,dataType:"html",data:c,complete:function(a,b,c){c=a.responseText,a.isResolved()&&(a.done(function(a){c=a}),i.html(g?f("
").append(c.replace(bJ,"")).find(g):c)),d&&i.each(d,[c,b,a])}});return this},serialize:function(){return f.param(this.serializeArray())},serializeArray:function(){return this.map(function(){return this.elements?f.makeArray(this.elements):this}).filter(function(){return this.name&&!this.disabled&&(this.checked||bK.test(this.nodeName)||bE.test(this.type))}).map(function(a,b){var c=f(this).val();return c==null?null:f.isArray(c)?f.map(c,function(a,c){return{name:b.name,value:a.replace(bB,"\r\n")}}):{name:b.name,value:c.replace(bB,"\r\n")}}).get()}}),f.each("ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".split(" "),function(a,b){f.fn[b]=function(a){return this.bind(b,a)}}),f.each(["get","post"],function(a,c){f[c]=function(a,d,e,g){f.isFunction(d)&&(g=g||e,e=d,d=b);return f.ajax({type:c,url:a,data:d,success:e,dataType:g})}}),f.extend({getScript:function(a,c){return f.get(a,b,c,"script")},getJSON:function(a,b,c){return f.get(a,b,c,"json")},ajaxSetup:function(a,b){b?bX(a,f.ajaxSettings):(b=a,a=f.ajaxSettings),bX(a,b);return a},ajaxSettings:{url:bR,isLocal:bF.test(bS[1]),global:!0,type:"GET",contentType:"application/x-www-form-urlencoded",processData:!0,async:!0,accepts:{xml:"application/xml, text/xml",html:"text/html",text:"text/plain",json:"application/json, text/javascript","*":bT},contents:{xml:/xml/,html:/html/,json:/json/},responseFields:{xml:"responseXML",text:"responseText"},converters:{"* text":a.String,"text html":!0,"text json":f.parseJSON,"text xml":f.parseXML},flatOptions:{context:!0,url:!0}},ajaxPrefilter:bV(bP),ajaxTransport:bV(bQ),ajax:function(a,c){function w(a,c,l,m){if(s!==2){s=2,q&&clearTimeout(q),p=b,n=m||"",v.readyState=a>0?4:0;var o,r,u,w=c,x=l?bZ(d,v,l):b,y,z;if(a>=200&&a<300||a===304){if(d.ifModified){if(y=v.getResponseHeader("Last-Modified"))f.lastModified[k]=y;if(z=v.getResponseHeader("Etag"))f.etag[k]=z}if(a===304)w="notmodified",o=!0;else try{r=b$(d,x),w="success",o=!0}catch(A){w="parsererror",u=A}}else{u=w;if(!w||a)w="error",a<0&&(a=0)}v.status=a,v.statusText=""+(c||w),o?h.resolveWith(e,[r,w,v]):h.rejectWith(e,[v,w,u]),v.statusCode(j),j=b,t&&g.trigger("ajax"+(o?"Success":"Error"),[v,d,o?r:u]),i.resolveWith(e,[v,w]),t&&(g.trigger("ajaxComplete",[v,d]),--f.active||f.event.trigger("ajaxStop"))}}typeof a=="object"&&(c=a,a=b),c=c||{};var d=f.ajaxSetup({},c),e=d.context||d,g=e!==d&&(e.nodeType||e instanceof f)?f(e):f.event,h=f.Deferred(),i=f._Deferred(),j=d.statusCode||{},k,l={},m={},n,o,p,q,r,s=0,t,u,v={readyState:0,setRequestHeader:function(a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this},getAllResponseHeaders:function(){return s===2?n:null},getResponseHeader:function(a){var c;if(s===2){if(!o){o={};while(c=bD.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c},overrideMimeType:function(a){s||(d.mimeType=a);return this},abort:function(a){a=a||"abort",p&&p.abort(a),w(0,a);return this}};h.promise(v),v.success=v.done,v.error=v.fail,v.complete=i.done,v.statusCode=function(a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this},d.url=((a||d.url)+"").replace(bC,"").replace(bH,bS[1]+"//"),d.dataTypes=f.trim(d.dataType||"*").toLowerCase().split(bL),d.crossDomain==null&&(r=bN.exec(d.url.toLowerCase()),d.crossDomain=!(!r||r[1]==bS[1]&&r[2]==bS[2]&&(r[3]||(r[1]==="http:"?80:443))==(bS[3]||(bS[1]==="http:"?80:443)))),d.data&&d.processData&&typeof d.data!="string"&&(d.data=f.param(d.data,d.traditional)),bW(bP,d,c,v);if(s===2)return!1;t=d.global,d.type=d.type.toUpperCase(),d.hasContent=!bG.test(d.type),t&&f.active++===0&&f.event.trigger("ajaxStart");if(!d.hasContent){d.data&&(d.url+=(bI.test(d.url)?"&":"?")+d.data,delete d.data),k=d.url;if(d.cache===!1){var x=f.now(),y=d.url.replace(bM,"$1_="+x);d.url=y+(y===d.url?(bI.test(d.url)?"&":"?")+"_="+x:"")}}(d.data&&d.hasContent&&d.contentType!==!1||c.contentType)&&v.setRequestHeader("Content-Type",d.contentType),d.ifModified&&(k=k||d.url,f.lastModified[k]&&v.setRequestHeader("If-Modified-Since",f.lastModified[k]),f.etag[k]&&v.setRequestHeader("If-None-Match",f.etag[k])),v.setRequestHeader("Accept",d.dataTypes[0]&&d.accepts[d.dataTypes[0]]?d.accepts[d.dataTypes[0]]+(d.dataTypes[0]!=="*"?", "+bT+"; q=0.01":""):d.accepts["*"]);for(u in d.headers)v.setRequestHeader(u,d.headers[u]);if(d.beforeSend&&(d.beforeSend.call(e,v,d)===!1||s===2)){v.abort();return!1}for(u in{success:1,error:1,complete:1})v[u](d[u]);p=bW(bQ,d,c,v);if(!p)w(-1,"No Transport");else{v.readyState=1,t&&g.trigger("ajaxSend",[v,d]),d.async&&d.timeout>0&&(q=setTimeout(function(){v.abort("timeout")},d.timeout));try{s=1,p.send(l,w)}catch(z){s<2?w(-1,z):f.error(z)}}return v},param:function(a,c){var d=[],e=function(a,b){b=f.isFunction(b)?b():b,d[d.length]=encodeURIComponent(a)+"="+encodeURIComponent(b)};c===b&&(c=f.ajaxSettings.traditional);if(f.isArray(a)||a.jquery&&!f.isPlainObject(a))f.each(a,function(){e(this.name,this.value)});else for(var g in a)bY(g,a[g],c,e);return d.join("&").replace(bz,"+")}}),f.extend({active:0,lastModified:{},etag:{}});var b_=f.now(),ca=/(\=)\?(&|$)|\?\?/i;f.ajaxSetup({jsonp:"callback",jsonpCallback:function(){return f.expando+"_"+b_++}}),f.ajaxPrefilter("json jsonp",function(b,c,d){var e=b.contentType==="application/x-www-form-urlencoded"&&typeof b.data=="string";if(b.dataTypes[0]==="jsonp"||b.jsonp!==!1&&(ca.test(b.url)||e&&ca.test(b.data))){var g,h=b.jsonpCallback=f.isFunction(b.jsonpCallback)?b.jsonpCallback():b.jsonpCallback,i=a[h],j=b.url,k=b.data,l="$1"+h+"$2";b.jsonp!==!1&&(j=j.replace(ca,l),b.url===j&&(e&&(k=k.replace(ca,l)),b.data===k&&(j+=(/\?/.test(j)?"&":"?")+b.jsonp+"="+h))),b.url=j,b.data=k,a[h]=function(a){g=[a]},d.always(function(){a[h]=i,g&&f.isFunction(i)&&a[h](g[0])}),b.converters["script json"]=function(){g||f.error(h+" was not called");return g[0]},b.dataTypes[0]="json";return"script"}}),f.ajaxSetup({accepts:{script:"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"},contents:{script:/javascript|ecmascript/},converters:{"text script":function(a){f.globalEval(a);return a}}}),f.ajaxPrefilter("script",function(a){a.cache===b&&(a.cache=!1),a.crossDomain&&(a.type="GET",a.global=!1)}),f.ajaxTransport("script",function(a){if(a.crossDomain){var d,e=c.head||c.getElementsByTagName("head")[0]||c.documentElement;return{send:function(f,g){d=c.createElement("script"),d.async="async",a.scriptCharset&&(d.charset=a.scriptCharset),d.src=a.url,d.onload=d.onreadystatechange=function(a,c){if(c||!d.readyState||/loaded|complete/.test(d.readyState))d.onload=d.onreadystatechange=null,e&&d.parentNode&&e.removeChild(d),d=b,c||g(200,"success")},e.insertBefore(d,e.firstChild)},abort:function(){d&&d.onload(0,1)}}}});var cb=a.ActiveXObject?function(){for(var a in cd)cd[a](0,1)}:!1,cc=0,cd;f.ajaxSettings.xhr=a.ActiveXObject?function(){return!this.isLocal&&ce()||cf()}:ce,function(a){f.extend(f.support,{ajax:!!a,cors:!!a&&"withCredentials"in a})}(f.ajaxSettings.xhr()),f.support.ajax&&f.ajaxTransport(function(c){if(!c.crossDomain||f.support.cors){var d;return{send:function(e,g){var h=c.xhr(),i,j;c.username?h.open(c.type,c.url,c.async,c.username,c.password):h.open(c.type,c.url,c.async);if(c.xhrFields)for(j in c.xhrFields)h[j]=c.xhrFields[j];c.mimeType&&h.overrideMimeType&&h.overrideMimeType(c.mimeType),!c.crossDomain&&!e["X-Requested-With"]&&(e["X-Requested-With"]="XMLHttpRequest");try{for(j in e)h.setRequestHeader(j,e[j])}catch(k){}h.send(c.hasContent&&c.data||null),d=function(a,e){var j,k,l,m,n;try{if(d&&(e||h.readyState===4)){d=b,i&&(h.onreadystatechange=f.noop,cb&&delete cd[i]);if(e)h.readyState!==4&&h.abort();else{j=h.status,l=h.getAllResponseHeaders(),m={},n=h.responseXML,n&&n.documentElement&&(m.xml=n),m.text=h.responseText;try{k=h.statusText}catch(o){k=""}!j&&c.isLocal&&!c.crossDomain?j=m.text?200:404:j===1223&&(j=204)}}}catch(p){e||g(-1,p)}m&&g(j,k,m,l)},!c.async||h.readyState===4?d():(i=++cc,cb&&(cd||(cd={},f(a).unload(cb)),cd[i]=d),h.onreadystatechange=d)},abort:function(){d&&d(0,1)}}}});var cg={},ch,ci,cj=/^(?:toggle|show|hide)$/,ck=/^([+\-]=)?([\d+.\-]+)([a-z%]*)$/i,cl,cm=[["height","marginTop","marginBottom","paddingTop","paddingBottom"],["width","marginLeft","marginRight","paddingLeft","paddingRight"],["opacity"]],cn;f.fn.extend({show:function(a,b,c){var d,e;if(a||a===0)return this.animate(cq("show",3),a,b,c);for(var g=0,h=this.length;g=e.duration+this.startTime){this.now=this.end,this.pos=this.state=1,this.update(),e.animatedProperties[this.prop]=!0;for(g in e.animatedProperties)e.animatedProperties[g]!==!0&&(c=!1);if(c){e.overflow!=null&&!f.support.shrinkWrapBlocks&&f.each(["","X","Y"],function(a,b){d.style["overflow"+b]=e.overflow[a]}),e.hide&&f(d).hide();if(e.hide||e.show)for(var i in e.animatedProperties)f.style(d,i,e.orig[i]);e.complete.call(d)}return!1}e.duration==Infinity?this.now=b:(h=b-this.startTime,this.state=h/e.duration,this.pos=f.easing[e.animatedProperties[this.prop]](this.state,h,0,1,e.duration),this.now=this.start+(this.end-this.start)*this.pos),this.update();return!0}},f.extend(f.fx,{tick:function(){for(var a=f.timers,b=0;b
";f.extend(b.style,{position:"absolute",top:0,left:0,margin:0,border:0,width:"1px",height:"1px",visibility:"hidden"}),b.innerHTML=j,a.insertBefore(b,a.firstChild),d=b.firstChild,e=d.firstChild,h=d.nextSibling.firstChild.firstChild,this.doesNotAddBorder=e.offsetTop!==5,this.doesAddBorderForTableAndCells=h.offsetTop===5,e.style.position="fixed",e.style.top="20px",this.supportsFixedPosition=e.offsetTop===20||e.offsetTop===15,e.style.position=e.style.top="",d.style.overflow="hidden",d.style.position="relative",this.subtractsBorderForOverflowNotVisible=e.offsetTop===-5,this.doesNotIncludeMarginInBodyOffset=a.offsetTop!==i,a.removeChild(b),f.offset.initialize=f.noop},bodyOffset:function(a){var b=a.offsetTop,c=a.offsetLeft;f.offset.initialize(),f.offset.doesNotIncludeMarginInBodyOffset&&(b+=parseFloat(f.css(a,"marginTop"))||0,c+=parseFloat(f.css(a,"marginLeft"))||0);return{top:b,left:c}},setOffset:function(a,b,c){var d=f.css(a,"position");d==="static"&&(a.style.position="relative");var e=f(a),g=e.offset(),h=f.css(a,"top"),i=f.css(a,"left"),j=(d==="absolute"||d==="fixed")&&f.inArray("auto",[h,i])>-1,k={},l={},m,n;j?(l=e.position(),m=l.top,n=l.left):(m=parseFloat(h)||0,n=parseFloat(i)||0),f.isFunction(b)&&(b=b.call(a,c,g)),b.top!=null&&(k.top=b.top-g.top+m),b.left!=null&&(k.left=b.left-g.left+n),"using"in b?b.using.call(a,k):e.css(k)}},f.fn.extend({position:function(){if(!this[0])return null;var a=this[0],b=this.offsetParent(),c=this.offset(),d=ct.test(b[0].nodeName)?{top:0,left:0}:b.offset();c.top-=parseFloat(f.css(a,"marginTop"))||0,c.left-=parseFloat(f.css(a,"marginLeft"))||0,d.top+=parseFloat(f.css(b[0],"borderTopWidth"))||0,d.left+=parseFloat(f.css(b[0],"borderLeftWidth"))||0;return{top:c.top-d.top,left:c.left-d.left}},offsetParent:function(){return this.map(function(){var a=this.offsetParent||c.body;while(a&&!ct.test(a.nodeName)&&f.css(a,"position")==="static")a=a.offsetParent;return a})}}),f.each(["Left","Top"],function(a,c){var d="scroll"+c;f.fn[d]=function(c){var e,g;if(c===b){e=this[0];if(!e)return null;g=cu(e);return g?"pageXOffset"in g?g[a?"pageYOffset":"pageXOffset"]:f.support.boxModel&&g.document.documentElement[d]||g.document.body[d]:e[d]}return this.each(function(){g=cu(this),g?g.scrollTo(a?f(g).scrollLeft():c,a?c:f(g).scrollTop()):this[d]=c})}}),f.each(["Height","Width"],function(a,c){var d=c.toLowerCase();f.fn["inner"+c]=function(){var a=this[0];return a&&a.style?parseFloat(f.css(a,d,"padding")):null},f.fn["outer"+c]=function(a){var b=this[0];return b&&b.style?parseFloat(f.css(b,d,a?"margin":"border")):null},f.fn[d]=function(a){var e=this[0];if(!e)return a==null?null:this;if(f.isFunction(a))return this.each(function(b){var c=f(this);c[d](a.call(this,b,c[d]()))});if(f.isWindow(e)){var g=e.document.documentElement["client"+c],h=e.document.body;return e.document.compatMode==="CSS1Compat"&&g||h&&h["client"+c]||g}if(e.nodeType===9)return Math.max(e.documentElement["client"+c],e.body["scroll"+c],e.documentElement["scroll"+c],e.body["offset"+c],e.documentElement["offset"+c]);if(a===b){var i=f.css(e,d),j=parseFloat(i);return f.isNaN(j)?i:j}return this.css(d,typeof a=="string"?a:a+"px")}}),jQueryInclude=f} -------------------------------------------------------------------------------- /lib/server/controller.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies 3 | */ 4 | 5 | var ejs = require("ejs"); 6 | var path = require("path"); 7 | var fs = require("fs"); 8 | var logger = require("../logger"); 9 | var Experiment = require('../experiment'); 10 | var Project = require('../project'); 11 | var Assets = require('../assets'); 12 | var Files = require('../files'); 13 | 14 | function LocalController(variation, port) { 15 | this.variation = variation; 16 | //assume the directory above the variation 17 | //maybe move this to Experiment? 18 | this.experiment = new Experiment({}, path.normalize(variation.baseDir + "/..")); 19 | if(!this.experiment.loadFromFile()) { 20 | throw new Error("no experiment.json found"); 21 | } 22 | this.project = new Project({}, "./"); 23 | if(!this.project.loadFromFile()) { 24 | throw new Error("no project.json found"); 25 | } 26 | 27 | this.port = port || 8080; 28 | 29 | this.locals = { 30 | _port: this.port, 31 | edit_url: this.experiment.getOptcliURL(), 32 | experiment_desc: this.experiment.attributes.description, 33 | variation_desc: this.variation.attributes.description 34 | }; 35 | 36 | //load the templates 37 | var originalPath = path.join( 38 | path.dirname(fs.realpathSync(__filename)), 39 | '../../'); 40 | 41 | this.installTemplate = fs.readFileSync( 42 | originalPath + "templates/install.user.js.ejs", { 43 | encoding: "utf-8" 44 | }); 45 | 46 | this.indexTemplate = fs.readFileSync( 47 | originalPath + "templates/index.ejs", { 48 | encoding: "utf-8" 49 | }); 50 | 51 | } 52 | 53 | // Compile assets and files in their own method so they can be dynamically updated 54 | LocalController.prototype.getAssets = function(getFiles) { 55 | //assume assets are in experiment.baseDir 56 | this.assets = this.assets || new Assets({}, this.experiment.baseDir); 57 | if (this.assets.JSONFileExists()) { 58 | logger.log("info", "assets file found, loading"); 59 | this.assets.loadFromFile(); 60 | } 61 | 62 | if(getFiles) { 63 | //get any available file data 64 | //pass in assets in case any of the files use additional EJS 65 | this.files = new Files({}, this.experiment.baseDir, this.assets); 66 | if (this.files.filenames.length) { 67 | logger.log("info", 'Loading these external files: ' + this.files.filenames.join('')); 68 | } 69 | } 70 | } 71 | 72 | LocalController.prototype.installUserScript = function(req, res) { 73 | res.end(String(ejs.render( 74 | this.indexTemplate, { 75 | locals: this.locals 76 | } 77 | ))); 78 | }; 79 | 80 | LocalController.prototype.userScript = function(req, res) { 81 | this.locals._jquery = this.project.attributes.include_jquery ? "jQuery" : ""; 82 | this.locals._url = this.experiment.attributes.edit_url; 83 | //Render Userscript 84 | res.end(String(ejs.render( 85 | this.installTemplate, { 86 | locals: this.locals 87 | } 88 | ))); 89 | }; 90 | 91 | LocalController.prototype.variationJS = function(req, res) { 92 | logger.log('debug', process.cwd()); 93 | var vJS = this.compileLocalJS(); 94 | this.getAssets(true); 95 | vJS = String(ejs.render(vJS, { 96 | locals: { 97 | assets: this.assets.attributes, 98 | files: this.files.data 99 | } 100 | })); 101 | //Render JS 102 | res.set({ 103 | "Content-Type": "text/javascript" 104 | }); 105 | res.end(vJS); 106 | }; 107 | 108 | LocalController.prototype.variationCSS = function(req, res) { 109 | var css = this.experiment.getCSS(); 110 | this.getAssets(); 111 | 112 | //Compile 113 | css = String(ejs.render(css, { 114 | locals: { 115 | assets: this.assets.attributes 116 | } 117 | })); 118 | //Render CSS 119 | res.set({ 120 | "Content-Type": "text/css" 121 | }); 122 | res.end(css); 123 | }; 124 | 125 | LocalController.prototype.compileLocalJS = function() { 126 | var eJS = this.experiment.getJS(); 127 | var vJS = this.variation.getJS(); 128 | 129 | if (this.project.attributes.include_jquery){ 130 | var jQueryString = "(" + String(require('./assets/jquery.min.js')) + ")(window)"; 131 | eJS = 132 | "var jQueryInclude;\n" + 133 | jQueryString + ";\n" + 134 | "(function($, jQuery){\n/*Experiment JavaScript*/\n" + 135 | eJS + "\n/*Experiment JavaScript*/\n" + 136 | "})(jQueryInclude, jQueryInclude);"; 137 | vJS = 138 | "(function($, jQuery){\n/*Variation JavaScript*/\n" + 139 | vJS + "\n/*Variation JavaScript*/\n" + 140 | "})(jQueryInclude, jQueryInclude);"; 141 | vJS = eJS + "\n\n" + vJS; 142 | return vJS; 143 | } else { 144 | eJS = 145 | "(function(){\n/*Experiment JavaScript*/\n" + 146 | eJS + "\n/*Experiment JavaScript*/\n" + 147 | "})();"; 148 | vJS = 149 | "(function(){\n/*Variation JavaScript*/\n" + 150 | vJS + "\n/*Variation JavaScript*/\n" + 151 | "})();"; 152 | vJS = eJS + "\n\n" + vJS; 153 | return vJS; 154 | } 155 | }; 156 | 157 | module.exports = LocalController; 158 | -------------------------------------------------------------------------------- /lib/set-token.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var promptly = require('promptly'); 3 | var q = require("q"); 4 | var writeConfig = require('./write-config'); 5 | 6 | module.exports = function(token){ 7 | if (token) return writeConfig("token", token); 8 | var d = q.defer(); 9 | promptly.prompt('Enter Your Optimizely Token (hidden): ',{ 10 | 'trim': true, 11 | 'silent': true 12 | },function (err, token) { 13 | if(err) token = undefined; 14 | writeConfig("token", token) 15 | .then(function(token){ 16 | console.log("token set"); 17 | d.resolve(token); 18 | }).fail(function(reason){ 19 | console.log("token set failed") 20 | d.reject(reason); 21 | }); 22 | }); 23 | return d.promise; 24 | } 25 | -------------------------------------------------------------------------------- /lib/variation.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | var path = require('path'); 3 | var _ = require("lodash"); 4 | var ejs = require("ejs"); 5 | 6 | var fileUtil = require("./file-util"); 7 | var logger = require("./logger"); 8 | var readConfig = require("./read-config"); 9 | var OptimizelyClient = require("optimizely-node-client"); 10 | var OptCLIBase = require("./optcli-base"); 11 | var Assets = require('./assets'); 12 | var Files = require('./files'); 13 | 14 | function Variation(attributes, baseDir) { 15 | Variation.super_.call(this, attributes, baseDir); 16 | } 17 | 18 | Variation.JSON_FILE_NAME = "variation.json"; 19 | Variation.JS_FILE_NAME = "variation.js"; 20 | 21 | util.inherits(Variation, OptCLIBase); 22 | 23 | Variation.create = function(attrs, baseDir) { 24 | //create directory 25 | fileUtil.writeDir(baseDir); 26 | fileUtil.writeText(path.join(baseDir, Variation.JS_FILE_NAME)); 27 | fileUtil.writeJSON(path.join(baseDir, Variation.JSON_FILE_NAME), attrs); 28 | return new Variation(attrs, baseDir); 29 | } 30 | 31 | Variation.prototype.getJSPath = function() { 32 | return this.getFilePath(Variation.JS_FILE_NAME); 33 | } 34 | 35 | Variation.prototype.getJS = function() { 36 | return fileUtil.loadFile(this.getJSPath()) || ""; 37 | } 38 | 39 | Variation.prototype.createRemote = function(client, remote) { 40 | //assume assets are in experiment.baseDir 41 | var assets = new Assets({}, path.normalize(this.baseDir + "/..")); 42 | if (assets.JSONFileExists()) { 43 | logger.log("info", "assets file found, loading"); 44 | assets.loadFromFile(); 45 | } 46 | 47 | //get any available file data 48 | //pass in assets in case any of the files use additional EJS 49 | var files = new Files({}, path.normalize(this.baseDir + "/.."), assets); 50 | if (files.filenames.length) { 51 | logger.log("info", 'Loading the following external files: "' + files.filenames.join('" "') + '"'); 52 | } 53 | 54 | //create new variation 55 | var varArgs = _.clone(this.attributes); 56 | varArgs['js_component'] = String(ejs.render(this.getJS(), { 57 | locals: { 58 | assets: assets.attributes, 59 | files: files.data 60 | } 61 | })); 62 | varArgs['experiment_id'] = experiment.attributes.id; 63 | 64 | var self = this; 65 | return client.createVariation(varArgs).then(function(variationAttrs) { 66 | //update the id 67 | self.attributes.id = variationAttrs.id; 68 | self.saveAttributes(); 69 | logger.log("info", "created remote variation: " + variationAttrs.id); 70 | }, function(error) { 71 | logger.log("error", error); 72 | logger.log("error", "unable to create remote variation: " + e.message); 73 | console.error(e.stack); 74 | }); 75 | } 76 | 77 | Variation.prototype.updateRemote = function(client) { 78 | //assume assets are in experiment.baseDir 79 | var assets = new Assets({}, path.normalize(this.baseDir + "/..")); 80 | if (assets.JSONFileExists()) { 81 | logger.log("info", "assets file found, loading"); 82 | assets.loadFromFile(); 83 | } 84 | 85 | //get any available file data 86 | //pass in assets in case any of the files use additional EJS 87 | var files = new Files({}, path.normalize(this.baseDir + "/.."), assets); 88 | if (files.filenames.length) { 89 | logger.log("info", 'Loading the following external files: "' + files.filenames.join('" "') + '"'); 90 | } 91 | 92 | var varArgs = _.clone(this.attributes); 93 | varArgs['js_component'] = String(ejs.render(this.getJS(), { 94 | locals: { 95 | assets: assets.attributes, 96 | files: files.data 97 | } 98 | })); 99 | 100 | var self = this; 101 | return client.updateVariation(varArgs).then(function(variationAttrs) { 102 | logger.log("info", "updated remote variation: " + variationAttrs.id); 103 | }, function(error) { 104 | logger.log("error", error); 105 | }).catch(function(e) { 106 | logger.log("error", "unable to update remote variation: " + e.message); 107 | console.error(e.stack); 108 | }); 109 | } 110 | 111 | Variation.prototype.saveAttributes = function() { 112 | fileUtil.writeJSON(path.join(this.baseDir, Variation.JSON_FILE_NAME), this.attributes); 113 | } 114 | 115 | module.exports = Variation; 116 | -------------------------------------------------------------------------------- /lib/write-config.js: -------------------------------------------------------------------------------- 1 | var q = require("q"); 2 | var fs = require("fs"); 3 | 4 | q.die = function(reason){ 5 | var d = q.defer(); 6 | d.reject(reason); 7 | return d.promise; 8 | }; 9 | 10 | module.exports = function(name, value){ 11 | try{ 12 | if( !fs.existsSync(".optcli") ) { 13 | fs.mkdirSync(".optcli") 14 | } 15 | fs.writeFileSync(".optcli/" + name, value); 16 | return q(value); 17 | }catch(e){ 18 | return q.die(e); 19 | } 20 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "optimizely-cli", 3 | "version": "0.17.0", 4 | "description": "A command line application to create Optimizely experiments and publish via the API", 5 | "main": "index.js", 6 | "bin": { 7 | "optcli": "bin/optcli.js" 8 | }, 9 | "scripts": { 10 | "test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec" 11 | }, 12 | "repository": "git@github.com:FunnelEnvy/optimizely-cli.git", 13 | "keywords": [ 14 | "optimizely", 15 | "cli" 16 | ], 17 | "author": "Arun Sivashankaran", 18 | "contributors": [ 19 | "Arun Sivashankaran", 20 | "John Henry" 21 | ], 22 | "license": "Apache 2.0", 23 | "dependencies": { 24 | "bluebird": "^2.3.2", 25 | "cli-table": "^0.3.1", 26 | "commander": "^2.5.0", 27 | "ejs": "^1.0.0", 28 | "express": "^4.10.4", 29 | "glob": "^4.2.2", 30 | "lodash": "^2.4.1", 31 | "open": "0.0.5", 32 | "optimizely-node-client": "^0.1.0", 33 | "promptly": "^0.2.0", 34 | "q": "^1.1.2" 35 | }, 36 | "engines": { 37 | "node": "^0.10.32" 38 | }, 39 | "devDependencies": { 40 | "chai": "^1.10.0", 41 | "codeclimate-test-reporter": "^0.1.0", 42 | "coveralls": "^2.11.2", 43 | "intercept-stdout": "^0.1.1", 44 | "istanbul": "^0.3.17", 45 | "mocha": "^2.1.0", 46 | "mocha-lcov-reporter": "0.0.2", 47 | "path": "^0.12.7", 48 | "proxyquire": "^1.3.1", 49 | "quick-temp": "^0.1.2", 50 | "sinon": "^1.12.2" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /ssl/server.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIC+zCCAeOgAwIBAgIJAOkxaGdm4N2rMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV 3 | BAMMCWxvY2FsaG9zdDAeFw0xNTA4MTgxNzMxMzdaFw0yNTA4MTUxNzMxMzdaMBQx 4 | EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC 5 | ggEBAMfdtvjggI0Gn5RRdr9Y9c93thDd2luhXiXyHExW/vPHEs6LYXdPibe74rjH 6 | CMwsNdvVwhuRoWTZGwdCp+D3xyebDK6EUHVxtDcvkvH0L/cFdIUet/FJTrhWpnYq 7 | exJtvr/u3KF2ESDQoz2aqtk8xmKo7dh5hKzSOEo7rFeK3BzGgGkIGklXbKs0qOak 8 | jFHm3E596mUmysMIKGauKw3oKZiOdoxYQUGtKFt+9JrbsbZXhuk9qANbdz8IDwlh 9 | j6IiYfrjOImZvEnjau/cM/J8GUXpKCLv+TGKfzfunOTt3A3z5kFBn4NR0zPEOLx7 10 | pw5b59lUdO84FAhMUbdNq5u//jsCAwEAAaNQME4wHQYDVR0OBBYEFEwPXy6PpLcd 11 | Evh2NECz0fMeQWHqMB8GA1UdIwQYMBaAFEwPXy6PpLcdEvh2NECz0fMeQWHqMAwG 12 | A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAIBqS9AIKeO09CwWTkepl3xW 13 | TaAP+Pu5ZdgNGThFH4xmUq+mu6rzEhGea/ZHgdlAsW+zF0GP91VdxY0O4qVh6pJO 14 | ZYkg/cAZa9ccjpuklJC5QETqqAGDblu9eO/LiuUAO2B6mc8F3xaPu2l5bY+oau3m 15 | OLrHJp4ZAU9qAkf7O5cYowoTrvcFiQ/609G/6pXnf2ZgcHO5aZ1RBqXQ2PD2lzxI 16 | pAA2d7hNrWm6v9zZNTtXkMkOY5Y8Zg69uDKQfcVgesqZLMc+rZOP+EwxeTJ0Q6Fa 17 | 5a0KjqKyZIASPOD0oqFJNyohrztZ/AxLsUvnlMDxtK7Imo62gQ8MJdxO2hE11w0= 18 | -----END CERTIFICATE----- 19 | -------------------------------------------------------------------------------- /ssl/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEowIBAAKCAQEAx922+OCAjQaflFF2v1j1z3e2EN3aW6FeJfIcTFb+88cSzoth 3 | d0+Jt7viuMcIzCw129XCG5GhZNkbB0Kn4PfHJ5sMroRQdXG0Ny+S8fQv9wV0hR63 4 | 8UlOuFamdip7Em2+v+7coXYRINCjPZqq2TzGYqjt2HmErNI4SjusV4rcHMaAaQga 5 | SVdsqzSo5qSMUebcTn3qZSbKwwgoZq4rDegpmI52jFhBQa0oW370mtuxtleG6T2o 6 | A1t3PwgPCWGPoiJh+uM4iZm8SeNq79wz8nwZRekoIu/5MYp/N+6c5O3cDfPmQUGf 7 | g1HTM8Q4vHunDlvn2VR07zgUCExRt02rm7/+OwIDAQABAoIBACmR7N+g2jv617Ai 8 | rX8pAp9vN7gUXLlYO9vKmqYqJgcQLdI13UTSj0Ne2c4y35qPy3f44tGXHal7GosN 9 | CxvYjVyofB/EN3Pl/WTJCVg3wM4xHUYe2IVgCPaAV0kWltiDaPxEszEF/JQFsR48 10 | EDL02BJnLmkrBTRo2PpfpP3kTNnS9PLy8z7lYMIE76np1k0l7U7y17vtyWwLptmA 11 | XYh3+/zDn2opZuL7SKJ6r4lFHZpVOn0no9vM9xDJHO+qVRGCoAJrZaqQUUojbNlA 12 | r5qaDzn5B5mWDU5LHM7wFpezhX6Rz0b2Mbo+uNO9MH3rzMb2C6UgBxlqshBkxXE/ 13 | fw8J9pkCgYEA/Gx8wn6pVg71V0LFZTXqGbRSrTHWGkBr4uHgaJqgnbsXYbuzH5p4 14 | EqtC3RdrIZNJtBelSqO0oN8VFZ+Sz4FaVeQSydhEN/aiFIZhKs3bmuER48LqtTl5 15 | 4QJaFR6nHCDjevVd1HdDEWz5s5WPHfHW8ZnshQcluS3TJe3JWo4+m/cCgYEAyrKb 16 | Q+yy8LQRKW70/Z98VDryo9KVruq/hmJuVYZ2sjhVZ3RI7502e1C6j5kjUgB7WyQK 17 | RQG7jhetdAlrf9g37tjBSyQ8G/1gWEIzOI8Z5UFb+synoHnxzAO4Jb48b3e8OmCA 18 | j8kiLML6cbZAwQauRKKcpoDM5Q8eaCq1T0vM9t0CgYEAl+COkFe1e1o0s/Qw4Ny3 19 | pg+hTyQVNmZcg350j3u5+C2BvAQ7mmBOrqzs5ioZA3KjfgBcK7SkEccn4ILKyp+B 20 | wvwfceL16NY0XzUbca263E1ffjLhFXknpALOQLbYxUvkky7e7d90Mx/mfe8W1WWV 21 | dniunrvWLr0rtj6EUEAV27ECgYAeQrvbUCMGvFszjFUW6BBvor1Gp9Gg43rkXR2L 22 | tx9RTAe0AjBBVX8kudCgT4RuYZQI70B5POD7PZ2vjRh3ZZg0GDgDN82hgBo6EugC 23 | sZp0F2Xb82GzG4F1q7h6KgRrv7xiGrYWwThQ5mrtPwA70PuDU6N/WAs+xxsLAhU/ 24 | WVmoIQKBgBCg5Cl77Vd62xWm2gr5CCAT6RtpI42OPRZpiTB/jYZ47wirmlFFx1ZA 25 | 1Y4ZJkBUKG0jnO6HNaSM5ifx9LXPXGrwmSWKtYI9KOsmNH2VCwFt+FOZ0iXt47+e 26 | dXe363QwVFls6daNLL1d4c0ZHYkunZEtqOgSSDMOIzkMPjuxjnXh 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /templates/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Optimizely-CLI 8 | 9 | 10 | 11 | 35 | 36 | 37 | 41 | 42 | 43 | 62 | 63 |
64 |

Optimizely-CLI Local Server

65 | Requires the Tampermonkey (Google Chrome) or Greasemonkey (Firefox) extension. 66 | 67 |
68 |
69 |
70 |
71 |

One Time Setup

72 |
73 |
74 |

Follow the steps below once to host a variation locally with Optimizely-CLI.

75 |
    76 |
  1. 77 | Install the TamperMonkey Chrome Extension or Greasemonkey Firefox Add On. 78 |
  2. 79 |
  3. 80 | Install the Optimizely-cli Injection Script. 81 | 82 | Install Optimizely-CLI User Script 83 | 84 |
  4. 85 |
  5. 86 | Refresh the browser. You can now navigate to the local variation using the links to the right. Happy testing! 87 |
  6. 88 |
89 |
90 |
91 |
92 |
93 |

Currently Hosting

94 | 106 | 107 |
108 |
109 | 110 | 111 |
112 | 113 |
114 |
115 |

Brought to you by your friends at FunnelEnvy.

116 |
117 |
118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /templates/install.user.js.ejs: -------------------------------------------------------------------------------- 1 | // ==UserScript== 2 | // @name Optimizely-cli Injection Script 3 | // @author FunnelEnvy 4 | // @homepage http://www.funnelenvy.com/optimizely-cli 5 | // @source https://github.com/funnelenvy/optimizely-cli 6 | // @namespace optcli 7 | // @description Inject local experiment JS / CSS into a Chrome page for development 8 | // @include /optcli=activate/ 9 | // @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js 10 | // ==/UserScript== 11 | 12 | var scriptElement = document.createElement('script'); 13 | scriptElement.type = 'text/javascript'; 14 | scriptElement.src = '//localhost:<%- _port %>/variation.js'; 15 | document.head.appendChild(scriptElement); 16 | 17 | var stylesheet = document.createElement('link'); 18 | stylesheet.rel = 'stylesheet'; 19 | stylesheet.href = '//localhost:<%- _port %>/variation.css'; 20 | document.head.appendChild(stylesheet); 21 | -------------------------------------------------------------------------------- /templates/script.ejs: -------------------------------------------------------------------------------- 1 | // ==UserScript== 2 | // @name <%- variation_id %> 3 | // @namespace <%- experiment_id %> 4 | // @description <%- description %> 5 | // @match <%- _url %> 6 | <% var jQuery = "jQuery"; %> 7 | <% var $ = "$"; %> 8 | <% if(_jquery === "jQuery"){ 9 | %> 10 | // @require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js 11 | <% }else if(_jquery === "lite"){ %> 12 | // @require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js 13 | <% }else{ 14 | jQuery = ""; 15 | $ = ""; 16 | } %> 17 | // ==/UserScript== 18 | (function(<%- $ %>){ 19 | <%- _js %> 20 | })(<%- jQuery %>); 21 | -------------------------------------------------------------------------------- /templates/server.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Optimizely Powertool 6 | 7 | 8 | <% experiments.forEach(function(experiment){%> 9 |

<%- experiment.id %> - <%- experiment.description %>

10 | <% experiment._variations.forEach(function(variation){%> 11 | Install <%- variation.description %>
12 | <% }) %> 13 | <% }) %> 14 | 15 | 16 | -------------------------------------------------------------------------------- /test/commands/create-experiment.test.js: -------------------------------------------------------------------------------- 1 | //Test the experiment command 2 | 3 | var fs = require('fs'); 4 | 5 | var assert = require('chai').assert; 6 | var quickTemp = require('quick-temp'); 7 | 8 | var utils = require('../utils'); 9 | 10 | var directory = {}; 11 | 12 | describe('Create Experiment Command', function () { 13 | before(function (done) { 14 | //Create temporary project directory and enter it 15 | quickTemp.makeOrRemake(directory, 'project'); 16 | directory.experiment = directory.project + '/test-experiment/'; 17 | utils.init(directory.project); 18 | utils.experiment(directory.experiment); 19 | done(); 20 | }); 21 | after(function (done) { 22 | //Remove the temporary directory 23 | quickTemp.remove(directory, 'project'); 24 | done(); 25 | }); 26 | it('Should create a folder called test-experiment', function (done) { 27 | fs.exists(directory.experiment, function (exists) { 28 | assert(exists, 'experiment folder not found'); 29 | done(); 30 | }) 31 | }); 32 | it('Should create experiment.json in the experiment folder', function (done) { 33 | fs.exists(directory.experiment + 'experiment.json', function (exists) { 34 | assert(exists, 'experiment.json not found'); 35 | done(); 36 | }); 37 | }); 38 | it('Should create global.js in the experiment folder', function (done) { 39 | fs.exists(directory.experiment + 'global.js', function (exists) { 40 | assert(exists, 'global.js not found'); 41 | done(); 42 | }); 43 | }); 44 | it('Should create global.css in the experiment folder', function (done) { 45 | fs.exists(directory.experiment + 'global.css', function (exists) { 46 | assert(exists, 'global.css not found'); 47 | done(); 48 | }); 49 | }) 50 | }) 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /test/commands/create-variation.test.js: -------------------------------------------------------------------------------- 1 | //Test the variation command 2 | var fs = require('fs'); 3 | 4 | var assert = require('chai').assert; 5 | var quickTemp = require('quick-temp'); 6 | 7 | var utils = require('../utils.js'); 8 | var directory = {}; 9 | 10 | describe('Create Variation Command', function () { 11 | before(function (done) { 12 | 13 | //Create temporary project directory 14 | quickTemp.makeOrRemake(directory, 'project'); 15 | directory.experiment = directory.project + '/test-experiment/'; 16 | directory.variation = directory.project + '/test-experiment/test-variation'; 17 | 18 | //Initialize a project, create experiment, and create a variation 19 | utils.init(directory.project); 20 | utils.experiment(directory.experiment); 21 | utils.variation(directory.experiment, '/test-variation', 'Variation 1'); 22 | done(); 23 | }); 24 | after(function (done) { 25 | //Remove the project directory 26 | quickTemp.remove(directory, 'project'); 27 | done(); 28 | }); 29 | it('Should create a variation folder within the experiment folder', function (done) { 30 | fs.exists(directory.variation, function (exists) { 31 | assert(exists, 'Variation folder does not exist'); 32 | done(); 33 | }); 34 | }) 35 | it('Should create variation.json within the variation folder', function (done) { 36 | fs.exists(directory.variation + '/variation.json', function (exists) { 37 | assert(exists, 'variation.json not found'); 38 | done(); 39 | }); 40 | }); 41 | it('Should create variation.js within the variation folder', function (done) { 42 | fs.exists(directory.variation + '/variation.js', function (exists) { 43 | assert(exists, 'variation.js not found'); 44 | done(); 45 | }); 46 | }) 47 | }); -------------------------------------------------------------------------------- /test/commands/host.test.js: -------------------------------------------------------------------------------- 1 | var ChildProcess = require('child_process'); 2 | var http = require('http'); 3 | var Promise = require('bluebird'); 4 | var fs = require('fs'); 5 | Promise.promisifyAll(fs); 6 | var quickTemp = require('quick-temp'); 7 | var expect = require('chai').expect; 8 | var spy = require('sinon').spy; 9 | 10 | var utils = require('../utils.js'); 11 | var openSpy = spy(); 12 | var host = require('proxyquire')('../../lib/commands/host.js', {'open': openSpy}); 13 | 14 | var directory = {}; 15 | var variationJS = '$(\'body\').addClass(\'test\'); $("body").append("<%-files.test %>");'; 16 | var experimentJS = 'function myFunc(){console.log("testing is fun");}'; 17 | var fileHTML = '
\n\tThis is a file test\n
'; 18 | var CSS = '.test {background: blue}'; 19 | var server, browser, oldDir; 20 | 21 | describe('Host Command', function(){ 22 | before(function(done){ 23 | //Create temporary project directory 24 | quickTemp.makeOrRemake(directory, 'project'); 25 | directory.experiment = directory.project + '/test-experiment/'; 26 | directory.variation = directory.project + '/test-experiment/test-variation'; 27 | 28 | //Initialize a project, create experiment, and create a variation 29 | utils.init(directory.project); 30 | utils.experiment(directory.experiment); 31 | utils.variation(directory.experiment, '/test-variation', 'Variation 1'); 32 | 33 | fs.writeFileAsync(directory.variation + '/variation.js', variationJS) 34 | .then(function(){ 35 | return fs.writeFileAsync(directory.experiment + '/experiment.css', CSS); 36 | }) 37 | .then(function(){ 38 | return fs.writeFileAsync(directory.experiment + '/experiment.js', experimentJS); 39 | }) 40 | .then(function(){ 41 | return fs.writeFileAsync(directory.experiment + '/test.html', fileHTML); 42 | }) 43 | .then(function() { 44 | // Don't host until all files are created, or test.html might be empty/non-existent 45 | oldDir = process.cwd(); 46 | process.chdir(directory.project); 47 | server = host(directory.variation, 9569 , {ssl: false, silence: true, open: true}); 48 | done(); 49 | }) 50 | .catch(function(error){ 51 | expect(error).to.be.null; 52 | done(); 53 | }); 54 | }); 55 | after(function(done){ 56 | process.chdir(oldDir); 57 | server.close(); 58 | //Remove the temporary directory 59 | quickTemp.remove(directory, 'project'); 60 | done(); 61 | }); 62 | it('Should host the landing page on the default port', function(done){ 63 | http.get('http://localhost:9569/', function(res){ 64 | expect(res.statusCode).to.equal(200); 65 | done(); 66 | }).on('error', function(err) { 67 | console.log("Got error: " + err.message); 68 | done(err); 69 | }); 70 | }); 71 | 72 | // Store the contents of variation.js so they can be used in the next assertion. 73 | var resJS = ''; 74 | it('Should host variation.js', function(done){ 75 | http.get('http://localhost:9569/variation.js', function(res){ 76 | expect(res.statusCode).to.equal(200); 77 | }).on('error', function(err){ 78 | console.log('Got error:' + err.message); 79 | done(); 80 | }).on('response', function(res) { 81 | res.on('readable', function() { 82 | resJS += res.read().toString('utf8'); 83 | }) 84 | // Don't call done() until resJS is completely populated. 85 | res.on('end', function() { 86 | done(); 87 | }); 88 | res.on('error', function(err) { 89 | console.log('Got error:' + err.message); 90 | done(); 91 | }); 92 | });; 93 | }); 94 | it('Should compile an external file using EJS', function(done) { 95 | expect(resJS).to.contain('
This is a file test
'); 96 | done(); 97 | }); 98 | it('Should host variation.css', function(done){ 99 | http.get('http://localhost:9569/variation.css', function(res){ 100 | expect(res.statusCode).to.equal(200); 101 | done(); 102 | }).on('error', function(err){ 103 | console.log('Got error:' + err.message); 104 | }); 105 | }); 106 | describe('Open flag', function(){ 107 | it('Should run the open command', function(done){ 108 | expect(openSpy.called).to.be.true; 109 | done(); 110 | }); 111 | }) 112 | 113 | }); -------------------------------------------------------------------------------- /test/commands/init-project.test.js: -------------------------------------------------------------------------------- 1 | //Test the init command 2 | var fs = require('fs'); 3 | 4 | var assert = require('chai').assert; 5 | var quickTemp = require('quick-temp'); 6 | 7 | var utils = require('../utils'); 8 | var directory = {}; 9 | 10 | describe('Init Project Command' , function () { 11 | before(function (done) { 12 | //Create the temporary project folder and enter it 13 | quickTemp.makeOrRemake(directory, 'project'); 14 | //Initialize the project 15 | utils.init(directory.project); 16 | done(); 17 | }); 18 | after(function (done) { 19 | quickTemp.remove(directory, 'project'); 20 | done(); 21 | }) 22 | it('Should create a project.json file', function (done) { 23 | fs.exists(directory.project + '/project.json', function (exists) { 24 | assert(exists, 'project.json does not exist'); 25 | done(); 26 | }); 27 | }); 28 | }) -------------------------------------------------------------------------------- /test/commands/push-experiment.test.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | 3 | var chai = require("chai"); 4 | var assert = chai.assert; 5 | var quickTemp = require('quick-temp'); 6 | var proxyquire = require('proxyquire'); 7 | var sinon = require('sinon'); 8 | 9 | var utils = require('../utils'); 10 | var logger = require("../../lib/logger.js"); 11 | 12 | //verbose logging 13 | // chai.config.includeStack = true; 14 | // logger.debugLevel = 'debug'; 15 | 16 | var ClientStub = function() {}; 17 | var VariationClientStub = sinon.spy(); 18 | var options = { 19 | 'cwd': __dirname 20 | }; 21 | var directory = {}; 22 | 23 | var pushExperiment = proxyquire('../../lib/commands/push-experiment', { 24 | 'optimizely-node-client': ClientStub, 25 | './push-variation': VariationClientStub 26 | }); 27 | 28 | describe('Push Experiment Command', function() { 29 | before(function(done) { 30 | //Create temporary project directory and enter it 31 | quickTemp.makeOrRemake(directory, 'project'); 32 | options.cwd = directory.project; 33 | directory.experiment = directory.project + '/test-experiment/'; 34 | 35 | //Initialize the project and create experiment 36 | utils.init(directory.project); 37 | utils.experiment(directory.experiment) 38 | utils.createOptimizelyToken(directory.project); 39 | process.chdir(directory.project); 40 | done(); 41 | }); 42 | 43 | 44 | after(function() { 45 | //Remove the temporary directory 46 | quickTemp.remove(directory, 'project'); 47 | }); 48 | 49 | beforeEach(function() { 50 | ClientStub.prototype.createExperiment = utils.clientFunctionStub('1234'); 51 | ClientStub.prototype.updateExperiment = utils.clientFunctionStub('1234'); 52 | assert(fs.existsSync(directory.experiment), 'experiment folder not found'); 53 | }); 54 | 55 | it('Should create a remote experiment', function(done) { 56 | 57 | //spy on the stub method 58 | sinon.spy(ClientStub.prototype, "createExperiment"); 59 | 60 | //call push experiment 61 | pushExperiment(directory.experiment, {}); 62 | 63 | setTimeout(function() { 64 | assert(ClientStub.prototype.createExperiment.calledOnce, "createExperiment was not called"); 65 | var experimentMeta = JSON.parse(fs.readFileSync(directory.experiment + 'experiment.json')); 66 | assert(experimentMeta.id === "1234", "Invalid experiment id"); 67 | done(); 68 | }, 10); 69 | }); 70 | 71 | it('Should update a remote experiment', function(done) { 72 | //spy on the stub method 73 | sinon.spy(ClientStub.prototype, "updateExperiment"); 74 | utils.addIdToFile(directory.experiment + 'experiment.json', '1234'); 75 | 76 | //call push experiment 77 | pushExperiment(directory.experiment, {}); 78 | 79 | setTimeout(function() { 80 | assert(ClientStub.prototype.updateExperiment.calledOnce, "updateExperiment was not called"); 81 | var experimentMeta = JSON.parse(fs.readFileSync(directory.experiment + 'experiment.json')); 82 | assert(experimentMeta.id === "1234", "Invalid experiment id"); 83 | done(); 84 | }, 10); 85 | }); 86 | describe("Iterate Option",function(done) { 87 | before(function(done) { 88 | var variationAFolder = 'test-variation-a/'; 89 | var variationBFolder = 'test-variation-b/'; 90 | directory.variation = {}; 91 | directory.variation.a = directory.experiment + variationAFolder; 92 | directory.variation.b = directory.experiment + variationBFolder; 93 | options.multipleVariations = true; 94 | 95 | 96 | utils.variation(directory.experiment, variationAFolder, 'Variation A'); 97 | utils.variation(directory.experiment, variationBFolder, 'Variation B'); 98 | process.chdir(directory.project); 99 | done(); 100 | }); 101 | it('Should push multiple variations', function(done) { 102 | utils.addIdToFile(directory.variation.a + 'variation.json', '4567'); 103 | utils.addIdToFile(directory.variation.b + 'variation.json', '4567'); 104 | 105 | pushExperiment(directory.experiment, {iterate: true}); 106 | 107 | setTimeout(function() { 108 | assert(VariationClientStub.called, "pushVariation was not called"); 109 | done(); 110 | }, 10); 111 | 112 | }); 113 | }); 114 | }); 115 | -------------------------------------------------------------------------------- /test/commands/push-variation.test.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | 3 | var chai = require("chai"); 4 | var assert = chai.assert; 5 | var quickTemp = require('quick-temp'); 6 | var proxyquire = require('proxyquire'); 7 | var sinon = require('sinon'); 8 | 9 | var utils = require('../utils'); 10 | var logger = require("../../lib/logger.js"); 11 | 12 | //verbose logging 13 | chai.config.includeStack = true; 14 | // logger.debugLevel = 'debug'; 15 | 16 | var ClientStub = function() {}; 17 | var options = { 18 | 'cwd': __dirname 19 | }; 20 | var directory = {}; 21 | var currentDir; 22 | var pushVariation = proxyquire('../../lib/commands/push-variation', { 'optimizely-node-client': ClientStub }); 23 | 24 | describe('Push Variation Command', function() { 25 | before(function(done) { 26 | //Create temporary project directory and enter it 27 | quickTemp.makeOrRemake(directory, 'project'); 28 | options.cwd = directory.project; 29 | directory.experiment = directory.project + '/test-experiment/'; 30 | directory.variation = directory.project + '/test-experiment/test-variation/'; 31 | 32 | //Initialize the project and create experiment 33 | utils.init(directory.project); 34 | utils.experiment(directory.experiment); 35 | utils.variation(directory.experiment, 'test-variation', 'test-variation'); 36 | utils.createOptimizelyToken(directory.project); 37 | currentDir = process.cwd(); 38 | process.chdir(directory.project); 39 | done(); 40 | }); 41 | 42 | after(function(done) { 43 | //Remove the temporary directory 44 | quickTemp.remove(directory, 'project'); 45 | process.chdir(currentDir); 46 | done(); 47 | }) 48 | 49 | beforeEach(function() { 50 | ClientStub.prototype.createVariation = utils.clientFunctionStub('4567'); 51 | ClientStub.prototype.updateVariation = utils.clientFunctionStub('4567'); 52 | assert(fs.existsSync(directory.experiment), 'experiment folder not found'); 53 | assert(fs.existsSync(directory.variation), 'variation folder does not exist'); 54 | utils.addIdToFile(directory.experiment + 'experiment.json', '1234'); 55 | }) 56 | 57 | it('Should create a remote variation', function(done) { 58 | sinon.spy(ClientStub.prototype, "createVariation"); 59 | 60 | pushVariation(directory.variation, {}); 61 | 62 | setTimeout(function() { 63 | assert(ClientStub.prototype.createVariation.calledOnce, "createVariation was not called"); 64 | var variationMeta = JSON.parse(fs.readFileSync(directory.variation + 'variation.json')); 65 | assert(variationMeta.id == '4567', "Invalid variation id"); 66 | done(); 67 | }, 10); 68 | }); 69 | 70 | it('Should update a remote variation', function(done) { 71 | sinon.spy(ClientStub.prototype, "updateVariation"); 72 | utils.addIdToFile(directory.variation + 'variation.json', '4567'); 73 | 74 | pushVariation(directory.variation, {}); 75 | 76 | setTimeout(function() { 77 | assert(ClientStub.prototype.updateVariation.calledOnce, "updateVariation was not called"); 78 | var variationMeta = JSON.parse(fs.readFileSync(directory.variation + 'variation.json')); 79 | assert(variationMeta.id == '4567', "Invalid variation id"); 80 | done(); 81 | }, 10); 82 | }); 83 | 84 | }); 85 | -------------------------------------------------------------------------------- /test/commands/set-token.test.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var expect = require('chai').expect; 3 | var quickTemp = require('quick-temp'); 4 | var setToken = require('../../lib/commands/set-token.js'); 5 | 6 | var directory = {}; 7 | var tokenString = 'abcdefghijklmnop'; 8 | var currentDir; 9 | 10 | describe('Set Token Command', function(){ 11 | before(function (done){ 12 | //Create temporary project directory 13 | quickTemp.makeOrRemake(directory, 'project'); 14 | currentDir = process.cwd(); 15 | process.chdir(directory.project); 16 | done(); 17 | }); 18 | after(function (done) { 19 | //Remove the temporary directory 20 | process.chdir(currentDir); 21 | quickTemp.remove(directory, 'project'); 22 | done(); 23 | }); 24 | it('Should set the token in the current directory', function(done){ 25 | setToken(tokenString , {}); 26 | fs.readFile(directory.project + '/.optcli/token', function(err, token){ 27 | expect(err).to.be.null; 28 | expect(String(token)).to.equal(tokenString); 29 | done(); 30 | }); 31 | }); 32 | }); -------------------------------------------------------------------------------- /test/experiment.test.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var chai = require('chai'); 3 | chai.config.truncateThreshold = 0; // disable truncating 4 | var expect = chai.expect; 5 | var path = require('path'); 6 | var slash = path.sep; 7 | var proxyquire = require('proxyquire'); 8 | var quickTemp = require('quick-temp'); 9 | var _ = require('lodash'); 10 | var Project = require('../lib/project.js'); 11 | var Experiment = require('../lib/experiment.js'); 12 | var Variation = require('../lib/variation.js'); 13 | var functionCalls = []; 14 | 15 | /** 16 | * Used to push any function calls and their 17 | * arguments to the funcitonCalls array. We 18 | * can use this function to replace any 19 | * function calls that we aren't trying to 20 | * test. 21 | * @param {String} functionName name of the function being called 22 | * @param {Object} [returnObject] object to return after recording the funciton call 23 | * @return {Function} function that when called will record functionName and it's arguments 24 | */ 25 | var recordFunctionCalls = function(functionName, returnObject){ 26 | return function(){ 27 | var callRecorder = {functionName : functionName}; 28 | for(var i = 0; i < arguments.length; i++){ 29 | callRecorder[i] = arguments[i]; 30 | } 31 | functionCalls.push(callRecorder); 32 | return returnObject; 33 | }; 34 | }; 35 | 36 | var experiment; 37 | var folder = slash + 'new-experiment'; 38 | var description = 'My new experiment'; 39 | var edit_url = 'http://www.example.com'; 40 | var project_id = 154321; 41 | var directories = {}; 42 | var experimentData = { 43 | description : description, 44 | edit_url : edit_url 45 | }; 46 | quickTemp.makeOrRemake(directories, 'project'); 47 | var experimentPath = directories.project + folder; 48 | 49 | describe('Experiment Object', function (){ 50 | 51 | before(function(done){ 52 | experiment = Experiment.create({ 53 | description: description, 54 | edit_url: edit_url 55 | }, experimentPath); 56 | fs.writeFile(directories.project + slash + 'project.json', '{"id":"'+project_id+'","include_jquery":"false","project_name":"my project"}', function(err){ 57 | if(!err){ 58 | done() 59 | } else { 60 | done(err) 61 | } 62 | }); 63 | }); 64 | 65 | after(function(done){ 66 | quickTemp.remove(directories, 'project'); 67 | done(); 68 | }); 69 | 70 | describe('#create()', function() { 71 | it('Should create an experiment directory', function (done){ 72 | fs.exists(experimentPath, function(exists){ 73 | expect(exists).to.be.true; 74 | }); 75 | done(); 76 | }); 77 | it('Should create a global.css file', function(done){ 78 | fs.exists(experimentPath + slash + 'global.css', function(exists){ 79 | expect(exists).to.be.true; 80 | done(); 81 | }) 82 | }); 83 | it('Should create a global.js file', function(done){ 84 | fs.exists(experimentPath + slash + 'global.js', function(exists){ 85 | expect(exists).to.be.true; 86 | done(); 87 | }) 88 | }); 89 | it('Should create an experiment.json file', function(done){ 90 | fs.readFile(experimentPath + slash + 'experiment.json', function(err, data){ 91 | expect(err).to.be.null; 92 | expect(JSON.parse(data)).to.deep.equal({ 93 | 'description': description, 94 | 'edit_url': edit_url 95 | }); 96 | done(); 97 | }); 98 | }); 99 | it('Should return an experiment object', function(){ 100 | expect(experiment).to.be.an.instanceOf(Experiment); 101 | }); 102 | }); 103 | 104 | describe('#locateAndLoad()', function(){ 105 | var experiment; 106 | before(function(done){ 107 | experiment = Experiment.locateAndLoad(experimentPath); 108 | done(); 109 | }); 110 | after(function(done){ 111 | done(); 112 | }) 113 | it('Should locate the experiment.json', function(){ 114 | expect(experiment).to.be.an.instanceOf(Experiment); 115 | }); 116 | it('Should load the experiment.json', function(){ 117 | expect(experiment.attributes).to.deep.equal(experimentData); 118 | }); 119 | }); 120 | 121 | describe('#getJSPath()', function(){ 122 | it('Should return the JS Path', function(){ 123 | var experiment = Experiment.locateAndLoad(experimentPath); 124 | expect(experiment.getJSPath()).to.equal(experimentPath + slash + 'global.js'); 125 | }) 126 | }); 127 | 128 | describe('#getCSSPath()', function(){ 129 | it('Should return the CSS Path', function(){ 130 | var experiment = Experiment.locateAndLoad(experimentPath); 131 | expect(experiment.getCSSPath()).to.equal(experimentPath + slash + 'global.css'); 132 | }) 133 | }); 134 | 135 | describe('#getCSS()',function(){ 136 | it('Should return global.css contents', function(done){ 137 | var mockCSS = '.my-class {background: white;}'; 138 | fs.writeFile(experimentPath + slash + 'global.css', mockCSS, function(err, data){ 139 | var experiment = Experiment.locateAndLoad(experimentPath); 140 | expect(experiment.getCSS()).to.equal(mockCSS); 141 | done(); 142 | }); 143 | }); 144 | }); 145 | 146 | describe('#getJS()', function(){ 147 | it('Should return global.js contents', function(done){ 148 | var mockJS = '$(\'body\').addClass(\'my-class\');'; 149 | fs.writeFile(experimentPath + slash + 'global.js', mockJS, function(err, data){ 150 | expect(err).to.be.null; 151 | var experiment = Experiment.locateAndLoad(experimentPath); 152 | expect(experiment.getJS()).to.equal(mockJS); 153 | done(); 154 | }); 155 | }); 156 | }); 157 | 158 | describe('#getVariations()', function(){ 159 | before(function(done){ 160 | variationOne = experimentPath + slash + 'variation1'; 161 | variationTwo = experimentPath + slash + 'variation2'; 162 | Variation.create({ 163 | description: description, 164 | }, variationOne); 165 | Variation.create({ 166 | description: description, 167 | }, variationTwo); 168 | done(); 169 | }) 170 | it('Should return an array with location of variations', function(){ 171 | var experiment = Experiment.locateAndLoad(experimentPath); 172 | // Convert Windows slashes to Unix slashes for comparison 173 | var var1Path = (variationOne + slash + 'variation.json').replace(/\\/g, '/'); 174 | var var2Path = (variationTwo + slash + 'variation.json').replace(/\\/g, '/'); 175 | expect(experiment.getVariations()).to.deep.equal([var1Path, var2Path]); 176 | }) 177 | }); 178 | 179 | describe('#createRemote()', function(){ 180 | it('Should create a remote experiment', function(){ 181 | 182 | functionCalls = []; 183 | var experiment = Experiment.locateAndLoad(experimentPath); 184 | var client = { 185 | createExperiment: recordFunctionCalls('updateExperiment', 186 | {then: recordFunctionCalls('then', 187 | { 188 | catch: recordFunctionCalls('catch') 189 | }) 190 | }) 191 | } 192 | var expArgs = _.clone(experiment.attributes); 193 | expArgs['custom_css'] = experiment.getCSS(); 194 | expArgs['custom_js'] = experiment.getJS(); 195 | expArgs['project_id'] = String(project_id); 196 | 197 | experiment.createRemote(client); 198 | expect(functionCalls[0]).to.have.a.property('functionName', 'updateExperiment'); 199 | expect(functionCalls[0][0]).to.deep.equal(expArgs); 200 | }); 201 | }); 202 | describe('#updateRemote()', function(){ 203 | it('Should update a remote experiment', function(){ 204 | functionCalls = []; 205 | var experiment = Experiment.locateAndLoad(experimentPath); 206 | var client = { 207 | updateExperiment: recordFunctionCalls('updateExperiment', 208 | {then: recordFunctionCalls('then', 209 | { 210 | catch: recordFunctionCalls('catch') 211 | }) 212 | }) 213 | }; 214 | var expArgs = _.clone(experiment.attributes); 215 | expArgs['custom_css'] = experiment.getCSS(); 216 | expArgs['custom_js'] = experiment.getJS(); 217 | 218 | experiment.updateRemote(client); 219 | expect(functionCalls[0]).to.have.a.property('functionName', 'updateExperiment'); 220 | expect(functionCalls[0][0]).to.deep.equal(expArgs); 221 | }); 222 | }); 223 | 224 | describe('#getOptcliURL()', function(){ 225 | it('Should put optcli=activate at the end with an &', function(){ 226 | var newURL = edit_url + '?myparam=true'; 227 | var experiment = new Experiment({ 228 | description: description, 229 | edit_url: newURL 230 | }, experimentPath+'1'); 231 | expect(experiment.getOptcliURL()).to.equal(newURL+'&optcli=activate'); 232 | }); 233 | it('Should put optcli=activate at the end with a ?', function(){ 234 | var newURL = edit_url; 235 | var experiment = new Experiment({ 236 | description: description, 237 | edit_url: newURL 238 | }, experimentPath+'1'); 239 | expect(experiment.getOptcliURL()).to.equal(newURL+'?optcli=activate'); 240 | }); 241 | it('Should put optcli=activate before the hash with an &', function(){ 242 | var newURL = edit_url + '?mygetparam=true#myhashparam=true'; 243 | var experiment = new Experiment({ 244 | description: description, 245 | edit_url: newURL 246 | }, experimentPath+'1'); 247 | expect(experiment.getOptcliURL()).to.equal(newURL.replace('#','&optcli=activate#')); 248 | }); 249 | it('Should put optcli=activate before the hash with a ?', function(){ 250 | var newURL = edit_url + '#myhashparam=true'; 251 | var experiment = new Experiment({ 252 | description: description, 253 | edit_url: newURL 254 | }, experimentPath+'1'); 255 | expect(experiment.getOptcliURL()).to.equal(newURL.replace('#','?optcli=activate#')); 256 | }); 257 | }); 258 | }); -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --recursive -------------------------------------------------------------------------------- /test/project.test.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var quickTemp = require('quick-temp'); 3 | var Project = require('../lib/project.js'); 4 | var utils = require('./utils.js'); 5 | var chai = require('chai'); 6 | var expect = chai.expect; 7 | 8 | var directory = {}; 9 | var projectDetails = {}; 10 | 11 | describe('Project Object', function(){ 12 | describe('#createFromFile()',function(){ 13 | before(function (done){ 14 | //Create temporary project directory 15 | quickTemp.makeOrRemake(directory, 'project'); 16 | projectDetails = utils.init(directory.project); 17 | done(); 18 | }); 19 | after(function (done) { 20 | //Remove the temporary directory 21 | quickTemp.remove(directory, 'project'); 22 | done(); 23 | }); 24 | it('Should return a project loaded from a file',function(done){ 25 | var currentDir = process.cwd(); 26 | var project; 27 | process.chdir(directory.project); 28 | project = Project.createFromFile(); 29 | expect(project).to.be.an.instanceOf(Project); 30 | expect(project.attributes).to.have.property('id', projectDetails.id); 31 | process.chdir(currentDir); 32 | done(); 33 | }); 34 | }); 35 | describe('#save()',function(){ 36 | before(function (done){ 37 | //Create temporary project directory 38 | quickTemp.makeOrRemake(directory, 'project'); 39 | done(); 40 | }); 41 | after(function (done) { 42 | //Remove the temporary directory 43 | quickTemp.remove(directory, 'project'); 44 | done(); 45 | }); 46 | it('Should create a project.json', function(done){ 47 | var currentDir = process.cwd(); 48 | var projectDetails = {id: '1234', include_jquery: 'true'}; 49 | var project; 50 | 51 | process.chdir(directory.project); 52 | project = new Project(projectDetails, directory.project); 53 | project.save(); 54 | fs.readFile(directory.project + '/project.json', function(err, file){ 55 | expect(JSON.parse(file)).to.deep.equal(projectDetails); 56 | process.chdir(currentDir); 57 | done(); 58 | }); 59 | }); 60 | }); 61 | }); -------------------------------------------------------------------------------- /test/utils.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Utility functions for tests 3 | */ 4 | var assert = require('chai').assert; 5 | var fs = require('fs'); 6 | var Promise = require('bluebird'); 7 | 8 | var optcli = __dirname + "/../bin/optcli.js"; 9 | var utils = {}; 10 | var initProject = require('../lib/commands/init-project.js'); 11 | var createExperiment = require('../lib/commands/create-experiment.js'); 12 | var createVariation = require('../lib/commands/create-variation.js'); 13 | 14 | 15 | var experimentName = 'Test Experiment'; 16 | var editURL = 'http://example.com'; 17 | var projectID = 12345; 18 | 19 | /** 20 | * Initializes a project within the cwd 21 | * specified in the options object 22 | */ 23 | utils.init = function(directory, options) { 24 | var include_jquery; 25 | if(options && options.jquery) { 26 | include_jquery = options.jquery; 27 | } else { 28 | include_jquery = false; 29 | } 30 | var initialDir = __dirname; 31 | var program = {remote: false, jquery: include_jquery}; 32 | process.chdir(directory); 33 | initProject(projectID, program); 34 | 35 | process.chdir(initialDir); 36 | return { 37 | directory: directory, 38 | id: projectID 39 | } 40 | } 41 | 42 | /** 43 | * Creates an experiment within the cwd 44 | * specified in the options object 45 | */ 46 | utils.experiment = function(directory, options) { 47 | if(!options) options = {}; 48 | createExperiment(directory, experimentName, editURL, options); 49 | return { 50 | directory: directory, 51 | name: experimentName, 52 | editURL: editURL 53 | } 54 | } 55 | 56 | /** 57 | * Creates a variation within the cwd 58 | * specified in the options object 59 | */ 60 | utils.variation = function(experimentDirectory, variationFolder, variationName) { 61 | program = {}; 62 | createVariation(experimentDirectory, variationFolder, variationName, program); 63 | } 64 | 65 | /** 66 | * Creates .optcli directory and token file 67 | */ 68 | utils.createOptimizelyToken = function(projectDir) { 69 | //create the .optcli and token directory 70 | fs.mkdirSync(projectDir + '/.optcli/'); 71 | fs.writeFileSync(projectDir + '/.optcli/token', '12345'); 72 | } 73 | 74 | /** 75 | * adds an id attribute to a JSON file 76 | * */ 77 | utils.addIdToFile = function(fileName, id) { 78 | var fileAttrs = JSON.parse(fs.readFileSync(fileName)); 79 | fileAttrs['id'] = id; 80 | return fs.writeFileSync(fileName, JSON.stringify(fileAttrs)); 81 | } 82 | 83 | utils.clientFunctionStub = function(id) { 84 | return function(args) { 85 | return new Promise(function(resolve, reject) { 86 | args['id'] = id; 87 | resolve(args); 88 | }) 89 | } 90 | }; 91 | 92 | module.exports = utils; 93 | --------------------------------------------------------------------------------