guratan 0.4.0 → 0.5.0

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/dist/cli.d.ts CHANGED
@@ -13,6 +13,14 @@ declare type OptsSend = Opts & {
13
13
  srcMimeType: string;
14
14
  printId: boolean;
15
15
  };
16
+ declare type OptsRecv = Opts & {
17
+ fileId: string;
18
+ parentId: string;
19
+ srcFileName: string;
20
+ destFileName: string;
21
+ destMimeType: string;
22
+ printId: boolean;
23
+ };
16
24
  declare type OptsShare = Opts & {
17
25
  fileId: string;
18
26
  parentId: string;
@@ -30,5 +38,6 @@ declare type OptsShare = Opts & {
30
38
  printId: boolean;
31
39
  };
32
40
  export declare const cliSend: ({ fileId, parentId, destFileName, srcFileName, destMimeType, srcMimeType, printId, stdout, stderr }: OptsSend) => Promise<number>;
41
+ export declare const cliRecv: ({ fileId, parentId, srcFileName, destFileName, destMimeType, printId, stdout, stderr }: OptsRecv) => Promise<number>;
33
42
  export declare const cliShare: ({ fileId, parentId, destFileName, type, role, emailAddress, domain, allowFileDiscovery, view, moveToNewOwnersRoot, transferOwnership, sendNotificationEmail, emailMessage, printId, stdout, stderr }: OptsShare) => Promise<number>;
34
43
  export {};
package/dist/cli.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { driveClient } from './tdrive.js';
2
2
  import { sendFile } from './tsend.js';
3
+ import { recvFile } from './trecv.js';
3
4
  import { createPermisson } from './tshare.js';
4
5
  export const cliSend = async ({ fileId, parentId, destFileName, srcFileName, destMimeType, srcMimeType, printId, stdout, stderr }) => {
5
6
  try {
@@ -22,6 +23,26 @@ export const cliSend = async ({ fileId, parentId, destFileName, srcFileName, des
22
23
  }
23
24
  return 0;
24
25
  };
26
+ export const cliRecv = async ({ fileId, parentId, srcFileName, destFileName, destMimeType, printId, stdout, stderr }) => {
27
+ try {
28
+ const id = await recvFile(driveClient(), {
29
+ fileId,
30
+ parentId,
31
+ srcFileName,
32
+ destFileName,
33
+ destMimeType
34
+ });
35
+ if (printId) {
36
+ stdout.write(id);
37
+ }
38
+ }
39
+ catch (err) {
40
+ stderr.write(err.toString());
41
+ stderr.write('\n');
42
+ return 1;
43
+ }
44
+ return 0;
45
+ };
25
46
  export const cliShare = async ({ fileId, parentId, destFileName, type, role, emailAddress, domain, allowFileDiscovery, view, moveToNewOwnersRoot, transferOwnership, sendNotificationEmail, emailMessage, printId, stdout, stderr }) => {
26
47
  try {
27
48
  const id = await createPermisson(driveClient(), {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { driveClient } from './tdrive.js';
2
2
  export { sendFile } from './tsend.js';
3
+ export { recvFile } from './trecv.js';
3
4
  export { createPermisson } from './tshare.js';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { driveClient } from './tdrive.js';
2
2
  export { sendFile } from './tsend.js';
3
+ export { recvFile } from './trecv.js';
3
4
  export { createPermisson } from './tshare.js';
package/dist/main.js CHANGED
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import yargs from 'yargs';
3
3
  import { hideBin } from 'yargs/helpers';
4
- import { cliSend, cliShare } from './cli.js';
5
4
  const envVarsPrefix = process.env['GURATAN_ENV_VARS_PREFIX'] || 'GURATAN';
6
5
  const argv = await yargs(hideBin(process.argv))
7
6
  .scriptName('guratan')
@@ -50,6 +49,45 @@ const argv = await yargs(hideBin(process.argv))
50
49
  description: 'Print the id of the file that is sended into remote'
51
50
  }
52
51
  });
52
+ })
53
+ .command('recv [OPTIONS]', 'receive file from Google Drive to local file', (yargs) => {
54
+ return yargs.options({
55
+ 'file-id': {
56
+ type: 'string',
57
+ default: '',
58
+ required: false,
59
+ description: 'The ID of the file or shared drive.'
60
+ },
61
+ 'parent-id': {
62
+ type: 'string',
63
+ default: '',
64
+ required: false,
65
+ description: 'The IDs of the parent folders which contain the file.'
66
+ },
67
+ 'src-file-name': {
68
+ type: 'string',
69
+ required: false,
70
+ description: 'The name of the file in remote'
71
+ },
72
+ 'dest-file-name': {
73
+ type: 'string',
74
+ default: '',
75
+ required: true,
76
+ description: 'The name(path) of the file in local filesystem'
77
+ },
78
+ 'dest-mime-type': {
79
+ type: 'string',
80
+ required: false,
81
+ default: '',
82
+ description: 'Media mime-type'
83
+ },
84
+ 'print-id': {
85
+ type: 'boolean',
86
+ required: false,
87
+ default: false,
88
+ description: 'Print the id of the file that is received from remote'
89
+ }
90
+ });
53
91
  })
54
92
  .command('share [OPTIONS]', 'share file in Google Drive', (yargs) => {
55
93
  return yargs.options({
@@ -140,6 +178,7 @@ const argv = await yargs(hideBin(process.argv))
140
178
  .help().argv;
141
179
  switch (`${argv._[0]}`) {
142
180
  case 'send':
181
+ const { cliSend } = await import('./cli.js');
143
182
  process.exit(await cliSend({
144
183
  fileId: argv['file-id'],
145
184
  parentId: argv['parent-id'] || '',
@@ -152,7 +191,21 @@ switch (`${argv._[0]}`) {
152
191
  stderr: process.stderr
153
192
  }));
154
193
  break;
194
+ case 'recv':
195
+ const { cliRecv } = await import('./cli.js');
196
+ process.exit(await cliRecv({
197
+ fileId: argv['file-id'],
198
+ parentId: argv['parent-id'] || '',
199
+ srcFileName: argv['src-file-name'] || '',
200
+ destFileName: argv['dest-file-name'] || '',
201
+ destMimeType: argv['dest-mime-type'] || '',
202
+ printId: argv['print-id'] || false,
203
+ stdout: process.stdout,
204
+ stderr: process.stderr
205
+ }));
206
+ break;
155
207
  case 'share':
208
+ const { cliShare } = await import('./cli.js');
156
209
  process.exit(await cliShare({
157
210
  fileId: argv['file-id'],
158
211
  parentId: argv['parent-id'] || '',
package/dist/tdrive.d.ts CHANGED
@@ -1,3 +1,15 @@
1
+ import { drive_v3 } from '@googleapis/drive';
2
+ export declare class GetFileIdError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ /**
6
+ * Get file id in spesiced parent.
7
+ * @param drive - drive instance.
8
+ * @param parentId - id of folder in Google Deive.
9
+ * @param fileName - file name.
10
+ * @returns id of file or blank(when file is not found)
11
+ */
12
+ export declare function getFileId(drive: drive_v3.Drive, parentId: string, fileName: string): Promise<string>;
1
13
  /**
2
14
  * Validate value that is used in query parameter.
3
15
  * return false if value has included "'">
@@ -9,4 +21,4 @@ export declare function validateQueryValue(s: string): boolean;
9
21
  * Make instacen of drive that is authenticated.
10
22
  * @returns instance of drive.
11
23
  */
12
- export declare function driveClient(): import("@googleapis/drive").drive_v3.Drive;
24
+ export declare function driveClient(): drive_v3.Drive;
package/dist/tdrive.js CHANGED
@@ -1,5 +1,44 @@
1
1
  import { GoogleAuth } from 'google-auth-library';
2
2
  import { drive as gdrive } from '@googleapis/drive';
3
+ export class GetFileIdError extends Error {
4
+ constructor(message) {
5
+ //https://stackoverflow.com/questions/41102060/typescript-extending-error-class
6
+ super(message);
7
+ Object.setPrototypeOf(this, GetFileIdError.prototype);
8
+ }
9
+ }
10
+ /**
11
+ * Get file id in spesiced parent.
12
+ * @param drive - drive instance.
13
+ * @param parentId - id of folder in Google Deive.
14
+ * @param fileName - file name.
15
+ * @returns id of file or blank(when file is not found)
16
+ */
17
+ export async function getFileId(drive, parentId, fileName) {
18
+ try {
19
+ if (validateQueryValue(parentId) === false) {
20
+ throw new GetFileIdError(`Invalid paretnt id : ${parentId}`);
21
+ }
22
+ if (validateQueryValue(fileName) === false) {
23
+ throw new GetFileIdError(`Invalid file name : ${fileName}`);
24
+ }
25
+ const list = await drive.files.list({
26
+ pageSize: 10,
27
+ q: `'${parentId}' in parents and name = '${fileName}'`,
28
+ fields: 'files(id, name)'
29
+ });
30
+ if (list.data.files && list.data.files.length > 0) {
31
+ return list.data.files[0].id || '';
32
+ }
33
+ return '';
34
+ }
35
+ catch (err) {
36
+ if (err.errors) {
37
+ throw new GetFileIdError(JSON.stringify(err.errors));
38
+ }
39
+ throw err;
40
+ }
41
+ }
3
42
  /**
4
43
  * Validate value that is used in query parameter.
5
44
  * return false if value has included "'">
@@ -0,0 +1,44 @@
1
+ import { drive_v3 } from '@googleapis/drive';
2
+ export declare class DownloadFileError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ /**
6
+ * Options for recvFile().
7
+ */
8
+ export declare type RecvFileOpts = {
9
+ /**
10
+ * @type The ID of the file or shared drive.
11
+ */
12
+ fileId: string;
13
+ /**
14
+ /**
15
+ * @type The IDs of the parent folders which contain the file.
16
+ */
17
+ parentId: string;
18
+ /**
19
+ * @type The name of the file in remote
20
+ */
21
+ srcFileName: string;
22
+ /**
23
+ @type The name(path) of the file in local filesystem
24
+ */
25
+ destFileName: string;
26
+ /**
27
+ * @type Media mime-type.
28
+ */
29
+ destMimeType: string;
30
+ };
31
+ /**
32
+ * Download the file from Google Drive to the locale file
33
+ * @param drive - drive instance.
34
+ * @param opts - options.
35
+ * @returns
36
+ */
37
+ export declare function downloadFile(drive: drive_v3.Drive, opts: Pick<RecvFileOpts, 'fileId' | 'destFileName' | 'destMimeType'>): Promise<void>;
38
+ /**
39
+ * Receive the file from Google Drive to the locale file
40
+ * @param drive - drive instance.
41
+ * @param opts - options.
42
+ * @returns id of file in Google Drive
43
+ */
44
+ export declare function recvFile(drive: drive_v3.Drive, opts: RecvFileOpts): Promise<string>;
package/dist/trecv.js ADDED
@@ -0,0 +1,79 @@
1
+ import * as fs from 'fs';
2
+ import { promisify } from 'util';
3
+ import { getFileId, GetFileIdError } from './tdrive.js';
4
+ export class DownloadFileError extends Error {
5
+ constructor(message) {
6
+ //https://stackoverflow.com/questions/41102060/typescript-extending-error-class
7
+ super(message);
8
+ Object.setPrototypeOf(this, DownloadFileError.prototype);
9
+ }
10
+ }
11
+ /**
12
+ * Download the file from Google Drive to the locale file
13
+ * @param drive - drive instance.
14
+ * @param opts - options.
15
+ * @returns
16
+ */
17
+ export async function downloadFile(drive, opts) {
18
+ let ret;
19
+ try {
20
+ const { fileId, destFileName, destMimeType } = opts;
21
+ const dest = fs.createWriteStream(destFileName);
22
+ try {
23
+ if (destMimeType) {
24
+ const params = {
25
+ fileId,
26
+ mimeType: destMimeType
27
+ };
28
+ const res = await drive.files.export(params, { responseType: 'stream' });
29
+ for await (const c of res.data) {
30
+ dest.write(c);
31
+ }
32
+ }
33
+ else {
34
+ const params = {
35
+ fileId,
36
+ alt: 'media'
37
+ };
38
+ const res = await drive.files.get(params, { responseType: 'stream' });
39
+ for await (const c of res.data) {
40
+ dest.write(c);
41
+ }
42
+ }
43
+ }
44
+ catch (err) {
45
+ if (err.erros) {
46
+ throw new DownloadFileError(JSON.stringify(err.errors));
47
+ }
48
+ if (err.response?.status) {
49
+ throw new DownloadFileError(`status:${err.response.status}\nstatusText:${err.response.statusText}`);
50
+ }
51
+ throw new DownloadFileError(JSON.stringify(err));
52
+ }
53
+ finally {
54
+ // return promisify(dest.close.bind(dest))() ここで return すると常に undfeind になる
55
+ ret = promisify(dest.close.bind(dest))();
56
+ }
57
+ }
58
+ catch (err) {
59
+ throw err;
60
+ }
61
+ return ret;
62
+ }
63
+ /**
64
+ * Receive the file from Google Drive to the locale file
65
+ * @param drive - drive instance.
66
+ * @param opts - options.
67
+ * @returns id of file in Google Drive
68
+ */
69
+ export async function recvFile(drive, opts) {
70
+ const { fileId: inFileId, parentId, srcFileName, destFileName, destMimeType } = opts;
71
+ let fileId = inFileId !== '' ? inFileId : await getFileId(drive, parentId, srcFileName);
72
+ if (fileId === '') {
73
+ throw new GetFileIdError(
74
+ // `The srouce file not found in paretnt id : ${srcFileName}, ${parentId}`
75
+ `The srouce file not found`);
76
+ }
77
+ await downloadFile(drive, { fileId, destFileName, destMimeType });
78
+ return fileId;
79
+ }
package/dist/tsend.d.ts CHANGED
@@ -1,7 +1,4 @@
1
1
  import { drive_v3 } from '@googleapis/drive';
2
- export declare class GetFileIdError extends Error {
3
- constructor(message: string);
4
- }
5
2
  export declare class UploadFileError extends Error {
6
3
  constructor(message: string);
7
4
  }
@@ -38,14 +35,6 @@ export declare type SendFileOpts = {
38
35
  */
39
36
  srcMimeType: string;
40
37
  };
41
- /**
42
- * Get file id in spesiced parent.
43
- * @param drive - drive instance.
44
- * @param parentId - id of folder in Google Deive.
45
- * @param fileName - file name.
46
- * @returns id of file or blank(when file is not found)
47
- */
48
- export declare function getFileId(drive: drive_v3.Drive, parentId: string, fileName: string): Promise<string>;
49
38
  /**
50
39
  * Create file using by source file into Google Drive.
51
40
  * @param drive - drive instance.
package/dist/tsend.js CHANGED
@@ -1,13 +1,7 @@
1
1
  import * as path from 'path';
2
2
  import * as fs from 'fs';
3
- import { validateQueryValue } from './tdrive.js';
4
- export class GetFileIdError extends Error {
5
- constructor(message) {
6
- //https://stackoverflow.com/questions/41102060/typescript-extending-error-class
7
- super(message);
8
- Object.setPrototypeOf(this, GetFileIdError.prototype);
9
- }
10
- }
3
+ import { promisify } from 'util';
4
+ import { getFileId } from './tdrive.js';
11
5
  export class UploadFileError extends Error {
12
6
  constructor(message) {
13
7
  //https://stackoverflow.com/questions/41102060/typescript-extending-error-class
@@ -22,38 +16,6 @@ export class UpdateFileError extends Error {
22
16
  Object.setPrototypeOf(this, UpdateFileError.prototype);
23
17
  }
24
18
  }
25
- /**
26
- * Get file id in spesiced parent.
27
- * @param drive - drive instance.
28
- * @param parentId - id of folder in Google Deive.
29
- * @param fileName - file name.
30
- * @returns id of file or blank(when file is not found)
31
- */
32
- export async function getFileId(drive, parentId, fileName) {
33
- try {
34
- if (validateQueryValue(parentId) === false) {
35
- throw new GetFileIdError(`Invalid paretnt id : ${parentId}`);
36
- }
37
- if (validateQueryValue(fileName) === false) {
38
- throw new GetFileIdError(`Invalid file name : ${fileName}`);
39
- }
40
- const list = await drive.files.list({
41
- pageSize: 10,
42
- q: `'${parentId}' in parents and name = '${fileName}'`,
43
- fields: 'files(id, name)'
44
- });
45
- if (list.data.files && list.data.files.length > 0) {
46
- return list.data.files[0].id || '';
47
- }
48
- return '';
49
- }
50
- catch (err) {
51
- if (err.errors) {
52
- throw new GetFileIdError(JSON.stringify(err.errors));
53
- }
54
- throw err;
55
- }
56
- }
57
19
  /**
58
20
  * Create file using by source file into Google Drive.
59
21
  * @param drive - drive instance.
@@ -61,30 +23,41 @@ export async function getFileId(drive, parentId, fileName) {
61
23
  * @returns Print the id of the file that is sended into remote
62
24
  */
63
25
  export async function uploadFile(drive, opts) {
26
+ let ret = '';
64
27
  try {
65
28
  const { parentId, destFileName, srcFileName, destMimeType, srcMimeType } = opts;
66
- const params = {
67
- requestBody: {
68
- name: path.basename(destFileName),
69
- parents: [parentId]
70
- },
71
- media: {
72
- body: fs.createReadStream(srcFileName)
73
- },
74
- fields: 'id'
75
- };
76
- if (destMimeType) {
77
- params.requestBody.mimeType = destMimeType;
29
+ const srcStream = fs.createReadStream(srcFileName);
30
+ try {
31
+ const params = {
32
+ requestBody: {
33
+ name: path.basename(destFileName),
34
+ parents: [parentId]
35
+ },
36
+ media: {
37
+ body: srcStream
38
+ },
39
+ fields: 'id'
40
+ };
41
+ if (destMimeType) {
42
+ params.requestBody.mimeType = destMimeType;
43
+ }
44
+ if (srcMimeType) {
45
+ params.media.mimeType = srcMimeType;
46
+ }
47
+ const res = await drive.files.create(params);
48
+ ret = res.data.id || '';
49
+ }
50
+ catch (err) {
51
+ throw new UploadFileError(JSON.stringify(err.errors));
78
52
  }
79
- if (srcMimeType) {
80
- params.media.mimeType = srcMimeType;
53
+ finally {
54
+ await promisify(srcStream.close.bind(srcStream))();
81
55
  }
82
- const res = await drive.files.create(params);
83
- return res.data.id || '';
84
56
  }
85
57
  catch (err) {
86
- throw new UploadFileError(JSON.stringify(err.errors));
58
+ throw err;
87
59
  }
60
+ return ret;
88
61
  }
89
62
  /**
90
63
  * Update file using by source file into Google Drive.
@@ -93,28 +66,39 @@ export async function uploadFile(drive, opts) {
93
66
  * @returns id of file in Google Drive
94
67
  */
95
68
  export async function updateFile(drive, opts) {
69
+ let ret = '';
96
70
  try {
97
71
  const { fileId, srcFileName, destMimeType, srcMimeType } = opts;
98
- const params = {
99
- fileId,
100
- requestBody: {},
101
- media: {
102
- body: fs.createReadStream(srcFileName)
103
- },
104
- fields: 'id'
105
- };
106
- if (destMimeType) {
107
- params.requestBody.mimeType = destMimeType;
72
+ const srcStream = fs.createReadStream(srcFileName);
73
+ try {
74
+ const params = {
75
+ fileId,
76
+ requestBody: {},
77
+ media: {
78
+ body: srcStream
79
+ },
80
+ fields: 'id'
81
+ };
82
+ if (destMimeType) {
83
+ params.requestBody.mimeType = destMimeType;
84
+ }
85
+ if (srcMimeType) {
86
+ params.media.mimeType = srcMimeType;
87
+ }
88
+ const res = await drive.files.update(params);
89
+ ret = res.data.id || '';
108
90
  }
109
- if (srcMimeType) {
110
- params.media.mimeType = srcMimeType;
91
+ catch (err) {
92
+ throw new UpdateFileError(JSON.stringify(err.errors));
93
+ }
94
+ finally {
95
+ await promisify(srcStream.close.bind(srcStream))();
111
96
  }
112
- const res = await drive.files.update(params);
113
- return res.data.id || '';
114
97
  }
115
98
  catch (err) {
116
- throw new UpdateFileError(JSON.stringify(err.errors));
99
+ throw err;
117
100
  }
101
+ return ret;
118
102
  }
119
103
  /**
120
104
  * Send file using by source file into Google Drive.
package/dist/tshare.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getFileId } from './tsend.js';
1
+ import { getFileId } from './tdrive.js';
2
2
  export class CreatePermissonError extends Error {
3
3
  constructor(message) {
4
4
  //https://stackoverflow.com/questions/41102060/typescript-extending-error-class
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "guratan",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Tiny send tool for Google Drive",
5
5
  "author": "hankei6km <hankei6km@gmail.com> (https://github.com/hankei6km)",
6
6
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/jest": "^27.4.1",
42
- "@types/node": "^17.0.21",
42
+ "@types/node": "^16.11.26",
43
43
  "@types/yargs": "^17.0.9",
44
44
  "jest": "^27.5.1",
45
45
  "rimraf": "^3.0.2",