bootstrap.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Bootstrap
  3. * (sails.config.bootstrap)
  4. *
  5. * An asynchronous bootstrap function that runs just before your Sails app gets lifted.
  6. * > Need more flexibility? You can also do this by creating a hook.
  7. *
  8. * For more information on bootstrapping your app, check out:
  9. * https://sailsjs.com/config/bootstrap
  10. */
  11. module.exports.bootstrap = async function(done) {
  12. // Import dependencies
  13. var path = require('path');
  14. // This bootstrap version indicates what version of fake data we're dealing with here.
  15. var HARD_CODED_DATA_VERSION = 0;
  16. // This path indicates where to store/look for the JSON file that tracks the "last run bootstrap info"
  17. // locally on this development computer (if we happen to be on a development computer).
  18. var bootstrapLastRunInfoPath = path.resolve(sails.config.appPath, '.tmp/bootstrap-version.json');
  19. // Whether or not to continue doing the stuff in this file (i.e. wiping and regenerating data)
  20. // depends on some factors:
  21. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. // If the hard-coded data version has been incremented, or we're being forced
  23. // (i.e. `--drop` or `--environment=test` was set), then run the meat of this
  24. // bootstrap script to wipe all existing data and rebuild hard-coded data.
  25. if (sails.config.models.migrate !== 'drop' && sails.config.environment !== 'test') {
  26. // If this is _actually_ a production environment (real or simulated), or we have
  27. // `migrate: safe` enabled, then prevent accidentally removing all data!
  28. if (process.env.NODE_ENV==='production' || sails.config.models.migrate === 'safe') {
  29. sails.log.warn('Since we are running with migrate: \'safe\' and/or NODE_ENV=production (in the "'+sails.config.environment+'" Sails environment, to be precise), skipping the rest of the bootstrap to avoid data loss...');
  30. return done();
  31. }//•
  32. // Compare bootstrap version from code base to the version that was last run
  33. var lastRunBootstrapInfo = await sails.helpers.fs.readJson(bootstrapLastRunInfoPath)
  34. .tolerate('doesNotExist');// (it's ok if the file doesn't exist yet-- just keep going.)
  35. if (lastRunBootstrapInfo && lastRunBootstrapInfo.lastRunVersion === HARD_CODED_DATA_VERSION) {
  36. sails.log('Skipping v'+HARD_CODED_DATA_VERSION+' bootstrap script... (because it\'s already been run)');
  37. sails.log('(last run on this computer: @ '+(new Date(lastRunBootstrapInfo.lastRunAt))+')');
  38. return done();
  39. }//•
  40. sails.log('Running v'+HARD_CODED_DATA_VERSION+' bootstrap script... ('+(lastRunBootstrapInfo ? 'before this, the last time the bootstrap ran on this computer was for v'+lastRunBootstrapInfo.lastRunVersion+' @ '+(new Date(lastRunBootstrapInfo.lastRunAt)) : 'looks like this is the first time the bootstrap has run on this computer')+')');
  41. }
  42. else {
  43. sails.log('Running bootstrap script because it was forced... (either `--drop` or `--environment=test` was used)');
  44. }
  45. // Since the hard-coded data version has been incremented, and we're running in
  46. // a "throwaway data" environment, delete all records from all models.
  47. for (let identity in sails.models) {
  48. await sails.models[identity].destroy({});
  49. }//∞
  50. // By convention, this is a good place to set up fake data during development.
  51. await User.createEach([
  52. { emailAddress: 'admin@example.com', fullName: 'Ryan Dahl', isSuperAdmin: true, password: await sails.helpers.passwords.hashPassword('abc123') },
  53. ]);
  54. // Save new bootstrap version
  55. await sails.helpers.fs.writeJson.with({
  56. destination: bootstrapLastRunInfoPath,
  57. json: {
  58. lastRunVersion: HARD_CODED_DATA_VERSION,
  59. lastRunAt: Date.now()
  60. },
  61. force: true
  62. })
  63. .tolerate((err)=>{
  64. sails.log.warn('For some reason, could not write bootstrap version .json file. This could be a result of a problem with your configured paths, or, if you are in production, a limitation of your hosting provider related to `pwd`. As a workaround, try updating app.js to explicitly pass in `appPath: __dirname` instead of relying on `chdir`. Current sails.config.appPath: `'+sails.config.appPath+'`. Full error details: '+err.stack+'\n\n(Proceeding anyway this time...)');
  65. });
  66. // Don't forget to trigger `done()` when this bootstrap function's logic is finished.
  67. // (otherwise your server will never lift, since it's waiting on the bootstrap)
  68. return done();
  69. };