-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·114 lines (87 loc) · 3.07 KB
/
cli.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env node
import fs from 'node:fs';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import Ora from 'ora';
import meow from 'meow';
import updateNotifier from 'update-notifier';
import readJson from 'read-package-json';
import createClient from './client.js';
import * as readysteady from './index.js';
readJson(fileURLToPath(new URL('package.json', import.meta.url)), (error, data) => {
updateNotifier({pkg: data, updateCheckInterval: 0}).notify({isGlobal: true});
app();
});
function app() {
const cli = meow({
importMeta: import.meta,
flags: {
owner: {
type: 'string',
isRequired: true,
},
repo: {
type: 'string',
isRequired: true,
},
tag: {
type: 'string',
isRequired: true,
},
force: {
type: 'boolean',
default: false,
},
files: {
type: 'string',
isMultiple: true,
default: [],
},
},
});
const {owner, repo, tag, force, files} = cli.flags;
(async () => {
let spinner = null;
spinner = createSpinner('Checking for GitHub authentication token');
try {
const client = createClient();
spinner.succeed('GitHub authentication token detected');
spinner = createSpinner(`Checking that tag is available: ${tag}`);
if (!await readysteady.isTagAvailable({client, owner, repo, tag})) {
throw new Error(`Tag is not available and has already been used: ${tag}`);
}
spinner.succeed(`Tag is available for a draft release: ${tag}`);
const releaseName = tag.slice(1);
spinner = createSpinner(`Checking if draft release is available: ${releaseName}`);
const existingDraftRelease = await readysteady.getExistingDraftRelease({client, owner, repo, releaseName});
if (existingDraftRelease) {
spinner.info(`A draft release already exists: ${releaseName}`);
if (force) {
spinner.info('--force flag detected, deleting the existing draft release');
await readysteady.deleteDraftRelease({client, owner, repo, release: existingDraftRelease});
spinner.succeed('Existing draft release deleted');
} else {
throw new Error('An existing draft release was found. Use --force to delete and replace with a new draft release');
}
} else {
spinner.succeed('No existing draft release found');
}
spinner = createSpinner(`Creating a new draft release: ${releaseName}`);
const newDraftRelease = await readysteady.createDraftRelease({client, owner, repo, tag, releaseName});
spinner.succeed(`A new draft release was created: ${releaseName}`);
if (files.length > 0) {
spinner = createSpinner('Uploading files to the draft release');
await readysteady.uploadFilesToDraftRelease({client, fs, owner, repo, release: newDraftRelease, files});
spinner.succeed('Files uploaded to draft release');
}
console.log('\nDraft release successfully created 🎉');
console.log(`You can edit the release here: ${newDraftRelease.data.html_url.replace('/tag/', '/edit/')}\n`);
} catch (error) {
spinner.fail(error.message);
process.exit(1);
}
})();
}
function createSpinner(text) {
return (new Ora(text)).start();
}