Permalink
Browse files

Add browser builder code back into repository

  • Loading branch information...
1 parent 4f08e00 commit 45bdeb240f44ee5404e15988fa9065cfb764b758 Loren Segal committed Jun 9, 2014
View
@@ -9,6 +9,7 @@ branches:
before_script:
- "npm install git://github.com/aws/aws-sdk-js-apis#$TRAVIS_BRANCH"
+ - "cd dist-tools && npm install"
script: "if [ $INTEGRATION ]; then cucumber.js -f pretty; else npm test; fi"
View
@@ -0,0 +1,10 @@
+{
+ "env": {
+ "node": true
+ },
+ "rules": {
+ "quotes": [2, "single"],
+ "strict": 0,
+ "curly": 0
+ }
+}
@@ -0,0 +1,142 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var path = require('path');
+
+var CacheStrategy = require('./strategies/cache');
+var DefaultStrategy = require('./strategies/default');
+
+var defaultServices = 'cloudwatch,dynamodb,kinesis,s3,sqs,sns,sts';
+var sanitizeRegex = /[^a-zA-Z0-9,-]/;
+
+function Builder(options) {
+ this.setDefaultOptions(options);
+ this.serviceCode = [];
+ this.builtServices = {};
+ this.buildStrategy = this.options.cache ?
+ new CacheStrategy(this) : new DefaultStrategy(this);
+}
+
+Builder.prototype.setDefaultOptions = function(options) {
+ this.options = options || {};
+ this.options.libPath = this.options.libPath || this.getRootPath();
+ this.options.cacheRoot = this.options.cacheRoot ||
+ path.join(this.options.libPath, 'dist-tools', 'cache');
+ this.options.cache = this.options.cache || false;
+ this.options.writeCache = this.options.writeCache || false;
+ this.options.minify = this.options.minify || false;
+ this.options.minifyOptions = this.options.minifyOptions || {compress: false};
+};
+
+Builder.prototype.getRootPath = function() {
+ return path.join(__dirname, '..');
+};
+
+Builder.prototype.cachePath = function(path) {
+ var fullPath = this.options.cacheRoot;
+ if (path) {
+ fullPath += '/' + path + (this.options.minify ? '.min' : '') + '.js';
+ }
+
+ return fullPath;
+};
+
+Builder.prototype.cacheExists = function(path) {
+ return fs.existsSync(this.cachePath(path));
+};
+
+Builder.prototype.buildService = function(name, usingDefaultServices) {
+ var match = name.match(/^(.+?)(?:-(.+?))?$/);
+ var service = match[1], version = match[2] || 'latest';
+ var contents = [];
+ var lines, err;
+
+ if (!this.builtServices[service]) {
+ this.builtServices[service] = {};
+
+ lines = this.buildStrategy.getServiceHeader(service);
+ if (lines === null) {
+ if (!usingDefaultServices) {
+ err = new Error('Invalid module: ' + service);
+ err.name = 'InvalidModuleError';
+ throw err;
+ }
+ } else {
+ contents.push(lines);
+ }
+ }
+
+ if (!this.builtServices[service][version]) {
+ this.builtServices[service][version] = true;
+
+ lines = this.buildStrategy.getService(service, version);
+ if (lines === null) {
+ if (!usingDefaultServices) {
+ err = new Error('Invalid module: ' + service + '-' + version);
+ err.name = 'InvalidModuleError';
+ throw err;
+ }
+ } else {
+ contents.push(lines);
+ }
+ }
+
+ return contents.join('\n');
+};
+
+Builder.prototype.addServices = function(services) {
+ var usingDefaultServices = false;
+ if (!services) {
+ usingDefaultServices = true;
+ services = defaultServices;
+ }
+ if (services.match(sanitizeRegex)) {
+ throw new Error('Incorrectly formatted service names');
+ }
+
+ var invalidModules = [];
+ var stsIncluded = false;
+ services.split(',').sort().forEach(function(name) {
+ if (name.match(/^sts\b/) || name === 'all') stsIncluded = true;
+ try {
+ this.serviceCode.push(this.buildService(name, usingDefaultServices));
+ } catch (e) {
+ if (e.name === 'InvalidModuleError') invalidModules.push(name);
+ else throw e;
+ }
+ }.bind(this));
+
+ if (!stsIncluded) {
+ this.serviceCode.push(this.buildService('sts'));
+ }
+
+ if (invalidModules.length > 0) {
+ throw new Error('Missing modules: ' + invalidModules.join(', '));
+ }
+
+ return this;
+};
+
+Builder.prototype.build = function(callback) {
+ this.buildStrategy.getCore(function(err, core) {
+ callback(err, err ? null : (core + ';' + this.serviceCode.join('\n')));
+ }.bind(this));
+};
+
+// run if we called this tool directly
+if (require.main === module) {
+ var options = {
+ minify: process.env.MINIFY ? true : false,
+ cache: process.env.CACHE ? true : false,
+ writeCache: process.env.WRITE_CACHE ? true : false,
+ cacheRoot: process.env.CACHE_ROOT,
+ libPath: process.env.LIB_PATH
+ };
+ var services = process.argv[2] || process.env.SERVICES;
+ new Builder(options).addServices(services).build(function (err, code) {
+ if (err) console.error(err.message);
+ else console.log(code);
+ });
+}
+
+module.exports = Builder;
@@ -0,0 +1,22 @@
+{
+ "name": "aws-sdk-browser-builder",
+ "description": "AWS SDK for JavaScript browser building tools",
+ "version": "0.0.1",
+ "private": true,
+ "dependencies": {
+ "browserify": "3.x",
+ "uglify-js": "2.x"
+ },
+ "devDependencies": {
+ "mocha": "*",
+ "chai": "*",
+ "coffee-script": "1.6.x",
+ "eslint": "*"
+ },
+ "scripts" : {
+ "test": "npm run-script lint && npm run-script unit",
+ "unit": "mocha --compilers coffee:coffee-script -s 1000 -t 10000",
+ "coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --compilers coffee:coffee-script -s 1000 -t 10000",
+ "lint": "eslint --rulesdir ../eslint-rules *.js strategies"
+ }
+}
@@ -0,0 +1,68 @@
+var fs = require('fs');
+
+function CacheStrategy(builder) {
+ this.builder = builder;
+ this.cacheRoot = this.builder.options.cacheRoot;
+ this.minify = this.builder.options.minify;
+
+ this.loadServices();
+ if (!this.builder.cacheExists()) {
+ fs.mkdirSync(this.builder.cachePath());
+ }
+}
+
+CacheStrategy.prototype.loadServices = function() {
+ this.services = {};
+ fs.readdirSync(this.cacheRoot).forEach(function(file) {
+ var match = file.match(/^([^_-]+)-(latest|\d+-\d+-\d+)\.(?:min\.)?js$/);
+ if (match) {
+ var service = match[1], version = match[2];
+ if (!this.services[service]) this.services[service] = {};
+ this.services[service][version] = service + '-' + version;
+ }
+ }.bind(this));
+};
+
+CacheStrategy.prototype.getServiceHeader = function(service) {
+ if (service === 'all') {
+ return Object.keys(this.services).map(function (name) {
+ return this.getServiceHeader(name);
+ }.bind(this)).join('\n');
+ }
+
+ if (this.services[service] && this.builder.cacheExists(service)) {
+ return this.read(service);
+ }
+ return null;
+};
+
+CacheStrategy.prototype.getService = function(service, version) {
+ if (service === 'all') {
+ return Object.keys(this.services).map(function (name) {
+ return this.getService(name);
+ }.bind(this)).join('\n');
+ }
+
+ var versions = this.services[service];
+ if (versions) {
+ var file = versions[version || 'latest'];
+ if (file && this.builder.cacheExists(file)) {
+ return this.read(file);
+ }
+ }
+ return null;
+};
+
+CacheStrategy.prototype.getCore = function(callback) {
+ if (this.builder.cacheExists('_core')) {
+ callback(null, this.read('_core'));
+ } else {
+ callback(new Error('Core not found.'));
+ }
+};
+
+CacheStrategy.prototype.read = function(path) {
+ return fs.readFileSync(this.builder.cachePath(path)).toString();
+};
+
+module.exports = CacheStrategy;
Oops, something went wrong.

0 comments on commit 45bdeb2

Please sign in to comment.