Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpicaud committed Apr 4, 2016
2 parents 1fa50af + e512b00 commit 9552269
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 105 deletions.
225 changes: 122 additions & 103 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ var url = require('url');
var ProgressBar = require('progress');
var fs = require('fs');
var AdmZip = require('adm-zip');
var winston = require('winston');
var exec = require('child_process').exec;
var validator = require('validator');
var mysql = require('mysql');
var Q = require('q');
var path = require('path');

module.exports = yeoman.Base.extend({

Expand Down Expand Up @@ -243,122 +244,140 @@ module.exports = yeoman.Base.extend({
},

writing: function () {
winston.level = 'info';
var zipUrl = 'https://www.prestashop.com/download/old/prestashop_' + this.props.release + '.zip';
var zipName = url.parse(zipUrl).pathname.split('/').pop();
var req = request(zipUrl);
var bar;
var zipDestination = this.destinationPath('tmp');
var extractDestination = this.destinationPath('presta');
var finalName = this.destinationPath(zipName).slice(0, -4);
req
.on('data', function (chunk) {
bar = bar || new ProgressBar('Downloading... [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 50,
total: parseInt(req.response.headers['content-length'], 10)
var parent = this;

function download () {
var req = request(zipUrl);
var deferred = Q.defer();
req
.on('data', function (chunk) {
bar = bar || new ProgressBar('Downloading... [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 50,
total: parseInt(req.response.headers['content-length'], 10)
});
bar.tick(chunk.length);
})
.pipe(fs.createWriteStream(zipDestination))
.on('close', function () {
bar.tick(bar.total - bar.curr);
});

bar.tick(chunk.length);
})
.pipe(fs.createWriteStream(zipDestination))
.on('close', function (err) {
if (err) {
winston.log('error', err.toString());
}
bar.tick(bar.total - bar.curr);
winston.log('info', 'Extracting the archive... Don\'t your dare ^C !');
req.on('end', deferred.makeNodeResolver());
return deferred.promise;
}

function extract () {
var deferred = Q.defer();
console.log('Extracting the archive... Don\'t your dare ^C !');
var zip = new AdmZip(zipDestination);
zip.extractAllTo(extractDestination);
fs.unlink(zipDestination, function (err) {
fs.unlink(zipDestination, deferred.makeNodeResolver());
return deferred.promise;
}

function renameFolder () {
var deferred = Q.defer();
console.log('Cleanup everything...');
fs.rename(extractDestination + '/prestashop', finalName, deferred.makeNodeResolver());
return deferred.promise;
}

function removeZip () {
var deferred = Q.defer();
console.log('Removing zip...');
exec('rm -r ' + extractDestination, deferred.makeNodeResolver());
return deferred.promise;
}

function installPrestaShop () {
console.log('Installing PrestaShop...');
var deferred = Q.defer();
if (parent.props.release === '0.9.7') {
deferred.reject('This release does not have a CLI installer. Please ' +
'process the installation from your browser');
} else {
var installScript = finalName + '/install/index_cli.php';
var args = ' --domain=' + parent.props.storeDomain +
' --db_server=' + parent.props.dbServer +
' --db_user=' + parent.props.dbUser +
' --db_password=' + parent.props.dbPassword +
' --db_name=' + parent.props.dbName +
' --db_clear=' + Number(parent.props.dbClear).toString() +
' --prefix=' + parent.props.dbPrefix +
' --engine=' + parent.props.dbEngine +
' --name=' + parent.props.storeName +
' --password=' + parent.props.boPassword +
' --email=' + parent.props.boEmail +
' --newsletter=0';
exec('php ' + installScript + args, deferred.makeNodeResolver());
}
return deferred.promise;
}

function fixPhysicalUri () {
console.log('Put the physical uri in database...');
var deferred = Q.defer();
var physicalUri = '/prestashop_' + parent.props.release + '/';
var updateQuery = 'UPDATE ' + parent.props.dbPrefix + 'shop_url SET physical_uri=\'' + physicalUri + '\' WHERE id_shop=1';
var connection = mysql.createConnection({
host: parent.props.dbServer,
user: parent.props.dbUser,
password: parent.props.dbPassword,
database: parent.props.dbName
});
connection.connect();
connection.query(updateQuery, deferred.makeNodeResolver());
connection.end();
return deferred.promise;
}

function deleteInstallDir () {
var deferred = Q.defer();
console.log('Deleting the install directory...');
exec('rm -r ' + finalName + '/install', deferred.makeNodeResolver());
return deferred.promise;
}

function renameAdminDir () {
var deferred = Q.defer();
console.log('Renaming the admin directory...');
fs.rename(finalName + '/admin', finalName + '/backoffice', deferred.makeNodeResolver());
return deferred.promise;
}

function outputSummary () {
var deferred = Q.defer()
fs.readFile(parent.templatePath('ascii-puffin.txt'), 'utf8', function (err, data) {
if (err) {
winston.log('error', err.toString());
deferred.reject(err);
} else {
console.log(chalk.blue(data));
}
});
winston.log('info', 'Cleanup everything...');
fs.rename(extractDestination + '/prestashop', finalName, function (err) {
if (err) {
winston.log('error', err.toString());
} else {
exec('rm -r ' + extractDestination, function (err, stdout, stderr) {
if (err) {
winston.log('error', err.toString());
} else if (stdout) {
winston.log('error', stdout.toString());
} else if (stderr) {
winston.log('error', stderr.toString());
} else {
}
});
var installScript = this.destinationPath('prestashop_' + this.props.release + '/install/index_cli.php');
var args = ' --domain=' + this.props.storeDomain +
' --db_server=' + this.props.dbServer +
' --db_user=' + this.props.dbUser +
' --db_password=' + this.props.dbPassword +
' --db_name=' + this.props.dbName +
' --db_clear=' + Number(this.props.dbClear).toString() +
' --prefix=' + this.props.dbPrefix +
' --engine=' + this.props.dbEngine +
' --name=' + this.props.storeName +
' --password=' + this.props.boPassword +
' --email=' + this.props.boEmail +
' --newsletter=0';
winston.log('info', 'Installation in progress...');
exec('php ' + installScript + args, function (err, stdout, stderr) {
if (err) {
winston.log('error', err.toString());
}
if (stdout) {
winston.log('info', 'stdout: ' + stdout);
}
if (stderr) {
winston.log('info', 'stderr: ' + stderr);
}
var connection = mysql.createConnection({
host: this.props.dbServer,
user: this.props.dbUser,
password: this.props.dbPassword,
database: this.props.dbName
});
connection.connect();

winston.log('info', 'Putting the right physical URI into the database...');
var physicalUri = '/prestashop_' + this.props.release + '/';
connection.query('UPDATE ' + this.props.dbPrefix + 'shop_url SET physical_uri=\'' + physicalUri + '\' WHERE id_shop=1', function (err) {
if (err) {
winston.log('error', err.toString());
}
});
connection.end();
console.log(chalk.blue('BackOffice: http://' + parent.props.storeDomain + '/' + path.basename(finalName) + '/backoffice'));
console.log(chalk.blue('Login: ' + parent.props.boEmail));
console.log(chalk.blue('Password: ' + parent.props.boPassword));
deferred.resolve('ok');
return deferred.promise;
}

winston.log('info', 'removing the install directory...');
exec('rm -r ' + this.destinationPath('prestashop_' + this.props.release + '/install'), function (err, stdout, stderr) {
if (err) {
winston.log('error', err.toString());
}
if (stdout) {
winston.log('info', stdout);
}
if (stderr) {
winston.log('error', stderr);
}
});
winston.log('info', 'renaming the admin directory...');
fs.rename(finalName + '/admin', finalName + '/admin1234', function (err) {
if (err) {
winston.log('error', err.toString());
} else {
console.log(chalk.green('A new PrestaShop store is born!'));
console.log(chalk.blue('BackOffice: http://' + this.props.storeDomain));
console.log(chalk.blue('Login: ' + this.props.boEmail));
console.log(chalk.blue('Password: ' + this.props.boPassword));
}
}.bind(this));
}.bind(this));
}
}.bind(this));
}.bind(this));
Q().then(download)
.then(extract)
.then(renameFolder)
.then(removeZip)
.then(installPrestaShop)
.then(fixPhysicalUri)
.then(deleteInstallDir)
.then(renameAdminDir)
.then(outputSummary)
.catch(console.log);
}
});
11 changes: 11 additions & 0 deletions generators/app/templates/ascii-puffin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.--.
/ ,~a`-, A new PrestaShop store is born !
\ \_.-"`
) (
,/ ."\
/ ( |
/ ) ;
/ / /
,/_."` /`
/_/\ |___
`~~~~~`
Empty file.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generator-prestashop",
"version": "1.0.0",
"version": "1.0.1",
"description": "One-liner command to download and install any version of PrestaShop",
"homepage": "https://github.com/marcpicaud/generator-prestashop",
"author": {
Expand All @@ -21,6 +21,7 @@
"chalk": "^1.0.0",
"mysql": "^2.10.2",
"progress": "^1.1.8",
"q": "^1.4.1",
"request": "^2.69.0",
"validator": "^5.1.0",
"yeoman-generator": "^0.22.5",
Expand All @@ -38,7 +39,6 @@
"gulp-mocha": "^2.0.0",
"gulp-nsp": "^2.1.0",
"gulp-plumber": "^1.0.0",
"winston": "^2.2.0",
"yeoman-assert": "^2.0.0"
},
"eslintConfig": {
Expand Down

0 comments on commit 9552269

Please sign in to comment.