diff --git a/docs/3.x.x/en/guides/models.md b/docs/3.x.x/en/guides/models.md index 1c455b6c33..96eeb0e8c3 100644 --- a/docs/3.x.x/en/guides/models.md +++ b/docs/3.x.x/en/guides/models.md @@ -651,12 +651,11 @@ module.exports = { * Triggered before user creation. */ beforeCreate: async (model, attrs, options) => { - // Hash password. - const passwordHashed = await strapi.api.user.services.user.hashPassword(model.attributes.password); + // Hash password. + const passwordHashed = await strapi.api.user.services.user.hashPassword(model.attributes.password); - // Set the password. - model.set('password', passwordHashed); - }); + // Set the password. + model.set('password', passwordHashed); } } ``` diff --git a/packages/strapi-admin/scripts/setup.js b/packages/strapi-admin/scripts/setup.js index c60587a03c..b2feefe066 100644 --- a/packages/strapi-admin/scripts/setup.js +++ b/packages/strapi-admin/scripts/setup.js @@ -5,7 +5,7 @@ const _ = require('lodash'); shell.echo(''); shell.echo('🕓 The setup process can take few minutes.'); shell.echo(''); -shell.echo(`🔸 Administration Panel`); +shell.echo('🔸 Administration Panel'); shell.echo('📦 Installing packages...'); const pwd = shell.pwd(); @@ -19,28 +19,28 @@ shell.rm('-rf', path.resolve(appPath, 'package-lock.json')); shell.rm('-rf', path.resolve(appPath, 'admin', 'package-lock.json')); // Install the project dependencies. -shell.exec(`cd ${appPath} && npm install --ignore-scripts`, { +shell.exec(`cd "${appPath}" && npm install --ignore-scripts`, { silent }); // Install the administration dependencies. -shell.exec(`cd ${path.resolve(appPath, 'admin')} && npm install`, { +shell.exec(`cd "${path.resolve(appPath, 'admin')}" && npm install`, { silent }); if (isDevelopmentMode) { - shell.exec(`cd ${path.resolve(appPath, 'admin')} && npm link strapi-helper-plugin && npm link strapi-utils`, { + shell.exec(`cd "${path.resolve(appPath, 'admin')}" && npm link strapi-helper-plugin && npm link strapi-utils`, { silent }); } else { - shell.exec(`cd ${path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin')} && npm install`, { + shell.exec(`cd "${path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin')}" && npm install`, { silent }); } shell.echo('🏗 Building...'); -const build = shell.exec(`cd ${path.resolve(appPath, 'admin')} && APP_PATH=${appPath} npm run build`, { +const build = shell.exec(`cd "${path.resolve(appPath, 'admin')}" && APP_PATH="${appPath}" npm run build`, { silent }); @@ -58,23 +58,23 @@ if (process.env.npm_config_plugins === 'true') { shell.ls('* -d', plugins).forEach(function (plugin) { shell.echo(`🔸 Plugin - ${_.upperFirst(plugin)}`); shell.echo('📦 Installing packages...'); - shell.exec(`cd ${path.resolve(plugins, plugin)} && npm install`, { + shell.exec(`cd "${path.resolve(plugins, plugin)}" && npm install`, { silent }); if (isDevelopmentMode) { - shell.exec(`cd ${path.resolve(plugins, plugin)} && npm link strapi-helper-plugin`, { + shell.exec(`cd "${path.resolve(plugins, plugin)}" && npm link strapi-helper-plugin`, { silent }); } else { - shell.exec(`cd ${path.resolve(plugins, plugin, 'node_modules', 'strapi-helper-plugin')} && npm install`, { + shell.exec(`cd "${path.resolve(plugins, plugin, 'node_modules', 'strapi-helper-plugin')}" && npm install`, { silent }); } shell.echo('🏗 Building...'); - const build = shell.exec(`cd ${path.resolve(plugins, plugin)} && APP_PATH=${appPath} npm run build`, { + const build = shell.exec(`cd "${path.resolve(plugins, plugin)}" && APP_PATH="${appPath}" npm run build`, { silent }); diff --git a/packages/strapi-bookshelf/lib/utils/connectivity.js b/packages/strapi-bookshelf/lib/utils/connectivity.js index 92f2b098dc..2924d88893 100644 --- a/packages/strapi-bookshelf/lib/utils/connectivity.js +++ b/packages/strapi-bookshelf/lib/utils/connectivity.js @@ -18,7 +18,7 @@ module.exports = (scope, success, error) => { knex.raw('select 1+1 as result').then(() => { logger.info('The app has been connected to the database successfully'); knex.destroy(); - execSync(`rm -r ${scope.tmpPath}`); + execSync(`rm -r "${scope.tmpPath}"`); logger.info('Copying the dashboard...'); diff --git a/packages/strapi-generate-api/templates/bookshelf/model.template b/packages/strapi-generate-api/templates/bookshelf/model.template index 474aeecc5a..6a197959cd 100755 --- a/packages/strapi-generate-api/templates/bookshelf/model.template +++ b/packages/strapi-generate-api/templates/bookshelf/model.template @@ -5,64 +5,43 @@ */ module.exports = { - // Before saving a value. // Fired before an `insert` or `update` query. - // beforeSave: function (model, attrs, options) { - // return new Promise(); - // }, + // beforeSave: async (model, attrs, options) => {}, // After saving a value. // Fired after an `insert` or `update` query. - // afterSave: function (model, response, options) { - // return new Promise(); - // }, + // afterSave: async (model, response, options) => {}, // Before fetching a value. // Fired before a `fetch` operation. - // beforeFetch: function (model, columns, options) { - // return new Promise(); - // }, + // beforeFetch: async (model, columns, options) => {}, // After fetching a value. // Fired after a `fetch` operation. - // afterFetch: function (model, response, options) { - // return new Promise(); - // }, + // afterFetch: async (model, response, options) => {}, // Before creating a value. - // Fired before `insert` query. - // beforeCreate: function (model, attrs, options) { - // return new Promise(); - // }, + // Fired before an `insert` query. + // beforeCreate: async (model, attrs, options) => {}, // After creating a value. - // Fired after `insert` query. - // afterCreate: function (model, attrs, options) { - // return new Promise(); - // }, + // Fired after an `insert` query. + // afterCreate: async (model, attrs, options) => {}, // Before updating a value. // Fired before an `update` query. - // beforeUpdate: function (model, attrs, options) { - // return new Promise(); - // }, + // beforeUpdate: async (model, attrs, options) => {}, // After updating a value. // Fired after an `update` query. - // afterUpdate: function (model, attrs, options) { - // return new Promise(); - // }, + // afterUpdate: async (model, attrs, options) => {}, // Before destroying a value. // Fired before a `delete` query. - // beforeDestroy: function (model, attrs, options) { - // return new Promise(); - // }, + // beforeDestroy: async (model, attrs, options) => {}, // After destroying a value. // Fired after a `delete` query. - // afterDestroy: function (model, attrs, options) { - // return new Promise(); - // } + // afterDestroy: async (model, attrs, options) => {} }; diff --git a/packages/strapi-generate-api/templates/mongoose/model.template b/packages/strapi-generate-api/templates/mongoose/model.template index 45c38e2375..300dc5a200 100755 --- a/packages/strapi-generate-api/templates/mongoose/model.template +++ b/packages/strapi-generate-api/templates/mongoose/model.template @@ -29,11 +29,11 @@ module.exports = { // afterFetch: async (model, result) => {}, // Before creating a value. - // Fired before `insert` query. + // Fired before an `insert` query. // beforeCreate: async (model) => {}, // After creating a value. - // Fired after `insert` query. + // Fired after an `insert` query. // afterCreate: async (model, result) => {}, // Before updating a value. diff --git a/packages/strapi-mongoose/lib/utils/connectivity.js b/packages/strapi-mongoose/lib/utils/connectivity.js index 11d92560cf..76fe1c9261 100644 --- a/packages/strapi-mongoose/lib/utils/connectivity.js +++ b/packages/strapi-mongoose/lib/utils/connectivity.js @@ -32,7 +32,7 @@ module.exports = (scope, success, error) => { Mongoose.connection.close(); - execSync(`rm -r ${scope.tmpPath}`); + execSync(`rm -r "${scope.tmpPath}"`); logger.info('Copying the dashboard...'); diff --git a/packages/strapi-mongoose/lib/utils/index.js b/packages/strapi-mongoose/lib/utils/index.js index a915585f3d..38c4e0f001 100755 --- a/packages/strapi-mongoose/lib/utils/index.js +++ b/packages/strapi-mongoose/lib/utils/index.js @@ -19,32 +19,33 @@ module.exports = mongoose => { return { convertType: mongooseType => { switch (mongooseType.toLowerCase()) { - case 'string': - case 'password': - case 'text': - case 'email': - return 'String'; - case 'integer': - case 'biginteger': - return 'Number'; - case 'float': - case 'decimal': - return 'Float'; - case 'date': - case 'time': - case 'datetime': - case 'timestamp': - return Date; + case 'array': + return 'Array'; case 'boolean': return 'Boolean'; case 'binary': return 'Buffer'; - case 'uuid': - return 'ObjectId'; - case 'enumeration': - return 'String'; + case 'date': + case 'datetime': + case 'time': + case 'timestamp': + return Date; + case 'decimal': + case 'float': + return 'Float'; case 'json': return 'Mixed'; + case 'biginteger': + case 'integer': + return 'Number'; + case 'uuid': + return 'ObjectId'; + case 'email': + case 'enumeration': + case 'password': + case 'string': + case 'text': + return 'String'; default: } diff --git a/packages/strapi-redis/lib/utils/connectivity.js b/packages/strapi-redis/lib/utils/connectivity.js index 8480390dd3..98f8f9bad5 100644 --- a/packages/strapi-redis/lib/utils/connectivity.js +++ b/packages/strapi-redis/lib/utils/connectivity.js @@ -26,7 +26,7 @@ module.exports = (scope, success, error) => { logger.info('The app has been connected to the database successfully!'); - execSync(`rm -r ${scope.tmpPath}`); + execSync(`rm -r "${scope.tmpPath}"`); logger.info('Copying the dashboard...'); diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index 4af4f114bb..dac47087b0 100755 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -87,6 +87,9 @@ class Strapi extends EventEmitter { try { this.config = assign(this.config, config) + // Emit starting event. + this.emit('server:starting'); + // Enhance app. await this.enhancer(); // Load the app. @@ -100,7 +103,9 @@ class Strapi extends EventEmitter { // Launch server. this.server.listen(this.config.port, err => { if (err) { - console.log(err); + this.log.debug(`Server wasn't able to start properly.`); + console.error(err); + return this.stop(); } this.log.info('Server started in ' + this.config.appPath); @@ -112,14 +117,16 @@ class Strapi extends EventEmitter { this.log.debug(`Version: ${this.config.info.strapi} (node v${this.config.info.node})`); this.log.info('To shut down your server, press + C at any time'); + // Emit started event. + this.emit('server:started'); + if (cb && typeof cb === 'function') { cb(); } }); - } catch (e) { + } catch (err) { this.log.debug(`Server wasn't able to start properly.`); - this.log.error(e); - console.error(e); + console.error(err); this.stop(); } } @@ -136,6 +143,17 @@ class Strapi extends EventEmitter { }); }); + this.server.on('error', err => { + if (err.code === 'EADDRINUSE') { + this.log.debug(`Server wasn't able to start properly.`); + this.log.error(`The port ${err.port} is already used by another application.`); + this.stop(); + return; + } + + console.error(err); + }); + this.server.destroy = cb => { this.server.close(cb); diff --git a/packages/strapi/lib/core/hooks.js b/packages/strapi/lib/core/hooks.js index ac1a4a79c7..847ac72b64 100755 --- a/packages/strapi/lib/core/hooks.js +++ b/packages/strapi/lib/core/hooks.js @@ -91,6 +91,12 @@ const mountHooks = function (files, cwd) { }); }), - resolve + (err, results) => { + if (err) { + return reject(err); + } + + resolve(); + } ); }; diff --git a/packages/strapi/lib/utils/index.js b/packages/strapi/lib/utils/index.js index 51aec60ed2..1be9480652 100755 --- a/packages/strapi/lib/utils/index.js +++ b/packages/strapi/lib/utils/index.js @@ -14,16 +14,10 @@ const exposer = require('./exposer'); module.exports = { loadFile: function(url) { - try { - // Clear cache. - delete require.cache[require.resolve(path.resolve(this.config.appPath, url))]; - // Require without cache. - return require(path.resolve(this.config.appPath, url)); - } catch (e) { - this.log.error(e); - - return {}; - } + // Clear cache. + delete require.cache[require.resolve(path.resolve(this.config.appPath, url))]; + // Require without cache. + return require(path.resolve(this.config.appPath, url)); }, setConfig: function(ctx, path, type, loader) { @@ -123,11 +117,12 @@ module.exports = { try { if (this.config.uuid) { const publicKey = fs.readFileSync(path.resolve(__dirname, 'resources', 'key.pub')); + const options = { timeout: 1000 }; const [usage, signedHash, required] = await Promise.all([ - fetch('https://strapi.io/assets/images/usage.gif'), - fetch('https://strapi.io/hash.txt'), - fetch('https://strapi.io/required.txt') + fetch('https://strapi.io/assets/images/usage.gif', options), + fetch('https://strapi.io/hash.txt', options), + fetch('https://strapi.io/required.txt', options) ]); if (usage.status === 200 && signedHash.status === 200) {