265 lines
7.4 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 exec = require('child_process').exec;
const execSync = require('child_process').execSync;
2016-03-18 11:12:50 +01:00
// Public node modules.
const _ = require('lodash');
const fs = require('fs-extra');
2018-01-05 15:17:59 +01:00
const inquirer = require('inquirer');
2018-01-19 15:41:01 +01:00
const shell = require('shelljs');
2016-03-18 11:12:50 +01:00
2016-03-25 22:22:34 +01:00
// Logger.
const logger = require('strapi-utils').logger;
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
*/
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, {
2017-06-08 17:16:20 +01: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 || '',
year: (new Date()).getFullYear(),
license: 'MIT',
database: {}
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 || '');
scope.tmpPath = path.resolve(process.cwd(), 'tmp');
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) {
2016-03-25 22:22:34 +01:00
return logger.error('`$ 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
// ...
}
2018-01-05 15:17:59 +01:00
logger.info('Let\s configurate the connection to your database:');
2018-01-08 12:00:56 +01:00
if (hasDatabaseConfig) {
logger.info(`Database determined by CLI args: ${scope.database.settings.client}`);
}
2018-01-08 12:00:56 +01:00
const connectionValidation = () => {
2018-01-10 18:08:43 +01:00
const databaseChoices = [
2018-01-10 14:45:32 +01:00
{
name: 'MongoDB (highly recommended)',
value: {
database: 'mongo',
connector: 'strapi-mongoose'
}
2018-01-10 14:45:32 +01:00
},
{
name: 'Postgres',
value: {
database: 'postgres',
connector: 'strapi-bookshelf',
module: 'pg'
}
},
{
name: 'MySQL',
value: {
database: 'mysql',
connector: 'strapi-bookshelf',
module: 'mysql'
}
},
{
name: 'Sqlite3',
value: {
database: 'sqlite3',
connector: 'strapi-bookshelf',
module: 'sqlite3'
}
},
{
name: 'Redis',
value: {
database: 'redis',
connector: 'strapi-redis'
}
}
];
inquirer
.prompt([
{
when: !hasDatabaseConfig,
2018-01-10 14:45:32 +01:00
type: 'list',
prefix: '',
name: 'client',
2018-01-10 18:08:43 +01:00
message: 'Choose your main database:',
choices: databaseChoices,
2018-01-10 14:45:32 +01:00
default: () => {
if (scope.client) {
2018-01-10 18:08:43 +01:00
return _.findIndex(databaseChoices, { value: _.omit(scope.client, ['version'])});
}
2018-01-08 12:00:56 +01:00
}
2018-01-10 14:45:32 +01:00
}
])
.then(answers => {
if (hasDatabaseConfig) {
const databaseChoice = _.find(databaseChoices, ['value.database', scope.database.settings.client]);
2018-02-07 20:37:33 +01:00
scope.database.connector = databaseChoice.value.connector;
answers.client = {
...databaseChoice.value
};
} else {
_.assign(scope.database, {
connector: answers.client.connector,
settings: {
client: answers.client.database
},
options: {}
});
}
2018-01-10 14:45:32 +01:00
scope.client = answers.client;
2018-01-08 12:00:56 +01:00
const asyncFn = [
new Promise(resolve => {
inquirer
.prompt([
{
when: !hasDatabaseConfig,
2018-01-08 12:00:56 +01:00
type: 'input',
prefix: '',
2018-02-08 00:50:16 +01:00
name: 'database',
2018-01-08 12:00:56 +01:00
message: 'Database name:',
default: _.get(scope.database, 'database', 'strapi')
2018-01-08 12:00:56 +01:00
},
{
when: !hasDatabaseConfig,
2018-01-08 12:00:56 +01:00
type: 'input',
prefix: '',
name: 'host',
message: 'Host:',
2018-02-06 14:28:12 +01:00
default: _.get(scope.database, 'host', '127.0.0.1')
2018-01-08 12:00:56 +01:00
},
{
when: !hasDatabaseConfig,
2018-01-08 12:00:56 +01:00
type: 'input',
prefix: '',
name: 'port',
message: 'Port:',
default: (answers) => {
if (_.get(scope.database, 'port')) {
return scope.database.port;
}
2018-01-08 12:00:56 +01:00
const ports = {
mongo: 27017,
postgres: 5432,
mysql: 3306,
sqlite3: 1433,
redis: 6379
};
return ports[scope.client.database];
}
},
{
when: !hasDatabaseConfig,
2018-01-08 12:00:56 +01:00
type: 'input',
prefix: '',
name: 'username',
message: 'Username:',
default: _.get(scope.database, 'username', undefined)
2018-01-08 12:00:56 +01:00
},
{
when: !hasDatabaseConfig,
2018-02-08 15:27:15 +01:00
type: 'password',
2018-01-08 12:00:56 +01:00
prefix: '',
name: 'password',
message: 'Password:',
2018-02-08 15:27:15 +01:00
mask: '*',
default: _.get(scope.database, 'password', undefined)
},
2018-03-08 15:51:03 +01:00
{
when: !hasDatabaseConfig && scope.client.database === 'mongo',
2018-03-08 15:51:03 +01:00
type: 'input',
prefix: '',
name: 'authenticationDatabase',
message: 'Authentication database:',
2018-03-13 09:19:54 +00:00
default: _.get(scope.database, 'authenticationDatabase', undefined)
2018-03-08 15:51:03 +01:00
},
{
when: !hasDatabaseConfig && scope.client.database === 'mongo',
type: 'boolean',
prefix: '',
name: 'ssl',
message: 'Enable SSL connection:',
default: _.get(scope.database, 'ssl', false)
}
2018-01-08 12:00:56 +01:00
])
.then(answers => {
if (hasDatabaseConfig) {
answers = _.omit(scope.database.settings, ['client'])
}
scope.database.settings.host = answers.host;
scope.database.settings.port = answers.port;
2018-02-08 00:50:16 +01:00
scope.database.settings.database = answers.database;
scope.database.settings.username = answers.username;
scope.database.settings.password = answers.password;
2018-03-08 15:51:03 +01:00
scope.database.settings.authenticationDatabase = answers.authenticationDatabase;
scope.database.settings.ssl = answers.ssl;
2018-01-08 12:00:56 +01:00
2018-01-10 14:45:32 +01:00
logger.info('Testing database connection...');
2018-01-08 12:00:56 +01:00
resolve();
});
}),
new Promise(resolve => {
let cmd = `npm install --prefix "${scope.tmpPath}" ${scope.client.connector}@alpha`;
if (scope.client.module) {
2018-01-08 12:00:56 +01:00
cmd += ` ${scope.client.module}`;
}
2018-01-08 12:00:56 +01:00
exec(cmd, () => {
if (scope.client.module) {
const lock = require(path.join(`${scope.tmpPath}`,`/node_modules/`,`${scope.client.module}/package.json`));
2018-01-08 12:00:56 +01:00
scope.client.version = lock.version;
}
2018-01-08 12:00:56 +01:00
resolve();
});
})
];
Promise.all(asyncFn)
.then(() => {
try {
require(path.join(`${scope.tmpPath}`,`/node_modules/`,`${scope.client.connector}/lib/utils/connectivity.js`))(scope, cb.success, connectionValidation);
} catch(err) {
shell.rm('-r', scope.tmpPath);
2018-01-08 12:00:56 +01:00
logger.info('Copying the dashboard...');
cb.success();
}
});
});
2018-01-08 12:00:56 +01:00
};
connectionValidation();
2016-03-18 11:12:50 +01:00
};