Skip to content

Commit

Permalink
Fix lint & prettier issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstubbs committed Jan 8, 2024
1 parent be554b7 commit a2fa72e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 26 deletions.
18 changes: 13 additions & 5 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,19 @@ $ pcc logout

## Import existing content from a Drupal site

You must ensure that the JSON API for your Drupal site is enabled (which it should be by default). https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/api-overview

Once you've ensured that it's working, you will need to determine the URL which PCC can use to get the initial results page of posts (e.g. https://example.com/jsonapi/node/article). But please note that the exact URL will depend on which resource type(s) you want to import.

The second and last piece of information you will need before proceeding to import, is the id of the PCC site which the posts should be imported into. Posts are NOT going to be published automatically after importing, but they will be automatically connected to the site id provided.
You must ensure that the JSON API for your Drupal site is enabled (which it
should be by default).
https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/api-overview

Once you've ensured that it's working, you will need to determine the URL which
PCC can use to get the initial results page of posts (e.g.
https://example.com/jsonapi/node/article). But please note that the exact URL
will depend on which resource type(s) you want to import.

The second and last piece of information you will need before proceeding to
import, is the id of the PCC site which the posts should be imported into. Posts
are NOT going to be published automatically after importing, but they will be
automatically connected to the site id provided.

With this information, you can now run the import command.

Expand Down
100 changes: 82 additions & 18 deletions packages/cli/src/cli/commands/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@ type ImportParams = {
verbose: boolean;
};

interface DrupalPost {
id: string;
attributes?: {
body?: {
processed: string;
};
title: string;
};
relationships: {
field_author: {
data: {
id: string;
};
};
field_topics?: {
data: [
{
id: string;
},
];
};
};
}

interface DrupalTopic {
id: string;
attributes?: {
name: string;
};
}

interface DrupalIncludedData {
id: string;
attributes?: {
name: string;
title: string;
};
}

async function getDrupalPosts(url: string) {
try {
console.log(`Importing from ${url}`);
Expand All @@ -29,8 +68,8 @@ async function getDrupalPosts(url: string) {
posts: result.data,
includedData: result.included,
};
} catch (e: any) {
console.log(e, e.message);
} catch (e) {
console.error(e);
throw e;
}
}
Expand All @@ -53,9 +92,15 @@ export const importFromDrupal = errorHandler<ImportParams>(
}

await login(["https://www.googleapis.com/auth/drive.file"]);
let authDetails = await getLocalAuthDetails();
const authDetails = await getLocalAuthDetails();

if (!authDetails) {
logger.error(chalk.red(`ERROR: Failed to retrieve login details. `));
exit(1);
}

const oauth2Client = new OAuth2Client();
oauth2Client.setCredentials(authDetails!);
oauth2Client.setCredentials(authDetails);
const drive = google.drive({
version: "v3",
auth: oauth2Client,
Expand All @@ -71,12 +116,23 @@ export const importFromDrupal = errorHandler<ImportParams>(
})
.catch(console.error)) as GaxiosResponse<drive_v3.Schema$File>;

const folderId = folderRes.data.id;

if (folderId == null) {
logger.error(
chalk.red(
`Failed to create parent folder which we would have imported posts into`,
),
);
exit(1);
}

// Get results.
let page = 0;
let { url, query } = queryString.parseUrl(baseUrl);
const { url, query } = queryString.parseUrl(baseUrl);
query.include = "field_author,field_topics";
let allPosts: any[] = [];
let allIncludedData: any[] = [];
const allPosts: DrupalPost[] = [];
const allIncludedData: DrupalIncludedData[] = [];
let nextURL = queryString.stringifyUrl({ url, query });

do {
Expand All @@ -92,7 +148,9 @@ export const importFromDrupal = errorHandler<ImportParams>(
}
} while (nextURL != null && ++page < 1000);

logger.log(chalk.green(`Retrieved ${allPosts.length} posts after ${page} pages`));
logger.log(
chalk.green(`Retrieved ${allPosts.length} posts after ${page} pages`),
);

// Ensure that these metadata fields exist.
await AddOnApiHelper.addSiteMetadataField(
Expand All @@ -117,45 +175,51 @@ export const importFromDrupal = errorHandler<ImportParams>(
}

// Create the google doc.
const authorName = allIncludedData.find(
const authorName: string | undefined = allIncludedData.find(
(x) => x.id === post.relationships.field_author.data.id,
)?.attributes?.title;

const res = (await drive.files.create({
requestBody: {
// Name from the article.
name: post.attributes.title,
mimeType: "application/vnd.google-apps.document",
parents: [folderRes.data.id!],
parents: [folderId],
},
media: {
mimeType: "text/html",
body: post.attributes.body.processed,
},
})) as GaxiosResponse<drive_v3.Schema$File>;
const fileId = res.data.id;

if (!fileId) {
throw new Error(`Failed to create file for ${post.attributes.title}`);
}

// Add it to the PCC site.
await AddOnApiHelper.getDocument(res.data.id!, true);
await AddOnApiHelper.getDocument(fileId, true);

try {
await AddOnApiHelper.updateDocument(
res.data.id!,
fileId,
siteId,
post.attributes.title,
post.relationships.field_topics?.data
?.map(
(topic: any) =>
allIncludedData.find((x: any) => x.id === topic.id)
?.attributes?.name,
(topic: DrupalTopic) =>
allIncludedData.find((x) => x.id === topic.id)?.attributes
?.name,
)
.filter((x: string | undefined) => x != null) || [],
.filter((x: string | undefined): x is string => x != null) || [],
{
author: authorName,
drupalId: post.id,
},
verbose,
);
} catch (e: any) {
console.error(e.response?.data);
} catch (e) {
console.error(e instanceof AxiosError ? e.response?.data : e);
throw e;
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cli/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function login(extraScopes: string[]): Promise<void> {
try {
const authData = await getLocalAuthDetails();
if (authData) {
let scopes = authData.scope?.split(" ");
const scopes = authData.scope?.split(" ");

if (
!extraScopes?.length ||
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/lib/addonApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AddOnApiHelper {

static async getDocument(
documentId: string,
insertIfMissing: boolean = false,
insertIfMissing = false,
): Promise<Article> {
const idToken = await this.getIdToken();

Expand Down Expand Up @@ -98,7 +98,10 @@ class AddOnApiHelper {
siteId: string,
title: string,
tags: string[],
metadataFields: any,
metadataFields: {
[key: string]: string | number | boolean | undefined | null;
},

verbose?: boolean,
): Promise<Article> {
const idToken = await this.getIdToken();
Expand Down

0 comments on commit a2fa72e

Please sign in to comment.