diff --git a/install-wp-tests.js b/install-wp-tests.js index fd68a23..00ed152 100644 --- a/install-wp-tests.js +++ b/install-wp-tests.js @@ -23,7 +23,7 @@ const tmpDir = await doesFileExist( '/tmp' ) ? '/tmp' : await fs.mkdtemp( os.tmpdir() ); const wpTestsDir = `${ tmpDir }/wordpress-tests-lib`; - const wpCoreDir = `${ tmpDir }/wordpress/`; + const wpCoreDir = `${ tmpDir }/wordpress`; /** * @async @@ -46,7 +46,7 @@ } await download( `https://wordpress.org/${ archiveName }.zip`, `${ tmpDir }/wordpress.zip` ); - await unzip( `${ tmpDir }/wordpress.zip`, wpCoreDir ); + await unzip( `${ tmpDir }/wordpress.zip`, tmpDir ); await download( 'https://raw.github.com/markoheijnen/wp-mysqli/master/db.php', `${ wpCoreDir }/wp-content/db.php` ); }; diff --git a/package.json b/package.json index 4b55e51..59a6a45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@godaddy-wordpress/scripts", - "version": "0.3.2", + "version": "0.3.3", "description": "Collection of scripts used for management of CI pipelines as well as general development scripts.", "homepage": "https://github.com/godaddy-wordpress/scripts", "repository": "godaddy-wordpress/scripts", diff --git a/utils/index.js b/utils/index.js index ba9d3e9..63e842c 100644 --- a/utils/index.js +++ b/utils/index.js @@ -4,6 +4,7 @@ const { createWriteStream } = require( 'fs' ); const { constants } = require( 'fs' ); const { pipeline } = require( 'stream' ); const { promisify } = require( 'util' ); +const path = require( 'path' ); const AdmZip = require( 'adm-zip' ); const fs = require( 'fs' ).promises; @@ -21,10 +22,11 @@ const RESET = '\x1b[0m'; /** * @function download File downloader. - * @param {string} url The URL to download. - * @param {string} path The path to save the file to. + * @param {string} url The URL to download. + * @param filePath + * @param {string} path The path to save the file to. */ -const download = async ( url, path ) => { +const download = async ( url, filePath ) => { const streamPipeline = promisify( pipeline ); const response = await fetch( url, { headers: { @@ -37,26 +39,33 @@ const download = async ( url, path ) => { throw new Error( `unexpected response ${ response.statusText }` ); } - await streamPipeline( response.body, createWriteStream( path ) ); + const checkPath = path.dirname( filePath ); + const pathExists = await doesFileExist( checkPath ); + + if ( ! pathExists ) { + await fs.mkdir( checkPath, { recursive: true } ); + } + + await streamPipeline( response.body, createWriteStream( filePath ) ); }; /** * @function unzip Extract a zip file to a destination. * @param {*} file - * @param {string} path + * @param {string} filePath */ -const unzip = async ( file, path ) => { +const unzip = async ( file, filePath ) => { const zip = new AdmZip( file ); - zip.extractAllTo( path, true ); + zip.extractAllTo( filePath, true ); }; /** * @function doesFileExist Async check if a inode exists and handle error messages. - * @param {path} path Path to the inode to check + * @param {path} filePath Path to the inode to check * @return {boolean} True if the folder exists false otherwise. */ -const doesFileExist = async ( path ) => { - const pathWithoutTrailingSlash = path.replace( /\/$/, '' ); +const doesFileExist = async ( filePath ) => { + const pathWithoutTrailingSlash = filePath.replace( /\/$/, '' ); // Access has no return value if file exists. // Catch error if occurs otherwise the file exists. const fileCheck = await fs.access( pathWithoutTrailingSlash, constants.F_OK ).catch( () => false );