-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·126 lines (104 loc) · 2.86 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
import fs from 'fs';
import util from 'util';
import { EOL } from 'os';
import prompts from 'prompts';
import gitRemoteOriginUrl from 'git-remote-origin-url';
import parseGitUrl from 'git-url-parse';
import { execa } from 'execa';
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const RELEASE_IT_CONFIG = '.release-it.json';
const PACKAGE_CONFIG = 'package.json';
(async () => {
let manifest = {};
let hasManifest = false;
let isManifestChanged = false;
let config = {};
let hasConfig = false;
let isConfigChanged = false;
let isGitHub = false;
let isGitLab = false;
let remoteUrl;
try {
manifest = JSON.parse(await readFile(PACKAGE_CONFIG));
hasManifest = true;
} catch (err) {}
try {
config = JSON.parse(await readFile(RELEASE_IT_CONFIG));
hasConfig = true;
} catch (err) {}
config = {
$schema: 'https://unpkg.com/release-it/schema/release-it.json',
...config
};
try {
remoteUrl = await gitRemoteOriginUrl();
} catch (err) {}
if (remoteUrl) {
const parsedRemoteUrl = parseGitUrl(remoteUrl);
isGitHub = parsedRemoteUrl.host.includes('github.com');
isGitLab = parsedRemoteUrl.host.includes('gitlab.com');
}
const questions = [];
if (isGitHub) {
questions.push({
type: 'confirm',
name: 'github',
message: 'Publish a GitHub Release with every release?',
initial: true
});
}
if (isGitLab) {
questions.push({
type: 'confirm',
name: 'gitlab',
message: 'Publish a GitLab Release with every release?',
initial: true
});
}
if (hasManifest) {
questions.push({
type: 'select',
name: 'config',
message: 'Where to add the release-it config?',
choices: [
{ title: '.release-it.json', value: RELEASE_IT_CONFIG },
{ title: 'package.json', value: PACKAGE_CONFIG }
],
initial: 0,
hint: ' '
});
}
const answers = await prompts(questions);
if (answers.github) {
config.github = {
release: true
};
isConfigChanged = true;
}
if (answers.gitlab) {
config.gitlab = {
release: true
};
isConfigChanged = true;
}
if (hasManifest) {
manifest.scripts = manifest.scripts || {};
if (!('release' in manifest.scripts)) {
manifest.scripts.release = 'release-it';
isManifestChanged = true;
}
}
if (isConfigChanged && (!answers.config || answers.config === RELEASE_IT_CONFIG)) {
await writeFile(RELEASE_IT_CONFIG, JSON.stringify(config, null, ' ') + EOL);
}
if (answers.config === PACKAGE_CONFIG) {
manifest['release-it'] = config;
isManifestChanged = true;
}
if (isManifestChanged) {
await writeFile(PACKAGE_CONFIG, JSON.stringify(manifest, null, ' ') + EOL);
}
await execa('npm', ['install', 'release-it', '--save-dev'], { stdio: 'inherit' });
})();