Skip to content

Commit

Permalink
Add --log-mode=stdout|file|directory option, better error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Aug 22, 2024
1 parent 69c7581 commit 9efdcf2
Show file tree
Hide file tree
Showing 17 changed files with 564 additions and 397 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ oclif.manifest.json
datocms.config.json
migrations
.env
**/.DS_Store/
**/.DS_Store/
**/api-calls/*.log
**/api-calls.log
647 changes: 315 additions & 332 deletions packages/cli-plugin-contentful/package-lock.json

Large diffs are not rendered by default.

16 changes: 4 additions & 12 deletions packages/cli-plugin-contentful/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@datocms/cli-plugin-contentful",
"version": "2.0.6",
"description": "Plugin for DatoCMS CLI to import projects from Contentful to DatoCMS",
"keywords": [
"contentful",
"datocms",
"import"
],
"keywords": ["contentful", "datocms", "import"],
"engines": {
"node": ">=12.0.0"
},
Expand All @@ -15,11 +11,7 @@
"license": "MIT",
"main": "lib/src/index.js",
"types": "lib/src/index.d.js",
"files": [
"/lib",
"/npm-shrinkwrap.json",
"/oclif.manifest.json"
],
"files": ["/lib", "/npm-shrinkwrap.json", "/oclif.manifest.json"],
"publishConfig": {
"access": "public"
},
Expand All @@ -39,7 +31,7 @@
"url": "https://github.com/datocms/cli/issues"
},
"dependencies": {
"@datocms/cli-utils": "^0.1.9",
"@datocms/cli-utils": "^2.0.5",
"@oclif/core": "^1.8.0",
"async-scheduler": "^1.4.4",
"contentful-management": "^10.6.0",
Expand All @@ -66,5 +58,5 @@
"commands": "./lib/commands",
"repositoryPrefix": "<%- repo %>/blob/v<%- version %>/packages/cli-plugin-contentful/<%- commandPath %>"
},
"gitHead": "fa1595ded6785e92d5483f464a139fcf7be5c395"
"gitHead": "69c758175e09559c3267542f5b70efa48292a7dc"
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export default class ImportCommand extends CmaClientCommand<
contentfulToken: this.parsedFlags['contentful-token'],
contentfulSpaceId: this.parsedFlags['contentful-space-id'],
contentfulEnvironment: this.parsedFlags['contentful-environment'],
logLevel: this.parsedFlags['log-level'],
logFn: this.buildBaseClientInitializationOptions().logFn,
});

