Skip to content

Commit

Permalink
Merge pull request #13 from BeTomorrow/add-wording
Browse files Browse the repository at this point in the history
Add an option to add a wording line to the google sheet
  • Loading branch information
oliviergauthier authored Dec 6, 2022
2 parents 896d931 + 5ba571d commit 9a6a0a5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"chalk": "^3.0.0",
"clear": "^0.1.0",
"colors": "^1.4.0",
"commander": "^4.0.1",
"commander": "^9.4.1",
"figlet": "^1.2.4",
"googleapis": "^108.0.0",
"open": "^7.0.0",
Expand Down
19 changes: 19 additions & 0 deletions src/google/Drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ export class Drive {
);
}

async addLine(spreadsheetId: string, sheetName: string, key: string, ...values: string[]) {
const service = google.sheets({ version: "v4", auth: this.auth });
try {
const result = await service.spreadsheets.values.append({
spreadsheetId,
range: sheetName,
valueInputOption: "RAW",
requestBody: {
values: [[key, ...values]],
},
});

console.log(`${result.data.updates?.updatedCells} cells appended.`);
return result;
} catch (err) {
throw err;
}
}

async exportAsXlsx(fileId: string, output: string, mimeType: string) {
const dest = fs.createWriteStream(output);
const res = await this.drive.files.export(
Expand Down
31 changes: 22 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import colors from 'colors/safe';
import program from "commander";
import { program } from "commander";
import { loadConfiguration } from "./config/WordingConfigLoader";
import { AngularJsonWordingExporter } from "./exporters/AngularJsonWordingExporter";
import { FlatJsonWordingExporter } from "./exporters/FlatJsonWordingExportter";
Expand All @@ -15,6 +15,7 @@ console.log("Running sync wording");

program
.description("Upgrade application wording from Google Sheet")
.option("--add <key> <values...>", "add a wording line to remote Google Sheet")
.option("--config <config>", "wording config path", "wording_config.json")
.option("--upgrade", "upgrade wording from remote Google Sheet")
.option("--update", "update wording from local xlsx")
Expand Down Expand Up @@ -44,16 +45,28 @@ function printReport(language: string, result : WordingResult) : void {
}
};

loadConfiguration(program.config).then(async config => {
if (program.verbose) {
const options = program.opts();

const scopes = [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/spreadsheets",
];

loadConfiguration(options.config).then(async (config) => {
if (options.verbose) {
console.log(config);
}

if (program.upgrade) {
if (options.add) {
const [key, ...values] = options.add;
const auth = await new GoogleAuth().authorize(config.credentials, scopes);
await new Drive(auth).addLine(config.sheetId, config.sheetNames[0], key, ...values);
}

if (options.upgrade) {
console.log("Download wording from Google Drive");
const auth = await new GoogleAuth().authorize(config.credentials, [
"https://www.googleapis.com/auth/drive.readonly"
]);
const auth = await new GoogleAuth().authorize(config.credentials, scopes);

await new Drive(auth).exportAsXlsx(
config.sheetId,
Expand All @@ -62,7 +75,7 @@ loadConfiguration(program.config).then(async config => {
);
}

if (program.upgrade || program.update) {
if (options.upgrade || options.update) {
console.log("Updating wording files");
const loader = new WordingLoader(config);

Expand All @@ -75,7 +88,7 @@ loadConfiguration(program.config).then(async config => {
hasError = hasError || result.hasInvalidKeys
});

if (hasError && program.invalid === "error") {
if (hasError && options.invalid === "error") {
process.exitCode = 1
}
}
Expand Down

0 comments on commit 9a6a0a5

Please sign in to comment.