UNPKG

6.67 kBJavaScriptView Raw
1"use strict";
2/* eslint-disable no-await-in-loop, @typescript-eslint/ban-ts-ignore */
3Object.defineProperty(exports, "__esModule", { value: true });
4const tslib_1 = require("tslib");
5const endent_1 = tslib_1.__importDefault(require("endent"));
6const enquirer_1 = require("enquirer");
7const fs = tslib_1.__importStar(require("fs"));
8const path = tslib_1.__importStar(require("path"));
9const util_1 = require("util");
10const release_1 = require("./release");
11const writeFile = util_1.promisify(fs.writeFile);
12/** Determine if any value is an object */
13const isObject = (value) => typeof value === 'object' && value !== null;
14/** Get all of the basic options for running "auto" */
15async function getFlags() {
16 return enquirer_1.prompt([
17 {
18 type: 'input',
19 name: 'repo',
20 message: 'GitHub Project Repository (press enter to use package definition)'
21 },
22 {
23 type: 'input',
24 name: 'owner',
25 message: 'GitHub Project Owner (press enter to use package definition)'
26 },
27 {
28 type: 'confirm',
29 name: 'noVersionPrefix',
30 message: 'Use the version as the tag without the `v` prefix?',
31 initial: 'no'
32 },
33 {
34 type: 'input',
35 name: 'githubApi',
36 message: 'GitHub API to use (press enter to use public)'
37 },
38 {
39 type: 'input',
40 name: 'githubGraphqlApi',
41 message: 'GitHub Graphql API base path to use (press enter to use githubApi)'
42 },
43 {
44 type: 'input',
45 name: 'baseBranch',
46 message: 'Branch to treat as the "master" branch (press enter to use "master")'
47 },
48 {
49 type: 'confirm',
50 name: 'onlyPublishWithReleaseLabel',
51 message: 'Only bump version if `release` label is on pull request',
52 initial: 'no'
53 },
54 {
55 type: 'input',
56 name: 'name',
57 message: 'Git name to commit and release with (press enter to use package definition)'
58 },
59 {
60 type: 'input',
61 name: 'email',
62 message: 'Git email to commit with (press enter to use package definition)'
63 }
64 ]);
65}
66/** Get label configuration from the user. */
67async function getCustomLabels(onlyLabels = false) {
68 const useCustomChangelogTitles = onlyLabels
69 ? { value: onlyLabels }
70 : await enquirer_1.prompt({
71 type: 'confirm',
72 name: 'value',
73 message: 'Would you like to use custom labels?',
74 initial: 'no'
75 });
76 let customLabels = {};
77 if (useCustomChangelogTitles.value) {
78 let i = 0;
79 while (release_1.defaultLabels[i]) {
80 const label = release_1.defaultLabels[i++];
81 const response = await enquirer_1.prompt({
82 type: 'snippet',
83 name: 'value',
84 message: `Customize the ${label.name} label:`,
85 initial: label,
86 // @ts-ignore
87 template: label.name === 'release' || label.name === 'skip-release'
88 ? endent_1.default `
89 label: #{name}
90 desc: #{description}
91 `
92 : endent_1.default `
93 label: #{name}
94 title: #{title}
95 desc: #{description}
96 `
97 });
98 const { name, title, description } = response.value.values;
99 const newLabel = {};
100 if (name !== label.name) {
101 newLabel.name = name;
102 }
103 if (name !== label.changelogTitle) {
104 newLabel.changelogTitle = title;
105 }
106 if (name !== label.description) {
107 newLabel.description = description;
108 }
109 if (Object.keys(newLabel).length === 1 && newLabel.name) {
110 customLabels = Object.assign(Object.assign({}, customLabels), { [label.name]: name });
111 }
112 else if (Object.keys(newLabel).length !== 0) {
113 customLabels = Object.assign(Object.assign({}, customLabels), { [label.name]: newLabel });
114 }
115 }
116 }
117 let getAnotherTitle = await enquirer_1.prompt({
118 type: 'confirm',
119 name: 'value',
120 message: 'Would you like to add additional labels?',
121 initial: 'no'
122 });
123 while (getAnotherTitle.value) {
124 const response = await enquirer_1.prompt({
125 type: 'snippet',
126 name: 'value',
127 message: 'Add another label:',
128 // @ts-ignore
129 template: endent_1.default `
130 label: #{name}
131 title: #{title}
132 desc: #{description}
133 `,
134 validate: (state) => {
135 if (!state.values.name) {
136 return 'Label is required for new label';
137 }
138 if (!state.values.changelogTitle) {
139 return 'Title is required for new label';
140 }
141 if (!state.values.description) {
142 return 'Description is required for new label';
143 }
144 return true;
145 }
146 });
147 const { name, title, description } = response.value.values;
148 customLabels = Object.assign(Object.assign({}, customLabels), { [name]: { name, title, description } });
149 getAnotherTitle = await enquirer_1.prompt({
150 type: 'confirm',
151 name: 'value',
152 message: 'Would you like to add another?',
153 initial: 'no'
154 });
155 }
156 return customLabels;
157}
158/** Run the interactive initialization prompt */
159async function init({ onlyLabels, dryRun }, logger) {
160 const flags = onlyLabels ? {} : await getFlags();
161 const labels = await getCustomLabels(onlyLabels);
162 const autoRc = Object.entries(Object.assign(Object.assign({}, flags), { labels })).reduce((all, [key, value]) => {
163 if (value === '' ||
164 value === false ||
165 (isObject(value) && Object.keys(value).length === 0)) {
166 return all;
167 }
168 return Object.assign(Object.assign({}, all), { [key]: value });
169 }, {});
170 if (Object.keys(autoRc).length === 0) {
171 return;
172 }
173 const jsonString = JSON.stringify(autoRc, undefined, 2);
174 if (dryRun) {
175 logger.log.note(`Initialization options would be:\n${jsonString}`);
176 }
177 else {
178 await writeFile(path.join(process.cwd(), '.autorc'), jsonString);
179 }
180}
181exports.default = init;
182//# sourceMappingURL=init.js.map
\No newline at end of file