const options: Omit<StepOptions, 'ctx' | 'task'> = {
Expand Down
23 changes: 22 additions & 1 deletion packages/cli-plugin-contentful/src/import/base-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import type { Environment } from 'contentful-management';
import type { ListrRendererFactory, ListrTaskWrapper } from 'listr2';
import type { Context, StepOptions } from '../commands/contentful/import';

export class CuncurrentItemError extends Error {
task: string;
subjectIdentifier: string;
subject: unknown;
originalError: unknown;

constructor(
task: string,
subjectIdentifier: string,
subject: unknown,
originalError: unknown,
) {
super(`Error when executing [${task} > ${subjectIdentifier}]`);

this.task = task;
this.subjectIdentifier = subjectIdentifier;
this.subject = subject;
this.originalError = originalError;
}
}

export default class BaseStep {
protected options: StepOptions;

Expand Down Expand Up @@ -80,7 +101,7 @@ export default class BaseStep {
} catch (e) {
failed += 1;
if (!this.ignoreErrors) {
throw e;
throw new CuncurrentItemError(title, identifier, item, e);
}
} finally {
finished += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Confirm that you want to destroy them?`,
task,
removeValidationsLog,
ctx.datoItemTypes,
(itemType) => itemType.id,
(itemType) => `Model ${itemType.id}`,
async (itemType) => {
const typeLinksField = (
await this.client.fields.list(itemType.id)
Expand Down Expand Up @@ -136,7 +136,7 @@ Confirm that you want to destroy them?`,
task,
destroyModelsLog,
ctx.itemTypesToDestroy,
(itemType) => itemType.id,
(itemType) => `Model ${itemType.id}`,
async (itemType) => {
await this.client.itemTypes.destroy(itemType);
},
Expand Down
9 changes: 6 additions & 3 deletions packages/cli-plugin-contentful/src/import/import-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ export default class ImportAssets extends BaseStep {
createAssetsLog,
contentfulAssets,
(contentfulAsset) =>
`Asset ${
contentfulAsset.fields.file?.[ctx.defaultLocale]?.fileName ||
contentfulAsset.sys.id
`Asset ${contentfulAsset.sys.id} ${
contentfulAsset.fields.file?.[ctx.defaultLocale]?.url
? ` (https://${
contentfulAsset.fields.file?.[ctx.defaultLocale]?.url
})`
: ''
}`,
async (contentfulAsset, notify) => {
const fileUrl = contentfulAsset.fields.file?.[ctx.defaultLocale]?.url;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
CmaClient,
type LogLevelFlagEnum,
logLevelMap,
} from '@datocms/cli-utils';
import {
type ClientAPI,
type Environment,
Expand All @@ -8,21 +13,75 @@ type ContentfulClientType = {
contentfulToken: string | undefined;
contentfulSpaceId: string | undefined;
contentfulEnvironment: string | undefined;
logLevel?: LogLevelFlagEnum;
logFn?: (message: string) => void;
};

let requestCount = 1;

export async function cfEnvironmentApi({
contentfulToken,
contentfulSpaceId,
contentfulEnvironment = 'master',
logLevel: logLevelString = 'NONE',
logFn: log = () => true,
}: ContentfulClientType): Promise<Environment> {
if (!(contentfulToken && contentfulSpaceId)) {
throw new Error(
'You need to provide a read-only Contentful API token and a Contentful space ID!',
);
}

const logLevel = logLevelMap[logLevelString];

const contentfulClient: ClientAPI = createClient({
accessToken: contentfulToken,
onBeforeRequest: (requestConfig) => {
const requestId = `CF${requestCount}`;

requestCount += 1;

return { ...requestConfig, __requestId: requestId };
},

responseLogger: (response) => {
if (response instanceof Error) {
return;
}

const { config: request, status, statusText } = response;
const {
url,
method,
__requestId: requestId,
} = request as typeof request & {
__requestId?: number;
};

if (logLevel >= CmaClient.LogLevel.BASIC) {
log(`[${requestId}] ${method?.toUpperCase()} ${url}`);
if (logLevel >= CmaClient.LogLevel.BODY_AND_HEADERS) {
for (const [key, value] of Object.entries(request.headers || {})) {
log(`[${requestId}] ${key}: ${value}`);
}
}
if (logLevel >= CmaClient.LogLevel.BODY && request.data) {
log(`[${requestId}] ${JSON.stringify(request.data, null, 2)}`);
}
}

if (logLevel >= CmaClient.LogLevel.BASIC) {
log(`[${requestId}] Status: ${status} (${statusText})`);
if (logLevel >= CmaClient.LogLevel.BODY_AND_HEADERS) {
for (const [key, value] of Object.entries(response.headers || {})) {
log(`[${requestId}] ${key}: ${value}`);
}
}
if (logLevel >= CmaClient.LogLevel.BODY && response.data) {
log(`[${requestId}] ${JSON.stringify(response.data, null, 2)}`);
}
}
},
});

const contentful = await contentfulClient.getSpace(contentfulSpaceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,50 +50,50 @@ export const toFieldApiKey = (value: string): string => {
return apiKey;
};

export function contentFieldTypeToDatoFieldType(field: ContentFields): string {
export function contentFieldTypeToDatoFieldType(field: ContentFields) {
switch (field.type) {
case 'Symbol':
return 'string';
return 'string' as const;
case 'Text':
return 'text';
return 'text' as const;
case 'Integer':
return 'integer';
return 'integer' as const;
case 'Number':
return 'float';
return 'float' as const;
case 'Date':
return 'date_time';
return 'date_time' as const;
case 'Location':
return 'lat_lon';
return 'lat_lon' as const;
case 'Boolean':
return 'boolean';
return 'boolean' as const;
case 'Object':
return 'json';
return 'json' as const;
case 'RichText':
return 'structured_text';
return 'structured_text' as const;
case 'Link':
switch (field.linkType) {
case 'Entry':
return 'link';
return 'link' as const;
case 'Asset':
return 'file';
return 'file' as const;
default:
return 'string';
return 'string' as const;
}

case 'Array':
switch (field.items?.linkType) {
case 'Asset':
return 'gallery';
return 'gallery' as const;
case 'Entry':
return 'links';
return 'links' as const;
case 'Symbol':
return 'string';
return 'string' as const;
default:
return 'string';
return 'string' as const;
}

default:
return 'string';
return 'string' as const;
}
}

Expand Down
11 changes: 2 additions & 9 deletions packages/cli-plugin-wordpress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
"url": "https://github.com/datocms/cli.git",
"directory": "packages/cli-plugin-wordpress"
},
"files": [
"/lib",
"/npm-shrinkwrap.json",
"/oclif.manifest.json"
],
"files": ["/lib", "/npm-shrinkwrap.json", "/oclif.manifest.json"],
"publishConfig": {
"access": "public"
},
Expand Down Expand Up @@ -43,10 +39,7 @@
"node": ">=12.0.0"
},
"bugs": "https://github.com/datocms/cli/issues",
"keywords": [
"datocms",
"cli"
],
"keywords": ["datocms", "cli"],
"types": "lib/src/index.d.ts",
"gitHead": "156eff36152666cf811463135b831818026501a0",
"devDependencies": {
Expand Down
23 changes: 22 additions & 1 deletion packages/cli-plugin-wordpress/src/import/base-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import type WPAPI from 'wpapi';
import type { WPRequest } from 'wpapi';
import type { Context, StepOptions } from '../commands/wordpress/import';

export class CuncurrentItemError extends Error {
task: string;
subjectIdentifier: string;
subject: unknown;
originalError: unknown;

constructor(
task: string,
subjectIdentifier: string,
subject: unknown,
originalError: unknown,
) {
super(`Error when executing [${task} > ${subjectIdentifier}]`);

this.task = task;
this.subjectIdentifier = subjectIdentifier;
this.subject = subject;
this.originalError = originalError;
}
}

export default class BaseStep {
protected options: StepOptions;

Expand Down Expand Up @@ -81,7 +102,7 @@ export default class BaseStep {
} catch (e) {
failed += 1;
if (!this.ignoreErrors) {
throw e;
throw new CuncurrentItemError(title, identifier, item, e);
}
} finally {
finished += 1;
Expand Down
Loading

0 comments on commit 9efdcf2

Please sign in to comment.