├── .asf.yaml ├── .eslintrc.yml ├── .github ├── ISSUE_TEMPLATE.md ├── ISSUE_TEMPLATE │ ├── BUG_REPORT.md │ ├── FEATURE_REQUEST.md │ └── SUPPORT_QUESTION.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ ├── ci.yml │ └── release-audit.yml ├── .gitignore ├── .npmignore ├── .ratignore ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE ├── README.md ├── RELEASENOTES.md ├── index.js ├── licence_checker.yml ├── package-lock.json ├── package.json └── spec ├── fetch.spec.js ├── helpers.js ├── support ├── dummy-local-plugin │ ├── package.json │ └── plugin.xml ├── jasmine.json └── repo-name-neq-plugin-id.git │ ├── HEAD │ ├── config │ ├── objects │ ├── 72 │ │ └── d6e1a6f842add9a34f15ae0392c76f4d872e4e │ ├── 9a │ │ └── 0124596ebbf1f1702c6b41bd94efbb91c47e33 │ ├── a3 │ │ └── 7a357fbc796e8484c6f31fb24be5364a03076b │ └── db │ │ └── b7bd7983bed50ba311daeea7dee6e8461d5909 │ └── refs │ └── heads │ └── master └── testpkg.json /.asf.yaml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | github: 19 | description: Apache Cordova Fetch Library 20 | homepage: https://cordova.apache.org/ 21 | 22 | labels: 23 | - cordova 24 | - mobile 25 | - javascript 26 | - nodejs 27 | - hacktoberfest 28 | 29 | features: 30 | wiki: false 31 | issues: true 32 | projects: true 33 | 34 | enabled_merge_buttons: 35 | squash: true 36 | merge: false 37 | rebase: false 38 | 39 | notifications: 40 | commits: commits@cordova.apache.org 41 | issues: issues@cordova.apache.org 42 | pullrequests_status: issues@cordova.apache.org 43 | pullrequests_comment: issues@cordova.apache.org 44 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | root: true 19 | extends: '@cordova/eslint-config/node' 20 | 21 | overrides: 22 | - files: [spec/**/*.js] 23 | extends: '@cordova/eslint-config/node-tests' 24 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 5 | 6 | ### Issue Type 7 | 8 | 9 | - [ ] Bug Report 10 | - [ ] Feature Request 11 | - [ ] Support Question 12 | 13 | ## Description 14 | 15 | ## Information 16 | 17 | 18 | ### Command or Code 19 | 20 | 21 | ### Environment, Platform, Device 22 | 23 | 24 | 25 | 26 | ### Version information 27 | 34 | 35 | 36 | 37 | ## Checklist 38 | 39 | 40 | - [ ] I searched for already existing GitHub issues about this 41 | - [ ] I updated all Cordova tooling to their most recent version 42 | - [ ] I included all the necessary information above 43 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/BUG_REPORT.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🐛 Bug Report 3 | about: If something isn't working as expected. 4 | 5 | --- 6 | 7 | # Bug Report 8 | 9 | ## Problem 10 | 11 | ### What is expected to happen? 12 | 13 | 14 | 15 | ### What does actually happen? 16 | 17 | 18 | 19 | ## Information 20 | 21 | 22 | 23 | 24 | ### Command or Code 25 | 26 | 27 | 28 | 29 | ### Environment, Platform, Device 30 | 31 | 32 | 33 | 34 | ### Version information 35 | 42 | 43 | 44 | 45 | ## Checklist 46 | 47 | 48 | - [ ] I searched for existing GitHub issues 49 | - [ ] I updated all Cordova tooling to most recent version 50 | - [ ] I included all the necessary information above 51 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🚀 Feature Request 3 | about: A suggestion for a new functionality 4 | 5 | --- 6 | 7 | # Feature Request 8 | 9 | ## Motivation Behind Feature 10 | 11 | 12 | 13 | 14 | ## Feature Description 15 | 20 | 21 | 22 | 23 | ## Alternatives or Workarounds 24 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/SUPPORT_QUESTION.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 💬 Support Question 3 | about: If you have a question, please check out our Slack or StackOverflow! 4 | 5 | --- 6 | 7 | 8 | 9 | Apache Cordova uses GitHub Issues as a feature request and bug tracker _only_. 10 | For usage and support questions, please check out the resources below. Thanks! 11 | 12 | --- 13 | 14 | You can get answers to your usage and support questions about **Apache Cordova** on: 15 | 16 | * Slack Community Chat: https://cordova.slack.com (you can sign-up at http://slack.cordova.io/) 17 | * StackOverflow: https://stackoverflow.com/questions/tagged/cordova using the tag `cordova` 18 | 19 | --- 20 | 21 | If you are using a tool that uses Cordova internally, like e.g. Ionic, check their support channels: 22 | 23 | * **Ionic Framework** 24 | * [Ionic Community Forum](https://forum.ionicframework.com/) 25 | * [Ionic Worldwide Slack](https://ionicworldwide.herokuapp.com/) 26 | * **PhoneGap** 27 | * [PhoneGap Developer Community](https://forums.adobe.com/community/phonegap) 28 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 8 | 9 | ### Platforms affected 10 | 11 | 12 | 13 | ### Motivation and Context 14 | 15 | 16 | 17 | 18 | 19 | ### Description 20 | 21 | 22 | 23 | 24 | ### Testing 25 | 26 | 27 | 28 | 29 | ### Checklist 30 | 31 | - [ ] I've run the tests to see all new and existing tests pass 32 | - [ ] I added automated test coverage as appropriate for this change 33 | - [ ] Commit is prefixed with `(platform)` if this change only applies to one platform (e.g. `(android)`) 34 | - [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) 35 | - [ ] I've updated the documentation if necessary 36 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | name: Node CI 19 | 20 | on: 21 | push: 22 | branches-ignore: 23 | - 'dependabot/**' 24 | pull_request: 25 | branches: 26 | - '*' 27 | 28 | jobs: 29 | test: 30 | name: NodeJS ${{ matrix.node-version }} on ${{ matrix.os }} 31 | runs-on: ${{ matrix.os }} 32 | strategy: 33 | matrix: 34 | node-version: [16.x, 18.x, 20.x, 22.x] 35 | os: [ubuntu-latest, windows-latest, macos-latest] 36 | 37 | steps: 38 | - uses: actions/checkout@v4 39 | 40 | - name: Use Node.js ${{ matrix.node-version }} 41 | uses: actions/setup-node@v4 42 | with: 43 | node-version: ${{ matrix.node-version }} 44 | 45 | - name: Environment Information 46 | run: | 47 | node --version 48 | npm --version 49 | 50 | - uses: github/codeql-action/init@v3 51 | with: 52 | languages: javascript 53 | queries: security-and-quality 54 | config: | 55 | paths-ignore: 56 | - coverage 57 | - node_modules 58 | 59 | - name: npm install and test 60 | run: npm cit 61 | env: 62 | CI: true 63 | 64 | - uses: github/codeql-action/analyze@v3 65 | 66 | - uses: codecov/codecov-action@v4 67 | if: success() 68 | with: 69 | name: ${{ runner.os }} node.js ${{ matrix.node-version }} 70 | token: ${{ secrets.CORDOVA_CODECOV_TOKEN }} 71 | fail_ci_if_error: false 72 | -------------------------------------------------------------------------------- /.github/workflows/release-audit.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | name: Release Auditing 19 | 20 | on: 21 | push: 22 | branches-ignore: 23 | - 'dependabot/**' 24 | pull_request: 25 | branches: 26 | - '*' 27 | 28 | jobs: 29 | test: 30 | name: Audit Licenses 31 | runs-on: ubuntu-latest 32 | steps: 33 | # Checkout project 34 | - uses: actions/checkout@v4 35 | 36 | # Check license headers 37 | - uses: erisu/apache-rat-action@555ae80334a535eb6c1f8920b121563a5a985a75 38 | 39 | # Setup environment with node 40 | - uses: actions/setup-node@v4 41 | with: 42 | node-version: 20 43 | 44 | # Install node packages 45 | - name: npm install packages 46 | run: npm i 47 | 48 | # Check node package licenses 49 | - uses: erisu/license-checker-action@e929758f9416f30234ac454fc9054ca4b803871d 50 | with: 51 | license-config: 'licence_checker.yml' 52 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .nyc_output/ 3 | coverage/ 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .* 2 | spec 3 | coverage 4 | temp 5 | -------------------------------------------------------------------------------- /.ratignore: -------------------------------------------------------------------------------- 1 | \.(.*) 2 | jasmine.json 3 | testpkg.json 4 | (.*).git 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 21 | 22 | # Contributing to Apache Cordova 23 | 24 | Anyone can contribute to Cordova. And we need your contributions. 25 | 26 | There are multiple ways to contribute: report bugs, improve the docs, and 27 | contribute code. 28 | 29 | For instructions on this, start with the 30 | [contribution overview](http://cordova.apache.org/contribute/). 31 | 32 | The details are explained there, but the important items are: 33 | - Check for Github issues that corresponds to your contribution and link or create them if necessary. 34 | - Run the tests so your patch doesn't break existing functionality. 35 | 36 | We look forward to your contributions! 37 | 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2020 Apache Cordova 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Apache Cordova 2 | Copyright 2012-2020 The Apache Software Foundation 3 | 4 | This product includes software developed at 5 | The Apache Software Foundation (http://www.apache.org/). 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 21 | 22 | # cordova-fetch 23 | 24 | [![NPM](https://nodei.co/npm/cordova-fetch.png)](https://nodei.co/npm/cordova-fetch/) 25 | 26 | [![Node CI](https://github.com/apache/cordova-fetch/workflows/Node%20CI/badge.svg?branch=master)](https://github.com/apache/cordova-fetch/actions?query=branch%3Amaster) 27 | 28 | This package can be used to install and uninstall Node.js packages using npm. 29 | 30 | ## Usage 31 | 32 | ### `fetch` 33 | 34 | Installs a module from: 35 | 36 | * `npm` registry 37 | * `git` url 38 | * `tarball` 39 | * url 40 | * file on local file system 41 | * `folder` path on local system 42 | 43 | Returns a `Promise` resolving to the absolute path of the installed package. 44 | 45 | ```js 46 | const fetch = require('cordova-fetch'); 47 | 48 | fetch(spec, dest, opts).then(pathToInstalledPackage => { 49 | // Do something 50 | }); 51 | ``` 52 | 53 | #### Fetch Parameters 54 | 55 | Parameter | Description 56 | -|- 57 | `spec` | A spec for the package to be installed (anything supported by `npm install`) 58 | `dest` | Location where to install the package 59 | `opts` | Additional options (optional) 60 | 61 | ##### Fetch Options 62 | 63 | Option | Default | Description 64 | -|-|- 65 | `save` | `false` | Adds the package as dependency to `package.json` iff `true` 66 | 67 | ### `uninstall` 68 | 69 | Uninstalls a package from the given directory. Returns a `Promise` that resolves when removal has finished. 70 | 71 | ```js 72 | const { uninstall } = require('cordova-fetch'); 73 | 74 | uninstall(packageName, dest, opts).then(() => { 75 | // Do something 76 | }); 77 | ``` 78 | 79 | #### Uninstall Parameters 80 | 81 | Parameter | Description 82 | -|- 83 | `packageName` | Name of the package to be uninstalled 84 | `dest` | Location from where to uninstall the package 85 | `opts` | An Object with additional options 86 | 87 | ##### Uninstall Options 88 | 89 | Option | Default | Description 90 | -|-|- 91 | `save` | `false` | Removes dependency from `package.json` iff `true` 92 | -------------------------------------------------------------------------------- /RELEASENOTES.md: -------------------------------------------------------------------------------- 1 | 21 | # Cordova-fetch Release Notes 22 | 23 | ### 4.0.0 (Apr 06, 2023) 24 | 25 | * [GH-108](https://github.com/apache/cordova-fetch/pull/108) dep!: bumped `node` & `npm` engine requirment & dependencies w/ `package-lock` rebuild 26 | * Engine Requirement Update 27 | * Node: `>= 16.13.0` 28 | * npm: `>= 8.1.0` 29 | * Package Upgrade 30 | * `@npmcli/arborist@6.2.5` 31 | * `pacote@15.1.1` 32 | * `npm-package-arg@10.1.0` 33 | * `@cordova/eslint-config@5.0.0` 34 | * `fs-extra@11.1.1` 35 | * `cordova-common@5.0.0` 36 | * `jasmine@4.6.0` 37 | * [GH-109](https://github.com/apache/cordova-fetch/pull/109) dep!: bump `which@3.0.0` 38 | * [GH-102](https://github.com/apache/cordova-fetch/pull/102) dep(npm): update `pacote@15.0.7` 39 | * [GH-75](https://github.com/apache/cordova-fetch/pull/75) refactor: replace `superspawn` with `execa` 40 | * [GH-110](https://github.com/apache/cordova-fetch/pull/110) test: remove private internal method testing and `rewire` 41 | * [GH-111](https://github.com/apache/cordova-fetch/pull/111) ci: sync `codecov-action` setup between other repos 42 | 43 | ### 3.1.0 (Dec 06, 2022) 44 | 45 | * [GH-101](https://github.com/apache/cordova-fetch/pull/101) ci(workflow): update node 14 to use npm ^7.2 46 | * [GH-100](https://github.com/apache/cordova-fetch/pull/100) ci(workflow): update action dependencies & node versions 47 | * [GH-98](https://github.com/apache/cordova-fetch/pull/98) dep(npm): bump w/ latetst minor & patch releases 48 | * `cordova-common@^4.1.0` 49 | * `fs-extra@^9.1.0` 50 | * `npm-package-arg@^8.1.5` 51 | * `pacote@^11.3.5` 52 | * `resolve@^1.22.1` 53 | * `semver@^7.3.8` 54 | * `jasmine@^3.99.0` 55 | * `nyc@^15.1.0` 56 | * [GH-97](https://github.com/apache/cordova-fetch/pull/97) chore(npm): rebuilt `package-lock` to v2 57 | * [GH-94](https://github.com/apache/cordova-fetch/pull/94) ci(workflow): replace node 15 with node 16 (npm@7) 58 | * [GH-93](https://github.com/apache/cordova-fetch/pull/93) chore(npm): update `package-lock` 59 | 60 | ### 3.0.1 (Feb 02, 2021) 61 | 62 | * [GH-91](https://github.com/apache/cordova-fetch/pull/91) fix: cordova-fetch with npm@7 63 | * [GH-89](https://github.com/apache/cordova-fetch/pull/89) refactor: use async/await where applicable 64 | * [GH-88](https://github.com/apache/cordova-fetch/pull/88) fix: do not pack rejections from resolve in array 65 | * [GH-90](https://github.com/apache/cordova-fetch/pull/90) fix: use POSIX-style paths for require.resolve & Co 66 | * [GH-86](https://github.com/apache/cordova-fetch/pull/86) ci: add node 14 to workflow 67 | 68 | ### 3.0.0 (Apr 07, 2020) 69 | 70 | * Added NOTICE & LICENSE file for release 71 | * [GH-84](https://github.com/apache/cordova-fetch/pull/84) doc(README): fix markdown lint warning 72 | * [GH-82](https://github.com/apache/cordova-fetch/pull/82) refactor: modernize code & update `README` 73 | * refactor: transform `var` to `let` & `const` 74 | * refactor: consolidate `cordova-common` vars 75 | * refactor: transform arrow functions & arrow returns 76 | * refactor: transform template strings 77 | * doc(README): update & formatting 78 | * chore(npm): update package repo & bugs url 79 | * chore: apply suggestions 80 | * chore: revert promise chain flattening 81 | * [GH-83](https://github.com/apache/cordova-fetch/pull/83) breaking(npm): bump dependencies 82 | * `@cordova/eslint-config@^3.0.0` 83 | * `nyc@^15.0.0` 84 | * `rewire@^5.0.0` 85 | * `cordova-common@^3.2.1` 86 | * `fs-extra@^9.0.0` 87 | * `npm-package-arg@^8.0.1` 88 | * `pify@^5.0.0` 89 | * `resolve@^1.15.1` 90 | * `semver@^7.1.3` 91 | * `which@^2.0.2` 92 | * `cordova-common@4.0.0` 93 | * [GH-79](https://github.com/apache/cordova-fetch/pull/79) feat: update package & ci services 94 | * ci: replace current services with gh-actions 95 | * ci(gh-action): update workflow 96 | * chore(package-lock): rebuild 97 | * chore(npm): drop appveyor from ignore list 98 | * chore(npm-script): rename cover to test:coverage 99 | * [GH-81](https://github.com/apache/cordova-fetch/pull/81) chore: consolidate eslint configs 100 | * [GH-70](https://github.com/apache/cordova-fetch/pull/70) refactor: eslint setup 101 | * Save platforms and plugins to devDependencies 102 | * [GH-72](https://github.com/apache/cordova-fetch/pull/72) chore: update `file-url` dependency to `^3.0.0` 103 | * [GH-80](https://github.com/apache/cordova-fetch/pull/80) Drop code supporting `npm@<5` 104 | * [GH-74](https://github.com/apache/cordova-fetch/pull/74) chore: improve npm ignore list 105 | * [GH-73](https://github.com/apache/cordova-fetch/pull/73) chore: bump production dependencies 106 | * [GH-71](https://github.com/apache/cordova-fetch/pull/71) chore: update `jasmine` dependencies 107 | * [GH-69](https://github.com/apache/cordova-fetch/pull/69) chore: drop node 6 and 8 support 108 | * [GH-68](https://github.com/apache/cordova-fetch/pull/68) chore: bump version to 3.0.0-dev 109 | * [GH-78](https://github.com/apache/cordova-fetch/pull/78) Work around npm bug when uninstalling old cordova platforms 110 | * [GH-77](https://github.com/apache/cordova-fetch/pull/77) chore: update `nyc` dev dependency 111 | * [GH-63](https://github.com/apache/cordova-fetch/pull/63) Add Node.js 12 to CI Services 112 | 113 | ### 2.0.1 (Mar 18, 2019) 114 | 115 | * [GH-61](https://github.com/apache/cordova-fetch/pull/61) Prepare Fetch 2.0.1 Patch Release 116 | * Bumped Dependencies 117 | * `cordova-common@^3.1.0` 118 | * `fs-extra@^7.0.1` 119 | * `pify@^4.0.1` 120 | * `resolve@^1.10.0` 121 | * `semver@^5.6.0` 122 | * Bumped Dev Dependencies 123 | * `eslint@^5.15.2` 124 | * `eslint-plugin-import@^2.16.0` 125 | * `eslint-plugin-node@^8.0.1` 126 | * `eslint-plugin-promise@^4.0.1` 127 | * `jasmine@^3.3.1` 128 | * `nyc@^13.3.0` 129 | * `rewire@^4.0.1` 130 | * [GH-60](https://github.com/apache/cordova-fetch/pull/60) Properly detect npm install package name 131 | 132 | ### 2.0.0 (Dec 20, 2018) 133 | * dependency updates (cordova-common@3, etc.) 134 | * Collect test coverage during `npm test` (#48) 135 | * Drop Q, use native promises (#49) 136 | * Handle broken NODE_PATH setups gracefully (#50) 137 | * Run test in well-defined directory (#51) 138 | * Look for node_modules in any recursive parent directory (#44) 139 | * Add ESLint config file for tests (#41) 140 | * Improve and update docs (#42) 141 | * GH-35 .ratignore ignore *.git 142 | * GH-35 add license header to dummy-local-plugin 143 | * dos2unix spec/support/dummy-local-plugin/plugin.xml 144 | * eslint devDep updates (#28) 145 | * [CB-14173](https://issues.apache.org/jira/browse/CB-14173) Fix cordova add --link (#26) 146 | * [CB-14133](https://issues.apache.org/jira/browse/CB-14133) Avoid fetching already installed packages 147 | * Speed up tests by about 250% 148 | * Simplify fetch unit tests 149 | * Move and rename some e2e tests 150 | * Refactor fetch e2e tests 151 | * Refactor fetch code 152 | * Use fs.readJsonSync for reading `package.json` to avoid caching 153 | * Replace shelljs with fs-extra and which 154 | * Simplify tempDir setup in tests 155 | * Don't manually delete node_modules contents 156 | * Simplify installation location retrieval (#18) 157 | * Handle missing options (#22) 158 | * Add workaround for failing tests on AppVeyor/Node10 159 | * [CB-14066](https://issues.apache.org/jira/browse/CB-14066) Drop support for Node 4 160 | * Fix repo url in `package.json` (#20) 161 | * Increase test timeouts to reduce spurious failures (#19) 162 | * Isolate Q usage to prepare later removal 163 | * Fail test if promise unexpectedly resolves 164 | * Let jasmine handle promises 165 | * Fix linting errors 166 | * Lint all *.js files in the repo 167 | * [CB-13503](https://issues.apache.org/jira/browse/CB-13503) fix trimID bug when using file:path/to/plugin 168 | * Adding a unit test 169 | * Enabling support for git+http 170 | 171 | ### 1.3.0 (Dec 14, 2017) 172 | * [CB-13055](https://issues.apache.org/jira/browse/CB-13055): added workaround for when `jsonDiff` has more than one different key. 173 | * Support git shortlink package references 174 | 175 | ### 1.2.1 (Oct 27, 2017) 176 | * [CB-13504](https://issues.apache.org/jira/browse/CB-13504) updating `package.json` versions for cordova-fetch 1.2.1 release 177 | * [CB-13501](https://issues.apache.org/jira/browse/CB-13501) : added support for node 8 178 | * [CB-13492](https://issues.apache.org/jira/browse/CB-13492) : updating opts.save and including a tests for no-save 179 | * [CB-13380](https://issues.apache.org/jira/browse/CB-13380) Incremented package version to -dev 180 | 181 | ### 1.2.0 (Oct 04, 2017) 182 | * [CB-13353](https://issues.apache.org/jira/browse/CB-13353) added `saveexact` as an option and updated fetch test 183 | * [CB-13308](https://issues.apache.org/jira/browse/CB-13308), [CB-13252](https://issues.apache.org/jira/browse/CB-13252) fix issue with plugins turning into symlinks on restore 184 | * [CB-13303](https://issues.apache.org/jira/browse/CB-13303) setting production flag to true by default during npm install 185 | * [CB-12895](https://issues.apache.org/jira/browse/CB-12895) setting up `eslint` and removing `jshint` 186 | * [CB-13010](https://issues.apache.org/jira/browse/CB-13010) Improve logic for searching packages which being installed from `git url` 187 | * [CB-11980](https://issues.apache.org/jira/browse/CB-11980) fixed incorrect `appveyor` image 188 | * [CB-12786](https://issues.apache.org/jira/browse/CB-12786) Improve logic for searching plugin id in case of module already exists in `node_modules` 189 | * [CB-12762](https://issues.apache.org/jira/browse/CB-12762) updated `packageJson` to github mirrors 190 | * [CB-12787](https://issues.apache.org/jira/browse/CB-12787) Fix plugin installation with `--link` option 191 | * [CB-12738](https://issues.apache.org/jira/browse/CB-12738) Cordova ignores plugin dependency version on **windows** platform 192 | 193 | ### 1.1.0 (May 02, 2017) 194 | * [CB-12665](https://issues.apache.org/jira/browse/CB-12665): removed `enginestrict` since it is deprecated 195 | * added support for dealing with local path targets 196 | 197 | ### 1.0.2 (Jan 17, 2017) 198 | * [CB-12358](https://issues.apache.org/jira/browse/cb-12358) updated cordova-common dep for cordova-fetch to 2.0.0 199 | 200 | ### 1.0.0 (May 12, 2016) 201 | * [CB-9858](https://issues.apache.org/jira/browse/CB-9858) Added jasmine tests 202 | * [CB-9858](https://issues.apache.org/jira/browse/CB-9858) Added `npm uninstall` method to cordova-fetch 203 | * [CB-9858](https://issues.apache.org/jira/browse/CB-9858) Initial implementation of `cordova-fetch` module 204 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | 'License'); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on an 12 | 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 13 | KIND, either express or implied. See the License for the 14 | specific language governing permissions and limitations 15 | under the License. 16 | */ 17 | 18 | const execa = require('execa'); 19 | const which = require('which'); 20 | const path = require('node:path'); 21 | const fs = require('node:fs/promises'); 22 | const { CordovaError, events } = require('cordova-common'); 23 | const npa = require('npm-package-arg'); 24 | const pacote = require('pacote'); 25 | const semver = require('semver'); 26 | 27 | function resolvePackage (...args) { 28 | return new Promise((resolve, reject) => { 29 | require('resolve')(...args, (err, ...result) => { 30 | if (err) { 31 | reject(err); 32 | } else { 33 | resolve(result); 34 | } 35 | }); 36 | }); 37 | } 38 | 39 | /** 40 | * Installs a module from npm, a git url or the local file system. 41 | * 42 | * @param {String} target A spec for the package to be installed 43 | * (anything supported by `npm install`) 44 | * @param {String} dest Location where to install the package 45 | * @param {Object} [opts={}] Additional options 46 | * 47 | * @return {Promise} Absolute path to the installed package 48 | */ 49 | module.exports = async function (target, dest, opts = {}) { 50 | try { 51 | if (!dest || !target) { 52 | throw new CordovaError('Need to supply a target and destination'); 53 | } 54 | 55 | // Create dest if it doesn't exist yet 56 | await fs.mkdir(dest, { recursive: true }); 57 | 58 | // First try to determine the name from the spec using npa. This is very cheap. 59 | let { name, rawSpec } = npa(target, dest); 60 | 61 | if (!name) { 62 | // If that fails, get out the big guns and fetch a full manifest using pacote. 63 | ({ name } = await pacote.manifest(target, { 64 | where: dest, 65 | Arborist: require('@npmcli/arborist') 66 | })); 67 | } else if (semver.validRange(rawSpec)) { 68 | // If the provided spec is a name and a version range, we look for 69 | // an installed package that satisfies the requested version range 70 | try { 71 | const [pkgPath, { version }] = await resolvePathToPackage(name, dest); 72 | if (semver.satisfies(version, rawSpec)) return pkgPath; 73 | } catch (err) { 74 | // Ignore MODULE_NOT_FOUND errors from resolvePathToPackage 75 | if (err.code !== 'MODULE_NOT_FOUND') throw err; 76 | } 77 | } 78 | 79 | await installPackage(target, dest, opts); 80 | 81 | return (await resolvePathToPackage(name, dest))[0]; 82 | } catch (err) { 83 | throw new CordovaError(err); 84 | } 85 | }; 86 | 87 | // Installs the package specified by target and returns the installation path 88 | async function installPackage (target, dest, opts) { 89 | await isNpmInstalled(); 90 | 91 | // Ensure that `npm` installs to `dest` and not any of its ancestors 92 | await fs.mkdir(path.join(dest, 'node_modules'), { recursive: true }); 93 | 94 | // Run `npm` to install requested package 95 | const args = npmArgs(target, opts); 96 | events.emit('verbose', `fetch: Installing ${target} to ${dest}`); 97 | return execa('npm', args, { cwd: dest }); 98 | } 99 | 100 | function npmArgs (target, opts) { 101 | const args = ['install', target]; 102 | opts = opts || {}; 103 | 104 | if (opts.save_exact) { 105 | args.push('--save-exact'); 106 | } else if (opts.save) { 107 | args.push('--save-dev'); 108 | } else { 109 | args.push('--no-save'); 110 | } 111 | return args; 112 | } 113 | 114 | // Resolves to installation path and package.json of package `name` starting 115 | // from `basedir` 116 | async function resolvePathToPackage (name, basedir) { 117 | const paths = (process.env.NODE_PATH || '') 118 | .split(path.delimiter) 119 | .filter(p => p); 120 | 121 | // We resolve the path to the module's package.json to avoid getting the 122 | // path to `main` which could be located anywhere in the package 123 | const [pkgJsonPath, pkgJson] = await resolvePackage(`${name}/package.json`, { paths, basedir }); 124 | 125 | return [path.dirname(pkgJsonPath), pkgJson]; 126 | } 127 | 128 | /** 129 | * Checks to see if npm is installed on the users system 130 | * 131 | * @return {Promise} Absolute path to npm. 132 | */ 133 | async function isNpmInstalled () { 134 | try { 135 | return await which('npm'); 136 | } catch (e) { 137 | throw new CordovaError('"npm" command line tool is not installed: make sure it is accessible on your PATH.'); 138 | } 139 | } 140 | 141 | module.exports.isNpmInstalled = isNpmInstalled; 142 | 143 | /** 144 | * Uninstalls the package `target` from `dest` using given options. 145 | * 146 | * @param {String} target Name of the package to be uninstalled 147 | * @param {String} dest Location from where to uninstall the package 148 | * @param {Object} [opts={}] Additional options 149 | * 150 | * @return {Promise} Resolves when removal has finished 151 | */ 152 | module.exports.uninstall = async (target, dest, opts) => { 153 | const fetchArgs = ['uninstall']; 154 | opts = opts || {}; 155 | 156 | try { 157 | // check if npm is installed on the system 158 | await isNpmInstalled(); 159 | 160 | if (dest && target) { 161 | // add target to fetchArgs Array 162 | fetchArgs.push(target); 163 | } else throw new CordovaError('Need to supply a target and destination'); 164 | 165 | // set the directory where npm uninstall will be run 166 | opts.cwd = dest; 167 | 168 | // if user added --save flag, pass --save-dev flag to npm uninstall command 169 | if (opts.save) { 170 | fetchArgs.push('--save-dev'); 171 | } else { 172 | fetchArgs.push('--no-save'); 173 | } 174 | 175 | // run npm uninstall, this will remove dependency 176 | // from package.json if --save was used. 177 | return execa('npm', fetchArgs, opts); 178 | } catch (err) { 179 | throw new CordovaError(err); 180 | } 181 | }; 182 | -------------------------------------------------------------------------------- /licence_checker.yml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | # Compiled list of allowed 3RD PARTY LICENSES from: 19 | # 20 | # ASF CATEGORY A: WHAT CAN WE INCLUDE IN AN ASF PROJECT 21 | # https://www.apache.org/legal/resolved.html#category-a 22 | # 23 | # Licenses converted into the SPDX standardized short identifier format. 24 | # https://spdx.org/licenses/ 25 | allowed-licenses: 26 | - 0BSD 27 | - AFL-3.0 28 | - Apache-1.1 29 | - Apache-2.0 30 | - APAFML 31 | - BlueOak-1.0.0 32 | - BSD-2-Clause 33 | - BSD-3-Clause 34 | - BSD-3-Clause-LBNL 35 | - BSL-1.0 36 | - CC-PDDC 37 | - CC0-1.0 38 | - EPICS 39 | - HPND 40 | - ICU 41 | - ISC 42 | - MIT 43 | - MIT-0 44 | - MS-PL 45 | - MulanPSL-2.0 46 | - NCSA 47 | - OGL-UK-3.0 48 | - PHP-3.01 49 | - PostgreSQL 50 | - PSF-2.0 51 | - SMLNJ 52 | - Unicode-DFS-2016 53 | - Unlicense 54 | - UPL-1.0 55 | - W3C 56 | - WTFPL 57 | - X11 58 | - Xnet 59 | - Zlib 60 | - ZPL-2.0 61 | - Python-2.0 62 | 63 | ignored-packages: 64 | - spdx-exceptions@2.5.0 65 | 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-fetch", 3 | "version": "4.0.1-dev", 4 | "description": "Apache Cordova fetch module. Fetches from git and npm.", 5 | "main": "index.js", 6 | "repository": "github:apache/cordova-fetch", 7 | "bugs": "https://github.com/apache/cordova-fetch/issues", 8 | "keywords": [ 9 | "cordova", 10 | "fetch", 11 | "apache", 12 | "ecosystem:cordova", 13 | "cordova:tool" 14 | ], 15 | "author": "Apache Software Foundation", 16 | "license": "Apache-2.0", 17 | "dependencies": { 18 | "@npmcli/arborist": "^7.4.0", 19 | "cordova-common": "^5.0.0", 20 | "execa": "^5.1.1", 21 | "npm-package-arg": "^11.0.1", 22 | "pacote": "^17.0.6", 23 | "resolve": "^1.22.1", 24 | "semver": "^7.3.8", 25 | "which": "^4.0.0" 26 | }, 27 | "devDependencies": { 28 | "@cordova/eslint-config": "^5.0.0", 29 | "file-url": "^3.0.0", 30 | "jasmine": "^4.6.0", 31 | "nyc": "^15.1.0" 32 | }, 33 | "scripts": { 34 | "test": "npm run lint && npm run test:coverage", 35 | "test:coverage": "nyc jasmine", 36 | "lint": "eslint --ignore-path .gitignore ." 37 | }, 38 | "engines": { 39 | "node": ">=16.13.0", 40 | "npm": ">=8.1.0" 41 | }, 42 | "nyc": { 43 | "include": [ 44 | "index.js" 45 | ], 46 | "reporter": [ 47 | "lcov", 48 | "text" 49 | ] 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spec/fetch.spec.js: -------------------------------------------------------------------------------- 1 | /** 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on an 12 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 13 | KIND, either express or implied. See the License for the 14 | specific language governing permissions and limitations 15 | under the License. 16 | */ 17 | 18 | const fetch = require('..'); 19 | const uninstall = fetch.uninstall; 20 | 21 | const path = require('node:path'); 22 | const fs = require('node:fs'); 23 | const fileUrl = require('file-url'); 24 | const helpers = require('./helpers.js'); 25 | 26 | let tmpDir, opts; 27 | 28 | beforeEach(() => { 29 | opts = {}; 30 | tmpDir = helpers.tmpDir(); 31 | process.chdir(tmpDir); 32 | }); 33 | 34 | afterEach(() => { 35 | process.chdir(__dirname); // Needed to rm the dir on Windows. 36 | fs.rmSync(tmpDir, { recursive: true, force: true }); 37 | }); 38 | 39 | function fetchAndMatch (target, pkgProps = { name: target }) { 40 | return fetch(target, tmpDir, opts) 41 | .then(expectPackageToMatch(pkgProps)); 42 | } 43 | 44 | function expectPackageToMatch (pkgProps) { 45 | return result => { 46 | expect(result).toBeDefined(); 47 | expect(fs.existsSync(result)).toBe(true); 48 | const pkg = JSON.parse(fs.readFileSync(path.join(result, 'package.json'), 'utf8')); 49 | expect(pkg).toEqual(jasmine.objectContaining(pkgProps)); 50 | return result; 51 | }; 52 | } 53 | 54 | function expectNotToBeInstalled (pkgName) { 55 | expect(fs.existsSync(path.join(tmpDir, 'node_modules', pkgName))).toBe(false); 56 | } 57 | 58 | function expectDevDependenciesToBe (deps) { 59 | const rootPJ = JSON.parse(fs.readFileSync(path.join(tmpDir, 'package.json'), 'utf8')); 60 | expect(rootPJ.devDependencies || {}).toEqual(deps); 61 | } 62 | 63 | describe('fetch/uninstall tests via npm & git', () => { 64 | it('should fetch and uninstall a cordova platform via npm & git', () => { 65 | return Promise.resolve() 66 | .then(_ => fetchAndMatch('cordova-android')) 67 | .then(_ => uninstall('cordova-android', tmpDir, opts)) 68 | .then(_ => expectNotToBeInstalled('cordova-android')) 69 | 70 | .then(_ => fetchAndMatch('https://github.com/apache/cordova-browser.git', { name: 'cordova-browser' })) 71 | .then(_ => uninstall('cordova-browser', tmpDir, opts)) 72 | .then(_ => expectNotToBeInstalled('cordova-browser')); 73 | }, 60000); 74 | 75 | it('should fetch a scoped plugin from npm', () => { 76 | return fetchAndMatch('@stevegill/cordova-plugin-device'); 77 | }, 30000); 78 | }); 79 | 80 | describe('fetch/uninstall with --save', () => { 81 | beforeEach(() => { 82 | opts = { save: true }; 83 | // copy package.json from spec directory to tmpDir 84 | fs.cpSync(path.join(__dirname, 'testpkg.json'), 'package.json'); 85 | }); 86 | 87 | it('should fetch and uninstall a cordova platform via npm & git tags/branches', () => { 88 | return Promise.resolve() 89 | // npm tag 90 | .then(_ => fetchAndMatch('cordova-android@8.1.0', { 91 | name: 'cordova-android', 92 | version: '8.1.0' 93 | })) 94 | .then(_ => expectDevDependenciesToBe({ 'cordova-android': '^8.1.0' })) 95 | .then(_ => uninstall('cordova-android', tmpDir, opts)) 96 | .then(_ => expectDevDependenciesToBe({})) 97 | .then(_ => expectNotToBeInstalled('cordova-android')) 98 | 99 | // git tag 100 | .then(_ => fetchAndMatch('https://github.com/apache/cordova-ios.git#rel/5.0.1', { 101 | name: 'cordova-ios', 102 | version: '5.0.1' 103 | })) 104 | .then(_ => expectDevDependenciesToBe({ 'cordova-ios': jasmine.stringMatching('#rel/5.0.1') })) 105 | .then(_ => uninstall('cordova-ios', tmpDir, opts)) 106 | .then(_ => expectDevDependenciesToBe({})) 107 | .then(_ => expectNotToBeInstalled('cordova-ios')) 108 | 109 | // git branch 110 | .then(_ => fetchAndMatch('https://github.com/apache/cordova-android.git#4.1.x', { 111 | name: 'cordova-android', 112 | version: '4.1.1' 113 | })) 114 | .then(_ => expectDevDependenciesToBe({ 'cordova-android': jasmine.stringMatching('#4.1.x') })) 115 | .then(_ => uninstall('cordova-android', tmpDir, opts)); 116 | }, 150000); 117 | 118 | it('should fetch and uninstall a cordova plugin via git commit sha', () => { 119 | const SHA = '7db612115755c2be73a98dda76ff4c5fd9d8a575'; 120 | const URL = `https://github.com/apache/cordova-plugin-contacts.git#${SHA}`; 121 | return Promise.resolve() 122 | .then(_ => fetchAndMatch(URL, { 123 | name: 'cordova-plugin-contacts', 124 | version: '2.0.2-dev' 125 | })) 126 | .then(_ => expectDevDependenciesToBe({ 'cordova-plugin-contacts': jasmine.stringMatching(`#${SHA}`) })) 127 | .then(_ => uninstall('cordova-plugin-contacts', tmpDir, opts)) 128 | .then(_ => expectDevDependenciesToBe({})) 129 | .then(_ => expectNotToBeInstalled('cordova-plugin-contacts')); 130 | }, 30000); 131 | }); 132 | 133 | describe('fetching already installed packages', () => { 134 | beforeEach(() => { 135 | fs.cpSync(path.join(__dirname, 'support'), 'support', { recursive: true }); 136 | }); 137 | 138 | it('should return package path for registry packages', () => { 139 | return Promise.resolve() 140 | .then(_ => fetchAndMatch('cordova-plugin-device')) 141 | .then(_ => fetchAndMatch('cordova-plugin-device')); 142 | }, 40000); 143 | 144 | it('should return package path if git repo name differs from plugin id', () => { 145 | const TARGET = `git+${fileUrl(path.resolve(__dirname, 'support/repo-name-neq-plugin-id.git'))}`; 146 | return Promise.resolve() 147 | .then(_ => fetchAndMatch(TARGET, { name: 'test-plugin' })) 148 | .then(_ => fetchAndMatch(TARGET, { name: 'test-plugin' })); 149 | }, 40000); 150 | 151 | it('should return package path if using a relative path', () => { 152 | const TARGET = 'file:support/dummy-local-plugin'; 153 | return Promise.resolve() 154 | .then(_ => fetchAndMatch(TARGET, { name: 'test-plugin' })) 155 | .then(_ => fetchAndMatch(TARGET, { name: 'test-plugin' })); 156 | }, 60000); 157 | 158 | it('should return package path for git+http variants', () => { 159 | return Promise.resolve() 160 | .then(_ => fetchAndMatch('github:apache/cordova-plugin-device', { name: 'cordova-plugin-device' })) 161 | .then(_ => fetchAndMatch('https://github.com/apache/cordova-plugin-device', { name: 'cordova-plugin-device' })) 162 | .then(_ => fetchAndMatch('git+https://github.com/apache/cordova-plugin-device', { name: 'cordova-plugin-device' })); 163 | }, 60000); 164 | }); 165 | 166 | describe('negative tests', () => { 167 | it('should fail fetching a module that does not exist on npm', () => { 168 | return expectAsync( 169 | fetch('NOTAMODULE', tmpDir, opts) 170 | ).toBeRejectedWithError(); 171 | }, 30000); 172 | 173 | it('should fail fetching a giturl which contains a subdirectory', () => { 174 | return expectAsync( 175 | fetch('https://github.com/apache/cordova-plugins.git#:keyboard', tmpDir, opts) 176 | ).toBeRejectedWithError(); 177 | }, 30000); 178 | }); 179 | 180 | describe('fetching with node_modules in ancestor dirs', () => { 181 | let fetchTarget, fetchDestination; 182 | 183 | beforeEach(() => { 184 | const testRoot = path.join(tmpDir, 'test-root'); 185 | fetchDestination = path.join(testRoot, 'fetch-dest'); 186 | fs.mkdirSync(fetchDestination, { recursive: true }); 187 | 188 | // Make test root look like a package root directory 189 | fs.mkdirSync(path.join(testRoot, 'node_modules'), { recursive: true }); 190 | fs.writeFileSync(path.join(testRoot, 'package.json'), JSON.stringify({ private: true }, null, 2), 'utf8'); 191 | 192 | // Copy test fixtures to avoid linking out of temp directory 193 | fs.cpSync(path.join(__dirname, 'support'), 'support', { recursive: true }); 194 | fetchTarget = fileUrl(path.resolve('support/dummy-local-plugin')); 195 | }); 196 | 197 | it('should still install to given destination', () => { 198 | const expectedInstallPath = path.join(fetchDestination, 'node_modules/test-plugin'); 199 | 200 | return fetch(fetchTarget, fetchDestination).then(pkgInstallPath => { 201 | expect(pkgInstallPath).toBe(expectedInstallPath); 202 | }); 203 | }, 10 * 1000); 204 | }); 205 | -------------------------------------------------------------------------------- /spec/helpers.js: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | const path = require('node:path'); 21 | const fs = require('node:fs'); 22 | const os = require('node:os'); 23 | 24 | const tmpDirTemplate = path.join(os.tmpdir(), 'cordova-fetch-tests-'); 25 | module.exports.tmpDir = () => fs.mkdtempSync(tmpDirTemplate); 26 | -------------------------------------------------------------------------------- /spec/support/dummy-local-plugin/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test-plugin", 3 | "version": "1.0.0", 4 | "description": "This plugin allows you to stream audio and video in a fullscreen, native player on iOS and Android.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /spec/support/dummy-local-plugin/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 26 | 27 | test plugin 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /spec/support/jasmine.json: -------------------------------------------------------------------------------- 1 | { 2 | "spec_dir": "spec", 3 | "spec_files": [ 4 | "**/*[sS]pec.js" 5 | ], 6 | "helpers": [ 7 | "helpers/**/*.js" 8 | ], 9 | "stopSpecOnExpectationFailure": false, 10 | "random": false 11 | } 12 | -------------------------------------------------------------------------------- /spec/support/repo-name-neq-plugin-id.git/HEAD: -------------------------------------------------------------------------------- 1 | ref: refs/heads/master 2 | -------------------------------------------------------------------------------- /spec/support/repo-name-neq-plugin-id.git/config: -------------------------------------------------------------------------------- 1 | [core] 2 | repositoryformatversion = 0 3 | filemode = true 4 | bare = true 5 | -------------------------------------------------------------------------------- /spec/support/repo-name-neq-plugin-id.git/objects/72/d6e1a6f842add9a34f15ae0392c76f4d872e4e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apache/cordova-fetch/12ee14a4eb915981b986ff86322974af1236c465/spec/support/repo-name-neq-plugin-id.git/objects/72/d6e1a6f842add9a34f15ae0392c76f4d872e4e -------------------------------------------------------------------------------- /spec/support/repo-name-neq-plugin-id.git/objects/9a/0124596ebbf1f1702c6b41bd94efbb91c47e33: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apache/cordova-fetch/12ee14a4eb915981b986ff86322974af1236c465/spec/support/repo-name-neq-plugin-id.git/objects/9a/0124596ebbf1f1702c6b41bd94efbb91c47e33 -------------------------------------------------------------------------------- /spec/support/repo-name-neq-plugin-id.git/objects/a3/7a357fbc796e8484c6f31fb24be5364a03076b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apache/cordova-fetch/12ee14a4eb915981b986ff86322974af1236c465/spec/support/repo-name-neq-plugin-id.git/objects/a3/7a357fbc796e8484c6f31fb24be5364a03076b -------------------------------------------------------------------------------- /spec/support/repo-name-neq-plugin-id.git/objects/db/b7bd7983bed50ba311daeea7dee6e8461d5909: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apache/cordova-fetch/12ee14a4eb915981b986ff86322974af1236c465/spec/support/repo-name-neq-plugin-id.git/objects/db/b7bd7983bed50ba311daeea7dee6e8461d5909 -------------------------------------------------------------------------------- /spec/support/repo-name-neq-plugin-id.git/refs/heads/master: -------------------------------------------------------------------------------- 1 | 72d6e1a6f842add9a34f15ae0392c76f4d872e4e 2 | -------------------------------------------------------------------------------- /spec/testpkg.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "fetch.spec.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC" 11 | } 12 | --------------------------------------------------------------------------------