|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 | 3 | var fs = require('fs');
|
| 4 | +var util = require('util'); |
4 | 5 | var path = require('path');
|
5 | 6 |
|
6 |
| -var CacheStrategy = require('./strategies/cache'); |
7 |
| -var DefaultStrategy = require('./strategies/default'); |
| 7 | +var AWS = require('../'); |
8 | 8 |
|
9 |
| -var defaultServices = 'cloudwatch,cognitoidentity,cognitosync,dynamodb,kinesis,elastictranscoder,s3,sqs,sns,sts'; |
10 |
| -var sanitizeRegex = /[^a-zA-Z0-9,-]/; |
| 9 | +var license = [ |
| 10 | + '// AWS SDK for JavaScript v' + AWS.VERSION, |
| 11 | + '// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.', |
| 12 | + '// License at https://sdk.amazonaws.com/js/BUNDLE_LICENSE.txt' |
| 13 | +].join('\n') + '\n'; |
11 | 14 |
|
12 |
| -function Builder(options) { |
13 |
| - this.setDefaultOptions(options); |
14 |
| - this.serviceCode = []; |
15 |
| - this.builtServices = {}; |
16 |
| - this.buildStrategy = this.options.cache ? |
17 |
| - new CacheStrategy(this) : new DefaultStrategy(this); |
| 15 | +function minify(code) { |
| 16 | + var uglify = require('uglify-js'); |
| 17 | + var minified = uglify.minify(code, {fromString: true}); |
| 18 | + return minified.code; |
18 | 19 | }
|
19 | 20 |
|
20 |
| -Builder.prototype.setDefaultOptions = function(options) { |
21 |
| - this.options = options || {}; |
22 |
| - this.options.libPath = this.options.libPath || this.getRootPath(); |
23 |
| - this.options.cacheRoot = this.options.cacheRoot || |
24 |
| - path.join(this.options.libPath, 'dist-tools', 'cache'); |
25 |
| - this.options.cache = this.options.cache || false; |
26 |
| - this.options.writeCache = this.options.writeCache || false; |
27 |
| - this.options.minify = this.options.minify || false; |
28 |
| - this.options.minifyOptions = this.options.minifyOptions || {compress: false}; |
29 |
| -}; |
30 |
| - |
31 |
| -Builder.prototype.getRootPath = function() { |
32 |
| - return path.join(__dirname, '..'); |
33 |
| -}; |
34 |
| - |
35 |
| -Builder.prototype.cachePath = function(path) { |
36 |
| - var fullPath = this.options.cacheRoot; |
37 |
| - if (path) { |
38 |
| - fullPath += '/' + path + (this.options.minify ? '.min' : '') + '.js'; |
39 |
| - } |
40 |
| - |
41 |
| - return fullPath; |
42 |
| -}; |
43 |
| - |
44 |
| -Builder.prototype.cacheExists = function(path) { |
45 |
| - return fs.existsSync(this.cachePath(path)); |
46 |
| -}; |
47 |
| - |
48 |
| -Builder.prototype.buildService = function(name, usingDefaultServices) { |
49 |
| - var match = name.match(/^(.+?)(?:-(.+?))?$/); |
50 |
| - var service = match[1], version = match[2] || 'latest'; |
51 |
| - var contents = []; |
52 |
| - var lines, err; |
53 |
| - |
54 |
| - if (!this.builtServices[service]) { |
55 |
| - this.builtServices[service] = {}; |
56 |
| - |
57 |
| - lines = this.buildStrategy.getServiceHeader(service); |
58 |
| - if (lines === null) { |
59 |
| - if (!usingDefaultServices) { |
60 |
| - err = new Error('Invalid module: ' + service); |
61 |
| - err.name = 'InvalidModuleError'; |
62 |
| - throw err; |
63 |
| - } |
64 |
| - } else { |
65 |
| - contents.push(lines); |
| 21 | +function stripComments(code) { |
| 22 | + var lines = code.split(/\r?\n/); |
| 23 | + var multiLine = false; |
| 24 | + lines = lines.map(function (line) { |
| 25 | + var rLine = line; |
| 26 | + if (line.match(/^\s*\/\//)) { |
| 27 | + rLine = null; |
| 28 | + } else if (line.match(/^\s*\/\*/)) { |
| 29 | + multiLine = true; |
| 30 | + rLine = null; |
66 | 31 | }
|
67 |
| - } |
68 | 32 |
|
69 |
| - if (!this.builtServices[service][version]) { |
70 |
| - this.builtServices[service][version] = true; |
71 |
| - |
72 |
| - lines = this.buildStrategy.getService(service, version); |
73 |
| - if (lines === null) { |
74 |
| - if (!usingDefaultServices) { |
75 |
| - err = new Error('Invalid module: ' + service + '-' + version); |
76 |
| - err.name = 'InvalidModuleError'; |
77 |
| - throw err; |
| 33 | + if (multiLine) { |
| 34 | + var multiLineEnd = line.match(/\*\/(.*)/); |
| 35 | + if (multiLineEnd) { |
| 36 | + multiLine = false; |
| 37 | + rLine = multiLineEnd[1]; |
| 38 | + } else { |
| 39 | + rLine = null; |
78 | 40 | }
|
79 |
| - } else { |
80 |
| - contents.push(lines); |
81 | 41 | }
|
82 |
| - } |
83 | 42 |
|
84 |
| - return contents.join('\n'); |
85 |
| -}; |
| 43 | + return rLine; |
| 44 | + }).filter(function(l) { return l !== null; }); |
86 | 45 |
|
87 |
| -Builder.prototype.addServices = function(services) { |
88 |
| - var usingDefaultServices = false; |
89 |
| - if (!services) { |
90 |
| - usingDefaultServices = true; |
91 |
| - services = defaultServices; |
92 |
| - } |
93 |
| - if (services.match(sanitizeRegex)) { |
94 |
| - throw new Error('Incorrectly formatted service names'); |
| 46 | + var newCode = lines.join('\n'); |
| 47 | + newCode = newCode.replace(/\/\*\*[\s\S]+?Copyright\s+.+?Amazon[\s\S]+?\*\//g, ''); |
| 48 | + return newCode; |
| 49 | +} |
| 50 | + |
| 51 | +function build(options, callback) { |
| 52 | + if (arguments.length === 1) { |
| 53 | + callback = options; |
| 54 | + options = {}; |
95 | 55 | }
|
96 | 56 |
|
97 |
| - var invalidModules = []; |
98 |
| - var stsIncluded = false; |
99 |
| - services.split(',').sort().forEach(function(name) { |
100 |
| - if (name.match(/^sts\b/) || name === 'all') stsIncluded = true; |
101 |
| - try { |
102 |
| - this.serviceCode.push(this.buildService(name, usingDefaultServices)); |
103 |
| - } catch (e) { |
104 |
| - if (e.name === 'InvalidModuleError') invalidModules.push(name); |
105 |
| - else throw e; |
106 |
| - } |
107 |
| - }.bind(this)); |
| 57 | + var img = require('browserify/node_modules/insert-module-globals'); |
| 58 | + img.vars.process = function() { return '{browser:true}'; }; |
108 | 59 |
|
109 |
| - if (!stsIncluded) { |
110 |
| - this.serviceCode.push(this.buildService('sts')); |
111 |
| - } |
| 60 | + if (options.services) process.env.AWS_SERVICES = options.services; |
112 | 61 |
|
113 |
| - if (invalidModules.length > 0) { |
114 |
| - throw new Error('Missing modules: ' + invalidModules.join(', ')); |
115 |
| - } |
| 62 | + var browserify = require('browserify'); |
| 63 | + var brOpts = { basedir: path.resolve(__dirname, '..') }; |
| 64 | + browserify(brOpts).add('./').ignore('domain').bundle(function(err, data) { |
| 65 | + if (err) return callback(err); |
116 | 66 |
|
117 |
| - return this; |
118 |
| -}; |
| 67 | + var code = (data || '').toString(); |
| 68 | + if (options.minify) code = minify(code); |
| 69 | + else code = stripComments(code); |
119 | 70 |
|
120 |
| -Builder.prototype.build = function(callback) { |
121 |
| - this.buildStrategy.getCore(function(err, core) { |
122 |
| - callback(err, err ? null : (core + ';' + this.serviceCode.join('\n'))); |
123 |
| - }.bind(this)); |
| 71 | + code = license + code; |
| 72 | + callback(null, code); |
| 73 | + }); |
124 | 74 | };
|
125 | 75 |
|
126 | 76 | // run if we called this tool directly
|
127 | 77 | if (require.main === module) {
|
128 | 78 | var options = {
|
129 |
| - minify: process.env.MINIFY ? true : false, |
130 |
| - cache: process.env.CACHE ? true : false, |
131 |
| - writeCache: process.env.WRITE_CACHE ? true : false, |
132 |
| - cacheRoot: process.env.CACHE_ROOT, |
133 |
| - libPath: process.env.LIB_PATH |
| 79 | + services: process.argv[2] || process.env.SERVICES, |
| 80 | + minify: process.env.MINIFY ? true : false |
134 | 81 | };
|
135 |
| - var services = process.argv[2] || process.env.SERVICES; |
136 |
| - new Builder(options).addServices(services).build(function (err, code) { |
| 82 | + build(options, function(err, code) { |
137 | 83 | if (err) console.error(err.message);
|
138 | 84 | else console.log(code);
|
139 | 85 | });
|
140 | 86 | }
|
141 | 87 |
|
142 |
| -module.exports = Builder; |
| 88 | +build.license = license; |
| 89 | +module.exports = build; |
0 commit comments