diff --git a/tasks/wp-install.js b/tasks/wp-install.js index c9bf10c..ea4cc60 100644 --- a/tasks/wp-install.js +++ b/tasks/wp-install.js @@ -1,16 +1,16 @@ +const fs = require('fs'); const gulp = require('gulp'); const request = require('request'); -const fs = require('fs'); const admZip = require('adm-zip'); const readlineSync = require('readline-sync'); + const helper = require('./helpers'); gulp.task('wp-install', function() { const packageJson = gulp.config.packageJson; - const wpIsInitialized = helper.fileExists('./wordpress/index.php'); const projectName = process.argv.splice(process.argv.indexOf('--name'))[1]; - if(packageJson.name === 'grao-de-milho' || packageJson.name === '') { + if (packageJson.name === 'grao-de-milho' || packageJson.name === '') { helper.log('Set the project name using gulp init --name "[Project Name]".', 'danger'); helper.log('Stoping...', 'danger'); process.exit(1); @@ -20,39 +20,51 @@ gulp.task('wp-install', function() { const latestWp = 'https://wordpress.org/latest.zip'; const outputZip = 'latest.zip'; - const wpConfigs = [ - 'wp-config.php', - 'wp-config-development.php', - 'wp-config-production.php', - 'wp-config-staging.php', - ]; request({ url: latestWp, encoding: null - }, function(reqError, _, data) { - if(reqError) { - throw reqError; + }, function(err, _, data) { + if (err) { + throw err; } - fs.writeFile(outputZip, data, error => { - if(error) { - throw error; + fs.writeFile(outputZip, data, err => { + if (err) { + throw err; } - helper.log('Unzipping WordPress...', 'success'); - const zip = new admZip(outputZip); + + helper.log('Unzipping WordPress...', 'success'); zip.extractAllTo('./'); - fs.unlink(outputZip); - helper.log('Coping wp-configs...', 'success'); + fs.unlink(outputZip, (err) => { + if (err) { + throw err; + } - wpConfigs.map(wpConfig => { - fs.createReadStream(`./tasks/config/wordpress/${wpConfig}`) - .pipe(fs.createWriteStream(`./wordpress/${wpConfig}`)); - }) + helper.log(`Removing ${outputZip}...`, ''); + }); + + copyWpConfigs(); }); }); + + function copyWpConfigs() { + helper.log('Copying wp-configs...', ''); + + const files = [ + 'wp-config.php', + 'wp-config-development.php', + 'wp-config-production.php', + 'wp-config-staging.php', + ]; + + files.map(file => { + fs.createReadStream(`./tasks/config/wordpress/${file}`) + .pipe(fs.createWriteStream(`./wordpress/${file}`)); + }); + } });