-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship.config.js
94 lines (85 loc) · 2.75 KB
/
ship.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require('fs')
const path = require('path')
const dotenv = require('dotenv')
const { Octokit } = require('@octokit/rest')
const { getRepoInfo } = require('shipjs-lib')
dotenv.config({ path: path.resolve('.', '.env') })
const { owner, name: repo } = getRepoInfo('origin', '.')
const getOctokit = () => {
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`,
})
return octokit
}
const isDryRun = argv => {
return argv.includes('--dry-run')
}
module.exports = {
updateChangelog: false,
publishCommand: ({ defaultCommand }) => `${defaultCommand} --access public`,
afterPublish: async () => {
const octokit = getOctokit()
const { data } = await octokit.repos.listReleases({ owner, repo })
const drafts = data.filter(d => d.draft === true && d.name.startsWith('v'))
if (drafts.length) {
fs.writeFileSync(
path.resolve('.', `changelog.json`),
JSON.stringify(drafts[0])
)
} else {
// eslint-disable-next-line no-console
console.log(`> Draft not found.`)
}
},
releases: {
extractChangelog: ({ version }) => {
try {
const dryRun = isDryRun(process.argv)
const changelogFilePath = path.resolve('.', 'changelog.json')
if (fs.existsSync(changelogFilePath)) {
const changelog = JSON.parse(fs.readFileSync(changelogFilePath))
if (!dryRun) {
// Delete temp changelog
fs.unlink(changelogFilePath, err => {
if (err) {
// eslint-disable-next-line no-console
console.log(err)
}
})
}
// Replact version no to tag name
const body = changelog['body'].replace(
`...${version}`,
`...v${version}`
)
if (!dryRun && changelog['draft']) {
const octokit = getOctokit()
octokit.repos
.deleteRelease({
owner,
repo,
release_id: changelog['id'],
})
.then(() => {
// eslint-disable-next-line no-console
console.log(`> Delete draft suceed.`)
})
.catch(() => {
// eslint-disable-next-line no-console
console.log(`> Delete draft failed.`)
})
}
return body
}
// eslint-disable-next-line no-console
console.log(`> ${changelogFilePath} not found.`)
} catch (e) {
// eslint-disable-next-line no-console
console.log(`> Exception raised.`)
// eslint-disable-next-line no-console
console.log(e.toString())
}
return `Add CHANGELOG manually.\nCopy from draft ${version}'s release note`
},
},
}