2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const path = require('path');
|
2018-01-05 17:40:56 +01:00
|
|
|
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');
|
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
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
module.exports = (scope, cb) => {
|
2016-03-18 11:12:50 +01:00
|
|
|
// App info.
|
|
|
|
_.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(),
|
2016-03-25 22:22:34 +01:00
|
|
|
license: 'MIT'
|
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 || '');
|
|
|
|
|
|
|
|
// Ensure we aren't going to inadvertently delete any files.
|
|
|
|
try {
|
|
|
|
const files = fs.readdirSync(scope.rootPath);
|
|
|
|
if (files.length) {
|
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
|
|
|
|
2018-01-08 12:24:51 +01:00
|
|
|
scope.database = {};
|
|
|
|
|
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-08 12:24:51 +01:00
|
|
|
}
|
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([
|
|
|
|
{
|
|
|
|
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:24:51 +01:00
|
|
|
}
|
2018-01-08 12:00:56 +01:00
|
|
|
}
|
2018-01-10 14:45:32 +01:00
|
|
|
}
|
|
|
|
])
|
|
|
|
.then(answers => {
|
|
|
|
scope.client = answers.client;
|
|
|
|
_.assign(scope.database, {
|
|
|
|
connector: answers.client.connector,
|
|
|
|
settings: {
|
|
|
|
client: answers.client.database
|
|
|
|
},
|
|
|
|
options: {}
|
2018-01-08 12:00:56 +01:00
|
|
|
});
|
2018-01-05 17:40:56 +01:00
|
|
|
|
2018-01-08 12:00:56 +01:00
|
|
|
const asyncFn = [
|
|
|
|
new Promise(resolve => {
|
|
|
|
inquirer
|
|
|
|
.prompt([
|
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
prefix: '',
|
|
|
|
name: 'name',
|
|
|
|
message: 'Database name:',
|
2018-01-08 12:24:51 +01:00
|
|
|
default: _.get(scope.database, 'database', 'strapi')
|
2018-01-08 12:00:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
prefix: '',
|
|
|
|
name: 'host',
|
|
|
|
message: 'Host:',
|
2018-01-08 12:24:51 +01:00
|
|
|
default: _.get(scope.database, 'host', 'localhost')
|
2018-01-08 12:00:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
prefix: '',
|
|
|
|
name: 'port',
|
|
|
|
message: 'Port:',
|
|
|
|
default: (answers) => {
|
2018-01-08 12:24:51 +01:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
prefix: '',
|
|
|
|
name: 'username',
|
2018-01-08 12:24:51 +01:00
|
|
|
message: 'Username:',
|
|
|
|
default: _.get(scope.database, 'username', undefined)
|
2018-01-08 12:00:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'input',
|
|
|
|
prefix: '',
|
|
|
|
name: 'password',
|
2018-01-08 12:24:51 +01:00
|
|
|
message: 'Password:',
|
|
|
|
default: _.get(scope.database, 'password', undefined)
|
2018-01-05 17:40:56 +01:00
|
|
|
}
|
2018-01-08 12:00:56 +01:00
|
|
|
])
|
|
|
|
.then(answers => {
|
2018-01-10 15:31:54 +01:00
|
|
|
scope.database.settings.host = answers.host;
|
|
|
|
scope.database.settings.port = answers.port;
|
|
|
|
scope.database.settings.database = answers.name;
|
|
|
|
scope.database.settings.username = answers.username;
|
|
|
|
scope.database.settings.password = answers.password;
|
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 => {
|
2018-01-10 18:48:35 +01:00
|
|
|
let cmd = `npm install --prefix ${scope.rootPath} ${scope.client.connector}@alpha`;
|
2018-01-05 17:40:56 +01:00
|
|
|
if (scope.client.module) {
|
2018-01-08 12:00:56 +01:00
|
|
|
cmd += ` ${scope.client.module}`;
|
2018-01-05 17:40:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-08 12:00:56 +01:00
|
|
|
exec(cmd, () => {
|
|
|
|
if (scope.client.module) {
|
2018-01-10 18:48:35 +01:00
|
|
|
const lock = require(`${scope.rootPath}/node_modules/${scope.client.module}/package.json`);
|
2018-01-08 12:00:56 +01:00
|
|
|
scope.client.version = lock.version;
|
|
|
|
}
|
2018-01-05 17:40:56 +01:00
|
|
|
|
2018-01-08 12:00:56 +01:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
|
|
|
Promise.all(asyncFn)
|
|
|
|
.then(() => {
|
2018-01-10 15:31:54 +01:00
|
|
|
try {
|
2018-01-10 18:48:35 +01:00
|
|
|
require(`${scope.rootPath}/node_modules/${scope.client.connector}/lib/utils/connectivity.js`)(scope, cb.success, connectionValidation);
|
2018-01-10 15:31:54 +01:00
|
|
|
} catch(err) {
|
2018-01-10 18:48:35 +01:00
|
|
|
execSync(`rm -r ${scope.rootPath}`);
|
2018-01-08 12:00:56 +01:00
|
|
|
|
|
|
|
logger.info('Copying the dashboard...');
|
|
|
|
|
|
|
|
cb.success();
|
|
|
|
}
|
|
|
|
});
|
2018-01-05 17:40:56 +01:00
|
|
|
});
|
2018-01-08 12:00:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
connectionValidation();
|
2016-03-18 11:12:50 +01:00
|
|
|
};
|