@pipedream/google_drive 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Mustaches from "google-docs-mustaches";
|
|
2
2
|
import googleDrive from "../../google_drive.app.mjs";
|
|
3
|
+
import { parseObjectEntries } from "../../common/utils.mjs";
|
|
3
4
|
|
|
4
5
|
const MODE_GOOGLE_DOC = "Google Doc";
|
|
5
6
|
const MODE_PDF = "Pdf";
|
|
@@ -8,7 +9,7 @@ export default {
|
|
|
8
9
|
key: "google_drive-create-file-from-template",
|
|
9
10
|
name: "Create New File From Template",
|
|
10
11
|
description: "Create a new Google Docs file from a template. Optionally include placeholders in the template document that will get replaced from this action. [See documentation](https://www.npmjs.com/package/google-docs-mustaches)",
|
|
11
|
-
version: "0.1.
|
|
12
|
+
version: "0.1.17",
|
|
12
13
|
annotations: {
|
|
13
14
|
destructiveHint: true,
|
|
14
15
|
openWorldHint: true,
|
|
@@ -17,10 +18,20 @@ export default {
|
|
|
17
18
|
type: "action",
|
|
18
19
|
props: {
|
|
19
20
|
googleDrive,
|
|
21
|
+
drive: {
|
|
22
|
+
propDefinition: [
|
|
23
|
+
googleDrive,
|
|
24
|
+
"watchedDrive",
|
|
25
|
+
],
|
|
26
|
+
optional: true,
|
|
27
|
+
},
|
|
20
28
|
templateId: {
|
|
21
29
|
propDefinition: [
|
|
22
30
|
googleDrive,
|
|
23
31
|
"fileId",
|
|
32
|
+
(c) => ({
|
|
33
|
+
drive: c.drive,
|
|
34
|
+
}),
|
|
24
35
|
],
|
|
25
36
|
description:
|
|
26
37
|
"Select the template document you'd like to use as the template, or use a custom expression to reference a document ID from a previous step. Template documents should contain placeholders in the format `{{xyz}}`.",
|
|
@@ -29,6 +40,9 @@ export default {
|
|
|
29
40
|
propDefinition: [
|
|
30
41
|
googleDrive,
|
|
31
42
|
"folderId",
|
|
43
|
+
(c) => ({
|
|
44
|
+
drive: c.drive,
|
|
45
|
+
}),
|
|
32
46
|
],
|
|
33
47
|
description:
|
|
34
48
|
"Select the folder of the newly created Google Doc and/or PDF, or use a custom expression to reference a folder ID from a previous step.",
|
|
@@ -65,19 +79,38 @@ export default {
|
|
|
65
79
|
mode: this.mode,
|
|
66
80
|
};
|
|
67
81
|
|
|
82
|
+
const isSharedDrive = this.drive && this.drive !== "My Drive";
|
|
83
|
+
|
|
68
84
|
const client = new Mustaches.default({
|
|
69
85
|
token: () => this.googleDrive.$auth.oauth_access_token,
|
|
70
86
|
});
|
|
71
87
|
|
|
88
|
+
// COPY THE TEMPLATE
|
|
89
|
+
|
|
90
|
+
const drive = this.googleDrive.drive();
|
|
91
|
+
const copiedTemplate = await drive.files.copy({
|
|
92
|
+
fileId: this.templateId,
|
|
93
|
+
requestBody: {
|
|
94
|
+
name: "template-copy",
|
|
95
|
+
parents: [
|
|
96
|
+
"root",
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
supportsAllDrives: true,
|
|
100
|
+
});
|
|
101
|
+
const templateId = copiedTemplate.data.id;
|
|
102
|
+
|
|
72
103
|
/* CREATE THE GOOGLE DOC */
|
|
73
104
|
|
|
74
105
|
let googleDocId;
|
|
75
106
|
try {
|
|
76
107
|
googleDocId = await client.interpolate({
|
|
77
|
-
source:
|
|
78
|
-
destination:
|
|
108
|
+
source: templateId,
|
|
109
|
+
destination: !isSharedDrive
|
|
110
|
+
? this.folderId
|
|
111
|
+
: undefined,
|
|
79
112
|
name: this.name,
|
|
80
|
-
data: this.replaceValues,
|
|
113
|
+
data: parseObjectEntries(this.replaceValues),
|
|
81
114
|
});
|
|
82
115
|
} catch (e) {
|
|
83
116
|
const {
|
|
@@ -93,22 +126,56 @@ export default {
|
|
|
93
126
|
|
|
94
127
|
/* CREATE THE PDF */
|
|
95
128
|
|
|
129
|
+
let pdfId;
|
|
96
130
|
if (this.mode.includes(MODE_PDF)) {
|
|
97
|
-
|
|
131
|
+
pdfId = await client.export({
|
|
98
132
|
file: googleDocId,
|
|
99
133
|
mimeType: "application/pdf",
|
|
100
134
|
name: this.name,
|
|
101
|
-
destination:
|
|
135
|
+
destination: !isSharedDrive
|
|
136
|
+
? this.folderId
|
|
137
|
+
: undefined,
|
|
102
138
|
});
|
|
103
139
|
result["pdfId"] = pdfId;
|
|
104
140
|
}
|
|
105
141
|
|
|
142
|
+
// MOVE FILE(S) TO SHARED DRIVE
|
|
143
|
+
|
|
144
|
+
if (isSharedDrive) {
|
|
145
|
+
if (this.mode.includes(MODE_GOOGLE_DOC)) {
|
|
146
|
+
const file = await this.googleDrive.getFile(googleDocId);
|
|
147
|
+
await this.googleDrive.updateFile(googleDocId, {
|
|
148
|
+
fields: "*",
|
|
149
|
+
removeParents: file.parents.join(","),
|
|
150
|
+
addParents: this.folderId || this.drive,
|
|
151
|
+
supportsAllDrives: true,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (pdfId) {
|
|
156
|
+
const pdf = await this.googleDrive.getFile(pdfId);
|
|
157
|
+
await this.googleDrive.updateFile(pdfId, {
|
|
158
|
+
fields: "*",
|
|
159
|
+
removeParents: pdf.parents.join(","),
|
|
160
|
+
addParents: this.folderId || this.drive,
|
|
161
|
+
supportsAllDrives: true,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
106
166
|
/* REMOVE THE GOOGLE DOC */
|
|
107
167
|
|
|
108
168
|
if (!this.mode.includes(MODE_GOOGLE_DOC)) {
|
|
109
169
|
await this.googleDrive.deleteFile(googleDocId);
|
|
110
170
|
}
|
|
111
171
|
|
|
172
|
+
// REMOVE THE COPIED TEMPLATE
|
|
173
|
+
|
|
174
|
+
await drive.files.delete({
|
|
175
|
+
fileId: templateId,
|
|
176
|
+
supportsAllDrives: true,
|
|
177
|
+
});
|
|
178
|
+
|
|
112
179
|
$.export("$summary", "New file successfully created");
|
|
113
180
|
return result;
|
|
114
181
|
},
|