Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·147 lines (121 loc) · 3.61 KB
/
Copy pathindex.js
File metadata and controls
executable file
·147 lines (121 loc) · 3.61 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env node
import fs from 'node:fs';
import util from 'node:util';
import { EOL } from 'node:os';
import prompts from 'prompts';
import gitRemoteOriginUrl from 'git-remote-origin-url';
import parseGitUrl from 'git-url-parse';
import { execa } from 'execa';
import { detect } from 'package-manager-detector/detect';
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const RELEASE_IT_CONFIG = '.release-it.json';
const PACKAGE_CONFIG = 'package.json';
const PACKAGE_INSTALL_COMMANDS = {
npm: ['npm', ['install', 'release-it', '-D']],
pnpm: ['pnpm', ['add', 'release-it', '-D']],
yarn: ['yarn', ['add', 'release-it', '-D']],
bun: ['bun', ['add', 'release-it', '-D']]
};
(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);
}
try {
const detectedPackageManager = await detect();
const packageManagerName = detectedPackageManager?.name || 'npm';
console.log(`Installing release-it using ${packageManagerName}...`);
const [execCommand, execArgs] = PACKAGE_INSTALL_COMMANDS[packageManagerName] || PACKAGE_INSTALL_COMMANDS.npm;
await execa(execCommand, execArgs, { stdio: 'inherit' });
console.log('Successfully installed release-it');
} catch (error) {
console.error('Failed to install release-it:', error.message);
process.exit(1);
}
})();