@quilted/create 0.1.20 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,432 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+ var path = require('path');
5
+ var child_process = require('child_process');
6
+ var index = require('./index.cjs');
7
+ var packageManager = require('./package-manager.cjs');
8
+ require('tty');
9
+ require('url');
10
+ require('readline');
11
+ require('events');
12
+
13
+ function _interopNamespace(e) {
14
+ if (e && e.__esModule) return e;
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n["default"] = e;
28
+ return Object.freeze(n);
29
+ }
30
+
31
+ var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
32
+ var path__namespace = /*#__PURE__*/_interopNamespace(path);
33
+
34
+ let _ = t => t,
35
+ _t,
36
+ _t2,
37
+ _t3,
38
+ _t4;
39
+ async function createProject() {
40
+ const argv = getArgv();
41
+
42
+ if (argv['--help']) {
43
+ var _argv$PackageManag;
44
+
45
+ const additionalOptions = index.stripIndent(_t || (_t = _`
46
+ ${0}, ${0}
47
+ Whether this package will use React. If you don’t provide this option, the command
48
+ will ask you about it later.
49
+
50
+ ${0}, ${0}
51
+ Whether this package will be published for other projects to install. If you do not
52
+ provide this option, the command will ask you about it later.
53
+
54
+ ${0}
55
+ The package registry to publish this package to. This option only applies if you create
56
+ a public package. If you do not provide this option, it will use the default NPM registry.
57
+ `), index.cyan_1(`--react`), index.cyan_1(`--no-react`), index.cyan_1(`--public`), index.cyan_1(`--private`), index.cyan_1(`--registry`));
58
+ index.printHelp({
59
+ kind: 'package',
60
+ options: additionalOptions,
61
+ packageManager: (_argv$PackageManag = argv['--package-manager']) === null || _argv$PackageManag === void 0 ? void 0 : _argv$PackageManag.toLowerCase()
62
+ });
63
+ return;
64
+ }
65
+
66
+ const inWorkspace = fs__namespace.existsSync('quilt.workspace.ts');
67
+ const name = await getName(argv);
68
+ const directory = await getDirectory(argv, {
69
+ name,
70
+ inWorkspace
71
+ });
72
+ const isPublic = await getPublic(argv);
73
+ const useReact = await getReact(argv);
74
+ const createAsMonorepo = !inWorkspace && (await index.getCreateAsMonorepo(argv));
75
+ const shouldInstall = await index.getShouldInstall(argv);
76
+ const packageManager$1 = await index.getPackageManager(argv);
77
+ const setupExtras = await index.getExtrasToSetup(argv, {
78
+ inWorkspace
79
+ });
80
+ const partOfMonorepo = inWorkspace || createAsMonorepo;
81
+ const packageDirectory = createAsMonorepo ? path__namespace.join(directory, `packages/${packageManager.toValidPackageName(name.split('/').pop())}`) : directory;
82
+
83
+ if (fs__namespace.existsSync(directory)) {
84
+ await packageManager.emptyDirectory(directory);
85
+
86
+ if (packageDirectory !== directory) {
87
+ fs__namespace.mkdirSync(packageDirectory, {
88
+ recursive: true
89
+ });
90
+ }
91
+ } else {
92
+ fs__namespace.mkdirSync(packageDirectory, {
93
+ recursive: true
94
+ });
95
+ }
96
+
97
+ const rootDirectory = inWorkspace ? process.cwd() : directory;
98
+ const outputRoot = packageManager.createOutputTarget(rootDirectory);
99
+ const packageTemplate = packageManager.loadTemplate('package');
100
+ const workspaceTemplate = packageManager.loadTemplate('workspace');
101
+ let quiltProject = await packageTemplate.read('quilt.project.ts');
102
+
103
+ if (useReact) {
104
+ quiltProject = quiltProject.replace('react: false', 'react: true');
105
+ } // If we aren’t already in a workspace, copy the workspace files over, which
106
+ // are needed if we are making a monorepo or not.
107
+
108
+
109
+ if (!inWorkspace) {
110
+ await workspaceTemplate.copy(directory, file => {
111
+ // When this is a single project, we use the project’s Quilt configuration as the base.
112
+ if (file === 'quilt.workspace.ts') return createAsMonorepo; // We need to make some adjustments to the root package.json
113
+
114
+ return file !== 'package.json';
115
+ }); // If we are creating a monorepo, we need to add the root package.json and
116
+ // package manager workspace configuration.
117
+
118
+ if (createAsMonorepo) {
119
+ const workspacePackageJson = JSON.parse(await workspaceTemplate.read('package.json'));
120
+ workspacePackageJson.name = packageManager.toValidPackageName(name);
121
+
122
+ if (packageManager$1 === 'pnpm') {
123
+ await outputRoot.write('pnpm-workspace.yaml', await packageManager.format(`
124
+ packages:
125
+ - './packages/*'
126
+ `, {
127
+ as: 'yaml'
128
+ }));
129
+ } else {
130
+ workspacePackageJson.workspaces = ['packages/*'];
131
+ }
132
+
133
+ await outputRoot.write('package.json', await packageManager.format(JSON.stringify(workspacePackageJson), {
134
+ as: 'json-stringify'
135
+ }));
136
+ } else {
137
+ const [projectPackageJson, projectTSConfig, workspacePackageJson] = await Promise.all([packageTemplate.read('package.json').then(content => JSON.parse(content)), packageTemplate.read('tsconfig.json').then(content => JSON.parse(content)), workspaceTemplate.read('package.json').then(content => JSON.parse(content))]);
138
+ workspacePackageJson.eslintConfig = projectPackageJson.eslintConfig;
139
+ workspacePackageJson.browserslist = projectPackageJson.browserslist;
140
+ const newPackageJson = {}; // We want to put the project’s dependencies in the package.json, respecting
141
+ // the preferred ordering (dependencies, peer dependencies, dev dependencies).
142
+
143
+ for (const [key, value] of Object.entries(projectPackageJson)) {
144
+ if (key !== 'devDependencies') {
145
+ newPackageJson[key] = value;
146
+ continue;
147
+ }
148
+
149
+ newPackageJson.dependencies = projectPackageJson.dependencies;
150
+ newPackageJson.peerDependencies = projectPackageJson.peerDependencies;
151
+ newPackageJson.peerDependenciesMeta = projectPackageJson.peerDependenciesMeta;
152
+ newPackageJson.devDependencies = sortKeys({ ...workspacePackageJson.devDependencies,
153
+ ...projectPackageJson.devDependencies
154
+ });
155
+ }
156
+
157
+ adjustPackageJson(newPackageJson, {
158
+ name: packageManager.toValidPackageName(name),
159
+ react: useReact,
160
+ isPublic,
161
+ registry: argv['--registry']
162
+ });
163
+ quiltProject = quiltProject.replace('quiltPackage', 'quiltWorkspace, quiltPackage').replace('quiltPackage(', 'quiltWorkspace(), quiltPackage(');
164
+ await outputRoot.write('quilt.project.ts', await packageManager.format(quiltProject, {
165
+ as: 'typescript'
166
+ }));
167
+ await outputRoot.write('package.json', await packageManager.format(JSON.stringify(newPackageJson), {
168
+ as: 'json-stringify'
169
+ }));
170
+ await outputRoot.write('tsconfig.json', await packageManager.format(JSON.stringify(projectTSConfig), {
171
+ as: 'json'
172
+ }));
173
+ }
174
+
175
+ if (setupExtras.has('github')) {
176
+ await packageManager.loadTemplate('github').copy(directory);
177
+ }
178
+
179
+ if (setupExtras.has('vscode')) {
180
+ await packageManager.loadTemplate('vscode').copy(directory);
181
+ }
182
+ }
183
+
184
+ await packageTemplate.copy(packageDirectory, file => {
185
+ // If we are in a monorepo, we can use all the template files as they are
186
+ if (file === 'tsconfig.json') {
187
+ return partOfMonorepo;
188
+ } // We need to make some adjustments the project’s package.json and Quilt config
189
+
190
+
191
+ return file !== 'package.json' && file !== 'quilt.project.ts';
192
+ });
193
+
194
+ if (partOfMonorepo) {
195
+ // Write the package’s package.json (the root one was already created)
196
+ const projectPackageJson = JSON.parse(await packageTemplate.read('package.json'));
197
+ projectPackageJson.repository.directory = path__namespace.relative(directory, packageDirectory);
198
+ adjustPackageJson(projectPackageJson, {
199
+ name: packageManager.toValidPackageName(name),
200
+ react: useReact,
201
+ isPublic,
202
+ registry: argv['--registry']
203
+ });
204
+ await outputRoot.write(path__namespace.join(packageDirectory, 'package.json'), await packageManager.format(JSON.stringify(projectPackageJson), {
205
+ as: 'json-stringify'
206
+ }));
207
+ await outputRoot.write(path__namespace.join(packageDirectory, 'quilt.project.ts'), quiltProject);
208
+ await Promise.all([packageManager.addToTsConfig(packageDirectory, outputRoot), packageManager.addToPackageManagerWorkspaces(packageDirectory, outputRoot, packageManager$1)]);
209
+ }
210
+
211
+ if (shouldInstall) {
212
+ process.stdout.write('\nInstalling dependencies...\n'); // TODO: better loading, handle errors
213
+
214
+ child_process.execSync(`${packageManager$1} install`, {
215
+ cwd: rootDirectory
216
+ });
217
+ process.stdout.moveCursor(0, -1);
218
+ process.stdout.clearLine(1);
219
+ console.log('Installed dependencies.');
220
+ }
221
+
222
+ const packageJsonInstructions = index.stripIndent(_t2 || (_t2 = _`
223
+ Your new package is ready to go! However, before you go too much further,
224
+ you should update the following fields in ${0}:
225
+
226
+ - ${0}, where you provide a description of what your package does
227
+ - ${0}, where you should include the ${0} of your project’s repo
228
+
229
+ Before you publish your package, you will also want to update the ${0}
230
+ field in the package.json file.
231
+ `), index.cyan_1(packageManager.relativeDirectoryForDisplay(path__namespace.relative(process.cwd(), path__namespace.join(packageDirectory, 'package.json')))), index.bold_1(`"description"`), index.bold_1(`"repository"`), index.bold_1(`"url"`), index.bold_1(`"version"`));
232
+ console.log();
233
+ console.log(packageJsonInstructions);
234
+ const commands = [];
235
+
236
+ if (!inWorkspace && directory !== process.cwd()) {
237
+ commands.push(`cd ${index.cyan_1(packageManager.relativeDirectoryForDisplay(path__namespace.relative(process.cwd(), directory)))} ${index.dim_1('# Move into your new package’s directory')}`);
238
+ }
239
+
240
+ if (!shouldInstall) {
241
+ commands.push(`pnpm install ${index.dim_1('# Install all your dependencies')}`);
242
+ }
243
+
244
+ if (!inWorkspace) {
245
+ // TODO: change this condition to check if git was initialized already
246
+ commands.push(`git init && git add -A && git commit -m "Initial commit" ${index.dim_1('# Start your git history (optional)')}`);
247
+ }
248
+
249
+ if (commands.length > 0) {
250
+ const whatsNext = index.stripIndent(_t3 || (_t3 = _`
251
+ After you update your package.json, there’s ${0} you’ll need to take
252
+ in order to start building:
253
+ `), commands.length > 1 ? 'a few more steps' : 'one more step');
254
+ console.log();
255
+ console.log(whatsNext);
256
+ console.log();
257
+ console.log(commands.map(command => ` ${command}`).join('\n'));
258
+ }
259
+
260
+ const followUp = index.stripIndent(_t4 || (_t4 = _`
261
+ Quilt can help you build, test, lint, and type-check your new package. You
262
+ can learn more about building packages with Quilt by reading the documentation:
263
+ ${0}
264
+
265
+ Have fun! 🎉
266
+ `), index.underline_1(index.magenta_1('https://github.com/lemonmade/quilt/tree/main/documentation')));
267
+ console.log();
268
+ console.log(followUp);
269
+ } // Argument handling
270
+
271
+ function getArgv() {
272
+ const argv = index.arg_1({
273
+ '--yes': Boolean,
274
+ '-y': '--yes',
275
+ '--name': String,
276
+ '--directory': String,
277
+ '--install': Boolean,
278
+ '--no-install': Boolean,
279
+ '--monorepo': Boolean,
280
+ '--no-monorepo': Boolean,
281
+ '--package-manager': String,
282
+ '--extras': [String],
283
+ '--no-extras': Boolean,
284
+ '--react': Boolean,
285
+ '--no-react': Boolean,
286
+ '--public': Boolean,
287
+ '--private': Boolean,
288
+ '--registry': String,
289
+ '--help': Boolean,
290
+ '-h': '--help'
291
+ }, {
292
+ permissive: true
293
+ });
294
+ return argv;
295
+ }
296
+
297
+ async function getName(argv) {
298
+ let {
299
+ '--name': name
300
+ } = argv;
301
+
302
+ if (name == null) {
303
+ name = await index.prompt({
304
+ type: 'text',
305
+ message: 'What would you like to name your new package?',
306
+ initial: '@my-team/package'
307
+ });
308
+ }
309
+
310
+ return name;
311
+ }
312
+
313
+ async function getDirectory(argv, {
314
+ name,
315
+ inWorkspace
316
+ }) {
317
+ let directory = argv['--directory'] ? path__namespace.resolve(argv['--directory']) : undefined;
318
+
319
+ if (directory == null) {
320
+ const basePackageName = packageManager.toValidPackageName(name.split('/').pop());
321
+ const defaultDirectory = inWorkspace ? `packages/${basePackageName}` : basePackageName;
322
+ directory = path__namespace.resolve(await index.prompt({
323
+ type: 'text',
324
+ message: 'Where would you like to create your new package?',
325
+ initial: defaultDirectory
326
+ }));
327
+ }
328
+
329
+ while (!argv['--yes']) {
330
+ if (fs__namespace.existsSync(directory) && !(await packageManager.isEmpty(directory))) {
331
+ const relativeDirectory = path__namespace.relative(process.cwd(), directory);
332
+ const empty = await index.prompt({
333
+ type: 'confirm',
334
+ message: `Directory ${index.bold_1(packageManager.relativeDirectoryForDisplay(relativeDirectory))} is not empty, is it safe to empty it?`,
335
+ initial: true
336
+ });
337
+ if (empty) break;
338
+ const promptDirectory = await index.prompt({
339
+ type: 'text',
340
+ message: 'What directory do you want to create your package in?'
341
+ });
342
+ directory = path__namespace.resolve(promptDirectory);
343
+ } else {
344
+ break;
345
+ }
346
+ }
347
+
348
+ return directory;
349
+ }
350
+
351
+ async function getPublic(argv) {
352
+ let isPublic;
353
+
354
+ if (argv['--public'] || argv['--yes']) {
355
+ isPublic = true;
356
+ } else if (argv['--private']) {
357
+ isPublic = false;
358
+ } else {
359
+ isPublic = await index.prompt({
360
+ type: 'confirm',
361
+ message: 'Will you publish this package to use in other projects?',
362
+ initial: true
363
+ });
364
+ }
365
+
366
+ return isPublic;
367
+ }
368
+
369
+ async function getReact(argv) {
370
+ let useReact;
371
+
372
+ if (argv['--react'] || argv['--yes']) {
373
+ useReact = true;
374
+ } else if (argv['--no-react']) {
375
+ useReact = false;
376
+ } else {
377
+ useReact = await index.prompt({
378
+ type: 'confirm',
379
+ message: 'Will this package depend on React?',
380
+ initial: true
381
+ });
382
+ }
383
+
384
+ return useReact;
385
+ }
386
+
387
+ function adjustPackageJson(packageJson, {
388
+ name,
389
+ react,
390
+ isPublic,
391
+ registry
392
+ }) {
393
+ packageJson.name = name;
394
+ const packageParts = name.split('/');
395
+ const scope = packageParts[0].startsWith('@') ? packageParts[0] : undefined;
396
+ const finalRegistry = registry !== null && registry !== void 0 ? registry : 'https://registry.npmjs.org';
397
+
398
+ if (scope) {
399
+ packageJson.publishConfig[`${scope}/registry`] = finalRegistry;
400
+ } else if (registry) {
401
+ packageJson.publishConfig.registry = finalRegistry;
402
+ }
403
+
404
+ if (isPublic) {
405
+ delete packageJson.private;
406
+ } else {
407
+ delete packageJson.publishConfig;
408
+ }
409
+
410
+ if (!react) {
411
+ delete packageJson.dependencies['@types/react'];
412
+ delete packageJson.devDependencies['react'];
413
+ delete packageJson.peerDependencies['react'];
414
+ delete packageJson.peerDependenciesMeta['react'];
415
+ packageJson.eslintConfig.extends = packageJson.eslintConfig.extends.filter(extend => !extend.includes('react'));
416
+ }
417
+
418
+ return packageJson;
419
+ }
420
+
421
+ function sortKeys(object) {
422
+ const newObject = {};
423
+ const sortedEntries = Object.entries(object).sort(([keyOne], [keyTwo]) => keyOne.localeCompare(keyTwo));
424
+
425
+ for (const [key, value] of sortedEntries) {
426
+ newObject[key] = value;
427
+ }
428
+
429
+ return newObject;
430
+ }
431
+
432
+ exports.createProject = createProject;