forked from yarnpkg/yarn
/
init.js
218 lines (196 loc) · 5.91 KB
/
init.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* @flow */
import type {Reporter} from '../../reporters/index.js';
import type Config from '../../config.js';
import {stringifyPerson, extractRepositoryUrl} from '../../util/normalize-manifest/util.js';
import {registryNames} from '../../registries/index.js';
import GitHubResolver from '../../resolvers/exotics/github-resolver.js';
import * as child from '../../util/child.js';
import * as fs from '../../util/fs.js';
import * as validate from '../../util/normalize-manifest/validate.js';
import {NODE_BIN_PATH} from '../../constants';
const objectPath = require('object-path');
const path = require('path');
const yn = require('yn');
export function setFlags(commander: Object) {
commander.description('Interactively creates or updates a package.json file.');
commander.option('-y, --yes', 'use default options');
commander.option('-p, --private', 'use default options and private true');
commander.option('-i, --install <value>', 'install a specific Yarn release');
}
export function hasWrapper(commander: Object, args: Array<string>): boolean {
return true;
}
export const shouldRunInCurrentCwd = true;
export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
if (flags.install) {
const lockfilePath = path.resolve(config.cwd, 'yarn.lock');
if (!await fs.exists(lockfilePath)) {
await fs.writeFile(lockfilePath, '');
}
await child.spawn(NODE_BIN_PATH, [process.argv[1], 'policies', 'set-version', flags.install], {
stdio: 'inherit',
cwd: config.cwd,
});
await child.spawn(
NODE_BIN_PATH,
[process.argv[1], 'init', ...(flags.yes ? ['-y'] : []), ...(flags.private ? ['-p'] : [])],
{
stdio: 'inherit',
cwd: config.cwd,
},
);
return;
}
const manifests = await config.getRootManifests();
let repository = {};
const author = {
name: config.getOption('init-author-name'),
email: config.getOption('init-author-email'),
url: config.getOption('init-author-url'),
};
if (await fs.exists(path.join(config.cwd, '.git'))) {
// get git origin of the cwd
try {
repository = {
type: 'git',
url: await child.spawn('git', ['config', 'remote.origin.url'], {
cwd: config.cwd,
}),
};
} catch (ex) {
// Ignore - Git repo may not have an origin URL yet (eg. if it only exists locally)
}
if (author.name === undefined) {
author.name = await getGitConfigInfo('user.name');
}
if (author.email === undefined) {
author.email = await getGitConfigInfo('user.email');
}
}
const keys = [
{
key: 'name',
question: 'name',
default: path.basename(config.cwd),
validation: validate.isValidPackageName,
validationError: 'invalidPackageName',
},
{
key: 'version',
question: 'version',
default: String(config.getOption('init-version')),
},
{
key: 'description',
question: 'description',
default: '',
},
{
key: 'main',
question: 'entry point',
default: 'index.js',
},
{
key: 'repository',
question: 'repository url',
default: extractRepositoryUrl(repository),
},
{
key: 'author',
question: 'author',
default: stringifyPerson(author),
},
{
key: 'license',
question: 'license',
default: String(config.getOption('init-license')),
},
{
key: 'private',
question: 'private',
default: config.getOption('init-private') || '',
inputFormatter: yn,
},
];
// get answers
const pkg = {};
for (const entry of keys) {
const {yes, private: privateFlag} = flags;
const {key: manifestKey} = entry;
let {question, default: def} = entry;
for (const registryName of registryNames) {
const {object} = manifests[registryName];
let val = objectPath.get(object, manifestKey);
if (!val) {
break;
}
if (typeof val === 'object') {
if (manifestKey === 'author') {
val = stringifyPerson(val);
} else if (manifestKey === 'repository') {
val = extractRepositoryUrl(val);
}
}
def = val;
}
if (manifestKey === 'private' && privateFlag) {
def = true;
}
if (def) {
question += ` (${String(def)})`;
}
let answer;
let validAnswer = false;
if (yes) {
answer = def;
} else {
// loop until a valid answer is provided, if validation is on entry
if (entry.validation) {
while (!validAnswer) {
answer = (await reporter.question(question)) || def;
// validate answer
if (entry.validation(String(answer))) {
validAnswer = true;
} else {
reporter.error(reporter.lang('invalidPackageName'));
}
}
} else {
answer = (await reporter.question(question)) || def;
}
}
if (answer) {
if (entry.inputFormatter) {
answer = entry.inputFormatter(answer);
}
objectPath.set(pkg, manifestKey, answer);
}
}
if (pkg.repository && GitHubResolver.isVersion(pkg.repository)) {
pkg.repository = `https://github.com/${pkg.repository}`;
}
// save answers
const targetManifests = [];
for (const registryName of registryNames) {
const info = manifests[registryName];
if (info.exists) {
targetManifests.push(info);
}
}
if (!targetManifests.length) {
targetManifests.push(manifests.npm);
}
for (const targetManifest of targetManifests) {
Object.assign(targetManifest.object, pkg);
reporter.success(`Saved ${path.basename(targetManifest.loc)}`);
}
await config.saveRootManifests(manifests);
}
export async function getGitConfigInfo(credential: string, spawn = child.spawn): Promise<string> {
try {
// try to get author default based on git config
return await spawn('git', ['config', credential]);
} catch (e) {
return '';
}
}