Skip to content

Commit

Permalink
Improve output (and suppress GaxiosError) on failed push (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nu11u5 authored Dec 27, 2023
1 parent cf2f91e commit 1931c5f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"find-up": "^6.0.0",
"fs-extra": "^10.0.0",
"fuzzy": "^0.1.3",
"gaxios": "^4.2.1",
"google-auth-library": "^7.6.2",
"googleapis": "^84.0.0",
"inquirer": "^8.1.2",
Expand Down
58 changes: 52 additions & 6 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import chalk from 'chalk';
import fs from 'fs-extra';
import makeDir from 'make-dir';
import multimatch from 'multimatch';
import path from 'path';
import pMap from 'p-map';
import recursive from 'recursive-readdir';
import typescript from 'typescript';
import {GaxiosError} from 'gaxios';

import {loadAPICredentials, script} from './auth.js';
import {ClaspError} from './clasp-error.js';
Expand Down Expand Up @@ -376,17 +378,61 @@ export const pushFiles = async (silent = false) => {
// Start pushing.
try {
await script.projects.updateContent({scriptId, requestBody: {scriptId, files}});
} catch (error) {
console.error(LOG.PUSH_FAILURE);
console.error(error);
} finally {
stopSpinner();

// No error
stopSpinner();
if (!silent) {
logFileList(filenames);
console.log(LOG.PUSH_SUCCESS(filenames.length));
}
} catch (error) {
stopSpinner();
console.error(LOG.PUSH_FAILURE);
if (error instanceof GaxiosError) {
let message = error.message;
let snippet = '';
const re = /Syntax error: (.+) line: (\d+) file: (.+)/;
const [, errorName, lineNum, fileName] = re.exec(error.message) ?? [];
if (fileName !== undefined) {
let filePath = path.resolve(rootDir ?? '.', fileName);
const parsedFilePath = path.parse(filePath);
// Check if the file exists locally as any supported type
const {fileExtension} = await getProjectSettings();
const extensions = ['gs', 'js', 'ts'];
if (fileExtension !== undefined) extensions.push(fileExtension);
for (const ext of extensions) {
const filePath_ext = path.join(parsedFilePath.dir, `${parsedFilePath.name}.${ext}`);
if (fs.existsSync(filePath_ext)) {
filePath = filePath_ext;
break;
}
}
message = `${errorName} - "${filePath}:${lineNum}"`;

// Get formatted code snippet
const contextCount = 4;
const parsedFileName = path.parse(fileName);
const fileNameKey = path.join(parsedFileName.dir, parsedFileName.name);
const reqFiles: ProjectFile[] = JSON.parse(error.config.body).files;
const errFile = reqFiles.find((x: ProjectFile) => x.name === fileNameKey && x.type === 'SERVER_JS');
if (errFile !== undefined) {
const srcLines = errFile.source.split('\n');

const errIndex = Math.max(parseInt(lineNum) - 1, 0);
const preIndex = Math.max(errIndex - contextCount, 0);
const postIndex = Math.min(errIndex + contextCount + 1, srcLines.length);

const preLines = chalk.dim(` ${srcLines.slice(preIndex, errIndex).join('\n ')}`);
const errLine = chalk.bold(`⇒ ${srcLines[errIndex]}`);
const postLines = chalk.dim(` ${srcLines.slice(errIndex + 1, postIndex).join('\n ')}`);

snippet = preLines + '\n' + errLine + '\n' + postLines;
}
}
console.error(chalk.red(message));
console.log(snippet);
} else {
console.error(error);
}
}
} else {
stopSpinner();
Expand Down

0 comments on commit 1931c5f

Please sign in to comment.