diff --git a/README.md b/README.md index b3588cb..1b2e33e 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,26 @@ Further reading: - [googleapis](https://www.npmjs.com/package/googleapis) npm module - [supported export types](https://developers.google.com/drive/api/v3/ref-export-formats) +## CLI + +There is now a basic CLI, so you can use this package without needing to integrate it into +a JS application first. You can install it either globally (assuming a Unix type environment): + +```bash +npm install -g sync-gdrive +export GOOGLE_CLIENT_EMAIL="xxxxxx" +export GOOGLE_PRIVATE_KEY="xxxxxx" +sync-gdrive "filefolderid" "dest_folder" +``` + +or if you already installed it as a dependency of your project: + +```bash +export GOOGLE_CLIENT_EMAIL="xxxxxx" +export GOOGLE_PRIVATE_KEY="xxxxxx" +./node_modules/.bin/sync-gdrive "filefolderid" "dest_folder" +``` + ## Contributions & Feedback Contributions and feedback is welcome. Please open diff --git a/dist/index.js b/dist/index.js index 781af05..441626b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -122,8 +122,10 @@ function downloadFile(drive, file, destFolder, options = {}) { return __awaiter(this, void 0, void 0, function* () { const filePath = path_1.default.join(destFolder, file.name); if (yield isGDriveFileNewer(file, filePath)) { - options.logger.debug('downloading newer: ', filePath); - options.logger.debug('creating file: ', filePath); + if (options.verbose) { + options.logger.debug('downloading newer: ', filePath); + options.logger.debug('creating file: ', filePath); + } const dest = fs_extra_1.default.createWriteStream(filePath); const response = yield drive.files.get({ fileId: file.id, @@ -217,38 +219,46 @@ function downloadContent(drive, file, path, options) { } function visitDirectory(drive, fileId, folderPath, options, callback) { return __awaiter(this, void 0, void 0, function* () { - const response = yield drive.files.list({ - includeRemoved: false, - spaces: 'drive', - fileId: fileId, - fields: 'nextPageToken, files(id, name, parents, mimeType, createdTime, modifiedTime)', - q: `'${fileId}' in parents` - }); - const { files } = response.data; + let nextPageToken; let allSyncStates = []; - let syncState; - for (let i = 0; i < files.length; i++) { - const file = files[i]; - if (file.mimeType === 'application/vnd.google-apps.folder') { - const childFolderPath = path_1.default.join(folderPath, file.name); - if (options.verbose) { - options.logger.debug('DIR', file.id, childFolderPath, file.name); + do { + const response = yield drive.files.list({ + pageToken: nextPageToken, + includeRemoved: false, + spaces: 'drive', + fileId: fileId, + fields: 'nextPageToken, files(id, name, parents, mimeType, createdTime, modifiedTime)', + q: `'${fileId}' in parents`, + pageSize: 200 + }); + // Needed to get further results + nextPageToken = response.data.nextPageToken; + const files = response.data.files; + let syncState; + for (let i = 0; i < files.length; i++) { + const file = files[i]; + if (file.mimeType === 'application/vnd.google-apps.folder') { + const childFolderPath = path_1.default.join(folderPath, file.name); + if (options.verbose) { + options.logger.debug('DIR', file.id, childFolderPath, file.name); + } + yield fs_extra_1.default.mkdirp(childFolderPath); + if (options.sleepTime) { + yield sleep(options.sleepTime); + } + syncState = yield visitDirectory(drive, file.id, childFolderPath, options); + allSyncStates = allSyncStates.concat(syncState); } - yield fs_extra_1.default.mkdirp(childFolderPath); - if (options.sleepTime) { - yield sleep(options.sleepTime); + else { + if (options.verbose) { + options.logger.debug('DIR', file.id, folderPath, file.name); + } + syncState = yield downloadContent(drive, file, folderPath, options); + allSyncStates.push(syncState); } - syncState = yield visitDirectory(drive, file.id, childFolderPath, options); - allSyncStates = allSyncStates.concat(syncState); } - else { - if (options.verbose) { - options.logger.debug('DIR', file.id, folderPath, file.name); - } - syncState = yield downloadContent(drive, file, folderPath, options); - allSyncStates.push(syncState); - } - } + // continue until there is no next page + } while (nextPageToken); return allSyncStates; }); } @@ -281,7 +291,7 @@ function syncGDrive(fileFolderId, destFolder, keyConfig, options) { ], null); googleapis_1.google.options({ auth }); const drive = googleapis_1.google.drive('v3'); - return yield fetchContents(drive, fileFolderId, destFolder, initIOptions(options)); + return fetchContents(drive, fileFolderId, destFolder, initIOptions(options)); } catch (error) { log(error); diff --git a/dist/index.js.map b/dist/index.js.map index 57ad061..f395ace 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;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,IAAI;YACA,MAAM,IAAI,GAAG,IAAI,mBAAM,CAAC,IAAI,CAAC,GAAG,CAC5B,SAAS,CAAC,WAAW,EACrB,IAAI,EACJ,SAAS,CAAC,UAAU,EACpB;gBACI,uCAAuC;gBACvC,+CAA+C;gBAC/C,4CAA4C;gBAC5C,gDAAgD;gBAChD,yDAAyD;gBACzD,uDAAuD;gBACvD,gDAAgD;aACnD,EACD,IAAI,CACP,CAAC;YAEF,mBAAM,CAAC,OAAO,CAAC,EAAC,IAAI,EAAC,CAAC,CAAC;YAEvB,MAAM,KAAK,GAAG,mBAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAEjC,OAAO,aAAa,CAAC,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;SAChF;QAAC,OAAO,KAAK,EAAE;YACZ,GAAG,CAAC,KAAK,CAAC,CAAC;SACd;IACL,CAAC;CAAA;AAEQ,gCAAU;AACnB,kBAAe,UAAU,CAAC;AAE1B,yDAAyD;AACzD,gDAAgD;AAChD,oEAAoE;AACpE,mEAAmE;AACnE,uEAAuE"} \ No newline at end of file diff --git a/package.json b/package.json index 3e93cdf..8dbec23 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,16 @@ { "name": "sync-gdrive", - "version": "0.9.2", + "version": "0.9.5", "description": "Module to synchronise a file or directory in Google Drive with the local file system", "main": "dist/index.js", + "bin": { + "sync-gdrive": "dist/cli.js" + }, "engines": { "node": ">=10.0.0" }, "scripts": { + "sync-gdrive": "ts-node src/cli.ts", "build": "tsc", "lint": "eslint -c .eslintrc.js --ext .ts src", "test": "mocha --exit -r ts-node/register -r esm test/*.ts" diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..6cb82c6 --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,57 @@ +#! /usr/bin/env node +/* eslint-disable no-console */ +import fs from 'fs'; +import syncGDrive, { IKeyConfig } from '../'; + +async function main () { + try { + let okay = true; + const clientEmail = process.env.GOOGLE_CLIENT_EMAIL; + if (!clientEmail) { + console.log('No client email specified. Be sure to set GOOGLE_CLIENT_EMAIL env variable.'); + okay = false; + } + + let privateKey = process.env.GOOGLE_PRIVATE_KEY; + if (!privateKey) { + console.log('No Google API private key specified. Be sure to set GOOGLE_PRIVATE_KEY env variable.'); + okay = false; + } + + if (!okay) { + process.exit(1); + } + + // Unescape new lines + privateKey = privateKey.replace(/\\n/g, '\n'); + + if (process.argv.length < 4) { + console.log('usage: sync-gdrive '); + process.exit(1); + } + + const fileFolderId = process.argv[2]; + const destFolder = process.argv[3]; + + try { + fs.accessSync(destFolder, fs.constants.R_OK | fs.constants.W_OK); + } catch (error) { + console.log(`Destination folder '${destFolder}' does not exist or is not writable by current user`); + process.exit(1); + } + + const keyConfig: IKeyConfig = { + clientEmail: clientEmail, + privateKey: privateKey + }; + + console.log(`Syncing Google Drive file/folder of id '${fileFolderId}' to '${destFolder}'`); + await syncGDrive(fileFolderId, destFolder, keyConfig); + } catch (error) { + console.log(error); + } +} + +if (require.main === module) { + main(); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 8c44951..4e158ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -121,8 +121,10 @@ async function isGDriveFileNewer(gDriveFile, filePath: string) { async function downloadFile (drive, file, destFolder: string, options: IOptions = {}) { const filePath = path.join(destFolder, file.name); if (await isGDriveFileNewer(file, filePath)) { - options.logger.debug('downloading newer: ', filePath); - options.logger.debug('creating file: ', filePath); + if (options.verbose) { + options.logger.debug('downloading newer: ', filePath); + options.logger.debug('creating file: ', filePath); + } const dest = fs.createWriteStream(filePath); const response = await drive.files.get({ @@ -229,42 +231,53 @@ async function downloadContent (drive, file, path: string, options: IOptions) { async function visitDirectory (drive, fileId: string, folderPath: string, options: IOptions, callback?: Function) { - const response = await drive.files.list({ - includeRemoved: false, - spaces: 'drive', - fileId: fileId, - fields: 'nextPageToken, files(id, name, parents, mimeType, createdTime, modifiedTime)', - q: `'${fileId}' in parents` - }); - const { files } = response.data; + let nextPageToken; let allSyncStates = []; - let syncState; - - for (let i = 0; i < files.length; i++) { - const file = files[i]; - if (file.mimeType === 'application/vnd.google-apps.folder') { - const childFolderPath = path.join(folderPath, file.name); - - if (options.verbose) { - options.logger.debug('DIR', file.id, childFolderPath, file.name) - } + do { + const response = await drive.files.list({ + pageToken: nextPageToken, + includeRemoved: false, + spaces: 'drive', + fileId: fileId, + fields: 'nextPageToken, files(id, name, parents, mimeType, createdTime, modifiedTime)', + q: `'${fileId}' in parents`, + pageSize: 200 + }); - await fs.mkdirp(childFolderPath); - if (options.sleepTime) { - await sleep(options.sleepTime); + // Needed to get further results + nextPageToken = response.data.nextPageToken; + + const files = response.data.files; + let syncState; + + for (let i = 0; i < files.length; i++) { + const file = files[i]; + + if (file.mimeType === 'application/vnd.google-apps.folder') { + const childFolderPath = path.join(folderPath, file.name); + + if (options.verbose) { + options.logger.debug('DIR', file.id, childFolderPath, file.name) + } + + await fs.mkdirp(childFolderPath); + if (options.sleepTime) { + await sleep(options.sleepTime); + } + syncState = await visitDirectory(drive, file.id, childFolderPath, options); + allSyncStates = allSyncStates.concat(syncState); + } else { + if (options.verbose) { + options.logger.debug('DIR', file.id, folderPath, file.name) + } + syncState = await downloadContent(drive, file, folderPath, options); + allSyncStates.push(syncState); } - syncState = await visitDirectory(drive, file.id, childFolderPath, options); - allSyncStates = allSyncStates.concat(syncState); - } else { - if (options.verbose) { - options.logger.debug('DIR', file.id, folderPath, file.name) - } - syncState = await downloadContent(drive, file, folderPath, options); - allSyncStates.push(syncState); } - } + // continue until there is no next page + } while (nextPageToken); return allSyncStates; } @@ -307,7 +320,7 @@ async function syncGDrive (fileFolderId, destFolder: string, keyConfig: IKeyConf const drive = google.drive('v3'); - return await fetchContents(drive, fileFolderId, destFolder, initIOptions(options)); + return fetchContents(drive, fileFolderId, destFolder, initIOptions(options)); } catch (error) { log(error); } diff --git a/test/index.ts b/test/index.ts index a9cc0bb..0e5e090 100644 --- a/test/index.ts +++ b/test/index.ts @@ -38,17 +38,17 @@ describe('Endpoints', async () => { filefolderId = process.env.GDRIVE_FILEFOLDER_ID; if (!filefolderId) { - throw new Error('No Google Drive file or folder id specified. Be sure to set env GDRIVE_FILEFOLDER_ID.'); + throw new Error('No Google Drive file or folder id specified. Be sure to set GDRIVE_FILEFOLDER_ID env variable.'); } clientEmail = process.env.GOOGLE_CLIENT_EMAIL; if (!clientEmail) { - throw new Error('No client email specified. Be sure to set GOOGLE_CLIENT_EMAIL.'); + throw new Error('No client email specified. Be sure to set GOOGLE_CLIENT_EMAIL env variable.'); } privateKey = process.env.GOOGLE_PRIVATE_KEY; if (!privateKey) { - throw new Error('No Google API privaye key specified. Be sure to set GOOGLE_PRIVATE_KEY.'); + throw new Error('No Google API private key specified. Be sure to set GOOGLE_PRIVATE_KEY env variable.'); } privateKey = privateKey.replace(/\\n/g, '\n').trim(); @@ -70,6 +70,7 @@ describe('Endpoints', async () => { const filefolderByPath = {}; expect(syncedFileFolders).to.be.not.null; + expect(syncedFileFolders).to.be.not.undefined; syncedFileFolders.forEach(filefolder => { filefolderByPath[filefolder.file] = filefolder;