UNPKG

@gravityforms/gulp-tasks

Version:
117 lines (116 loc) 5.05 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkFileExistence = exports.checkFolderExistence = exports.overwrite = exports.resolveIdString = exports.resolveId = void 0; const path = require("path"); const utilsGDriveError_1 = require("./utilsGDriveError"); function resolveId(utilsGDrive, identifiers) { // default to root if (!identifiers) return 'root'; // handle string if (typeof identifiers === 'string') return resolveIdString(utilsGDrive, identifiers); // pass fileId through if already specified if (identifiers.fileId) return identifiers.fileId; // validate identifiers const validIdentifiers = ['fileId', 'fileName', 'parentId', 'parentName']; for (const identifier of Object.keys(identifiers)) { if (!validIdentifiers.includes(identifier)) { throw new utilsGDriveError_1.UtilsGDriveError(`Invalid property name: ${identifier}`); } } return utilsGDrive.getFileId(identifiers); } exports.resolveId = resolveId; async function resolveIdString(utilsGDrive, str) { const names = str.split(path.sep); if (names.length === 1) return names[0]; let currentId = await utilsGDrive.getFileId({ fileName: names[0] }); for (const name of names.slice(1)) { currentId = await utilsGDrive.getFileId({ fileName: name, parentId: currentId, }); } return currentId; } exports.resolveIdString = resolveIdString; async function overwrite(utilsGDrive, fileMetadata) { const { name, mimeType, parents } = fileMetadata; const q = `name='${name}' and mimeType='${mimeType}' and '${parents[0]}' in parents and trashed=false`; const data = await utilsGDrive.listFiles({ q }); if (data) { if (data.files.length > 0) { await utilsGDrive.del(data.files[0].id); } } } exports.overwrite = overwrite; /** * Check if a folder with a given name exists under a parent folder. * @param utilsGDrive The UtilsGDrive instance. * @param folderName The name of the folder to check. * @param parentId The ID of the parent folder. * @param isSharedDrive Whether or not to check in a shared drive. * @param sharedDriveId Optional ID of the shared drive. * @returns Promise that resolves to the folder ID if it exists, null otherwise. */ async function checkFolderExistence(utilsGDrive, folderName, parentId, isSharedDrive, sharedDriveId) { var _a; // Base query string, taking into account MIME type for folders and trashed state let q = `name='${folderName}' and mimeType='application/vnd.google-apps.folder' and trashed=false`; // If a parent ID is given, add it to the query if (parentId) { q += ` and '${parentId}' in parents`; } // If it's a shared drive, include the shared drive ID in the API call const options = { q, supportsAllDrives: true, // Needed for shared drive support }; if (isSharedDrive && sharedDriveId) { options.corpora = 'drive'; options.driveId = sharedDriveId; options.includeItemsFromAllDrives = true; // To search across all drives } // Search for folders that match the query const data = await utilsGDrive.listFiles(options); // Return the ID of the first match, or null if no matches found return ((_a = data === null || data === void 0 ? void 0 : data.files) === null || _a === void 0 ? void 0 : _a.length) > 0 ? data.files[0].id : null; } exports.checkFolderExistence = checkFolderExistence; /** * Check if a file with a given name exists under a parent folder. * @param utilsGDrive The UtilsGDrive instance. * @param fileName The name of the file to check. * @param parentId The ID of the parent folder. * @param sharedDriveId Optional ID of the shared drive. * @returns Promise that resolves to the file ID if it exists, null otherwise. */ async function checkFileExistence(utilsGDrive, fileName, parentId, sharedDriveId) { var _a; // Base query string, taking into account the name and trashed state let q = `name='${fileName}' and trashed=false`; // If a parent ID is given, add it to the query if (parentId) { q += ` and '${parentId}' in parents`; } // Options object to be passed to the API call const options = { q, supportsAllDrives: true, // Needed for shared drive support }; // If it's a shared drive, include additional parameters if (sharedDriveId) { options.corpora = 'drive'; options.driveId = sharedDriveId; options.includeItemsFromAllDrives = true; // To search across all drives } // Search for files that match the query const data = await utilsGDrive.listFiles(options); // Return the ID of the first match, or null if no matches found return ((_a = data === null || data === void 0 ? void 0 : data.files) === null || _a === void 0 ? void 0 : _a.length) > 0 ? data.files[0].id : null; } exports.checkFileExistence = checkFileExistence;