@gravityforms/gulp-tasks
Version:
Configurable Gulp tasks for use in Gravity Forms projects.
60 lines (59 loc) • 1.9 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFileId = void 0;
const path = require("path");
const utilsGDriveError_1 = require("../utils/utilsGDriveError");
const utilsMethods_1 = require("../utils/utilsMethods");
/**
* Get the id of a file or folder in Google Drive.
*/
async function getFileId(identifiers, options = {}) {
let fileName;
let parentId;
let parentName;
if (typeof identifiers === 'string') {
if (identifiers.match(path.sep)) {
return await (0, utilsMethods_1.resolveIdString)(this, identifiers);
}
else {
fileName = identifiers;
}
}
else {
({ fileName, parentId, parentName } = identifiers);
}
let q = `name="${fileName}"`;
if (parentId !== null && parentId !== void 0 ? parentId : parentName) {
let p;
if (parentId) {
p = parentId;
}
else if (parentName) {
p = await this.getFileId(parentName);
}
q += ` and "${p}" in parents`;
}
const listParams = {
q,
fields: 'files(id)',
supportsAllDrives: true,
includeItemsFromAllDrives: true // Add this line
};
if (options.isSharedDrive) {
listParams.corpora = 'drive';
if (options.sharedDriveId) {
listParams.driveId = options.sharedDriveId;
}
}
const data = await this.listFiles(listParams);
const filesData = data.files;
const nFiles = filesData.length;
if (nFiles === 0) {
throw new utilsGDriveError_1.UtilsGDriveError(`No files found matching identifiers specified: ${fileName}.`);
}
else if (nFiles > 1) {
throw new utilsGDriveError_1.UtilsGDriveError(`Multiple files found: ${fileName}. Consider specifying parent.`);
}
return filesData[0].id;
}
exports.getFileId = getFileId;
;