Skip to content

Commit

Permalink
Implement deployment of authors and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalikL committed Dec 24, 2024
1 parent 1089ce2 commit 473d7a9
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 105 deletions.
78 changes: 58 additions & 20 deletions ops/deploy/deploy-authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import yaml from "js-yaml"
import fs from "fs-extra"
import { fdir } from "fdir"
import { getResourceInfo } from "./deploy-resources.js"
import { isMainModule, parseResourcePath } from "../helpers/helpers.js"
import { isMainModule, parseResourcePath, sortResourcesByPattern } from "../helpers/helpers.js"
import {
API_DIST,
SOURCE_DIR,
Expand All @@ -15,11 +15,18 @@ import {
AUTHORS_FEED_FILENAME,
AUTHORS_INFO_FILENAME,
FEED_VIEWS,
FEED_SCOPES,
FEED_SCOPES, FEED_DIRECTION,
} from "../helpers/constants.js"
import crypto from "crypto"
import { getLanguageInfo } from "./deploy-languages.js"
import picomatch from "picomatch"

let getAuthorInfo = async function (author) {
let getAuthorInfo = async function (author, full) {
const authorInfo = yaml.load(fs.readFileSync(author, "utf8"))
if (!full) {
delete authorInfo.details
delete authorInfo.links
}
return authorInfo
}

Expand Down Expand Up @@ -65,59 +72,73 @@ let processAuthors = async function () {
.sync();

const allTaggedResources = await getAllTaggedResources()
const allCategories = {}
const allAuthors = {}

for (let author of authors) {

try {
const authorInfo = await getAuthorInfo(`${SOURCE_DIR}/${author}`)
const authorInfo = await getAuthorInfo(`${SOURCE_DIR}/${author}`, true)
const authorInfoDetail = { ...authorInfo }
const authorPathInfo = parseResourcePath(`${SOURCE_DIR}/${author}`)
const languageInfo = await getLanguageInfo(authorPathInfo.language)
const authorFeed = await getAuthorFeed(`${SOURCE_DIR}/${authorPathInfo.language}/${AUTHORS_DIRNAME}/${authorPathInfo.title}/${AUTHORS_FEED_FILENAME}`)

authorInfoDetail.feed = []
authorInfoDetail.feed = {}
authorInfoDetail.feed.title = authorInfo.title
authorInfoDetail.feed.groups = []

const authorResources = allTaggedResources.resources.filter(r => r.author === authorPathInfo.title)

authorFeed.map(g => {
authorInfoDetail.feed.push({
authorFeed.groups.map((g, index) => {
authorInfoDetail.feed.groups.push({
...g,
title: g.group,
resources: [],
author: g.author || null,
resourceIds: g.resources || [],
scope: g.scope || null,
view: g.view || FEED_VIEWS.TILE,
view: g.view || FEED_VIEWS.FOLIO,
direction: g.direction || FEED_DIRECTION.HORIZONTAL,
type: "author",
seeAll: languageInfo.feedSeeAll ?? "See All",
id: crypto.createHash("sha256").update(
`${authorPathInfo.language}-authors-${g.group}-${index}`
).digest("hex"),
})
})

for (let authorResource of authorResources) {
// name
let groupByName = authorInfoDetail.feed.find(g => g.resourceIds.indexOf(authorResource.id) >= 0)
let groupByName = authorInfoDetail.feed.groups.find(g => g.resourceIds.some(i => picomatch(i)(authorResource.id) ))

if (groupByName) {
groupByName.resources.push(authorResource)
groupByName.resources = sortResourcesByPattern(groupByName.resources, groupByName.resourceIds)
continue
}

let groupByAuthor = authorInfoDetail.feed.find(g => g.author === authorResource.author)
let groupByAuthor = authorInfoDetail.feed.groups.find(g => g.author === authorResource.author)
if (groupByAuthor) {
groupByAuthor.resources.push(authorResource)
groupByAuthor.resources = sortResourcesByPattern(groupByAuthor.resources, groupByAuthor.resourceIds)
continue
}

let groupByKind = authorInfoDetail.feed.find(g => g.kind === authorResource.kind)
let groupByKind = authorInfoDetail.feed.groups.find(g => g.kind === authorResource.kind)
if (groupByKind) {
groupByKind.resources.push(authorResource)
groupByKind.resources = sortResourcesByPattern(groupByKind.resources, groupByKind.resourceIds)
continue
}

let groupByScope = authorInfoDetail.feed.find(g => g.scope === FEED_SCOPES.RESOURCE)
let groupByScope = authorInfoDetail.feed.groups.find(g => g.scope === FEED_SCOPES.RESOURCE)
if (groupByScope) {
groupByScope.resources.push(authorResource)
groupByScope.resources = sortResourcesByPattern(groupByScope.resources, groupByScope.resourceIds)
}
}


authorInfoDetail.feed = authorInfoDetail.feed.filter(g => {
authorInfoDetail.feed.groups = authorInfoDetail.feed.groups.filter(g => {
// filter feed groups that do not have any resources or docs
return g.resources.length
}).map(g => {
Expand All @@ -133,10 +154,27 @@ let processAuthors = async function () {
return g
})

if (!allCategories[authorPathInfo.language]) {
allCategories[authorPathInfo.language] = [authorInfo]
if (!allAuthors[authorPathInfo.language]) {
allAuthors[authorPathInfo.language] = [authorInfo]
} else {
allCategories[authorPathInfo.language].push(authorInfo)
allAuthors[authorPathInfo.language].push(authorInfo)
}

if (authorInfoDetail.feed.groups.length === 1) {
authorInfoDetail.feed.groups[0].direction = FEED_DIRECTION.VERTICAL
delete authorInfoDetail.feed.groups[0].seeAll
}

for(let authorFeedGroup of authorInfoDetail.feed.groups) {
let authorFeedGroupFinal = JSON.parse(JSON.stringify(authorFeedGroup))
authorFeedGroupFinal.direction = FEED_DIRECTION.VERTICAL
delete authorFeedGroupFinal.seeAll

fs.outputFileSync(`${API_DIST}/${authorPathInfo.language}/${authorPathInfo.type}/${authorPathInfo.title}/feeds/${authorFeedGroup.id}/index.json`, JSON.stringify(authorFeedGroupFinal))

if (authorInfoDetail.feed.groups.length > 1 && authorFeedGroup["resources"].length > 10) {
authorFeedGroup["resources"] = authorFeedGroup["resources"].slice(0, 10)
}
}

fs.outputFileSync(`${API_DIST}/${authorPathInfo.language}/${authorPathInfo.type}/${authorPathInfo.title}/index.json`, JSON.stringify(authorInfoDetail))
Expand All @@ -145,8 +183,8 @@ let processAuthors = async function () {
}
}

for (let language of Object.keys(allCategories)) {
fs.outputFileSync(`${API_DIST}/${language}/${AUTHORS_DIRNAME}/index.json`, JSON.stringify(allCategories[language]))
for (let language of Object.keys(allAuthors)) {
fs.outputFileSync(`${API_DIST}/${language}/${AUTHORS_DIRNAME}/index.json`, JSON.stringify(allAuthors[language]))
}
}

Expand Down
124 changes: 78 additions & 46 deletions ops/deploy/deploy-categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import yaml from "js-yaml"
import { fdir } from "fdir"
import { getResourceInfo } from "./deploy-resources.js"
import { getDocumentInfoYml } from "./deploy-documents.js"
import { isMainModule, parseResourcePath } from "../helpers/helpers.js"
import { isMainModule, parseResourcePath, sortResourcesByPattern } from "../helpers/helpers.js"
import {
API_DIST,
SOURCE_DIR,
Expand All @@ -18,6 +18,9 @@ import {
FEED_VIEWS,
FEED_SCOPES, FEED_DIRECTION,
} from "../helpers/constants.js"
import crypto from "crypto"
import { getLanguageInfo } from "./deploy-languages.js"
import picomatch from "picomatch"

let getCategoryInfo = async function (category) {
const categoryInfo = yaml.load(fs.readFileSync(category, "utf8"))
Expand All @@ -39,17 +42,17 @@ let getAllTaggedResources = async function () {
.crawl(SOURCE_DIR)
.sync();

const documents = new fdir()
.withBasePath()
.withRelativePaths()
.withMaxDepth(5)
.glob(`**/+(${RESOURCE_TYPE.DEVO}|${RESOURCE_TYPE.PM})/**/content/**/info.yml`)
.crawl(SOURCE_DIR)
.sync();
// const documents = new fdir()
// .withBasePath()
// .withRelativePaths()
// .withMaxDepth(5)
// .glob(`**/+(${RESOURCE_TYPE.DEVO}|${RESOURCE_TYPE.PM})/**/content/**/info.yml`)
// .crawl(SOURCE_DIR)
// .sync();

let allTaggedResources = {
resources: [],
documents: [],
// documents: [],
}

for (let resource of resources) {
Expand All @@ -63,19 +66,19 @@ let getAllTaggedResources = async function () {
}
}

for (let document of documents) {
try {
let documentInfo = await getDocumentInfoYml(`${SOURCE_DIR}/${document}`)
if (documentInfo.categories) {
const documentPathInfo = parseResourcePath(`${SOURCE_DIR}/${document}`)
const parentResource = await getResourceInfo(`${SOURCE_DIR}/${documentPathInfo.language}/${documentPathInfo.type}/${documentPathInfo.title}/${RESOURCE_INFO_FILENAME}`)
documentInfo.parentResource = parentResource
allTaggedResources.documents.push(documentInfo)
}
} catch (e) {
console.error(`Error processing tagged document: ${e}`);
}
}
// for (let document of documents) {
// try {
// let documentInfo = await getDocumentInfoYml(`${SOURCE_DIR}/${document}`)
// if (documentInfo.categories) {
// const documentPathInfo = parseResourcePath(`${SOURCE_DIR}/${document}`)
// const parentResource = await getResourceInfo(`${SOURCE_DIR}/${documentPathInfo.language}/${documentPathInfo.type}/${documentPathInfo.title}/${RESOURCE_INFO_FILENAME}`)
// documentInfo.parentResource = parentResource
// allTaggedResources.documents.push(documentInfo)
// }
// } catch (e) {
// console.error(`Error processing tagged document: ${e}`);
// }
// }

return allTaggedResources
}
Expand All @@ -97,79 +100,91 @@ let processCategories = async function () {
const categoryInfo = await getCategoryInfo(`${SOURCE_DIR}/${category}`)
const categoryInfoDetail = { ...categoryInfo }
const categoryPathInfo = parseResourcePath(`${SOURCE_DIR}/${category}`)
const languageInfo = await getLanguageInfo(categoryPathInfo.language)
const categoryFeed = await getCategoryFeed(`${SOURCE_DIR}/${categoryPathInfo.language}/${CATEGORIES_DIRNAME}/${categoryPathInfo.title}/${CATEGORY_FEED_FILENAME}`)

categoryInfoDetail.feed = []
categoryInfoDetail.feed = {}
categoryInfoDetail.title = categoryInfo.title
categoryInfoDetail.feed.groups = []

const categoryResources = allTaggedResources.resources.filter(r => r.categories.indexOf(categoryPathInfo.title) >= 0)
const categoryDocuments = allTaggedResources.documents.filter(d => d.categories.indexOf(categoryPathInfo.title) >= 0)
// const categoryDocuments = allTaggedResources.documents.filter(d => d.categories.indexOf(categoryPathInfo.title) >= 0)

categoryFeed.map(g => {
categoryInfoDetail.feed.push({
categoryFeed.map((g, index) => {
categoryInfoDetail.feed.groups.push({
...g,
title: g.group,
resources: [],
documents: [],
// documents: [],
author: g.author || null,
resourceIds: g.resources || [],
documentIds: [],
// documentIds: [],
scope: g.scope || null,
view: g.view || FEED_VIEWS.TILE,
view: g.view || FEED_VIEWS.FOLIO,
direction: g.direction || FEED_DIRECTION.HORIZONTAL,
type: "category",
seeAll: languageInfo.feedSeeAll ?? "See All",
id: crypto.createHash("sha256").update(
`${categoryPathInfo.language}-authors-${g.group}-${index}`
).digest("hex"),
})
})

for (let categoryResource of categoryResources) {
let groupByName = categoryInfoDetail.feed.find(g => g.resourceIds.indexOf(categoryResource.id) >= 0)
let groupByName = categoryInfoDetail.feed.groups.find(g => g.resourceIds.some(i => picomatch(i)(authorResource.id) ))
if (groupByName) {
groupByName.resources.push(categoryResource)
groupByName.resources = sortResourcesByPattern(groupByName.resources, groupByName.resourceIds)
continue
}

let groupByAuthor = categoryInfoDetail.feed.find(g => g.author === categoryResource.author)
let groupByAuthor = categoryInfoDetail.feed.groups.find(g => g.author === categoryResource.author)
if (groupByAuthor) {
groupByAuthor.resources.push(categoryResource)
groupByAuthor.resources = sortResourcesByPattern(groupByAuthor.resources, groupByAuthor.resourceIds)
continue
}

let groupByKind = categoryInfoDetail.feed.find(g => g.kind === categoryResource.kind)
let groupByKind = categoryInfoDetail.feed.groups.find(g => g.kind === categoryResource.kind)
if (groupByKind) {
groupByKind.resources.push(categoryResource)
groupByKind.resources = sortResourcesByPattern(groupByKind.resources, groupByKind.resourceIds)
continue
}

let groupByScope = categoryInfoDetail.feed.find(g => g.scope === FEED_SCOPES.RESOURCE)
let groupByScope = categoryInfoDetail.feed.groups.find(g => g.scope === FEED_SCOPES.RESOURCE)
if (groupByScope) {
groupByScope.resources.push(categoryResource)
groupByScope.resources = sortResourcesByPattern(groupByScope.resources, groupByScope.resourceIds)
}
}

for (let categoryDocument of categoryDocuments) {
let groupByScope = categoryInfoDetail.feed.find(g => g.scope === FEED_SCOPES.DOCUMENT)
if (groupByScope) {
groupByScope.documents.push(categoryDocument)
}
}
// for (let categoryDocument of categoryDocuments) {
// let groupByScope = categoryInfoDetail.feed.find(g => g.scope === FEED_SCOPES.DOCUMENT)
// if (groupByScope) {
// groupByScope.documents.push(categoryDocument)
// }
// }

categoryInfoDetail.feed = categoryInfoDetail.feed.filter(g => {
categoryInfoDetail.feed.groups = categoryInfoDetail.feed.groups.filter(g => {
// filter feed groups that do not have any resources or docs
return g.resources.length || g.documents.length
return g.resources.length // || g.documents.length
}).map(g => {
// remove unnecessary params
delete g.kind
delete g.resourceIds
delete g.documentIds
// delete g.documentIds
delete g.author
delete g.group
if (!g.scope && g.resources.length) {
g.scope = FEED_SCOPES.RESOURCE
}

// If scope is document, force "square" view
if ((!g.scope && g.document.length) || (g.scope === FEED_SCOPES.DOCUMENT)-0) {
g.scope = FEED_SCOPES.DOCUMENT
g.view = FEED_VIEWS.SQUARE
}
// if ((!g.scope && g.document.length) || (g.scope === FEED_SCOPES.DOCUMENT)-0) {
// g.scope = FEED_SCOPES.DOCUMENT
// g.view = FEED_VIEWS.SQUARE
// }
return g
})

Expand All @@ -179,6 +194,23 @@ let processCategories = async function () {
allCategories[categoryPathInfo.language].push(categoryInfo)
}

if (categoryInfoDetail.feed.groups.length === 1) {
categoryInfoDetail.feed.groups[0].direction = FEED_DIRECTION.VERTICAL
delete categoryInfoDetail.feed.groups[0].seeAll
}

for(let categoryFeedGroup of categoryInfoDetail.feed.groups) {
let categoryFeedGroupFinal = JSON.parse(JSON.stringify(categoryFeedGroup))
categoryFeedGroupFinal.direction = FEED_DIRECTION.VERTICAL
delete categoryFeedGroupFinal.seeAll

fs.outputFileSync(`${API_DIST}/${categoryPathInfo.language}/${categoryPathInfo.type}/${categoryPathInfo.title}/feeds/${categoryFeedGroup.id}/index.json`, JSON.stringify(categoryFeedGroupFinal))

if (categoryInfoDetail.feed.groups.length > 1 && categoryFeedGroup["resources"].length > 10) {
categoryFeedGroup["resources"] = categoryFeedGroup["resources"].slice(0, 10)
}
}

fs.outputFileSync(`${API_DIST}/${categoryPathInfo.language}/${categoryPathInfo.type}/${categoryPathInfo.title}/index.json`, JSON.stringify(categoryInfoDetail))
} catch (e) {
console.error(`Error processing categories: ${e}`);
Expand Down
Loading

0 comments on commit 473d7a9

Please sign in to comment.