dorky 2.1.5 → 2.1.7

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.
Files changed (2) hide show
  1. package/bin/index.js +47 -16
  2. package/package.json +2 -2
package/bin/index.js CHANGED
@@ -104,7 +104,7 @@ async function init(storage) {
104
104
  var credentials;
105
105
  switch (storage) {
106
106
  case "aws":
107
- credentials = { storage: "aws", acessKey: process.env.AWS_ACCESS_KEY, secretKey: process.env.AWS_SECRET_KEY, region: process.env.AWS_REGION, bucket: process.env.BUCKET_NAME }
107
+ credentials = { storage: "aws", accessKey: process.env.AWS_ACCESS_KEY, secretKey: process.env.AWS_SECRET_KEY, awsRegion: process.env.AWS_REGION, bucket: process.env.BUCKET_NAME }
108
108
  setupFilesAndFolders(metaData, credentials);
109
109
  break;
110
110
  case "google-drive":
@@ -172,22 +172,53 @@ function rm(listOfFiles) {
172
172
  else console.log(chalk.red("No files found that can be removed."));
173
173
  }
174
174
 
175
- function checkCredentials() {
176
- const credentials = JSON.parse(fs.readFileSync(".dorky/credentials.json"));
177
- // This only works for AWS S3, add credential checker for google drive also, fix this => TP | 2024-09-28 16:04:41
178
- if (credentials.accessKey && credentials.secretKey && credentials.region && credentials.bucket) {
179
- if (process.env.AWS_ACCESS_KEY && process.env.AWS_SECRET_KEY && process.env.AWS_REGION && process.env.BUCKET_NAME) {
180
- return true;
175
+ async function checkCredentials() {
176
+ try {
177
+ if (fs.existsSync(".dorky/credentials.json")) {
178
+ const credentials = JSON.parse(fs.readFileSync(".dorky/credentials.json"));
179
+ if (credentials.storage === "google-drive") {
180
+ if (credentials.access_token && credentials.scope && credentials.token_type && credentials.expiry_date) return true;
181
+ else return false;
182
+ } else {
183
+ if (credentials.accessKey && credentials.secretKey && credentials.awsRegion && credentials.bucket) return true;
184
+ else return false;
185
+ }
181
186
  } else {
182
- console.log(chalk.red("Please provide credentials in .dorky/credentials.json"));
183
- return false;
187
+ console.log("Setting the credentials again.")
188
+ if (process.env.AWS_ACCESS_KEY && process.env.AWS_SECRET_KEY && process.env.AWS_REGION && process.env.BUCKET_NAME) {
189
+ fs.writeFileSync(".dorky/credentials.json", JSON.stringify({
190
+ "storage": "aws",
191
+ "accessKey": process.env.AWS_ACCESS_KEY,
192
+ "secretKey": process.env.AWS_SECRET_KEY,
193
+ "awsRegion": process.env.AWS_REGION,
194
+ "bucket": process.env.BUCKET_NAME
195
+ }, null, 2));
196
+ return true;
197
+ } else {
198
+ try {
199
+ let credentials;
200
+ const client = await authorizeGoogleDriveClient();
201
+ credentials = { storage: "google-drive", ...client.credentials };
202
+ fs.writeFileSync(".dorky/credentials.json", JSON.stringify(credentials, null, 2));
203
+ console.log(chalk.green("Credentials saved in .dorky/credentials.json"));
204
+ console.log(chalk.red("Please ignore the warning to set credentials below and run the command again."));
205
+ return false;
206
+ } catch (err) {
207
+ console.log(chalk.red("Failed to authorize Google Drive client: " + err.message));
208
+ console.log(chalk.red("Please provide credentials in .dorky/credentials.json"));
209
+ return false;
210
+ }
211
+ }
184
212
  }
185
- } else return true;
213
+ } catch (err) {
214
+ console.log(chalk.red("Please provide credentials in .dorky/credentials.json"));
215
+ return false;
216
+ }
186
217
  }
187
218
 
188
- function push() {
219
+ async function push() {
189
220
  checkIfDorkyProject();
190
- if (!checkCredentials()) {
221
+ if (!(await checkCredentials())) {
191
222
  console.log(chalk.red("Please setup credentials in environment variables or in .dorky/credentials.json"));
192
223
  return;
193
224
  }
@@ -228,7 +259,7 @@ function push() {
228
259
  function pushToS3(files, credentials) {
229
260
  const s3 = new S3Client({
230
261
  credentials: {
231
- accessKeyId: credentials.acessKey ?? process.env.AWS_ACCESS_KEY,
262
+ accessKeyId: credentials.accessKey ?? process.env.AWS_ACCESS_KEY,
232
263
  secretAccessKey: credentials.secretKey ?? process.env.AWS_SECRET_KEY
233
264
  },
234
265
  region: credentials.awsRegion ?? process.env.AWS_REGION
@@ -304,9 +335,9 @@ async function pushToGoogleDrive(files) {
304
335
  }
305
336
  }
306
337
 
307
- function pull() {
338
+ async function pull() {
308
339
  checkIfDorkyProject();
309
- if (!checkCredentials()) {
340
+ if (!(await checkCredentials())) {
310
341
  console.log(chalk.red("Please setup credentials in environment variables or in .dorky/credentials.json"));
311
342
  return;
312
343
  }
@@ -330,7 +361,7 @@ function pull() {
330
361
  function pullFromS3(files, credentials) {
331
362
  const s3 = new S3Client({
332
363
  credentials: {
333
- accessKeyId: credentials.acessKey ?? process.env.AWS_ACCESS_KEY,
364
+ accessKeyId: credentials.accessKey ?? process.env.AWS_ACCESS_KEY,
334
365
  secretAccessKey: credentials.secretKey ?? process.env.AWS_SECRET_KEY
335
366
  },
336
367
  region: credentials.awsRegion ?? process.env.AWS_REGION
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dorky",
3
- "version": "2.1.5",
3
+ "version": "2.1.7",
4
4
  "description": "DevOps Records Keeper.",
5
5
  "bin": {
6
6
  "dorky": "bin/index.js"
@@ -33,7 +33,7 @@
33
33
  "@aws-sdk/client-s3": "^3.658.1",
34
34
  "@google-cloud/local-auth": "^3.0.1",
35
35
  "chalk": "^4.1.2",
36
- "glob": "^11.0.0",
36
+ "glob": "^11.1.0",
37
37
  "googleapis": "^144.0.0",
38
38
  "md5": "^2.3.0",
39
39
  "mime-type": "^4.0.0",