Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

beforePluginInstallHook.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. Hook is executed when plugin is added to the project.
  3. It will check all necessary module dependencies and install the missing ones locally.
  4. */
  5. var path = require('path');
  6. var fs = require('fs');
  7. var spawnSync = require('child_process').spawnSync;
  8. var pluginNpmDependencies = require('../package.json').dependencies;
  9. var INSTALLATION_FLAG_FILE_NAME = '.npmInstalled';
  10. // region mark that we installed npm packages
  11. /**
  12. * Check if we already executed this hook.
  13. *
  14. * @param {Object} ctx - cordova context
  15. * @return {Boolean} true if already executed; otherwise - false
  16. */
  17. function isInstallationAlreadyPerformed(ctx) {
  18. var pathToInstallFlag = path.join(ctx.opts.projectRoot, 'plugins', ctx.opts.plugin.id, INSTALLATION_FLAG_FILE_NAME);
  19. try {
  20. fs.accessSync(pathToInstallFlag, fs.F_OK);
  21. return true;
  22. } catch (err) {
  23. return false;
  24. }
  25. }
  26. /**
  27. * Create empty file - indicator, that we tried to install dependency modules after installation.
  28. * We have to do that, or this hook is gonna be called on any plugin installation.
  29. */
  30. function createPluginInstalledFlag(ctx) {
  31. var pathToInstallFlag = path.join(ctx.opts.projectRoot, 'plugins', ctx.opts.plugin.id, INSTALLATION_FLAG_FILE_NAME);
  32. fs.closeSync(fs.openSync(pathToInstallFlag, 'w'));
  33. }
  34. // endregion
  35. module.exports = function(ctx) {
  36. if (isInstallationAlreadyPerformed(ctx)) {
  37. return;
  38. }
  39. console.log('Installing dependency packages: ');
  40. console.log(JSON.stringify(pluginNpmDependencies, null, 2));
  41. var npm = (process.platform === "win32" ? "npm.cmd" : "npm");
  42. var result = spawnSync(npm, ['install', '--production'], { cwd: './plugins/' + ctx.opts.plugin.id });
  43. if (result.error) {
  44. throw result.error;
  45. }
  46. createPluginInstalledFlag(ctx);
  47. };