beforePluginInstallHook.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Also, it will suggest to user to install CLI client for that plugin.
  5. It can be found in https://github.com/nordnet/cordova-hot-code-push-cli
  6. */
  7. var path = require('path');
  8. var fs = require('fs');
  9. var spawnSync = require('child_process').spawnSync;
  10. var pluginNpmDependencies = require('../package.json').dependencies;
  11. var INSTALLATION_FLAG_FILE_NAME = '.npmInstalled';
  12. // region CLI specific
  13. /**
  14. * Check if cordova-hcp utility is installed. If not - suggest user to install it.
  15. */
  16. function checkCliDependency(ctx) {
  17. var result = spawnSync('cordova-hcp', [], { cwd: './plugins/' + ctx.opts.plugin.id });
  18. if (!result.error) {
  19. return;
  20. }
  21. suggestCliInstallation();
  22. }
  23. /**
  24. * Show message, that developer should install CLI client for the plugin, so it would be easier to use.
  25. */
  26. function suggestCliInstallation() {
  27. console.log('---------CHCP-------------');
  28. console.log('To make the development process easier for you - we developed a CLI client for our plugin.');
  29. console.log('To install it, please, use command:');
  30. console.log('npm install -g cordova-hot-code-push-cli');
  31. console.log('For more information please visit https://github.com/nordnet/cordova-hot-code-push-cli');
  32. console.log('--------------------------');
  33. }
  34. // endregion
  35. // region mark that we installed npm packages
  36. /**
  37. * Check if we already executed this hook.
  38. *
  39. * @param {Object} ctx - cordova context
  40. * @return {Boolean} true if already executed; otherwise - false
  41. */
  42. function isInstallationAlreadyPerformed(ctx) {
  43. var pathToInstallFlag = path.join(ctx.opts.projectRoot, 'plugins', ctx.opts.plugin.id, INSTALLATION_FLAG_FILE_NAME);
  44. try {
  45. fs.accessSync(pathToInstallFlag, fs.F_OK);
  46. return true;
  47. } catch (err) {
  48. return false;
  49. }
  50. }
  51. /**
  52. * Create empty file - indicator, that we tried to install dependency modules after installation.
  53. * We have to do that, or this hook is gonna be called on any plugin installation.
  54. */
  55. function createPluginInstalledFlag(ctx) {
  56. var pathToInstallFlag = path.join(ctx.opts.projectRoot, 'plugins', ctx.opts.plugin.id, INSTALLATION_FLAG_FILE_NAME);
  57. fs.closeSync(fs.openSync(pathToInstallFlag, 'w'));
  58. }
  59. // endregion
  60. module.exports = function(ctx) {
  61. if (isInstallationAlreadyPerformed(ctx)) {
  62. return;
  63. }
  64. console.log('Installing dependency packages: ');
  65. console.log(JSON.stringify(pluginNpmDependencies, null, 2));
  66. var npm = (process.platform === "win32" ? "npm.cmd" : "npm");
  67. var result = spawnSync(npm, ['install', '--production'], { cwd: './plugins/' + ctx.opts.plugin.id });
  68. if (result.error) {
  69. throw result.error;
  70. }
  71. createPluginInstalledFlag(ctx);
  72. console.log('Checking cordova-hcp CLI client...');
  73. checkCliDependency(ctx);
  74. };