Skip to content

Commit

Permalink
Handle output path completely and consistently (#6)
Browse files Browse the repository at this point in the history
* Reorganize archie parsing and writing

* Handle no relative path

* Add necessary imports
  • Loading branch information
erxclau authored Feb 18, 2022
1 parent 4d2b7f8 commit bc99456
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
23 changes: 13 additions & 10 deletions src/sink-gdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { program } from "commander/esm.mjs";

import { load_config, success, get_auth } from "./_utils.js";
import { drive } from "@googleapis/drive";
import { writeFileSync } from "fs";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { decode } from "html-entities";

import archieml from "archieml";
Expand Down Expand Up @@ -105,20 +105,23 @@ const parse = (file) => {
});
};

export const fetchDoc = ({ id, output, auth }) => {
const authObject = get_auth(auth, ["https://www.googleapis.com/auth/drive"])
export const fetchDoc = async ({ id, output, auth }) => {
const scopes = ["https://www.googleapis.com/auth/drive"];
const authObject = get_auth(auth, scopes);

const gdrive = drive({ version: "v3", auth: authObject });
gdrive.files
.export({ fileId: id, mimeType: "text/html" })
.then(parse)
.then((res) => writeFileSync(output, JSON.stringify(res)))
.then(() => success(`Wrote output to ${output}`))
.catch(console.error);

const file = await gdrive.files.export({ fileId: id, mimeType: "text/html" });
const json = await parse(file);

const dir = output.substring(0, output.lastIndexOf("/"));
!existsSync((dir.length > 0) ? dir : ".") && mkdirSync(dir, { recursive: true });
writeFileSync(output, JSON.stringify(json));
success(`Wrote output to ${output}`);
};

const main = async (opts) => {
const { config } = await load_config(opts.config);

const files = config.fetch.filter((d) => d.sheetId == null);
files.forEach(fetchDoc);
};
Expand Down
5 changes: 2 additions & 3 deletions src/sink-gsheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ const parse = (res) => {

export const fetchSheet = async ({ id, sheetId, output, auth }) => {
const scopes = ["https://www.googleapis.com/auth/spreadsheets"];
const authObject = get_auth(auth, scopes)

const authObject = get_auth(auth, scopes);

const sheet = sheets({ version: "v4", auth: authObject });
const gidQ = await sheet.spreadsheets.getByDataFilter({
Expand All @@ -41,7 +40,7 @@ export const fetchSheet = async ({ id, sheetId, output, auth }) => {
const csv = parse(nameQ);

const dir = output.substring(0, output.lastIndexOf("/"));
!existsSync(dir) && mkdirSync(dir, { recursive: true });
!existsSync((dir.length > 0) ? dir : ".") && mkdirSync(dir, { recursive: true });
writeFileSync(output, csv);
success(`Wrote output to ${output}`);
};
Expand Down

0 comments on commit bc99456

Please sign in to comment.