2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
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-25 22:22:34 +01:00
|
|
|
let defaultName = scope.name;
|
2016-03-18 11:12:50 +01:00
|
|
|
if (defaultName === '.' || !defaultName) {
|
|
|
|
defaultName = path.basename(process.cwd());
|
|
|
|
}
|
|
|
|
|
|
|
|
// App info.
|
|
|
|
_.defaults(scope, {
|
|
|
|
name: defaultName,
|
|
|
|
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
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger callback with no error to proceed.
|
|
|
|
return cb.success();
|
|
|
|
};
|