sfc-utils 1.4.78 → 1.4.80
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.
- package/copy/googleauth.js +52 -39
- package/copy/sheets.js +99 -72
- package/package.json +1 -1
package/copy/googleauth.js
CHANGED
|
@@ -8,49 +8,57 @@ var os = require("os");
|
|
|
8
8
|
var path = require("path");
|
|
9
9
|
var url = require("url");
|
|
10
10
|
var fs = require("fs");
|
|
11
|
-
var writeFile = require(
|
|
11
|
+
var writeFile = require("write");
|
|
12
12
|
|
|
13
13
|
// Prep the service account for drive
|
|
14
|
-
var serviceAccountCreds = path.join(
|
|
14
|
+
var serviceAccountCreds = path.join(
|
|
15
|
+
os.homedir(),
|
|
16
|
+
"service-account-google-creds.json"
|
|
17
|
+
);
|
|
15
18
|
var tokenLocation = path.join(os.homedir(), ".google_oauth_token");
|
|
16
19
|
|
|
17
|
-
var fallbackAuth = function() {
|
|
20
|
+
var fallbackAuth = function () {
|
|
18
21
|
// If it's coming from EC2, pull from project
|
|
19
|
-
if (process.env.GOOGLE_OAUTH_SYSTEM === "EC2"){
|
|
22
|
+
if (process.env.GOOGLE_OAUTH_SYSTEM === "EC2") {
|
|
20
23
|
tokenLocation = "../.google_oauth_token";
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
try {
|
|
24
27
|
var tokens = fs.readFileSync(tokenLocation, "utf-8");
|
|
25
28
|
tokens = JSON.parse(tokens);
|
|
26
|
-
auth = new google.auth.OAuth2(
|
|
29
|
+
auth = new google.auth.OAuth2(
|
|
30
|
+
process.env.GOOGLE_OAUTH_CLIENT_ID,
|
|
31
|
+
process.env.GOOGLE_OAUTH_CONSUMER_SECRET
|
|
32
|
+
);
|
|
27
33
|
auth.setCredentials(tokens);
|
|
28
34
|
|
|
29
|
-
auth.on("tokens", function(update) {
|
|
35
|
+
auth.on("tokens", function (update) {
|
|
30
36
|
Object.assign(tokens, update);
|
|
31
37
|
fs.writeFileSync(tokenLocation, JSON.stringify(tokens, null, 2));
|
|
32
38
|
});
|
|
33
|
-
} catch(err){
|
|
39
|
+
} catch (err) {
|
|
34
40
|
// If we error here, fire up local (as long as we're on on EC2)
|
|
35
|
-
if (process.env.GOOGLE_OAUTH_SYSTEM !== "EC2"){
|
|
36
|
-
task()
|
|
41
|
+
if (process.env.GOOGLE_OAUTH_SYSTEM !== "EC2") {
|
|
42
|
+
task();
|
|
37
43
|
}
|
|
38
44
|
}
|
|
39
45
|
return auth;
|
|
40
|
-
}
|
|
46
|
+
};
|
|
41
47
|
|
|
42
|
-
var authenticate = function({fallback}) {
|
|
43
|
-
if (fallback){
|
|
48
|
+
var authenticate = function ({ fallback }) {
|
|
49
|
+
if (fallback) {
|
|
44
50
|
return new Promise((resolve, reject) => {
|
|
45
|
-
console.log(
|
|
51
|
+
console.log(
|
|
52
|
+
"Service account failed, falling back to regular token (to use the service account, share this sheet or doc with sfchronicle-gatsby@zinc-proton-250521.iam.gserviceaccount.com)"
|
|
53
|
+
);
|
|
46
54
|
resolve(fallbackAuth());
|
|
47
|
-
})
|
|
55
|
+
});
|
|
48
56
|
}
|
|
49
57
|
// Try to use the service account first
|
|
50
58
|
return new Promise((resolve, reject) => {
|
|
51
59
|
try {
|
|
52
60
|
// If it's coming from EC2, pull from project
|
|
53
|
-
if (process.env.GOOGLE_OAUTH_SYSTEM === "EC2"){
|
|
61
|
+
if (process.env.GOOGLE_OAUTH_SYSTEM === "EC2") {
|
|
54
62
|
serviceAccountCreds = "../service-account-google-creds.json";
|
|
55
63
|
}
|
|
56
64
|
|
|
@@ -63,52 +71,56 @@ var authenticate = function({fallback}) {
|
|
|
63
71
|
null,
|
|
64
72
|
serviceAccountJSON.private_key,
|
|
65
73
|
[
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
"https://www.googleapis.com/auth/spreadsheets",
|
|
75
|
+
"https://www.googleapis.com/auth/drive",
|
|
68
76
|
]
|
|
69
77
|
);
|
|
70
78
|
//authenticate request
|
|
71
79
|
jwtClient.authorize(function (err, tokens) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
if (err) {
|
|
81
|
+
console.log("Stage 1 error, fallback auth");
|
|
82
|
+
resolve(fallbackAuth());
|
|
83
|
+
} else {
|
|
84
|
+
console.log("Successfully connected to service account!");
|
|
85
|
+
// Return the jwtClient as auth
|
|
86
|
+
resolve(jwtClient);
|
|
87
|
+
}
|
|
80
88
|
});
|
|
81
|
-
} catch (err){
|
|
89
|
+
} catch (err) {
|
|
82
90
|
// It's ok if it errors, we have the fallback
|
|
83
91
|
console.log("Stage 2 error, fallback auth");
|
|
84
92
|
resolve(fallbackAuth());
|
|
85
93
|
}
|
|
86
|
-
})
|
|
94
|
+
});
|
|
87
95
|
};
|
|
88
96
|
|
|
89
|
-
var task = function() {
|
|
97
|
+
var task = function () {
|
|
90
98
|
// var done = this.async();
|
|
91
99
|
|
|
92
100
|
var clientID = process.env.GOOGLE_OAUTH_CLIENT_ID;
|
|
93
101
|
var secret = process.env.GOOGLE_OAUTH_CONSUMER_SECRET;
|
|
94
102
|
|
|
95
|
-
var client = new google.auth.OAuth2(
|
|
103
|
+
var client = new google.auth.OAuth2(
|
|
104
|
+
clientID,
|
|
105
|
+
secret,
|
|
106
|
+
"http://localhost:8000/authenticate/"
|
|
107
|
+
);
|
|
96
108
|
google.options({
|
|
97
|
-
auth: client
|
|
109
|
+
auth: client,
|
|
98
110
|
});
|
|
99
111
|
|
|
100
112
|
var scopes = [
|
|
101
113
|
"https://www.googleapis.com/auth/drive",
|
|
102
|
-
"https://www.googleapis.com/auth/spreadsheets"
|
|
114
|
+
"https://www.googleapis.com/auth/spreadsheets",
|
|
103
115
|
];
|
|
104
116
|
|
|
105
117
|
var authURL = client.generateAuthUrl({
|
|
106
118
|
access_type: "offline",
|
|
107
119
|
scope: scopes.join(" "),
|
|
108
|
-
prompt: "consent"
|
|
120
|
+
prompt: "consent",
|
|
109
121
|
});
|
|
110
122
|
|
|
111
|
-
var onRequest = function(request, response) {
|
|
123
|
+
var onRequest = function (request, response) {
|
|
112
124
|
response.setHeader("Connection", "close");
|
|
113
125
|
if (request.url.indexOf("authenticate") > -1) {
|
|
114
126
|
return onAuthenticated(request, response);
|
|
@@ -121,15 +133,16 @@ var task = function() {
|
|
|
121
133
|
response.end();
|
|
122
134
|
};
|
|
123
135
|
|
|
124
|
-
var onAuthenticated = async function(request, response) {
|
|
125
|
-
var requestURL =
|
|
136
|
+
var onAuthenticated = async function (request, response) {
|
|
137
|
+
var requestURL =
|
|
138
|
+
request.url[0] == "/" ? "localhost:8000" + request.url : request.url;
|
|
126
139
|
var query = new url.URL(requestURL).searchParams;
|
|
127
140
|
var code = query.get("code");
|
|
128
141
|
if (!code) return;
|
|
129
142
|
try {
|
|
130
143
|
var token = await client.getToken(code);
|
|
131
144
|
var tokens = token.tokens;
|
|
132
|
-
writeFile(tokenLocation, JSON.stringify(tokens, null, 2), function(err) {
|
|
145
|
+
writeFile(tokenLocation, JSON.stringify(tokens, null, 2), function (err) {
|
|
133
146
|
if (err) {
|
|
134
147
|
console.log(err);
|
|
135
148
|
} else {
|
|
@@ -144,10 +157,10 @@ var task = function() {
|
|
|
144
157
|
|
|
145
158
|
var server = http.createServer(onRequest);
|
|
146
159
|
server.listen(8000, () => opn("http://localhost:8000/authorize"));
|
|
147
|
-
}
|
|
160
|
+
};
|
|
148
161
|
|
|
149
162
|
let fullAuth = {
|
|
150
163
|
task: task,
|
|
151
|
-
authenticate: authenticate
|
|
152
|
-
}
|
|
164
|
+
authenticate: authenticate,
|
|
165
|
+
};
|
|
153
166
|
module.exports = fullAuth;
|
package/copy/sheets.js
CHANGED
|
@@ -12,16 +12,6 @@ var { google } = require("googleapis");
|
|
|
12
12
|
var api = google.sheets("v4");
|
|
13
13
|
var writeFile = require("write");
|
|
14
14
|
var authObj = require("./googleauth");
|
|
15
|
-
// Try to import story_settings.sheet.json
|
|
16
|
-
let languageSwap;
|
|
17
|
-
try {
|
|
18
|
-
const storySettings = require("../../../src/data/story_settings.sheet.json");
|
|
19
|
-
if (storySettings) {
|
|
20
|
-
languageSwap = storySettings[0].Language_Swap;
|
|
21
|
-
}
|
|
22
|
-
} catch (err) {
|
|
23
|
-
// It's ok
|
|
24
|
-
}
|
|
25
15
|
|
|
26
16
|
var cast = function (str, forceStr) {
|
|
27
17
|
if (!forceStr) {
|
|
@@ -93,7 +83,15 @@ let grabSheets = (auth, project, directory, forceStr) => {
|
|
|
93
83
|
let promiseStack = [];
|
|
94
84
|
for (var spreadsheetId of sheetKeys) {
|
|
95
85
|
let promiseItem = new Promise((resolve, reject) => {
|
|
96
|
-
getSheet(
|
|
86
|
+
getSheet(
|
|
87
|
+
resolve,
|
|
88
|
+
reject,
|
|
89
|
+
auth,
|
|
90
|
+
spreadsheetId,
|
|
91
|
+
directory,
|
|
92
|
+
forceStr,
|
|
93
|
+
project
|
|
94
|
+
);
|
|
97
95
|
});
|
|
98
96
|
promiseStack.push(promiseItem);
|
|
99
97
|
}
|
|
@@ -115,7 +113,8 @@ let getSheet = async (
|
|
|
115
113
|
auth,
|
|
116
114
|
spreadsheetId,
|
|
117
115
|
directory,
|
|
118
|
-
forceStr
|
|
116
|
+
forceStr,
|
|
117
|
+
project
|
|
119
118
|
) => {
|
|
120
119
|
let output = await api.spreadsheets
|
|
121
120
|
.get({
|
|
@@ -131,78 +130,106 @@ let getSheet = async (
|
|
|
131
130
|
}
|
|
132
131
|
var book = output.data;
|
|
133
132
|
var { sheets, spreadsheetId } = book;
|
|
133
|
+
console.log("SHEETS", sheets);
|
|
134
|
+
let languageSwap;
|
|
135
|
+
if (project.LANGUAGE_SWAP) {
|
|
136
|
+
languageSwap = project.LANGUAGE_SWAP;
|
|
137
|
+
}
|
|
138
|
+
console.log("language swap:", languageSwap);
|
|
139
|
+
// Process all other sheets with the language swap
|
|
134
140
|
for (var sheet of sheets) {
|
|
135
141
|
if (sheet.properties.title[0] == "_") continue;
|
|
136
|
-
|
|
142
|
+
await processSheetData(
|
|
137
143
|
auth,
|
|
138
144
|
spreadsheetId,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
145
|
+
sheet,
|
|
146
|
+
forceStr,
|
|
147
|
+
directory,
|
|
148
|
+
languageSwap
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
resolve("Complete");
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const processSheetData = async (
|
|
155
|
+
auth,
|
|
156
|
+
spreadsheetId,
|
|
157
|
+
sheet,
|
|
158
|
+
forceStr,
|
|
159
|
+
directory,
|
|
160
|
+
languageSwap = null
|
|
161
|
+
) => {
|
|
162
|
+
var response = await api.spreadsheets.values.get({
|
|
163
|
+
auth,
|
|
164
|
+
spreadsheetId,
|
|
165
|
+
range: `${sheet.properties.title}!A:AAA`,
|
|
166
|
+
majorDimension: "ROWS",
|
|
167
|
+
});
|
|
168
|
+
var { values } = response.data;
|
|
169
|
+
var header = values.shift();
|
|
170
|
+
var swapIndexes = [];
|
|
171
|
+
console.log("Swap type:", typeof languageSwap);
|
|
172
|
+
if (languageSwap) {
|
|
173
|
+
console.log("Running swap logic");
|
|
174
|
+
for (var i = 0; i < header.length; i++) {
|
|
175
|
+
var lastIndex = header[i].lastIndexOf("_");
|
|
176
|
+
if (lastIndex > -1) {
|
|
177
|
+
// It has an underscore! Check for match
|
|
178
|
+
var substring = header[i].substring(lastIndex + 1).toLowerCase();
|
|
179
|
+
if (substring === languageSwap) {
|
|
180
|
+
// Match! Save the swap index
|
|
181
|
+
swapIndexes.push(i - 1);
|
|
155
182
|
}
|
|
156
183
|
}
|
|
157
184
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// Not great but ok
|
|
185
|
+
}
|
|
186
|
+
var isKeyed = header.indexOf("key") > -1;
|
|
187
|
+
var isValued = header.indexOf("value") > -1;
|
|
188
|
+
var out = isKeyed ? {} : [];
|
|
189
|
+
for (var row of values) {
|
|
190
|
+
// skip blank rows
|
|
191
|
+
if (!row.length) continue;
|
|
192
|
+
var obj = {};
|
|
193
|
+
var rowSkip = true;
|
|
194
|
+
row.forEach(function (value, i) {
|
|
195
|
+
var key = header[i];
|
|
196
|
+
// Handle language swap
|
|
197
|
+
if (swapIndexes.indexOf(i) > -1) {
|
|
198
|
+
// If we have a swap index, swap the value
|
|
199
|
+
// NOTE: This assumes the translation is ALWAYS one cell to the right
|
|
200
|
+
// If we ever want to have MULTIPLE translations for a single key, we'll need seek out the actual matching key
|
|
201
|
+
try {
|
|
202
|
+
if (row[i + 1]) {
|
|
203
|
+
value = row[i + 1];
|
|
178
204
|
}
|
|
205
|
+
} catch (err) {
|
|
206
|
+
// Not great but ok
|
|
179
207
|
}
|
|
180
|
-
obj[key] = cast(value, forceStr);
|
|
181
|
-
if (value && value !== "FALSE") {
|
|
182
|
-
rowSkip = false;
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
// If only values in row are garbage or blank-ish, skip
|
|
186
|
-
if (rowSkip) continue;
|
|
187
|
-
// Handle actual value
|
|
188
|
-
if (isKeyed) {
|
|
189
|
-
out[obj.key] = isValued ? obj.value : obj;
|
|
190
|
-
} else {
|
|
191
|
-
out.push(obj);
|
|
192
208
|
}
|
|
209
|
+
obj[key] = cast(value, forceStr);
|
|
210
|
+
if (value && value !== "FALSE") {
|
|
211
|
+
rowSkip = false;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
// If only values in row are garbage or blank-ish, skip
|
|
215
|
+
if (rowSkip) continue;
|
|
216
|
+
// Handle actual value
|
|
217
|
+
if (isKeyed) {
|
|
218
|
+
out[obj.key] = isValued ? obj.value : obj;
|
|
219
|
+
} else {
|
|
220
|
+
out.push(obj);
|
|
193
221
|
}
|
|
194
|
-
|
|
195
|
-
//set alternate dir if we have it
|
|
196
|
-
directory = directory || "src/data/";
|
|
197
|
-
var file_path = `${directory}${sheet.properties.title.replace(
|
|
198
|
-
/\s+/g,
|
|
199
|
-
"_"
|
|
200
|
-
)}.sheet.json`;
|
|
201
|
-
console.log(`Saving sheet to ${file_path}`);
|
|
202
|
-
// grunt.file.write(filename, JSON.stringify(out, null, 2));
|
|
203
|
-
writeFile(file_path, JSON.stringify(out, null, 2));
|
|
204
222
|
}
|
|
205
|
-
|
|
223
|
+
|
|
224
|
+
//set alternate dir if we have it
|
|
225
|
+
directory = directory || "src/data/";
|
|
226
|
+
var file_path = `${directory}${sheet.properties.title.replace(
|
|
227
|
+
/\s+/g,
|
|
228
|
+
"_"
|
|
229
|
+
)}.sheet.json`;
|
|
230
|
+
console.log(`Saving sheet to ${file_path}`);
|
|
231
|
+
// grunt.file.write(filename, JSON.stringify(out, null, 2));
|
|
232
|
+
writeFile(file_path, JSON.stringify(out, null, 2));
|
|
206
233
|
};
|
|
207
234
|
|
|
208
235
|
module.exports = { googleAuth };
|