221 lines
5.7 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.
_.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
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-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: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-08 12:00:56 +01:00
const asyncFn = [
new Promise(resolve => {
inquirer
.prompt([
{
type: 'input',
prefix: '',
name: 'name',
message: 'Database name:',
default: _.get(scope.database, 'database', 'strapi')
2018-01-08 12:00:56 +01:00
},
{
type: 'input',
prefix: '',
name: 'host',
message: 'Host:',
default: _.get(scope.database, 'host', 'localhost')
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];
}
},
{
type: 'input',
prefix: '',
name: 'username',
message: 'Username:',
default: _.get(scope.database, 'username', undefined)
2018-01-08 12:00:56 +01:00
},
{
type: 'input',
prefix: '',
name: 'password',
message: 'Password:',
default: _.get(scope.database, 'password', undefined)
}
2018-01-08 12:00:56 +01:00
])
.then(answers => {
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`;
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) {
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-08 12:00:56 +01:00
resolve();
});
})
];
Promise.all(asyncFn)
.then(() => {
try {
require(path.resolve(`${scope.rootPath}/node_modules/${scope.client.connector}/lib/utils/connectivity.js`))(scope, cb.success, connectionValidation);
} catch(err) {
2018-01-19 15:41:01 +01:00
shell.rm('-r', scope.rootPath);
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
};