Skip to content

Commit

Permalink
Merge pull request #45 from pipocadigital/fix-issue-39
Browse files Browse the repository at this point in the history
handle errors after `unlink` the zip
  • Loading branch information
alisonmonteiro authored May 4, 2018
2 parents 44806e4 + d7c45c6 commit 0a95b91
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions tasks/wp-install.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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}`));
});
}
});

0 comments on commit 0a95b91

Please sign in to comment.