@nitpicker/report-google-sheets 0.22.2 → 0.24.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/lib/is-drive-folder-url.d.ts +12 -0
- package/lib/is-drive-folder-url.js +15 -0
- package/lib/report.d.ts +27 -4
- package/lib/report.js +42 -3
- package/lib/resolve-spreadsheet-url.d.ts +40 -0
- package/lib/resolve-spreadsheet-url.js +49 -0
- package/package.json +7 -7
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether `sheetUrl` is a Drive folder URL, i.e. whether `report` must create the
|
|
3
|
+
* Spreadsheet (which needs the Drive OAuth scope) instead of writing to an existing one.
|
|
4
|
+
* @param sheetUrl - The `--sheet` value.
|
|
5
|
+
* @returns `true` for `https://drive.google.com/drive/folders/<id>`-style URLs.
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* isDriveFolderUrl('https://drive.google.com/drive/folders/abc123'); //=> true
|
|
9
|
+
* isDriveFolderUrl('https://docs.google.com/spreadsheets/d/xyz/edit'); //=> false
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function isDriveFolderUrl(sheetUrl: string): boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { parseGoogleUrl } from '@d-zero/google-sheets';
|
|
2
|
+
/**
|
|
3
|
+
* Whether `sheetUrl` is a Drive folder URL, i.e. whether `report` must create the
|
|
4
|
+
* Spreadsheet (which needs the Drive OAuth scope) instead of writing to an existing one.
|
|
5
|
+
* @param sheetUrl - The `--sheet` value.
|
|
6
|
+
* @returns `true` for `https://drive.google.com/drive/folders/<id>`-style URLs.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isDriveFolderUrl('https://drive.google.com/drive/folders/abc123'); //=> true
|
|
10
|
+
* isDriveFolderUrl('https://docs.google.com/spreadsheets/d/xyz/edit'); //=> false
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export function isDriveFolderUrl(sheetUrl) {
|
|
14
|
+
return parseGoogleUrl(sheetUrl)?.kind === 'drive-folder';
|
|
15
|
+
}
|
package/lib/report.d.ts
CHANGED
|
@@ -14,10 +14,23 @@ export type SheetName = (typeof SHEET_PRIORITY_ORDER)[number];
|
|
|
14
14
|
export interface ReportParams {
|
|
15
15
|
/** Path to the `.nitpicker` archive file. */
|
|
16
16
|
readonly filePath: string;
|
|
17
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* URL of the target Google Spreadsheet, or of a Drive folder
|
|
19
|
+
* (`https://drive.google.com/drive/folders/<id>`). A folder URL creates a new
|
|
20
|
+
* Spreadsheet in it, titled with the archive's file name minus `.nitpicker`, and
|
|
21
|
+
* reports into that; this additionally requests the Drive OAuth scope, so a token
|
|
22
|
+
* cached from an earlier Spreadsheet-only run must be deleted to re-authorize.
|
|
23
|
+
*/
|
|
18
24
|
readonly sheetUrl: string;
|
|
19
|
-
/**
|
|
20
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Path to the credentials JSON file (OAuth2 Desktop, service account, or
|
|
27
|
+
* authorized user — detected by `@d-zero/google-auth` from the contents).
|
|
28
|
+
* When omitted, `@d-zero/google-auth` resolves `GOOGLE_AUTH_CREDENTIALS`
|
|
29
|
+
* and then Application Default Credentials. This package never reads the
|
|
30
|
+
* environment itself; the CLI's `report`/`pipeline` commands settle the
|
|
31
|
+
* `./credentials.json` fallback before calling here.
|
|
32
|
+
*/
|
|
33
|
+
readonly credentialFilePath?: string;
|
|
21
34
|
/** Path to the nitpicker config file, or `null` for defaults. */
|
|
22
35
|
readonly configPath: string | null;
|
|
23
36
|
/** When `true`, generate all sheets without interactive prompt. */
|
|
@@ -92,6 +105,7 @@ export interface ReportParams {
|
|
|
92
105
|
* `TaskList` display) has finished, mirroring this file's `console.log`
|
|
93
106
|
* calls, which likewise only ever run outside that active display window.
|
|
94
107
|
* @param params - レポート生成に必要なパラメータ
|
|
108
|
+
* @returns レポートを書き込んだスプレッドシートの URL。`sheetUrl` が Drive フォルダ URL のときは新規作成したものの URL(`silent` でも呼び出し側が知れるようにするため)、それ以外は `sheetUrl` そのまま。対話選択がキャンセルされたときは `undefined`
|
|
95
109
|
* @example
|
|
96
110
|
* ```ts
|
|
97
111
|
* await report({
|
|
@@ -103,6 +117,15 @@ export interface ReportParams {
|
|
|
103
117
|
* ```
|
|
104
118
|
* @example
|
|
105
119
|
* ```ts
|
|
120
|
+
* // Omit `credentialFilePath` to use GOOGLE_AUTH_CREDENTIALS, then ADC.
|
|
121
|
+
* await report({
|
|
122
|
+
* filePath: './output.nitpicker',
|
|
123
|
+
* sheetUrl: 'https://docs.google.com/spreadsheets/d/xxx/edit',
|
|
124
|
+
* configPath: null,
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
106
129
|
* // Skip the interactive prompt and generate exactly these sheets.
|
|
107
130
|
* await report({
|
|
108
131
|
* filePath: './output.nitpicker',
|
|
@@ -113,4 +136,4 @@ export interface ReportParams {
|
|
|
113
136
|
* });
|
|
114
137
|
* ```
|
|
115
138
|
*/
|
|
116
|
-
export declare function report(params: ReportParams): Promise<
|
|
139
|
+
export declare function report(params: ReportParams): Promise<string | undefined>;
|
package/lib/report.js
CHANGED
|
@@ -11,9 +11,11 @@ import { createResourcesRelationalTable } from './data/create-resources-relation
|
|
|
11
11
|
import { createResources } from './data/create-resources.js';
|
|
12
12
|
import { createViolations } from './data/create-violations.js';
|
|
13
13
|
import { log } from './debug.js';
|
|
14
|
+
import { isDriveFolderUrl } from './is-drive-folder-url.js';
|
|
14
15
|
import { loadConfig } from './load-config.js';
|
|
15
16
|
import { openReportArchive } from './open-report-archive.js';
|
|
16
17
|
import { getPluginReports } from './reports/get-plugin-reports.js';
|
|
18
|
+
import { resolveSpreadsheetUrl } from './resolve-spreadsheet-url.js';
|
|
17
19
|
import { createSheets } from './sheets/create-sheets.js';
|
|
18
20
|
/**
|
|
19
21
|
* Every generatable sheet name, in **priority order** — the order
|
|
@@ -81,6 +83,7 @@ const URL_FILTERABLE_SHEETS = [
|
|
|
81
83
|
* `TaskList` display) has finished, mirroring this file's `console.log`
|
|
82
84
|
* calls, which likewise only ever run outside that active display window.
|
|
83
85
|
* @param params - レポート生成に必要なパラメータ
|
|
86
|
+
* @returns レポートを書き込んだスプレッドシートの URL。`sheetUrl` が Drive フォルダ URL のときは新規作成したものの URL(`silent` でも呼び出し側が知れるようにするため)、それ以外は `sheetUrl` そのまま。対話選択がキャンセルされたときは `undefined`
|
|
84
87
|
* @example
|
|
85
88
|
* ```ts
|
|
86
89
|
* await report({
|
|
@@ -92,6 +95,15 @@ const URL_FILTERABLE_SHEETS = [
|
|
|
92
95
|
* ```
|
|
93
96
|
* @example
|
|
94
97
|
* ```ts
|
|
98
|
+
* // Omit `credentialFilePath` to use GOOGLE_AUTH_CREDENTIALS, then ADC.
|
|
99
|
+
* await report({
|
|
100
|
+
* filePath: './output.nitpicker',
|
|
101
|
+
* sheetUrl: 'https://docs.google.com/spreadsheets/d/xxx/edit',
|
|
102
|
+
* configPath: null,
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
95
107
|
* // Skip the interactive prompt and generate exactly these sheets.
|
|
96
108
|
* await report({
|
|
97
109
|
* filePath: './output.nitpicker',
|
|
@@ -106,12 +118,24 @@ export async function report(params) {
|
|
|
106
118
|
const { filePath, sheetUrl, credentialFilePath, configPath, all, silent, dedupeResources, onExtractProgress, } = params;
|
|
107
119
|
log('Initialization');
|
|
108
120
|
const warnings = [];
|
|
109
|
-
|
|
121
|
+
// The Drive scope is requested only when a Spreadsheet has to be created, so
|
|
122
|
+
// users writing into an existing Spreadsheet keep their cached token as is.
|
|
123
|
+
const createsSpreadsheet = isDriveFolderUrl(sheetUrl);
|
|
124
|
+
const SCOPES = createsSpreadsheet
|
|
125
|
+
? [
|
|
126
|
+
'https://www.googleapis.com/auth/spreadsheets',
|
|
127
|
+
'https://www.googleapis.com/auth/drive.file',
|
|
128
|
+
]
|
|
129
|
+
: ['https://www.googleapis.com/auth/spreadsheets'];
|
|
110
130
|
log('Authenticating');
|
|
111
131
|
const auth = await authentication(credentialFilePath, SCOPES, {
|
|
112
132
|
tokenFilePath: 'token.json',
|
|
113
133
|
});
|
|
114
134
|
log('Authentication succeeded');
|
|
135
|
+
// An existing Spreadsheet URL is validated by the `Sheets` constructor, so it
|
|
136
|
+
// is built up front to keep a malformed `--sheet` failing before the archive
|
|
137
|
+
// extraction and the interactive prompt. A folder URL has no Spreadsheet yet.
|
|
138
|
+
const existingSheets = createsSpreadsheet ? undefined : new Sheets(sheetUrl, auth);
|
|
115
139
|
log('Opening archive: %s', filePath);
|
|
116
140
|
await using archiveHandle = await openReportArchive(filePath, onExtractProgress);
|
|
117
141
|
const { accessor } = archiveHandle;
|
|
@@ -130,8 +154,6 @@ export async function report(params) {
|
|
|
130
154
|
log('Loading plugin reports');
|
|
131
155
|
const reports = await getPluginReports(accessor);
|
|
132
156
|
log('Plugin reports loaded: %d', reports.length);
|
|
133
|
-
const sheets = new Sheets(sheetUrl, auth);
|
|
134
|
-
log('Reporting starts');
|
|
135
157
|
const availableSheetNames = normalizedUrls === undefined ? SHEET_PRIORITY_ORDER : URL_FILTERABLE_SHEETS;
|
|
136
158
|
if (normalizedUrls !== undefined) {
|
|
137
159
|
const excluded = SHEET_PRIORITY_ORDER.filter((name) => !URL_FILTERABLE_SHEETS.includes(name));
|
|
@@ -221,7 +243,23 @@ export async function report(params) {
|
|
|
221
243
|
}
|
|
222
244
|
}
|
|
223
245
|
}
|
|
246
|
+
// Created only now — after archive/config/selection errors and prompt
|
|
247
|
+
// cancellation can no longer happen — so a failed or aborted run does not
|
|
248
|
+
// leave an empty Spreadsheet behind in the Drive folder.
|
|
249
|
+
const targetUrl = await resolveSpreadsheetUrl({
|
|
250
|
+
sheetUrl,
|
|
251
|
+
archiveFilePath: filePath,
|
|
252
|
+
auth,
|
|
253
|
+
});
|
|
254
|
+
const sheets = existingSheets ?? new Sheets(targetUrl, auth);
|
|
255
|
+
if (createsSpreadsheet) {
|
|
256
|
+
log('Created Spreadsheet: %s', targetUrl);
|
|
257
|
+
}
|
|
224
258
|
if (!silent) {
|
|
259
|
+
if (createsSpreadsheet) {
|
|
260
|
+
// eslint-disable-next-line no-console
|
|
261
|
+
console.log(`\nCreated Spreadsheet: ${targetUrl}`);
|
|
262
|
+
}
|
|
225
263
|
// eslint-disable-next-line no-console
|
|
226
264
|
console.log(`\nGenerating ${createSheetList.length} sheet(s)...\n`);
|
|
227
265
|
}
|
|
@@ -248,4 +286,5 @@ export async function report(params) {
|
|
|
248
286
|
// eslint-disable-next-line no-console
|
|
249
287
|
console.log('\nReport complete.');
|
|
250
288
|
}
|
|
289
|
+
return targetUrl;
|
|
251
290
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createSpreadsheet } from '@d-zero/google-sheets';
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for {@link resolveSpreadsheetUrl}.
|
|
4
|
+
*/
|
|
5
|
+
export interface ResolveSpreadsheetUrlParams {
|
|
6
|
+
/** The `--sheet` value: a Spreadsheet URL, or a Drive folder URL to create one in. */
|
|
7
|
+
readonly sheetUrl: string;
|
|
8
|
+
/** Path to the `.nitpicker` archive; its file name (without extension) titles a created Spreadsheet. */
|
|
9
|
+
readonly archiveFilePath: string;
|
|
10
|
+
/** Authenticated client. Must carry the Drive scope when `sheetUrl` is a Drive folder URL. */
|
|
11
|
+
readonly auth: Parameters<typeof createSpreadsheet>[2];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Resolves the Spreadsheet URL the report should be written to.
|
|
15
|
+
*
|
|
16
|
+
* - **Drive folder URL** (`https://drive.google.com/drive/folders/<id>`): creates a
|
|
17
|
+
* new Spreadsheet in that folder, titled with the archive's file name minus its
|
|
18
|
+
* `.nitpicker` extension, and returns its edit URL. Every call creates a new
|
|
19
|
+
* file — `createSpreadsheet` neither searches for a same-named one nor retries
|
|
20
|
+
* (`files.create` is non-idempotent).
|
|
21
|
+
* - **Anything else** (including URLs `parseGoogleUrl` does not recognize): returned
|
|
22
|
+
* unchanged, so the existing "write into this Spreadsheet" behavior — and its
|
|
23
|
+
* error reporting for a malformed URL — is untouched.
|
|
24
|
+
*
|
|
25
|
+
* The caller decides which OAuth scopes to request with `isDriveFolderUrl` before
|
|
26
|
+
* authenticating, so users who pass a Spreadsheet URL are never asked to
|
|
27
|
+
* re-authorize for Drive access.
|
|
28
|
+
* @param params - See {@link ResolveSpreadsheetUrlParams}.
|
|
29
|
+
* @returns The Spreadsheet URL to write to.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const url = await resolveSpreadsheetUrl({
|
|
33
|
+
* sheetUrl: 'https://drive.google.com/drive/folders/abc123',
|
|
34
|
+
* archiveFilePath: './example.com.nitpicker',
|
|
35
|
+
* auth,
|
|
36
|
+
* });
|
|
37
|
+
* // Creates "example.com" in the folder and returns its edit URL.
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function resolveSpreadsheetUrl(params: ResolveSpreadsheetUrlParams): Promise<string>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { createSpreadsheet } from '@d-zero/google-sheets';
|
|
3
|
+
import { isDriveFolderUrl } from './is-drive-folder-url.js';
|
|
4
|
+
/**
|
|
5
|
+
* Resolves the Spreadsheet URL the report should be written to.
|
|
6
|
+
*
|
|
7
|
+
* - **Drive folder URL** (`https://drive.google.com/drive/folders/<id>`): creates a
|
|
8
|
+
* new Spreadsheet in that folder, titled with the archive's file name minus its
|
|
9
|
+
* `.nitpicker` extension, and returns its edit URL. Every call creates a new
|
|
10
|
+
* file — `createSpreadsheet` neither searches for a same-named one nor retries
|
|
11
|
+
* (`files.create` is non-idempotent).
|
|
12
|
+
* - **Anything else** (including URLs `parseGoogleUrl` does not recognize): returned
|
|
13
|
+
* unchanged, so the existing "write into this Spreadsheet" behavior — and its
|
|
14
|
+
* error reporting for a malformed URL — is untouched.
|
|
15
|
+
*
|
|
16
|
+
* The caller decides which OAuth scopes to request with `isDriveFolderUrl` before
|
|
17
|
+
* authenticating, so users who pass a Spreadsheet URL are never asked to
|
|
18
|
+
* re-authorize for Drive access.
|
|
19
|
+
* @param params - See {@link ResolveSpreadsheetUrlParams}.
|
|
20
|
+
* @returns The Spreadsheet URL to write to.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const url = await resolveSpreadsheetUrl({
|
|
24
|
+
* sheetUrl: 'https://drive.google.com/drive/folders/abc123',
|
|
25
|
+
* archiveFilePath: './example.com.nitpicker',
|
|
26
|
+
* auth,
|
|
27
|
+
* });
|
|
28
|
+
* // Creates "example.com" in the folder and returns its edit URL.
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export async function resolveSpreadsheetUrl(params) {
|
|
32
|
+
const { sheetUrl, archiveFilePath, auth } = params;
|
|
33
|
+
if (!isDriveFolderUrl(sheetUrl)) {
|
|
34
|
+
return sheetUrl;
|
|
35
|
+
}
|
|
36
|
+
const title = path.basename(archiveFilePath, '.nitpicker');
|
|
37
|
+
try {
|
|
38
|
+
const { url } = await createSpreadsheet(sheetUrl, title, auth);
|
|
39
|
+
return url;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
// An OAuth token cached by an earlier run only carries the scopes it was
|
|
43
|
+
// granted with, and `@d-zero/google-auth` never re-prompts for a cached
|
|
44
|
+
// token — the resulting 403 is opaque without this pointer.
|
|
45
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
46
|
+
throw new Error(`Failed to create a Spreadsheet in the Drive folder: ${reason}\n` +
|
|
47
|
+
'If this is a permission error, delete token.json and re-run to authorize Drive access.', { cause: error });
|
|
48
|
+
}
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nitpicker/report-google-sheets",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "Nitpicker reporter that outputs crawl and analysis results to Google Sheets",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -32,11 +32,11 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@d-zero/dealer": "1.14.0",
|
|
34
34
|
"@d-zero/google-auth": "0.7.6",
|
|
35
|
-
"@d-zero/google-sheets": "0.
|
|
35
|
+
"@d-zero/google-sheets": "0.12.0",
|
|
36
36
|
"@d-zero/shared": "0.23.0",
|
|
37
|
-
"@nitpicker/crawler": "0.
|
|
38
|
-
"@nitpicker/query": "0.
|
|
39
|
-
"@nitpicker/types": "0.
|
|
37
|
+
"@nitpicker/crawler": "0.24.0",
|
|
38
|
+
"@nitpicker/query": "0.24.0",
|
|
39
|
+
"@nitpicker/types": "0.24.0",
|
|
40
40
|
"ansi-colors": "4.1.3",
|
|
41
41
|
"debug": "4.4.3",
|
|
42
42
|
"enquirer": "2.4.1"
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/debug": "4.1.13",
|
|
46
46
|
"googleapis": "173.0.0",
|
|
47
|
-
"vitest": "5.0.
|
|
47
|
+
"vitest": "5.0.2"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "840a594573c4abe0aaff1257469b7f2108fcdb19"
|
|
50
50
|
}
|