Skip to content

Commit

Permalink
CLI Preview command: Added support for passing document URL (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
aumkar authored Jun 17, 2024
1 parent 95a80e2 commit 5a6bbd9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ $ pcc site webhooks history 123456789example1234
# Generate preview link for given document ID
$ pcc document preview 1234567890example1234567890exam_ple123456789

# Generate preview link for given document URL
$ pcc document preview https://docs.google.com/document/d/1234567890example1234567890exam_ple123456789

# Get details of logged-in user
$ pcc whoami

Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/cli/commands/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ type GeneratePreviewParam = {
baseUrl: string;
};

const GDOCS_URL_REGEX =
/^(https|http):\/\/(www.)?docs.google.com\/document\/d\/(?<id>[^/]+).*$/;

export const generatePreviewLink = errorHandler<GeneratePreviewParam>(
async ({ documentId, baseUrl }: GeneratePreviewParam) => {
const logger = new Logger();

let cleanedId = documentId.trim();
const match = cleanedId.match(GDOCS_URL_REGEX);
if (match?.groups?.id) cleanedId = match.groups.id;

if (baseUrl) {
try {
new URL(baseUrl);
Expand All @@ -38,7 +45,7 @@ export const generatePreviewLink = errorHandler<GeneratePreviewParam>(
const generateLinkLogger = new SpinnerLogger("Generating preview link");
generateLinkLogger.start();

const previewLink = await AddOnApiHelper.previewFile(documentId, {
const previewLink = await AddOnApiHelper.previewFile(cleanedId, {
baseUrl,
});

Expand All @@ -54,4 +61,9 @@ export const DOCUMENT_EXAMPLES = [
description: "Generate preview link for given document ID",
command: "$0 document preview 1234567890example1234567890exam_ple123456789",
},
{
description: "Generate preview link for given document URL",
command:
"$0 document preview https://docs.google.com/document/d/1234567890example1234567890exam_ple123456789",
},
];
2 changes: 1 addition & 1 deletion packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ yargs(hideBin(process.argv))
yargs
.strictCommands()
.positional("<id>", {
describe: "ID of the document.",
describe: "ID or URL of the document.",
demandOption: true,
type: "string",
})
Expand Down

0 comments on commit 5a6bbd9

Please sign in to comment.