Skip to content

Commit 7fc90c5

Browse files
committed
🛠 update publish task
1 parent d45299c commit 7fc90c5

File tree

13 files changed

+323
-258
lines changed

13 files changed

+323
-258
lines changed

package.json

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@auto/core": "^0.1.7",
1313
"@auto/github": "^0.1.2",
1414
"@auto/slack": "^0.1.2",
15-
"@auto/tag": "^0.1.1",
1615
"@auto/telegram": "^0.1.2",
1716
"@babel/core": "^7.10.5",
1817
"@babel/preset-env": "^7.10.4",
@@ -43,16 +42,11 @@
4342
"xml2js": "^0.4.23"
4443
},
4544
"start": {
46-
"file": "tasks/",
47-
"auto": {
48-
"shouldMakeGitTags": true,
49-
"shouldWriteChangelogFiles": true
50-
}
45+
"file": "tasks/"
5146
},
5247
"auto": {
5348
"npm": {
54-
"access": "public",
55-
"registry": "https://package.openupm.com"
49+
"access": "public"
5650
},
5751
"prefixes": {
5852
"major": "💥",
@@ -61,25 +55,21 @@
6155
"publish": "📦",
6256
"dependencies": "♻️",
6357
"initial": "🐣",
64-
"lint": "🚷",
6558
"test": "👾",
6659
"docs": "📝",
6760
"refactor": "🛠",
68-
"wip": "🚧",
69-
"screenshots": "📸"
61+
"wip": "🚧"
7062
},
7163
"commit": {
7264
"major": "Breaking Change",
7365
"minor": "New feature",
7466
"patch": "Bugfix",
7567
"initial": "Initial",
7668
"depepdencies": "Dependencies",
77-
"lint": "Lint",
7869
"test": "Test",
7970
"docs": "Docs",
8071
"refactor": "Refactor",
81-
"wip": "WIP",
82-
"screenshots": "Snapshots / Screenshots"
72+
"wip": "WIP"
8373
}
8474
},
8575
"eslintConfig": {

tasks/hooks/create-openupm-tags.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { THook } from '@auto/core'
2+
import execa from 'execa'
3+
import { compileTagForOpenUpm } from '../utils/publish-helpers'
4+
5+
export const createOpenUpmTags: THook = async ({ packages }) => {
6+
for (const pkg of packages) {
7+
if (pkg.version === null) {
8+
continue
9+
}
10+
11+
const tagText = compileTagForOpenUpm(pkg)
12+
13+
await execa(
14+
'git',
15+
[
16+
'tag',
17+
'-a',
18+
tagText,
19+
'-m',
20+
tagText,
21+
],
22+
{
23+
stdout: 'ignore',
24+
stderr: 'inherit',
25+
}
26+
)
27+
}
28+
}

tasks/hooks/make-github-release.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { THook, TMessage, TLogReleaseType, TPackageRelease } from '@auto/core'
2+
import type { TGithubConfig } from '@auto/github'
3+
import fetch from 'node-fetch'
4+
import type { TReadonly } from 'tsfn'
5+
import { compileTagForOpenUpm, compileGithubReleaseName } from '../utils/publish-helpers'
6+
7+
const GITHUB_API_REPOS_URL = 'https://api.github.com/repos/'
8+
9+
export const makeGithubReleases = (githubConfig: TReadonly<TGithubConfig>): THook => async ({ packages, prefixes }) => {
10+
const compileMessages = (pkg: TReadonly<TPackageRelease>): string => {
11+
let result = (pkg.messages || []) as TMessage<TLogReleaseType>[]
12+
13+
if (pkg.deps !== null && pkg.type !== 'initial') {
14+
const depNames = Object.keys(pkg.deps)
15+
.filter((name) => Boolean(packages.find((pkg) => pkg.name === name)?.type !== 'initial'))
16+
17+
if (depNames.length > 0) {
18+
result = result.concat({
19+
type: 'dependencies',
20+
message: `update dependencies \`${depNames.join('`, `')}\``,
21+
})
22+
}
23+
}
24+
25+
return result
26+
.map((message) => `* ${prefixes[message.type]} ${message.message}`)
27+
.join('\n')
28+
}
29+
30+
for (const pkg of packages) {
31+
if (pkg.version === null) {
32+
return
33+
}
34+
35+
await fetch(
36+
`${GITHUB_API_REPOS_URL}${githubConfig.username}/${githubConfig.repo}/releases`,
37+
{
38+
method: 'POST',
39+
headers: {
40+
'Content-Type': 'application/json',
41+
Authorization: `token ${githubConfig.token}`,
42+
'User-Agent': 'auto-tools',
43+
},
44+
body: JSON.stringify({
45+
tag_name: compileTagForOpenUpm(pkg),
46+
name: compileGithubReleaseName(pkg),
47+
body: compileMessages(pkg),
48+
}),
49+
}
50+
)
51+
}
52+
}

tasks/hooks/prepare-packages.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import path from 'path'
12
import type { THook } from '@auto/core'
3+
import globby from 'globby'
4+
import { mkdir, copyFile } from 'pifs'
5+
import { getPublishDir } from '../utils/publish-helpers'
26

37
const makePusblishDir = async (packageDir: string): Promise<string> => {
4-
const { mkdir } = await import('pifs')
5-
const { getPublishDir } = await import('../utils/get-publish-dir')
68
const publishDir = getPublishDir(packageDir)
79

810
await mkdir(publishDir, { recursive: true })
@@ -11,8 +13,6 @@ const makePusblishDir = async (packageDir: string): Promise<string> => {
1113
}
1214

1315
const createFileCopier = async (packageDir: string) => {
14-
const path = await import('path')
15-
const { mkdir, copyFile } = await import('pifs')
1616
const publishDir = await makePusblishDir(packageDir)
1717

1818
return async (filePath: string) => {
@@ -26,7 +26,6 @@ const createFileCopier = async (packageDir: string) => {
2626
const EXCLUDE_FILE_GLOBS = ['!**/*.meta', '!**/Tests/**', '!changelog.md']
2727

2828
const preparePackage = async (packageDir: string) => {
29-
const { default: globby } = await import('globby')
3029
const paths = await globby([`${packageDir}/**`, ...EXCLUDE_FILE_GLOBS], { absolute: false })
3130
const copyFile = await createFileCopier(packageDir)
3231

tasks/hooks/publish-packages.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { THook, TPackageRelease } from '@auto/core'
22
import execa from 'execa'
33
import prompts from 'prompts'
44
import type { TReadonly } from 'tsfn'
5-
import { isFunction, isString } from 'tsfn'
6-
import { getPublishDir } from '../utils/get-publish-dir'
5+
import { isString } from 'tsfn'
6+
import { getPublishDir } from '../utils/publish-helpers'
77
import { resolveNpmConfig } from '../utils/resolve-npm-config'
88
import type { TNpmConfig } from '../utils/resolve-npm-config'
99

@@ -24,18 +24,14 @@ type TPublishPackage = Pick<TPackageRelease, 'name' | 'dir'>
2424
const isNpmAlreadyExistsError = (err: unknown) => isString(err) && err.includes('previously published versions')
2525

2626
const publishPackage = async (packageRelease: TReadonly<TPublishPackage>, npmConfig: TReadonly<Required<TNpmConfig>>, logMessage: (message: string) => void, logError: (err: Error) => void): Promise<void> => {
27-
const publishDir = getPublishDir(packageRelease.dir)
28-
29-
console.log(publishDir)
30-
3127
const invokePublish = () =>
3228
execa('npm', [
3329
'publish',
3430
'--registry',
3531
npmConfig.registry,
3632
'--access',
3733
npmConfig.access,
38-
publishDir,
34+
getPublishDir(packageRelease.dir),
3935
], {
4036
stdin: process.stdin,
4137
stdout: process.stdout,
@@ -63,13 +59,11 @@ const publishPackage = async (packageRelease: TReadonly<TPublishPackage>, npmCon
6359

6460
export type TPublishPackageConfig = {
6561
registry?: string,
66-
onMessage?: (message: string) => void,
67-
onError?: (e: Error) => void,
6862
}
6963

7064
export const publishPackages = (publishConfig: TPublishPackageConfig = {}): THook => {
71-
const logMessage = isFunction(publishConfig.onMessage) ? publishConfig.onMessage : () => {}
72-
const logError = isFunction(publishConfig.onError) ? publishConfig.onError : () => {}
65+
const logMessage = console.log
66+
const logError = console.error
7367

7468
return async ({ packages, config }) => {
7569
for (const pkg of packages) {

tasks/plugins/publish.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,23 @@ import plugin from '@start/plugin'
22

33
export const publish = () => plugin('publish', ({ logMessage }) => async () => {
44
const { auto } = await import('@auto/core')
5-
const { writePublishTags } = await import('@auto/tag')
6-
const { makeGithubReleases } = await import('@auto/github')
75
const { sendSlackMessage } = await import('@auto/slack')
86
const { sendTelegramMessage } = await import('@auto/telegram')
97
const { writeChangelogFiles } = await import('@auto/changelog')
10-
const { concurrentHooks } = await import('../utils/concurrent-hooks')
8+
const { createOpenUpmTags } = await import('../hooks/create-openupm-tags')
9+
const { makeGithubReleases } = await import('../hooks/make-github-release')
1110
const { preparePackages } = await import('../hooks/prepare-packages')
11+
const { concurrentHooks } = await import('../utils/concurrent-hooks')
1212
const { getAutoConfig, getSlackConfig, getGithubConfig, getTelegramConfig } = await import('../utils/publish-helpers')
13-
const {
14-
shouldWriteChangelogFiles,
15-
shouldMakeGitHubReleases,
16-
shouldSendSlackMessage,
17-
shouldSendTelegramMessage,
18-
shouldMakeGitTags,
19-
} = await getAutoConfig()
13+
const { shouldSendSlackMessage, shouldSendTelegramMessage } = await getAutoConfig()
2014

2115
try {
2216
await auto({
23-
prePublishCommit: shouldWriteChangelogFiles && writeChangelogFiles,
17+
prePublishCommit: writeChangelogFiles,
2418
prePublish: preparePackages,
25-
prePush: shouldMakeGitTags && writePublishTags,
19+
prePush: createOpenUpmTags,
2620
postPush: concurrentHooks(
27-
shouldMakeGitHubReleases && makeGithubReleases(getGithubConfig()),
21+
makeGithubReleases(getGithubConfig()),
2822
shouldSendSlackMessage && sendSlackMessage(getSlackConfig()),
2923
shouldSendTelegramMessage && sendTelegramMessage(getTelegramConfig())
3024
),

tasks/plugins/test-publish.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export const testPublish = () => plugin('test-publish', ({ logMessage }) => asyn
1717
),
1818
publish: publishPackages({
1919
registry: 'http://localhost:4873',
20-
onMessage: logMessage,
21-
onError: console.error,
2220
}),
2321
push: false,
2422
})

0 commit comments

Comments
 (0)