简体   繁体   English

如何为我的ionic框架的npm安装添加基于git repo的插件?

[英]How do i add a git repo based plugin to my npm install for ionic framework?

I am using the Ionic Framework with AngularJs to make a web application. 我将Ionic Framework与AngularJs结合使用来制作Web应用程序。 Now that i have a bunch of my application in place with static information, I wanted to now update the services to leverage database. 现在,我的应用程序已经有了很多静态信息,现在我想更新服务以利用数据库。 I was looking up the best course of actions for databases with the ionic framework and angular. 我一直在寻找具有离子框架和角度的数据库的最佳方法。 I was told that Sqlite would be the best option. 有人告诉我Sqlite是最好的选择。

I ended up at the following website from my search: https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/ which was mentioning to me to run: cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin.git . 我最终从搜索中访问了以下网站: https : cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin.git提到要运行: cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin.git

My first attempt of running the command seemed to update a lot of my files under the platforms/ directory of my application which I thought was odd. 我第一次尝试运行该命令似乎是在我的应用程序的platforms /目录下更新了很多文件,我认为这很奇怪。 I was not sure if i should be adding them to my repository or not. 我不确定是否应该将它们添加到我的存储库中。

Anyways the question I am getting at is. 无论如何,我要解决的问题是。 I want to run an npm install on my machine and everything would be good to go for whoever pulls my code. 我想在我的机器上运行npm安装,无论谁拉我的代码,一切都会很好。 How would I had this plugin git website so when the install runs, it will then run this file. 我将如何拥有此插件git网站,以便在安装运行时,它将运行此文件。

EDIT I was not sure if it was as simple as adding the URL to the cordovaPlugins list in the packages.json file. 编辑我不确定这是否像将URL添加到packages.json文件中的cordovaPlugins列表中那样简单。 I am going to test that now. 我现在要测试。

Whenever you add plugins, you can add the --save flag to the cordova/ionic command to save them to your package.json. 每当添加插件时,都可以将--save标志添加到cordova / ionic命令中,以将其保存到package.json中。 That way when someone pulls in your project, they can run ionic state reset , which causes ionic to install all the cordova plugins from the package.json file. 这样,当有人进入您的项目时,他们可以运行ionic state reset ,这将导致ionic从package.json文件安装所有cordova插件。

I am not sure if this is the best way but what I did was: 我不确定这是否是最好的方法,但是我所做的是:

created a script file in scripts/setup.js which carried the following code. 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);

This file would then check the cordovaPlatforms section and cordovaPlugins sections for things which need to be added and then add them. 然后,此文件将检查cordovaPlatforms部分和cordovaPlugins部分中需要添加的内容,然后添加它们。

I then went into packages.json and added: 然后,我进入packages.json并添加:

"scripts": {
  "postinstall":"node scripts/setup.js"
}

and then after the npm install finished, it would then run the set up script which would add all the required platforms and plugins accordingly. 然后在npm安装完成后,它将运行设置脚本,该脚本将相应地添加所有必需的平台和插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM