fleet/website/config/bootstrap.js
Mike McNeil 27eae209fd
Move fleetdm.com into main Fleet repo (#83)
* rename dir

* no need to install website or docs from npm

At some point, would also be nice to be able to exclude assets/ as well, and to only install a pre-built version of Fleet's frontend code

* Bring in fleetdm.com website

From https://github.com/fleetdm/fleetdm.com as of https://github.com/fleetdm/fleetdm.com/releases/tag/v0.0.21

* add procfile for heroku

Using https://github.com/timanovsky/subdir-heroku-buildpack

* avoid getting anybody's hopes up

* Create deploy-fleet-website.yml (#82)

* Create deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* update pjs with SPDX-like license expressions.  also fix repo URL

and remove package lock

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* remove dummy uri

* Dissect deploy script

* Update deploy-fleet-website.yml

* workaround for eslintrc nesting issue

* lint fixes

* forgot the .js

* add per-commit git config

* Update deploy-fleet-website.yml

* might as well remove that

* cleanup

* connect w/ heroku app and have it actually push

* fix bug I introduced in 578a1a01ffb8404aae869e05005e30a6ba2b2a95

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* the beauty, the glory, of javascript

* GH actions don't like "\n"

* Update deploy-fleet-website.yml

* restore \n chars from 0d45e568f693efba7d7072085bc98d72a482d9ae

hoping I was wrong in 0d45e568f693efba7d7072085bc98d72a482d9ae but see also https://github.community/t/what-is-the-correct-character-escaping-for-workflow-command-values-e-g-echo-xxxx/118465/5

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* rename script to prevent duplicate building

* Configure the real website

* clean up

* a test of the deploy workflow

* add handbook to npmignore

* I guess you could call this fixing a typo

* point workflow at master branch

* now clearly bogus: this completely unused version string
2020-12-02 14:48:03 -06:00

80 lines
4.1 KiB
JavaScript
Vendored

/**
* Seed Function
* (sails.config.bootstrap)
*
* A function that runs just before your Sails app gets lifted.
* > Need more flexibility? You can also create a hook.
*
* For more information on seeding your app with fake data, check out:
* https://sailsjs.com/config/bootstrap
*/
module.exports.bootstrap = async function() {
// Import dependencies
var path = require('path');
// This bootstrap version indicates what version of fake data we're dealing with here.
var HARD_CODED_DATA_VERSION = 0;
// This path indicates where to store/look for the JSON file that tracks the "last run bootstrap info"
// locally on this development computer (if we happen to be on a development computer).
var bootstrapLastRunInfoPath = path.resolve(sails.config.appPath, '.tmp/bootstrap-version.json');
// Whether or not to continue doing the stuff in this file (i.e. wiping and regenerating data)
// depends on some factors:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// If the hard-coded data version has been incremented, or we're being forced
// (i.e. `--drop` or `--environment=test` was set), then run the meat of this
// bootstrap script to wipe all existing data and rebuild hard-coded data.
if (sails.config.models.migrate !== 'drop' && sails.config.environment !== 'test') {
// If this is _actually_ a production environment (real or simulated), or we have
// `migrate: safe` enabled, then prevent accidentally removing all data!
if (process.env.NODE_ENV==='production' || sails.config.models.migrate === 'safe') {
sails.log('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...');
return;
}//•
// Compare bootstrap version from code base to the version that was last run
var lastRunBootstrapInfo = await sails.helpers.fs.readJson(bootstrapLastRunInfoPath)
.tolerate('doesNotExist');// (it's ok if the file doesn't exist yet-- just keep going.)
if (lastRunBootstrapInfo && lastRunBootstrapInfo.lastRunVersion === HARD_CODED_DATA_VERSION) {
sails.log('Skipping v'+HARD_CODED_DATA_VERSION+' bootstrap script... (because it\'s already been run)');
sails.log('(last run on this computer: @ '+(new Date(lastRunBootstrapInfo.lastRunAt))+')');
return;
}//•
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')+')');
}
else {
sails.log('Running bootstrap script because it was forced... (either `--drop` or `--environment=test` was used)');
}
// Since the hard-coded data version has been incremented, and we're running in
// a "throwaway data" environment, delete all records from all models.
for (let identity in sails.models) {
await sails.models[identity].destroy({});
}//∞
// By convention, this is a good place to set up fake data during development.
await User.createEach([
{ emailAddress: 'admin@example.com', fullName: 'Ryan Dahl', isSuperAdmin: true, password: await sails.helpers.passwords.hashPassword('abc123') },
]);
// Save new bootstrap version
await sails.helpers.fs.writeJson.with({
destination: bootstrapLastRunInfoPath,
json: {
lastRunVersion: HARD_CODED_DATA_VERSION,
lastRunAt: Date.now()
},
force: true
})
.tolerate((err)=>{
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...)');
});
};