UNPKG

@gravityforms/gulp-tasks

Version:
112 lines (92 loc) 3.2 kB
#!/usr/bin/env node const fs = require( 'fs/promises' ); const { UtilsGDrive } = require( 'utils-google-drive' ); const { loadEnv } = require( '../utils/tools' ); const getGoogleDriveConfig = require( '../utils/get-google-drive-config' ); const sendSlackMessage = require( '../utils/send-slack-message' ); const getConfig = require('../../config'); const { config } = getConfig(); loadEnv( config.paths.root ); const uploadToDrive = async () => { // Check if not in production mode and exit early if ( process.env.GULP_PRODUCTION_MODE !== 'true' ) { console.log( 'Skipping uploading to Google Drive because GULP_PRODUCTION_MODE is not set to true.' ); return; } const { files = [], path: uploadPath = '', } = await getGoogleDriveConfig(); const credentials = process.env.GOOGLE_DRIVE_CREDENTIALS; const token = process.env.GOOGLE_DRIVE_TOKEN; const sharedDriveId = process.env.GOOGLE_DRIVE_SHARED_ID; if ( ! credentials || ! token || ! sharedDriveId || ! uploadPath || ! files.length ) { console.log( 'Missing one or more of: google drive credentials, token, files, uploadPath, or driveId, aborting.' ); process.exit( 0 ); return; } const utilsGDrive = new UtilsGDrive( { credentials, token, } ); try { const parentFileId = await utilsGDrive.makeNestedFolder( { folderPath: uploadPath, options : { isSharedDrive: true, sharedDriveId, }, } ); let checksumFolderId = ''; const uploadPromises = []; let firstFileId = ''; // Upload the first file separately to ensure its ID is captured. if ( files.length ) { const firstFilePath = files[ 0 ]; await fs.access( firstFilePath ); // Check if the first file exists firstFileId = await utilsGDrive.upload( { localPath: firstFilePath, parentIdentifiers: parentFileId, sharedDriveId, update: true, } ); } await fs.writeFile( 'process_data.json', JSON.stringify( { googleDriveLink: `https://drive.google.com/file/d/${ firstFileId }/view?usp=sharing` } ), 'utf8' ); // Upload the remaining files for ( const filePath of files.slice( 1 ) ) { // Skip the first file let currentParentId = parentFileId; // Check for 'checksums' in the file path if ( filePath.includes( 'checksums/' ) ) { if ( ! checksumFolderId ) { // Create or get the 'checksums' folder within the main folder checksumFolderId = await utilsGDrive.makeNestedFolder( { folderPath: `${ uploadPath }/checksums`, options : { isSharedDrive: true, sharedDriveId, }, } ); } currentParentId = checksumFolderId; } try { await fs.access( filePath ); const uploadPromise = utilsGDrive.upload( { localPath: filePath, parentIdentifiers: currentParentId, sharedDriveId, update: true, } ); uploadPromises.push( uploadPromise ); } catch ( err ) { console.warn( `File ${ filePath } does not exist or couldn't be read.` ); } } await Promise.all( uploadPromises ); } catch ( err ) { console.error( 'An error occurred:', err ); await sendSlackMessage( `Google drive upload failed during build: ${ err.message }`, 'error' ); process.exit( 1 ); } }; uploadToDrive();