├── .github └── workflows │ └── cla.yml ├── .gitignore ├── LICENSE ├── README.md ├── lib ├── createError.js ├── download.js ├── extract.js ├── index.js ├── request.js └── utils.js └── package.json /.github/workflows/cla.yml: -------------------------------------------------------------------------------- 1 | name: "CLA Assistant" 2 | on: 3 | # issue_comment triggers this action on each comment on issues and pull requests 4 | issue_comment: 5 | types: [created] 6 | pull_request_target: 7 | types: [opened,synchronize] 8 | 9 | jobs: 10 | CLAssistant: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions-ecosystem/action-regex-match@v2 14 | id: sign-or-recheck 15 | with: 16 | text: ${{ github.event.comment.body }} 17 | regex: '\s*(I have read the CLA Document and I hereby sign the CLA)|(recheckcla)\s*' 18 | 19 | - name: "CLA Assistant" 20 | if: ${{ steps.sign-or-recheck.outputs.match != '' || github.event_name == 'pull_request_target' }} 21 | # Alpha Release 22 | uses: cla-assistant/github-action@v2.1.1-beta 23 | env: 24 | # Generated and maintained by github 25 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 26 | # JFrog organization secret 27 | PERSONAL_ACCESS_TOKEN : ${{ secrets.CLA_SIGN_TOKEN }} 28 | with: 29 | path-to-signatures: 'signed_clas.json' 30 | path-to-document: 'https://jfrog.com/cla/' 31 | remote-organization-name: 'jfrog' 32 | remote-repository-name: 'jfrog-signed-clas' 33 | # branch should not be protected 34 | branch: 'master' 35 | allowlist: bot* 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | # Idea 30 | *.iml 31 | .idea 32 | -------------------------------------------------------------------------------- /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 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bower-art-resolver 2 | A custom Bower resolver dedicated to allow integration of Bower with Artifactory 3 | 4 | ## Installation 5 | `npm install -g bower-art-resolver` 6 | 7 | In order to use Bower with Artifactory you need 2 components (npm packages): 8 | 9 | 1. [bower-art-resolver](https://www.npmjs.com/package/bower-art-resolver) - A custom, pluggable Bower resolver which is dedicated to allow integratione of Bower with Artifactory. 10 | 2. [bower](https://www.npmjs.com/package/bower) - Bower version 1.5.0 and above. 11 | 12 | ## Client Configuration 13 | Edit your ~/.bowerrc and add: 14 | 1. The *bower-art-resolver*. 15 | 2. The Artifactory URL as the first search URL. 16 | 17 | ```json 18 | { 19 | "registry": { 20 | "register": "https://bower.herokuapp.com", 21 | "search": [ 22 | "http:///artifactory/api/bower/" 23 | ] 24 | }, 25 | "resolvers": [ 26 | "bower-art-resolver" 27 | ] 28 | } 29 | ``` 30 | 31 | For authenticated access, please add the user and password to Artifactory URL as follows: 32 | ```json 33 | "search": [ 34 | "http://user:password@/artifactory/api/bower/" 35 | ] 36 | ``` 37 | 38 | You can also use an encrypted Artifactory password. 39 | 40 | ## Artifactory Configuration 41 | 42 | ### Bower remote repository 43 | 1. Create a new remote repository and set its Package Type to be “Bower”. You might call it “bower-remote” 44 | 2. Set the Repository Key value, and enter the SCM URL e.g. https://github.com, https://bitbucket.org, http://remote.org/artifactory/api/vcs/vcs-repo, or enter your own custom VCS. 45 | 3. In the Bower Settings section, select GitHub as the Git Provider, and leave the default Registry URL (https://bower.herokuapp.com). 46 | 5. Finally, click "Save & Finish" 47 | 48 | ### Bower local/virtual repository 49 | 1. Create a new local/virtual repository and enter a repository key. For example, "bower-local" or "bower-virtual". 50 | 2. Packages -> Check "Enable Bower Support" and enter your bower registry url (by default https://bower.herokuapp.com) 51 | 3. Save 52 | 53 | ## Usage 54 | 55 | Use the client to install packages from Artifactory, For example, `bower install bootstrap` 56 | 57 | For more information, please refer to the [Artifactory User Guide](http://www.jfrog.com/confluence/display/RTF/Bower+Repositories) 58 | -------------------------------------------------------------------------------- /lib/createError.js: -------------------------------------------------------------------------------- 1 | var mout = require('mout'); 2 | 3 | function createError(msg, code, props) { 4 | var err = new Error(msg); 5 | err.code = code; 6 | 7 | if (props) { 8 | mout.object.mixIn(err, props); 9 | } 10 | 11 | return err; 12 | } 13 | 14 | module.exports = createError; 15 | -------------------------------------------------------------------------------- /lib/download.js: -------------------------------------------------------------------------------- 1 | var progress = require('request-progress'); 2 | var Q = require('q'); 3 | var object = require('mout/object'); 4 | var retry = require('retry'); 5 | var fs = require('graceful-fs'); 6 | var createError = require('./createError'); 7 | var url = require('url') 8 | var request = require('request'); 9 | var tmp = require('tmp'); 10 | var path = require('path'); 11 | 12 | var errorCodes = [ 13 | 'EADDRINFO', 14 | 'ETIMEDOUT', 15 | 'ECONNRESET', 16 | 'EINCOMPLETE', 17 | 'ESOCKETTIMEDOUT' 18 | ]; 19 | 20 | function download(requestUrl, downloadPath, config) { 21 | var parsedUrl = url.parse(requestUrl); 22 | 23 | var file = tmp.tmpNameSync({ dir: downloadPath, postfix: '.tar.gz' }); 24 | 25 | var operation; 26 | var response; 27 | var deferred = Q.defer(); 28 | var progressDelay = 8000; 29 | 30 | if (config === undefined) { 31 | config = {}; 32 | } 33 | 34 | var retryOptions = object.mixIn({ 35 | retries: 5, 36 | factor: 2, 37 | minTimeout: 1000, 38 | maxTimeout: 35000, 39 | randomize: true 40 | }, config.retry || {}); 41 | 42 | var _request = request.defaults({ 43 | ca: config.ca.search[0], 44 | strictSSL: config.strictSsl, 45 | timeout: config.timeout 46 | }) 47 | 48 | _request = _request.defaults(config.request || {}) 49 | 50 | // Retry on network errors 51 | operation = retry.operation(retryOptions); 52 | operation.attempt(function () { 53 | var req; 54 | var writeStream; 55 | var contentLength; 56 | var bytesDownloaded = 0; 57 | 58 | req = progress(_request(requestUrl), { 59 | delay: progressDelay 60 | }) 61 | .on('response', function (res) { 62 | var status = res.statusCode; 63 | 64 | if (status < 200 || status >= 300) { 65 | return deferred.reject(createError('Status code of ' + status + ' for ' + requestUrl, 'EHTTP', { 66 | details: res.toString() 67 | })); 68 | } 69 | 70 | response = res; 71 | contentLength = Number(res.headers['content-length']); 72 | }) 73 | .on('data', function (data) { 74 | bytesDownloaded += data.length; 75 | }) 76 | .on('progress', function (state) { 77 | deferred.notify(state); 78 | }) 79 | .on('end', function () { 80 | // Check if the whole file was downloaded 81 | // In some unstable connections the ACK/FIN packet might be sent in the 82 | // middle of the download 83 | // See: https://github.com/joyent/node/issues/6143 84 | if (contentLength && bytesDownloaded < contentLength) { 85 | req.emit('error', createError('Transfer closed with ' + (contentLength - bytesDownloaded) + ' bytes remaining to read', 'EINCOMPLETE')); 86 | } 87 | }) 88 | .on('error', function (error) { 89 | var timeout = operation._timeouts[0]; 90 | 91 | // Reject if error is not a network error 92 | if (errorCodes.indexOf(error.code) === -1) { 93 | return deferred.reject(error); 94 | } 95 | 96 | // Next attempt will start reporting download progress immediately 97 | progressDelay = 0; 98 | 99 | // Check if there are more retries 100 | if (operation.retry(error)) { 101 | // Ensure that there are no more events from this request 102 | req.removeAllListeners(); 103 | req.on('error', function () {}); 104 | // Ensure that there are no more events from the write stream 105 | writeStream.removeAllListeners(); 106 | writeStream.on('error', function () {}); 107 | 108 | return deferred.notify({ 109 | retry: true, 110 | delay: timeout, 111 | error: error 112 | }); 113 | } 114 | 115 | // No more retries, reject! 116 | deferred.reject(error); 117 | }); 118 | 119 | // Pipe read stream to write stream 120 | writeStream = req 121 | .pipe(fs.createWriteStream(file)) 122 | .on('error', deferred.reject) 123 | .on('close', function () { 124 | deferred.resolve(file); 125 | }); 126 | }); 127 | 128 | return deferred.promise; 129 | } 130 | 131 | module.exports = download; 132 | -------------------------------------------------------------------------------- /lib/extract.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var fs = require('graceful-fs'); 3 | var zlib = require('zlib'); 4 | var DecompressZip = require('decompress-zip'); 5 | var tar = require('tar-fs'); 6 | var Q = require('q'); 7 | var mout = require('mout'); 8 | var junk = require('junk'); 9 | var createError = require('./createError'); 10 | var tmp = require('tmp'); 11 | 12 | // This forces the default chunk size to something small in an attempt 13 | // to avoid issue #314 14 | zlib.Z_DEFAULT_CHUNK = 1024 * 8; 15 | 16 | var extractors; 17 | var extractorTypes; 18 | 19 | extractors = { 20 | '.zip': extractZip, 21 | '.tar': extractTar, 22 | '.tar.gz': extractTarGz, 23 | '.tgz': extractTarGz, 24 | '.gz': extractGz, 25 | 'application/zip': extractZip, 26 | 'application/x-zip': extractZip, 27 | 'application/x-zip-compressed': extractZip, 28 | 'application/x-tar': extractTar, 29 | 'application/x-tgz': extractTarGz, 30 | 'application/x-gzip': extractGz 31 | }; 32 | 33 | extractorTypes = Object.keys(extractors); 34 | 35 | function extractZip(archive, dst) { 36 | var deferred = Q.defer(); 37 | 38 | new DecompressZip(archive) 39 | .on('error', deferred.reject) 40 | .on('extract', deferred.resolve.bind(deferred, dst)) 41 | .extract({ 42 | path: dst, 43 | follow: false, // Do not follow symlinks (#699) 44 | filter: filterSymlinks // Filter symlink files 45 | }); 46 | 47 | return deferred.promise; 48 | } 49 | 50 | function extractTar(archive, dst) { 51 | var deferred = Q.defer(); 52 | 53 | fs.createReadStream(archive) 54 | .on('error', deferred.reject) 55 | .pipe(tar.extract(dst, { 56 | ignore: isSymlink, // Filter symlink files 57 | dmode: 0555, // Ensure dirs are readable 58 | fmode: 0444 // Ensure files are readable 59 | })) 60 | .on('error', deferred.reject) 61 | .on('finish', deferred.resolve.bind(deferred, dst)); 62 | 63 | return deferred.promise; 64 | } 65 | 66 | function extractTarGz(archive, dst) { 67 | var deferred = Q.defer(); 68 | 69 | fs.createReadStream(archive) 70 | .on('error', deferred.reject) 71 | .pipe(zlib.createGunzip()) 72 | .on('error', deferred.reject) 73 | .pipe(tar.extract(dst, { 74 | ignore: isSymlink, // Filter symlink files 75 | dmode: 0555, // Ensure dirs are readable 76 | fmode: 0444 // Ensure files are readable 77 | })) 78 | .on('error', deferred.reject) 79 | .on('finish', deferred.resolve.bind(deferred, dst)); 80 | 81 | return deferred.promise; 82 | } 83 | 84 | function extractGz(archive, dst) { 85 | var deferred = Q.defer(); 86 | 87 | fs.createReadStream(archive) 88 | .on('error', deferred.reject) 89 | .pipe(zlib.createGunzip()) 90 | .on('error', deferred.reject) 91 | .pipe(fs.createWriteStream(dst)) 92 | .on('error', deferred.reject) 93 | .on('close', deferred.resolve.bind(deferred, dst)); 94 | 95 | return deferred.promise; 96 | } 97 | 98 | function isSymlink(entry) { 99 | return entry.type === 'SymbolicLink'; 100 | } 101 | 102 | function filterSymlinks(entry) { 103 | return entry.type !== 'SymbolicLink'; 104 | } 105 | 106 | function getExtractor(archive) { 107 | // Make the archive lower case to match against the types 108 | // This ensures that upper-cased extensions work 109 | archive = archive.toLowerCase(); 110 | 111 | var type = mout.array.find(extractorTypes, function (type) { 112 | return mout.string.endsWith(archive, type); 113 | }); 114 | 115 | return type ? extractors[type] : null; 116 | } 117 | 118 | function isSingleDir(dir) { 119 | return Q.nfcall(fs.readdir, dir) 120 | .then(function (files) { 121 | var singleDir; 122 | 123 | // Remove any OS specific files from the files array 124 | // before checking its length 125 | files = files.filter(junk.isnt); 126 | 127 | if (files.length !== 1) { 128 | return false; 129 | } 130 | 131 | singleDir = path.join(dir, files[0]); 132 | 133 | return Q.nfcall(fs.stat, singleDir) 134 | .then(function (stat) { 135 | return stat.isDirectory() ? singleDir : false; 136 | }); 137 | }); 138 | } 139 | 140 | function moveSingleDirContents(dir) { 141 | var destDir = path.dirname(dir); 142 | 143 | return Q.nfcall(fs.readdir, dir) 144 | .then(function (files) { 145 | var promises; 146 | 147 | promises = files.map(function (file) { 148 | var src = path.join(dir, file); 149 | var dst = path.join(destDir, file); 150 | 151 | return Q.nfcall(fs.rename, src, dst); 152 | }); 153 | 154 | return Q.all(promises); 155 | }) 156 | .then(function () { 157 | return Q.nfcall(fs.rmdir, dir); 158 | }); 159 | } 160 | 161 | // Available options: 162 | // - keepArchive: true to keep the archive afterwards (defaults to false) 163 | // - keepStructure: true to keep the extracted structure unchanged (defaults to false) 164 | function extract(src, dst, opts) { 165 | var extractor; 166 | var promise; 167 | 168 | opts = opts || {}; 169 | extractor = getExtractor(src); 170 | 171 | // Try to get extractor from mime type 172 | if (!extractor && opts.mimeType) { 173 | extractor = getExtractor(opts.mimeType); 174 | } 175 | 176 | // If extractor is null, then the archive type is unknown 177 | if (!extractor) { 178 | return Q.reject(createError('File ' + src + ' is not a known archive', 'ENOTARCHIVE')); 179 | } 180 | 181 | // Check archive file size 182 | promise = Q.nfcall(fs.stat, src) 183 | .then(function (stat) { 184 | if (stat.size <= 8) { 185 | throw createError('File ' + src + ' is an invalid archive', 'ENOTARCHIVE'); 186 | } 187 | 188 | // Extract archive 189 | return extractor(src, dst); 190 | }); 191 | 192 | // TODO: There's an issue here if the src and dst are the same and 193 | // The zip name is the same as some of the zip file contents 194 | // Maybe create a temp directory inside dst, unzip it there, 195 | // unlink zip and then move contents 196 | 197 | // Remove archive 198 | if (!opts.keepArchive) { 199 | promise = promise 200 | .then(function () { 201 | return Q.nfcall(fs.unlink, src); 202 | }); 203 | } 204 | 205 | // Move contents if a single directory was extracted 206 | if (!opts.keepStructure) { 207 | promise = promise 208 | .then(function () { 209 | return isSingleDir(dst); 210 | }) 211 | .then(function (singleDir) { 212 | return singleDir ? moveSingleDirContents(singleDir) : null; 213 | }); 214 | } 215 | 216 | // Resolve promise to the dst dir 217 | return promise.then(function () { 218 | return dst; 219 | }); 220 | } 221 | 222 | module.exports = extract; 223 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var tmp = require('tmp'); 2 | tmp.setGracefulCleanup(); 3 | 4 | var request = require('./request'); 5 | var download = require('./download'); 6 | var extract = require('./extract'); 7 | var utils = require('./utils'); 8 | 9 | module.exports = function (bower) { 10 | function getFragments(source) { 11 | return { 12 | registryUrl: utils.getRegistryUrl(bower.config), 13 | repositoryName: utils.getRepositoryName(source) 14 | } 15 | }; 16 | 17 | return { 18 | match: function (source) { 19 | return source.indexOf(utils.ARTIFACTORY_PREFIX) === 0; 20 | }, 21 | 22 | releases: function (source) { 23 | var fragments = getFragments(source); 24 | var requestUrl = fragments.registryUrl + '/refs/' + fragments.repositoryName; 25 | 26 | return request(requestUrl, bower.config).then(function (response) { 27 | return utils.extractReleases(response); 28 | }); 29 | }, 30 | 31 | fetch: function (endpoint, cached) { 32 | // If cached package is semver, reuse it 33 | if (cached.version === endpoint.target) return; 34 | 35 | var fragments = getFragments(endpoint.source); 36 | 37 | var downloadUrl = fragments.registryUrl + '/binaries/' + fragments.repositoryName + '.git/' + endpoint.target; 38 | 39 | var downloadPath = tmp.dirSync(); 40 | 41 | return download(downloadUrl, downloadPath.name, bower.config).then(function (archivePatch) { 42 | var extractPath = tmp.dirSync(); 43 | 44 | return extract(archivePatch, extractPath.name).then(function () { 45 | downloadPath.removeCallback(); 46 | 47 | return { 48 | tempPath: extractPath.name, 49 | removeIgnores: true 50 | }; 51 | }); 52 | }); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /lib/request.js: -------------------------------------------------------------------------------- 1 | var request = require('request'); 2 | var Q = require('q'); 3 | var url = require('url'); 4 | var object = require('mout/object'); 5 | var createError = require('./createError'); 6 | 7 | function requestWrapper(requestUrl, config) { 8 | var deferred = Q.defer(); 9 | 10 | var protocol = url.parse(requestUrl).protocol; 11 | 12 | var _request = request.defaults({ 13 | ca: config.ca.search[0], 14 | strictSSL: config.strictSsl, 15 | timeout: config.timeout 16 | }) 17 | 18 | _request = _request.defaults(config.request || {}) 19 | 20 | _request(requestUrl, function (error, response, body) { 21 | if (error) { 22 | deferred.reject(createError('Request to ' + requestUrl + ' failed: ' + error.message, error.code)); 23 | } else { 24 | if (response.statusCode === 200) { 25 | deferred.resolve(body); 26 | } else { 27 | deferred.reject(createError('Request to ' + requestUrl + ' returned ' + response.statusCode + ' status code.', 'EREQUEST', { 28 | details: response.toString() 29 | })); 30 | } 31 | } 32 | }); 33 | 34 | return deferred.promise; 35 | }; 36 | 37 | module.exports = requestWrapper; 38 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | var semver = require('semver'); 2 | var createError = require('./createError'); 3 | var string = require('mout/string'); 4 | 5 | var ARTIFACTORY_PREFIX = "art://"; 6 | 7 | function clean(version) { 8 | var parsed = semver.parse(version); 9 | 10 | if (!parsed) { 11 | return null; 12 | } 13 | 14 | // Keep builds! 15 | return parsed.version + (parsed.build.length ? '+' + parsed.build.join('.') : ''); 16 | } 17 | 18 | function releases(tags) { 19 | //WORKAROUND: fallback to the latest commit on master 20 | // In case no version (tag) is available for the package, and the target is probable * 21 | // (at this point the target must be a valid semver version) 22 | 23 | if (!tags.length) { 24 | return [{ 25 | release: 'master', 26 | target: 'master', 27 | version: '0.0.0' 28 | }]; 29 | } 30 | 31 | return tags.map(function (tag) { 32 | return { 33 | release: clean(tag.tag) || tag.tag, 34 | target: tag.tag, 35 | version: clean(tag.tag) 36 | }; 37 | }); 38 | }; 39 | 40 | function tags(refs) { 41 | var tags = []; 42 | 43 | // For each line in the refs, match only the tags 44 | refs.forEach(function (line) { 45 | var match = line.match(/^([a-f0-9]{44})\s+refs\/tags\/(\S+)/); 46 | 47 | if (match && !string.endsWith(match[2], '^{}')) { 48 | tags.push({ tag: match[2], commit: match[1] }) 49 | } 50 | }); 51 | 52 | return tags; 53 | }; 54 | 55 | function extractRefs(response) { 56 | return response 57 | .trim() // Trim trailing and leading spaces 58 | .split(/[\r\n]+/); 59 | } 60 | 61 | function extractReleases(response) { 62 | return releases(tags(extractRefs(response))); 63 | } 64 | 65 | function getRegistryUrl(config) { 66 | return config.registry.search[0]; 67 | }; 68 | 69 | function getRepositoryName(source) { 70 | var match = source.replace(ARTIFACTORY_PREFIX, "").split("/") 71 | 72 | if (match.length < 2) { 73 | throw createError('Invalid Artifactory Package Name', 'EINVEND', { 74 | details: source + ' does not seem to be a valid Artifactory package name!' 75 | }); 76 | } 77 | 78 | return match[0] + "/" + match[1]; 79 | }; 80 | 81 | exports.clean = clean; 82 | exports.extractReleases = extractReleases; 83 | exports.getRegistryUrl = getRegistryUrl; 84 | exports.getRepositoryName = getRepositoryName; 85 | exports.ARTIFACTORY_PREFIX = ARTIFACTORY_PREFIX; 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bower-art-resolver", 3 | "version": "2.0.10", 4 | "description": "The Artifactory Bower Resolver", 5 | "main": "./lib/index.js", 6 | "keywords": ["bower-resolver"], 7 | "repository" : { 8 | "type" : "git", 9 | "url" : "https://github.com/JFrogDev/bower-art-resolver.git" 10 | }, 11 | "licenses": [{ 12 | "type": "Apache-2.0", 13 | "url": "http://www.apache.org/licenses/LICENSE-2.0" 14 | }], 15 | "dependencies": { 16 | "decompress-zip": "^0.2.0", 17 | "graceful-fs": "^4.1.2", 18 | "junk": "^1.0.2", 19 | "mout": "~0.9.0", 20 | "path": "~0.12.7", 21 | "q": "~1.0.1", 22 | "request": "~2.87.0", 23 | "request-progress": "^0.3.1", 24 | "retry": "^0.6.1", 25 | "semver": "^4.3.6", 26 | "tar-fs": "^1.7.0", 27 | "tmp": "0.0.26", 28 | "url": "~0.11.0" 29 | } 30 | } 31 | --------------------------------------------------------------------------------