如何缓存cordova';s平台项目在运行时防止远程下载;cordova平台添加<;平台>&引用;?

如何缓存cordova';s平台项目在运行时防止远程下载;cordova平台添加<;平台>&引用;?,cordova,Cordova,每次运行Cordova platform add时,Cordova都会从internet获取指定平台的Cordova文件。是否可以将这些文件下载到本地目录并强制cordova使用它们而不是远程文件?也许有一个神奇的环境变量 您可以从github本地克隆平台,并从路径添加,而不是从npm/github添加,但您不能使用 cordova platform add <platform> 我知道这超出了我的问题范围,但我仍然认为它值得发布,因为它可能对其他人有用 下面的说明解释了如何设置本

每次运行Cordova platform add时,Cordova都会从internet获取指定平台的Cordova文件。是否可以将这些文件下载到本地目录并强制cordova使用它们而不是远程文件?也许有一个神奇的环境变量

您可以从github本地克隆平台,并从路径添加,而不是从npm/github添加,但您不能使用

cordova platform add <platform>

我知道这超出了我的问题范围,但我仍然认为它值得发布,因为它可能对其他人有用

下面的说明解释了如何设置本地/脱机cordova环境。这将节省您的时间,特别是如果您和我一样,有许多应用程序要构建并部署到不同的平台。作为奖励,我还解释了如何设置本地gradle,这对于android来说是一项需要更多时间的任务

mkdir local_cordova && cd local_cordova && npm init
# confirm all stuff 
npm install cordova-fetch 
touch download-cordova-ios.js 
touch download-cordova-android.js 
open download-cordova-ios.js
# paste the lines below:   

var fetch = require('cordova-fetch'); 
var spec = 'cordova-ios@~4.5.1'; 
/* Version can be ommited */  
var dest = './bin/ios' 
var opts = { save: true } fetch(spec, dest, opts);

# save the file open download-cordova-android.js
# paste the lines below

# Now open the android file and do the same
open download-cordova-android.js


var fetch = require('cordova-fetch'); 
var spec = 'cordova-android@~6.3.0';
/* Version can be ommited */  
var dest = './bin/android' 
var opts = { save: true } 
fetch(spec, dest, opts);


# Now run the node command and wait for the script to complete
node download-cordova-android.js && node download-cordova-ios.js

# Now lets create some environment variables for each our local cordova platforms.
vim ~/.bash_profile
# add the following lines 
export CORDOVA_DROID="/path/to/your/cordova/cordova-fetch/bin/android/node_modules/cordova-android"; 

export CORDOVA_IOS="/path/to/your/cordova/cordova-fetch/bin/ios/node_modules/cordova-ios";

source ~/.bash_profile 

cd ~/Desktop/ 
cordova create test-app com.test.app test-app && cd test-app 
cordova platform add $CORDOVA_DROID 
cordova platform add $CORDOVA_IOS

cordova build android
#In a normal situation cordova will download a gradle which is 60+MB. Since this operation can take a while I suggest you to setup an environment variable that will save you lots of time.

# Go to https://services.gradle.org/distributions/
# and find the distribution that fits your needs
# mine was gradle-4.0.2-all.zip
# download it and place it somewhere in your disk. Consider a location that can be persisted over time.


vim ~/.bash_profile 
export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=file:///path/to/your/gradle-4.0-all.zip

# That's all

不,不可能。如果再多一些变体
cordova平台添加cordova android@{version}
cordova平台添加cordova ios@{version}
将从互联网上下载,则可以使用本地副本。要从本地副本添加旧版本,如果是github克隆,只需签出所需版本的分支即可。您是对的。我只是决定把它放在这里,以防有人发现这个答案。至少那个人现在会支持更多的命令变体,其中一个是指向每个cordova平台的特定远程版本。顺便说一下,我不知道是不是我,但每次我使用本地cordova平台设置平台时,
cordova\u插件
目录都不会在
/platforms>上创建/{the_platform}/platform_www
会破坏应用程序,因为插件在运行时将不可用。我没有注意到这一点,但看起来像是一个bug。我只在对本地平台进行更改时使用它们,所以通常不添加插件
mkdir local_cordova && cd local_cordova && npm init
# confirm all stuff 
npm install cordova-fetch 
touch download-cordova-ios.js 
touch download-cordova-android.js 
open download-cordova-ios.js
# paste the lines below:   

var fetch = require('cordova-fetch'); 
var spec = 'cordova-ios@~4.5.1'; 
/* Version can be ommited */  
var dest = './bin/ios' 
var opts = { save: true } fetch(spec, dest, opts);

# save the file open download-cordova-android.js
# paste the lines below

# Now open the android file and do the same
open download-cordova-android.js


var fetch = require('cordova-fetch'); 
var spec = 'cordova-android@~6.3.0';
/* Version can be ommited */  
var dest = './bin/android' 
var opts = { save: true } 
fetch(spec, dest, opts);


# Now run the node command and wait for the script to complete
node download-cordova-android.js && node download-cordova-ios.js

# Now lets create some environment variables for each our local cordova platforms.
vim ~/.bash_profile
# add the following lines 
export CORDOVA_DROID="/path/to/your/cordova/cordova-fetch/bin/android/node_modules/cordova-android"; 

export CORDOVA_IOS="/path/to/your/cordova/cordova-fetch/bin/ios/node_modules/cordova-ios";

source ~/.bash_profile 

cd ~/Desktop/ 
cordova create test-app com.test.app test-app && cd test-app 
cordova platform add $CORDOVA_DROID 
cordova platform add $CORDOVA_IOS

cordova build android
#In a normal situation cordova will download a gradle which is 60+MB. Since this operation can take a while I suggest you to setup an environment variable that will save you lots of time.

# Go to https://services.gradle.org/distributions/
# and find the distribution that fits your needs
# mine was gradle-4.0.2-all.zip
# download it and place it somewhere in your disk. Consider a location that can be persisted over time.


vim ~/.bash_profile 
export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=file:///path/to/your/gradle-4.0-all.zip

# That's all