Skip to content

Commit

Permalink
fix: package.json sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jill64 committed Dec 20, 2023
1 parent f429907 commit 2402239
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 36 deletions.
3 changes: 1 addition & 2 deletions packages/action/src/ghosts/docs/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Ghost } from '@/action/types/Ghost.js'
import { updatePackageJsonList } from './updatePackageJsonList.js'
import { updateReadme } from './updateReadme.js'
import { listWorkflowFiles } from './utils/listWorkflowFiles.js'
import { updateReadmeList } from './updateReadmeList.js'

Check warning on line 3 in packages/action/src/ghosts/docs/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/index.ts#L3

Added line #L3 was not covered by tests
import { listWorkflowFiles } from './utils/listWorkflowFiles.js'

export const docs: Ghost = async ({
payload: {
Expand Down
48 changes: 21 additions & 27 deletions packages/action/src/ghosts/docs/updatePackageJson.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { readFile, writeFile } from 'node:fs/promises'
import path from 'node:path'
import * as core from 'octoflare/action/core'

Check warning on line 3 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L2-L3

Added lines #L2 - L3 were not covered by tests
import { ActionRepository } from '../../types/ActionRepository.js'
import { isValidPackageJson } from './utils/isValidPackageJson.js'
import * as core from 'octoflare/action/core'

type Dict = {
[key: string]: string | Dict | undefined
}

Check warning on line 9 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L6-L9

Added lines #L6 - L9 were not covered by tests

export const updatePackageJson =
(repository: ActionRepository) =>
({
repository,
repoLevelConfig
}: {
repository: ActionRepository
repoLevelConfig: Dict
}) =>

Check warning on line 18 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L12-L18

Added lines #L12 - L18 were not covered by tests
async (packageJsonPath: string): Promise<boolean> => {
const packageJsonStr = await readFile(packageJsonPath, 'utf-8')
const json = JSON.parse(packageJsonStr)
Expand All @@ -15,41 +26,24 @@ export const updatePackageJson =
return false
}

const { topics, owner } = repository
const isRepoRoot =
path.relative(process.cwd(), packageJsonPath) === 'package.json'

Check warning on line 30 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L29-L30

Added lines #L29 - L30 were not covered by tests

const publishConfig = packageJson.name?.startsWith('@')
? { publishConfig: { access: 'public' } }
: {}

const license = repository.license?.spdx_id
? { license: repository.license.spdx_id }
const description = isRepoRoot
? { description: repository.description }

Check warning on line 37 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L36-L37

Added lines #L36 - L37 were not covered by tests
: {}

const keywords = topics ?? []

const html = await fetch(repository.html_url).then((res) => res.text())
const repo_image =
html.match(/<meta property="og:image" content="(\S*)"\s*\/>/)?.[1] ?? ''
const keywords = isRepoRoot ? { keywords: repository.topics ?? [] } : {}

Check warning on line 40 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L40

Added line #L40 was not covered by tests

const repoInfo = {
description: repository.description ?? '',
...license,
bugs: `${repository.html_url}/issues`,
homepage: `${repository.html_url}#readme`,
author: {
name: owner.login,
email: 'intents.turrets0h@icloud.com',
url: owner.html_url,
image: owner.avatar_url
},
repository: {
type: 'git',
url: repository.clone_url,
image: repo_image
},
...repoLevelConfig,
...description,

Check warning on line 44 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L43-L44

Added lines #L43 - L44 were not covered by tests
...publishConfig,
keywords,
prettier: '@jill64/prettier-config'
...keywords

Check warning on line 46 in packages/action/src/ghosts/docs/updatePackageJson.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJson.ts#L46

Added line #L46 was not covered by tests
}

const oldJson = JSON.stringify(packageJson, null, 2)
Expand Down
43 changes: 41 additions & 2 deletions packages/action/src/ghosts/docs/updatePackageJsonList.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from 'octoflare/action/core'

Check warning on line 1 in packages/action/src/ghosts/docs/updatePackageJsonList.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJsonList.ts#L1

Added line #L1 was not covered by tests
import { ActionRepository } from '../../types/ActionRepository.js'
import { findFile } from '../../utils/findFile.js'
import { pushCommit } from '../../utils/pushCommit.js'
Expand All @@ -8,8 +9,46 @@ export const updatePackageJsonList = async ({
}: {
repository: ActionRepository
}) => {
const files = await findFile('package.json')
const result = await Promise.all(files.map(updatePackageJson(repository)))
const { owner } = repository

const [files, html] = await Promise.all([
findFile('package.json'),
fetch(repository.html_url)
.then((res) => res.text())
.catch((e) => {
core.error(e)
return ''
})
])

const repoImage =
html.match(/<meta property="og:image" content="(\S*)"\s*\/>/)?.[1] ?? ''

const license = repository.license?.spdx_id
? { license: repository.license.spdx_id }
: {}

const repoLevelConfig = {
...license,
bugs: `${repository.html_url}/issues`,
homepage: `${repository.html_url}#readme`,
author: {
name: owner.login,
email: owner.email ?? 'intents.turrets0h@icloud.com',
url: owner.html_url,
image: owner.avatar_url
},
repository: {
type: 'git',
url: repository.clone_url,
image: repoImage
},
prettier: '@jill64/prettier-config'
}

const result = await Promise.all(
files.map(updatePackageJson({ repository, repoLevelConfig }))
)

Check warning on line 51 in packages/action/src/ghosts/docs/updatePackageJsonList.ts

View check run for this annotation

Codecov / codecov/patch

packages/action/src/ghosts/docs/updatePackageJsonList.ts#L12-L51

Added lines #L12 - L51 were not covered by tests

if (result.includes(true)) {
await pushCommit('chore: synchronize package.json')
Expand Down
3 changes: 1 addition & 2 deletions packages/action/src/ghosts/docs/updateReadme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export const updateReadme =
workflowFiles,
packageJson,
readme: insertSection(readme),
repository,
isRepoRoot
repository
})

const license = isRepoRoot
Expand Down
4 changes: 1 addition & 3 deletions packages/action/src/ghosts/docs/utils/syncHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ export const syncHeader = ({
readme,
packageJson,
repository,
workflowFiles,
isRepoRoot
workflowFiles
}: {
workflowFiles: WorkflowFile[]
packageJson: PackageJson | undefined | null
readme: string
repository: ActionRepository
isRepoRoot: boolean
}) => {
const escapedWebsiteUrl = repository.homepage
? encodeURIComponent(repository.homepage)
Expand Down

0 comments on commit 2402239

Please sign in to comment.