Merge pull request #4380 from strapi/enhancement/initialise-fn

Update initialisation detection when starting a project
This commit is contained in:
Alexandre BODIN 2019-10-28 17:51:15 +01:00 committed by GitHub
commit c0c7f134b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 10 deletions

View File

@ -1,3 +0,0 @@
{
"init": true
}

View File

@ -199,8 +199,8 @@ class Strapi extends EventEmitter {
await this.runBootstrapFunctions(); await this.runBootstrapFunctions();
// Freeze object. // Freeze object.
await this.freeze(); await this.freeze();
// Init first start // Is the project initialised?
utils.init(this.config); const isInitialised = await utils.isInitialised(this);
this.app.use(this.router.routes()).use(this.router.allowedMethods()); this.app.use(this.router.routes()).use(this.router.allowedMethods());
@ -208,7 +208,7 @@ class Strapi extends EventEmitter {
this.server.listen(this.config.port, async err => { this.server.listen(this.config.port, async err => {
if (err) return this.stopWithError(err); if (err) return this.stopWithError(err);
if (this.config.init) { if (!isInitialised) {
this.logFirstStartupMessage(); this.logFirstStartupMessage();
} else { } else {
this.logStartupMessage(); this.logStartupMessage();
@ -228,7 +228,7 @@ class Strapi extends EventEmitter {
'server.admin.autoOpen', 'server.admin.autoOpen',
true true
) !== false) || ) !== false) ||
this.config.init !isInitialised
) { ) {
await utils.openBrowser.call(this); await utils.openBrowser.call(this);
} }

View File

@ -12,6 +12,7 @@ const {
get, get,
difference, difference,
intersection, intersection,
isEmpty,
isObject, isObject,
isFunction, isFunction,
} = require('lodash'); } = require('lodash');
@ -23,9 +24,22 @@ const exposer = require('./exposer');
const openBrowser = require('./openBrowser'); const openBrowser = require('./openBrowser');
module.exports = { module.exports = {
init(config) { /*
if (config.init) { * Return false where there is no administrator, otherwise return true.
fs.unlinkSync(path.resolve(config.appPath, 'config', '.init.json')); */
async isInitialised(strapi) {
try {
if (isEmpty(strapi.admin)) {
return true;
}
const numberOfAdministrators = await strapi
.query('administrator', 'admin')
.find({ _limit: 1 });
return numberOfAdministrators.length > 0;
} catch (err) {
strapi.stopWithError(err);
} }
}, },