dorky 2.3.4 → 2.3.5

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 (3) hide show
  1. package/README.md +1 -0
  2. package/bin/index.js +15 -20
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -91,6 +91,7 @@ Anyhow, we shall store it on a private storage, using **dorky**, that stores it
91
91
  [*] Update .gitignore automatically, to ignore .dorky/credentials.json.
92
92
  [*] Handle reauthentication loop for google-drive. (Bug fix release)
93
93
  [ ] Fix error while adding a file that does not exist, it does give an error but still prints that an entry is added.
94
+ [ ] Update README. (Bug fix release)
94
95
  [ ] Fix issue when the file is added again with the same contents, it still adds it, not checks it with the hash.
95
96
  [ ] Handle invalid access token for google-drive.
96
97
  [ ] Extension for VS Code to list and highlight them like git. (Major release)
package/bin/index.js CHANGED
@@ -105,12 +105,10 @@ async function authorizeGoogleDriveClient(forceReauth = false) {
105
105
  const content = await fs.readFileSync(TOKEN_PATH);
106
106
  const savedCredentials = JSON.parse(content);
107
107
 
108
- // Check if this is OAuth2 credentials (has access_token, refresh_token, etc.)
109
108
  if (!savedCredentials.access_token && !savedCredentials.refresh_token) {
110
109
  return null;
111
110
  }
112
111
 
113
- // Load the client secrets to create an OAuth2 client
114
112
  const keys = JSON.parse(fs.readFileSync(CREDENTIALS_PATH));
115
113
  const key = keys.installed || keys.web;
116
114
  const oAuth2Client = new google.auth.OAuth2(
@@ -119,7 +117,6 @@ async function authorizeGoogleDriveClient(forceReauth = false) {
119
117
  key.redirect_uris[0]
120
118
  );
121
119
 
122
- // Remove the 'storage' field and set credentials
123
120
  const { storage, ...authCredentials } = savedCredentials;
124
121
  oAuth2Client.setCredentials(authCredentials);
125
122
 
@@ -133,13 +130,10 @@ async function authorizeGoogleDriveClient(forceReauth = false) {
133
130
  if (!credentials.expiry_date) {
134
131
  return true;
135
132
  }
136
- // Both Date.now() and expiry_date are in milliseconds since Unix epoch (UTC)
137
- // Check if token expires in less than 5 minutes (300000 ms)
138
133
  const expiryBuffer = 300000;
139
134
  const currentTimeUTC = Date.now();
140
135
  const expiryTimeUTC = credentials.expiry_date;
141
136
 
142
- // Token is expired if current time is past (expiry time - buffer)
143
137
  return currentTimeUTC >= (expiryTimeUTC - expiryBuffer);
144
138
  }
145
139
 
@@ -158,13 +152,11 @@ async function authorizeGoogleDriveClient(forceReauth = false) {
158
152
  }
159
153
  }
160
154
 
161
- // If not forcing reauth, try to load existing credentials
162
155
  if (!forceReauth) {
163
156
  let client = await loadSavedCredentialsIfExist();
164
157
  if (client) {
165
158
  const credentials = JSON.parse(fs.readFileSync(TOKEN_PATH));
166
159
 
167
- // Ensure we're using the client's credentials which may have been updated
168
160
  const clientCredentials = client.credentials || credentials;
169
161
 
170
162
  if (await isTokenExpired(clientCredentials)) {
@@ -172,21 +164,18 @@ async function authorizeGoogleDriveClient(forceReauth = false) {
172
164
  if (client) {
173
165
  return client;
174
166
  }
175
- // If refresh failed, fall through to full reauth
176
167
  } else {
177
168
  return client;
178
169
  }
179
170
  }
180
171
  }
181
172
 
182
- // Perform full authentication
183
173
  client = await authenticate({
184
174
  scopes: SCOPES,
185
175
  keyfilePath: CREDENTIALS_PATH,
186
176
  });
187
177
 
188
- // Save credentials after full authentication
189
- if (client && client.credentials) {
178
+ if (client && client.credentials && existsSync(path.dirname(TOKEN_PATH))) {
190
179
  const credentialsToSave = {
191
180
  storage: "google-drive",
192
181
  ...client.credentials
@@ -206,7 +195,7 @@ async function init(storage) {
206
195
  setupFilesAndFolders(metaData, credentials);
207
196
  break;
208
197
  case "google-drive":
209
- const client = await authorizeGoogleDriveClient(true); // Force reauth on init
198
+ const client = await authorizeGoogleDriveClient(true);
210
199
  credentials = { storage: "google-drive", ...client.credentials };
211
200
  setupFilesAndFolders(metaData, credentials);
212
201
  break;
@@ -264,6 +253,7 @@ function add(listOfFiles) {
264
253
  checkIfDorkyProject();
265
254
  console.log("Adding files to stage-1 to push to storage");
266
255
  const metaData = JSON.parse(fs.readFileSync(".dorky/metadata.json"));
256
+ const addedFiles = [];
267
257
  listOfFiles.forEach((file) => {
268
258
  if (!fs.existsSync(file)) {
269
259
  console.log(chalk.red(`File ${file} does not exist.`));
@@ -271,13 +261,20 @@ function add(listOfFiles) {
271
261
  }
272
262
  const fileContents = fs.readFileSync(file);
273
263
  const fileType = mimeTypes.lookup(file);
264
+ const newHash = md5(fileContents);
265
+ const existingEntry = metaData["stage-1-files"][file];
266
+ if (existingEntry && existingEntry.hash === newHash) {
267
+ console.log(chalk.yellow(`File ${file} has no changes, skipping.`));
268
+ return;
269
+ }
274
270
  metaData["stage-1-files"][file] = {
275
271
  "mime-type": fileType ? fileType : "application/octet-stream",
276
- "hash": md5(fileContents)
272
+ "hash": newHash
277
273
  };
274
+ addedFiles.push(file);
278
275
  });
279
276
  fs.writeFileSync(".dorky/metadata.json", JSON.stringify(metaData, null, 2));
280
- listOfFiles.map((file) => console.log(chalk.green(`Added ${file} to stage-1.`)));
277
+ addedFiles.forEach((file) => console.log(chalk.green(`Added ${file} to stage-1.`)));
281
278
  }
282
279
 
283
280
  function rm(listOfFiles) {
@@ -319,7 +316,7 @@ async function checkCredentials() {
319
316
  } else {
320
317
  try {
321
318
  let credentials;
322
- const client = await authorizeGoogleDriveClient(true); // Force reauth when creating new credentials
319
+ const client = await authorizeGoogleDriveClient(true);
323
320
  credentials = { storage: "google-drive", ...client.credentials };
324
321
  fs.writeFileSync(".dorky/credentials.json", JSON.stringify(credentials, null, 2));
325
322
  console.log(chalk.green("Credentials saved in .dorky/credentials.json"));
@@ -430,9 +427,8 @@ async function pushToGoogleDrive(files) {
430
427
  return parentId;
431
428
  }
432
429
  console.log("Uploading to google drive");
433
- const client = await authorizeGoogleDriveClient(false); // Use existing token if valid
430
+ const client = await authorizeGoogleDriveClient(false);
434
431
 
435
- // Update credentials file with potentially refreshed token
436
432
  const credentialsToSave = {
437
433
  storage: "google-drive",
438
434
  ...client.credentials
@@ -521,9 +517,8 @@ async function pullFromGoogleDrive(files) {
521
517
  return { name: file, ...files[file] };
522
518
  });
523
519
 
524
- const client = await authorizeGoogleDriveClient(false); // Use existing token if valid
520
+ const client = await authorizeGoogleDriveClient(false);
525
521
 
526
- // Update credentials file with potentially refreshed token
527
522
  const credentialsToSave = {
528
523
  storage: "google-drive",
529
524
  ...client.credentials
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dorky",
3
- "version": "2.3.4",
3
+ "version": "2.3.5",
4
4
  "description": "DevOps Records Keeper.",
5
5
  "bin": {
6
6
  "dorky": "bin/index.js"