我不知道這是否是最好的方式,但我所做的就是:
創造scripts/setup.js
一個腳本文件,該文件執行以下代碼。
#!/usr/bin/env node
//This script will add or remove all plugins listed in package.json
//usage: node platforms.js [add | remove]
var command = process.argv[2] || 'add';
var packageJson = require('../package.json');
var fs = require('fs');
var path = require('path');
var sys = require('sys')
var exec = require('child_process').exec;
packageJson.cordovaPlatforms.forEach(function(platform) {
var platformCmd = 'cordova platform ' + command + ' ' + platform;
exec(platformCmd);
});
//Set up Plugins.
var command = process.argv[2] || 'add';
var packageJson = require('../package.json');
var fs = require('fs');
var path = require('path');
var sys = require('sys')
var exec = require('child_process').exec;
function createAddRemoveStatement(plugin) {
var pluginCmd = 'cordova plugin ' + command + ' ';
if(typeof plugin === 'string') {
pluginCmd += plugin;
} else {
if(command === 'add') {
pluginCmd += plugin.locator + ' ';
if(plugin.variables) {
Object.keys(plugin.variables).forEach(function(variable){
pluginCmd += '--variable ' + variable + '="' + plugin.variables[variable] + '" ';
});
}
} else {
pluginCmd += plugin.id;
}
}
return pluginCmd;
}
function processPlugin(index) {
if(index >= packageJson.cordovaPlugins.length)
return;
var plugin = packageJson.cordovaPlugins[index];
var pluginCommand = createAddRemoveStatement(plugin);
console.log(pluginCommand);
exec(pluginCommand, function(){
processPlugin(index + 1);
});
}
processPlugin(0);
該文件隨後將檢查哪些東西需要添加,然後添加他們cordovaPlatforms部分和cordovaPlugins部分。
然後我走進packages.json並補充說:
"scripts": {
"postinstall":"node scripts/setup.js"
}
,然後故宮後安裝完畢後,它會再運行設置腳本這將增加所有相應所需的平臺和插件。
所以,這是否意味着我應該在我的package.json文件中添加''腳本「:{」postinstall「:」ionic state reset「},所以在我運行'npm install'後,它會自動運行狀態重置? – Fallenreaper
如果您試圖僅使用'npm install'自動運行命令,那麼是的,您應該可以添加'「腳本」:{「postinstall」:「ionic state reset」}來實現這一點。 –