-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (249 loc) · 8.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import debounce from 'debounce';
import fs from 'fs';
import { exec } from 'child_process';
import googleapis from 'googleapis';
import path from 'path';
import readline from 'readline';
const { google } = googleapis;
// If modifying these scopes, delete token.json.
// const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
const SCOPES = ['https://www.googleapis.com/auth/drive'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
const resolveHome = filePath => { if (filePath[0] === '~') {
return path.join(process.env.HOME, filePath.slice(1));
}
return filePath;
};
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
const getAccessToken = async oAuth2Client => new Promise((resolve, reject) => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
// eslint-disable-next-line no-console
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', code => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) {
reject(new Error(`Error retrieving access token ${err}`));
return;
}
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), writeErr => {
if (writeErr) {
reject(new Error(writeErr));
return;
}
resolve(oAuth2Client);
});
});
});
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
const authorize = async credentials => new Promise(resolve => {
// eslint-disable-next-line camelcase
const { client_secret, client_id, redirect_uris } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, async (err, token) => {
if (err) {
await getAccessToken(oAuth2Client);
} else {
oAuth2Client.setCredentials(JSON.parse(token));
}
resolve(oAuth2Client);
});
});
const getOAuth2Client = async () => new Promise((resolve, reject) => {
// Load client secrets from a local file.
fs.readFile('credentials.json', async (err, content) => {
if (err) {
reject(new Error(`Error loading client secret file: ${err}`));
return;
}
// Authorize a client with credentials, then call the Google Drive API.
const oAuth2Client = await authorize(JSON.parse(content));
resolve(oAuth2Client);
});
});
const deleteFile = async (oAuth2Client, fileId) => new Promise((resolve, reject) => {
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
drive.files.delete({ fileId }, delErr => {
if (delErr) {
reject(new Error(`The API returned an error: ${delErr}`));
return;
}
resolve();
});
});
const getFileIds = async (oAuth2Client, name) => new Promise((resolve, reject) => {
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
drive.files.list({
pageSize: 30,
fields: 'nextPageToken, files(id, name)',
q: `name = "${name}"`,
}, (err, res) => {
if (err) {
reject(new Error(`The API returned an error: ${err}`));
return;
}
const { files } = res.data;
const fileIds = files.filter(file => file.name === name).map(file => file.id);
resolve(fileIds);
});
});
const uploadDocument = async (oAuth2Client, name, docPath) => new Promise((resolve, reject) => {
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
const fileMetadata = {
name,
mimeType: 'application/vnd.google-apps.document',
};
const media = {
mimeType: 'application/vnd.oasis.opendocument.text',
body: fs.createReadStream(docPath),
};
drive.files.create({
resource: fileMetadata,
media,
fields: 'id',
}, (err, file) => {
if (err) {
reject(err);
} else {
resolve(file.id);
}
});
});
const updateDocument = async (oAuth2Client, name, docPath) => {
const fileIds = await getFileIds(oAuth2Client, name);
if (fileIds.length > 1) {
throw new Error(`more than one version of document exists: ${name}`);
} else if (fileIds.length === 0) {
return uploadDocument(oAuth2Client, name, docPath);
} else {
const fileId = fileIds[0];
return new Promise((resolve, reject) => {
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
const fileMetadata = {
name,
mimeType: 'application/vnd.google-apps.document',
};
const media = {
mimeType: 'application/vnd.oasis.opendocument.text',
body: fs.createReadStream(docPath),
};
drive.files.update({
fileId,
uploadType: 'media',
resource: fileMetadata,
media,
}, err => {
if (err) {
reject(err);
} else {
resolve(fileId);
}
});
});
}
};
// %s/<text:bookmark[^\/]*\/>//gc
const exportToODT = async orgPath => {
const absPath = path.resolve(resolveHome(orgPath));
const command = `emacs ${absPath} --batch -f org-odt-export-to-odt --kill`;
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
console.warn(stderr);
}
const absDir = path.dirname(absPath);
const baseName = path.join(absDir, path.basename(absPath, path.extname(absPath)));
const outputPath = path.resolve(`${baseName}.odt`);
const convertCommand = `soffice --headless --convert-to fodt ${outputPath} --outdir ${absDir}`
exec(convertCommand, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
console.warn(stderr);
}
const outputPath = path.resolve(`${baseName}.fodt`);
fs.readFile(outputPath, 'utf8', (err, data) => {
if (err) {
reject(err);
return;
}
const result = data.replace(/<text:bookmark[^\/]*\/>/g, '');
fs.writeFile(outputPath, result, 'utf8', (err) => {
if (err) {
reject(error);
return;
}
resolve(outputPath);
});
});
});
});
});
};
const update = async (filename, docName) => {
// eslint-disable-next-line no-console
console.log(`updating ${docName} from ${filename}`);
try {
const outputPath = await exportToODT(filename);
const oAuth2Client = await getOAuth2Client();
await updateDocument(oAuth2Client, docName, outputPath);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
};
const watchFile = (orgfilePath, docName) => {
const watcher = fs.watch(orgfilePath, debounce(() => {
update(orgfilePath, docName);
// Need to start watching again, because vim renames the file to a backup and starts anew.
watcher.close();
watchFile(orgfilePath, docName);
}, 500));
};
const start = () => {
const args = process.argv.slice(2);
if (args.length !== 2) {
const __filename = new URL(import.meta.url).pathname;
const scriptName = path.basename(__filename);
// eslint-disable-next-line no-console
console.error(`
Usage:
${scriptName} <google doc title> <org file path>
`);
return;
}
const docName = args[0];
const orgfilePath = resolveHome(args[1]);
watchFile(orgfilePath, docName);
};
start();