421 lines
11 KiB
JavaScript
Raw Normal View History

2016-03-18 11:12:50 +01:00
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const path = require('path');
const os = require('os');
const crypto = require('crypto');
2018-05-04 17:50:39 +02:00
2016-03-18 11:12:50 +01:00
// Public node modules.
const _ = require('lodash');
2019-04-30 16:15:12 +02:00
const { cyan, green } = require('chalk');
2016-03-18 11:12:50 +01:00
const fs = require('fs-extra');
2018-01-05 15:17:59 +01:00
const inquirer = require('inquirer');
2019-04-30 16:15:12 +02:00
const execa = require('execa');
2019-01-18 16:08:15 +01:00
const uuid = require('uuid/v4');
2019-04-30 16:15:12 +02:00
const rimraf = require('rimraf');
2016-03-18 11:12:50 +01:00
2016-03-25 22:22:34 +01:00
// Logger.
const { packageManager } = require('strapi-utils');
2019-01-28 15:50:13 +01:00
const trackSuccess = require('./success');
2016-03-25 22:22:34 +01:00
2016-03-18 11:12:50 +01:00
/**
* This `before` function is run before generating targets.
* Validate, configure defaults, get extra dependencies, etc.
*
* @param {Object} scope
* @param {Function} cb
*/
2018-05-04 17:50:39 +02:00
/* eslint-disable no-useless-escape */
module.exports = (scope, cb) => {
2016-03-18 11:12:50 +01:00
// App info.
const hasDatabaseConfig = !!scope.database;
2016-03-18 11:12:50 +01:00
_.defaults(scope, {
2019-04-30 16:15:12 +02:00
name:
scope.name === '.' || !scope.name
? scope.name
: path.basename(process.cwd()),
2016-03-18 11:12:50 +01:00
author: process.env.USER || 'A Strapi developer',
email: process.env.EMAIL || '',
2019-04-30 16:15:12 +02:00
year: new Date().getFullYear(),
license: 'MIT',
2019-04-05 16:11:09 +02:00
database: {},
additionalsDependencies: [
'strapi-plugin-settings-manager',
'strapi-plugin-content-type-builder',
'strapi-plugin-content-manager',
'strapi-plugin-users-permissions',
'strapi-plugin-email',
'strapi-plugin-upload',
2019-04-30 16:15:12 +02:00
],
2016-03-18 11:12:50 +01:00
});
// Make changes to the rootPath where the Strapi project will be created.
scope.rootPath = path.resolve(process.cwd(), scope.name || '');
2019-04-30 16:15:12 +02:00
scope.tmpPath = path.resolve(
os.tmpdir(),
`strapi${crypto.randomBytes(6).toString('hex')}`
);
2019-01-18 16:08:15 +01:00
scope.uuid = uuid();
2016-03-18 11:12:50 +01:00
2019-01-28 15:50:13 +01:00
trackSuccess('willCreateProject', scope);
2016-03-18 11:12:50 +01:00
// Ensure we aren't going to inadvertently delete any files.
try {
const files = fs.readdirSync(scope.rootPath);
2018-02-12 19:08:37 +01:00
if (files.length > 1) {
2019-04-30 16:15:12 +02:00
return console.log(
`⛔️ ${cyan('strapi new')} can only be called in an empty directory.`
);
2016-03-18 11:12:50 +01:00
}
2016-03-25 22:22:34 +01:00
} catch (err) {
2016-03-18 11:12:50 +01:00
// ...
}
if (hasDatabaseConfig) {
2019-04-30 16:15:12 +02:00
console.log(
`Database determined by CLI args: ${scope.database.settings.client}`
);
}
2019-02-01 14:56:46 +01:00
const connectionValidation = async () => {
2018-01-10 18:08:43 +01:00
const databaseChoices = [
2018-12-29 21:35:07 +01:00
{
name: 'SQLite',
value: {
database: 'sqlite',
connector: 'strapi-hook-bookshelf',
2019-04-30 16:15:12 +02:00
module: 'sqlite3',
},
2018-12-29 21:35:07 +01:00
},
2018-01-10 14:45:32 +01:00
{
2018-10-09 14:41:56 +02:00
name: 'MongoDB',
2018-01-10 14:45:32 +01:00
value: {
database: 'mongo',
2019-04-30 16:15:12 +02:00
connector: 'strapi-hook-mongoose',
},
2018-01-10 14:45:32 +01:00
},
{
2018-12-29 21:35:07 +01:00
name: 'MySQL',
2018-01-10 14:45:32 +01:00
value: {
2018-12-29 21:35:07 +01:00
database: 'mysql',
2018-07-11 16:23:14 +02:00
connector: 'strapi-hook-bookshelf',
2019-04-30 16:15:12 +02:00
module: 'mysql',
},
2018-01-10 14:45:32 +01:00
},
{
2018-12-29 21:35:07 +01:00
name: 'Postgres',
2018-01-10 14:45:32 +01:00
value: {
2018-12-29 21:35:07 +01:00
database: 'postgres',
2018-07-11 16:23:14 +02:00
connector: 'strapi-hook-bookshelf',
2019-04-30 16:15:12 +02:00
module: 'pg',
},
},
2018-01-10 14:45:32 +01:00
];
2019-04-30 16:15:12 +02:00
const answers = await inquirer.prompt([
{
when: !scope.quick && !hasDatabaseConfig,
type: 'list',
name: 'type',
message: 'Choose your installation type',
choices: [
{
2019-02-01 14:56:46 +01:00
name: 'Quickstart (recommended)',
2019-04-30 16:15:12 +02:00
value: 'quick',
},
{
2019-02-01 14:56:46 +01:00
name: 'Custom (manual settings)',
2019-04-30 16:15:12 +02:00
value: 'custom',
2019-02-01 14:56:46 +01:00
},
2019-04-30 16:15:12 +02:00
],
},
{
when: answers => {
return !hasDatabaseConfig && answers.type === 'custom';
},
type: 'list',
name: 'client',
message: 'Choose your main database:',
choices: databaseChoices,
default: () => {
if (scope.client) {
return _.findIndex(databaseChoices, {
value: _.omit(scope.client, ['version']),
});
}
2019-04-30 16:15:12 +02:00
},
},
]);
2018-01-08 12:00:56 +01:00
2019-02-01 14:56:46 +01:00
scope.quick = answers.type === 'quick' || scope.quick;
const isQuick = scope.quick;
2019-04-05 16:11:09 +02:00
2019-02-01 14:56:46 +01:00
if (isQuick) {
answers.client = databaseChoices[0].value;
}
2019-02-01 14:56:46 +01:00
if (hasDatabaseConfig) {
2019-04-30 16:15:12 +02:00
const databaseChoice = _.find(databaseChoices, [
'value.database',
scope.database.settings.client,
]);
2019-02-01 14:56:46 +01:00
scope.database.connector = databaseChoice.value.connector;
answers.client = {
2019-04-30 16:15:12 +02:00
...databaseChoice.value,
2019-02-01 14:56:46 +01:00
};
} else {
_.assign(scope.database, {
connector: answers.client.connector,
settings: {
2019-04-30 16:15:12 +02:00
client: answers.client.database,
2019-02-01 14:56:46 +01:00
},
2019-04-30 16:15:12 +02:00
options: {},
2019-02-01 14:56:46 +01:00
});
}
scope.client = answers.client;
const asyncFn = [
new Promise(async resolve => {
const isMongo = scope.client.database === 'mongo';
const isSQLite = scope.database.settings.client === 'sqlite';
2019-04-30 16:15:12 +02:00
let answers = await inquirer.prompt([
{
when: !hasDatabaseConfig && !isSQLite,
type: 'input',
name: 'database',
message: 'Database name:',
default: _.get(scope.database, 'database', scope.name),
},
{
when: !hasDatabaseConfig && !isSQLite,
type: 'input',
name: 'host',
message: 'Host:',
default: _.get(scope.database, 'host', '127.0.0.1'),
},
{
when: !hasDatabaseConfig && isMongo,
type: 'boolean',
name: 'srv',
message: '+srv connection:',
default: _.get(scope.database, 'srv', false),
},
{
when: !hasDatabaseConfig && !isSQLite,
type: 'input',
name: 'port',
message: `Port${
isMongo ? ' (It will be ignored if you enable +srv)' : ''
}:`,
default: answers => {
// eslint-disable-line no-unused-vars
if (_.get(scope.database, 'port')) {
return scope.database.port;
2019-02-01 14:56:46 +01:00
}
2019-04-30 16:15:12 +02:00
const ports = {
mongo: 27017,
postgres: 5432,
mysql: 3306,
};
return ports[scope.client.database];
2019-02-01 14:56:46 +01:00
},
2019-04-30 16:15:12 +02:00
},
{
when: !hasDatabaseConfig && !isSQLite,
type: 'input',
name: 'username',
message: 'Username:',
default: _.get(scope.database, 'username', undefined),
},
{
when: !hasDatabaseConfig && !isSQLite,
type: 'password',
name: 'password',
message: 'Password:',
mask: '*',
default: _.get(scope.database, 'password', undefined),
},
{
when: !hasDatabaseConfig && isMongo,
type: 'input',
name: 'authenticationDatabase',
message: 'Authentication database (Maybe "admin" or blank):',
default: _.get(scope.database, 'authenticationDatabase', undefined),
},
{
when: !hasDatabaseConfig && !isSQLite,
type: 'boolean',
name: 'ssl',
message: 'Enable SSL connection:',
default: _.get(scope.database, 'ssl', false),
},
{
when: !hasDatabaseConfig && isSQLite && !isQuick,
type: 'input',
name: 'filename',
message: 'Filename:',
default: () => '.tmp/data.db',
},
]);
2018-06-21 15:07:50 +02:00
2019-02-01 14:56:46 +01:00
if (isQuick) {
answers.filename = '.tmp/data.db';
}
2019-02-01 14:56:46 +01:00
if (hasDatabaseConfig) {
2019-04-30 16:15:12 +02:00
answers = _.merge(
_.omit(scope.database.settings, ['client']),
scope.database.options
);
2019-02-01 14:56:46 +01:00
}
2019-02-01 14:56:46 +01:00
scope.database.settings.host = answers.host;
scope.database.settings.port = answers.port;
scope.database.settings.database = answers.database;
scope.database.settings.username = answers.username;
scope.database.settings.password = answers.password;
2019-02-01 14:56:46 +01:00
if (answers.filename) {
scope.database.settings.filename = answers.filename;
}
if (answers.srv) {
2019-04-30 16:15:12 +02:00
scope.database.settings.srv = _.toString(answers.srv) === 'true';
2019-02-01 14:56:46 +01:00
}
if (answers.authenticationDatabase) {
2019-04-30 16:15:12 +02:00
scope.database.options.authenticationDatabase =
answers.authenticationDatabase;
2019-02-01 14:56:46 +01:00
}
2019-02-15 22:22:13 +01:00
2019-02-01 14:56:46 +01:00
// SQLite requirements.
if (isSQLite) {
// Necessary for SQLite configuration (https://knexjs.org/#Builder-insert).
scope.database.options = {
2019-04-30 16:15:12 +02:00
useNullAsDefault: true,
2019-02-01 14:56:46 +01:00
};
}
2018-05-04 17:50:39 +02:00
2019-02-01 14:56:46 +01:00
if (answers.ssl && scope.client.database === 'mongo') {
scope.database.options.ssl = _.toString(answers.ssl) === 'true';
} else if (answers.ssl) {
scope.database.settings.ssl = _.toString(answers.ssl) === 'true';
}
2019-02-01 14:56:46 +01:00
console.log();
2019-04-17 11:24:22 +02:00
if (isQuick) {
console.log('✅ Connected to the database');
} else {
2019-04-30 16:15:12 +02:00
console.log(
'⏳ Testing database connection...\r\nIt might take a minute, please have a coffee ☕️'
);
2019-04-17 11:24:22 +02:00
}
2019-02-01 14:56:46 +01:00
resolve();
}),
new Promise(resolve => {
const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM();
2019-04-30 16:15:12 +02:00
let packageCmd = packageManager.commands(
'install --prefix',
scope.tmpPath
);
2019-02-01 14:56:46 +01:00
// Manually create the temp directory for yarn
if (!isStrapiInstalledWithNPM) {
2019-04-17 11:24:22 +02:00
fs.ensureDirSync(scope.tmpPath);
2019-02-01 14:56:46 +01:00
}
2019-04-30 16:15:12 +02:00
let cmd = `${packageCmd} ${scope.client.connector}@${
scope.strapiPackageJSON.version
}`;
2019-02-01 14:56:46 +01:00
if (scope.client.module) {
cmd += ` ${scope.client.module}`;
}
if (scope.client.connector === 'strapi-hook-bookshelf') {
cmd += ` strapi-hook-knex@${scope.strapiPackageJSON.version}`;
2019-04-30 16:15:12 +02:00
scope.additionalsDependencies = scope.additionalsDependencies.concat([
'strapi-hook-knex',
'knex',
]);
2019-02-01 14:56:46 +01:00
}
if (isQuick) {
scope.client.version = 'latest';
return resolve();
}
2019-04-30 16:15:12 +02:00
execa.shell(cmd).then(() => {
2019-02-01 14:56:46 +01:00
if (scope.client.module) {
2019-04-30 16:15:12 +02:00
const lock = require(path.join(
`${scope.tmpPath}`,
'/node_modules/',
`${scope.client.module}/package.json`
));
2019-02-01 14:56:46 +01:00
scope.client.version = lock.version;
}
2019-04-17 11:24:22 +02:00
resolve();
2019-02-01 14:56:46 +01:00
});
2019-04-30 16:15:12 +02:00
}),
2019-02-01 14:56:46 +01:00
];
const connectedToTheDatabase = (withMessage = true) => {
2019-02-15 22:22:13 +01:00
if (withMessage) {
console.log();
2019-04-30 16:15:12 +02:00
console.log(
`The app has been connected to the database ${green('successfully')}!`
);
}
2019-04-05 16:11:09 +02:00
if (isQuick) {
trackSuccess('didChooseQuickstart', scope);
} else {
trackSuccess('didChooseCustomDatabase', scope);
}
2019-02-15 22:22:13 +01:00
trackSuccess('didConnectDatabase', scope);
cb.success();
};
2019-04-30 16:15:12 +02:00
Promise.all(asyncFn).then(() => {
// Bypass real connection test.
if (isQuick) {
return connectedToTheDatabase(false);
}
2019-04-30 16:15:12 +02:00
try {
const connectivityFile = path.join(
scope.tmpPath,
'node_modules',
scope.client.connector,
'lib',
'utils',
'connectivity.js'
);
require(connectivityFile)(
scope,
connectedToTheDatabase,
connectionValidation
);
} catch (err) {
trackSuccess('didNotConnectDatabase', scope, err);
console.log(err);
rimraf.sync(scope.tmpPath);
cb.error();
}
});
2018-01-08 12:00:56 +01:00
};
connectionValidation();
2016-03-18 11:12:50 +01:00
};