Error Fix and Console Logging format.
This commit is contained in:
+27
-24
@@ -1,7 +1,28 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const {google} = require('googleapis');
|
||||
const {applyRateLimitBuffer} = require('./rate');
|
||||
const {fetchSpreadsheetValuesBatch} = require('./sheets');
|
||||
|
||||
function getConfigBaseDir(config) {
|
||||
const configPath = config && (config.__configPath || config.configPath);
|
||||
if (configPath) {
|
||||
return path.dirname(configPath);
|
||||
}
|
||||
|
||||
return process.cwd();
|
||||
}
|
||||
|
||||
function resolveConfigPath(config, candidatePath) {
|
||||
if (!candidatePath) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (path.isAbsolute(candidatePath)) {
|
||||
return candidatePath;
|
||||
}
|
||||
|
||||
return path.resolve(getConfigBaseDir(config), candidatePath);
|
||||
}
|
||||
|
||||
function loadApiKeys(config) {
|
||||
const apiKeys = [];
|
||||
@@ -60,7 +81,7 @@ function createServiceAccountBatchFetcher(config) {
|
||||
// selecting a credential whose slot is available now. If none are
|
||||
// immediately available, wait the minimum required time.
|
||||
const fetchers = accounts.map(({path: credentialsPath, rateLimitPerMinute}) => {
|
||||
const resolvedPath = path.isAbsolute(credentialsPath) ? credentialsPath : path.resolve(process.cwd(), credentialsPath);
|
||||
const resolvedPath = resolveConfigPath(config, credentialsPath);
|
||||
if (!fs.existsSync(resolvedPath)) {
|
||||
throw new Error(`Service account credentials not found: ${resolvedPath}`);
|
||||
}
|
||||
@@ -84,16 +105,7 @@ function createServiceAccountBatchFetcher(config) {
|
||||
const interval = Math.ceil(60000 / bufferedRateLimit);
|
||||
|
||||
const fetchRaw = async (documentId, sheetNames) => {
|
||||
const sheets = google.sheets({version: 'v4', auth: authClient});
|
||||
const request = {spreadsheetId: documentId, ranges: sheetNames};
|
||||
const response = await sheets.spreadsheets.values.batchGet(request);
|
||||
const valueRanges = response.data.valueRanges || [];
|
||||
const rowsBySheet = {};
|
||||
for (let i = 0; i < sheetNames.length; i += 1) {
|
||||
const sheetName = sheetNames[i];
|
||||
rowsBySheet[sheetName] = valueRanges[i]?.values || [];
|
||||
}
|
||||
return rowsBySheet;
|
||||
return fetchSpreadsheetValuesBatch({authClient}, documentId, sheetNames);
|
||||
};
|
||||
|
||||
return {authClient, fetchRaw, interval, nextAvailable: 0};
|
||||
@@ -148,16 +160,7 @@ function createApiKeyBatchFetcher(config) {
|
||||
const interval = Math.ceil(60000 / bufferedRateLimit);
|
||||
|
||||
const fetchRaw = async (documentId, sheetNames) => {
|
||||
const sheets = google.sheets({version: 'v4'});
|
||||
const request = {spreadsheetId: documentId, ranges: sheetNames, key};
|
||||
const response = await sheets.spreadsheets.values.batchGet(request);
|
||||
const valueRanges = response.data.valueRanges || [];
|
||||
const rowsBySheet = {};
|
||||
for (let i = 0; i < sheetNames.length; i += 1) {
|
||||
const sheetName = sheetNames[i];
|
||||
rowsBySheet[sheetName] = valueRanges[i]?.values || [];
|
||||
}
|
||||
return rowsBySheet;
|
||||
return fetchSpreadsheetValuesBatch({apiKey: key}, documentId, sheetNames);
|
||||
};
|
||||
|
||||
return {key, fetchRaw, interval, nextAvailable: 0};
|
||||
@@ -215,7 +218,7 @@ function getFirstServiceAccountAuthClient(config) {
|
||||
const credentialsPath = entryObj.path || entryObj.credentialsPath;
|
||||
if (!credentialsPath) return null;
|
||||
|
||||
const resolvedPath = path.isAbsolute(credentialsPath) ? credentialsPath : path.resolve(process.cwd(), credentialsPath);
|
||||
const resolvedPath = resolveConfigPath(config, credentialsPath);
|
||||
if (!fs.existsSync(resolvedPath)) return null;
|
||||
|
||||
try {
|
||||
@@ -235,7 +238,7 @@ function loadAuth(config) {
|
||||
|
||||
if (config.credentialsPath) {
|
||||
const credentialsPath = config.credentialsPath;
|
||||
const resolvedPath = path.isAbsolute(credentialsPath) ? credentialsPath : path.resolve(process.cwd(), credentialsPath);
|
||||
const resolvedPath = resolveConfigPath(config, credentialsPath);
|
||||
if (!fs.existsSync(resolvedPath)) {
|
||||
throw new Error(`Credentials file not found: ${resolvedPath}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user