@quilted/create 0.1.12 → 0.1.15

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/build/cjs/app.cjs +303 -0
  3. package/build/cjs/index.cjs +5220 -5198
  4. package/build/cjs/index2.cjs +374 -0
  5. package/build/cjs/index3.cjs +7854 -0
  6. package/build/cjs/minimatch.cjs +1202 -0
  7. package/build/cjs/package-manager.cjs +300 -0
  8. package/build/cjs/package.cjs +410 -0
  9. package/build/cjs/standalone.cjs +151 -0
  10. package/build/esm/app.mjs +280 -0
  11. package/build/esm/index.mjs +5206 -5195
  12. package/build/esm/index2.mjs +365 -0
  13. package/build/esm/index3.mjs +7852 -0
  14. package/build/esm/minimatch.mjs +1200 -0
  15. package/build/esm/package-manager.mjs +270 -0
  16. package/build/esm/package.mjs +387 -0
  17. package/build/esm/standalone.mjs +149 -0
  18. package/build/tsconfig.tsbuildinfo +1 -1
  19. package/build/typescript/app.d.ts +2 -0
  20. package/build/typescript/app.d.ts.map +1 -0
  21. package/build/typescript/help.d.ts +6 -0
  22. package/build/typescript/help.d.ts.map +1 -0
  23. package/build/typescript/package.d.ts +2 -0
  24. package/build/typescript/package.d.ts.map +1 -0
  25. package/build/typescript/shared/package-manager.d.ts +3 -0
  26. package/build/typescript/shared/package-manager.d.ts.map +1 -0
  27. package/build/typescript/shared/prompts.d.ts +22 -0
  28. package/build/typescript/shared/prompts.d.ts.map +1 -0
  29. package/build/typescript/shared/tsconfig.d.ts +3 -0
  30. package/build/typescript/shared/tsconfig.d.ts.map +1 -0
  31. package/build/typescript/shared.d.ts +21 -0
  32. package/build/typescript/shared.d.ts.map +1 -0
  33. package/package.json +13 -3
  34. package/source/app.ts +387 -0
  35. package/source/create.ts +43 -383
  36. package/source/help.ts +132 -0
  37. package/source/package.ts +510 -0
  38. package/source/shared/package-manager.ts +74 -0
  39. package/source/shared/prompts.ts +133 -0
  40. package/source/shared/tsconfig.ts +90 -0
  41. package/source/shared.ts +165 -0
  42. package/templates/{app → app-basic}/App.tsx +4 -7
  43. package/templates/{app → app-basic}/features/Start/Start.module.css +0 -0
  44. package/templates/{app → app-basic}/features/Start/Start.tsx +1 -1
  45. package/templates/{app → app-basic}/features/Start/index.ts +0 -0
  46. package/templates/{app → app-basic}/foundation/Head.tsx +8 -8
  47. package/templates/{app → app-basic}/foundation/Http.tsx +10 -6
  48. package/templates/{app → app-basic}/package.json +10 -1
  49. package/templates/{app → app-basic}/quilt.project.ts +0 -0
  50. package/templates/{app → app-basic}/server.tsx +0 -0
  51. package/templates/{app → app-basic}/shared/types.ts +0 -0
  52. package/templates/{app → app-basic}/tsconfig.json +0 -0
  53. package/templates/app-single-file/App.tsx +168 -0
  54. package/templates/app-single-file/package.json +30 -0
  55. package/templates/app-single-file/quilt.project.ts +6 -0
  56. package/templates/app-single-file/tsconfig.json +9 -0
  57. package/templates/{workspace → github}/_github/workflows/actions/prepare/action.yml +0 -0
  58. package/templates/{workspace → github}/_github/workflows/ci.yml +0 -0
  59. package/templates/package/package.json +24 -9
  60. package/templates/package/quilt.project.ts +1 -1
  61. package/templates/vscode/_vscode/extensions.json +7 -0
  62. package/templates/vscode/_vscode/settings.json +17 -0
  63. package/templates/workspace/_gitignore +1 -1
  64. package/templates/workspace/_prettierignore +1 -0
  65. package/templates/workspace/package.json +1 -6
  66. package/templates/workspace/tsconfig.json +6 -1
  67. package/templates/workspace/pnpm-workspace.yaml +0 -3
package/source/app.ts ADDED
@@ -0,0 +1,387 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import {execSync} from 'child_process';
4
+
5
+ import arg from 'arg';
6
+ import * as color from 'colorette';
7
+ import {stripIndent} from 'common-tags';
8
+
9
+ import {printHelp} from './help';
10
+ import {
11
+ format,
12
+ loadTemplate,
13
+ createOutputTarget,
14
+ isEmpty,
15
+ emptyDirectory,
16
+ toValidPackageName,
17
+ relativeDirectoryForDisplay,
18
+ } from './shared';
19
+ import {
20
+ prompt,
21
+ getCreateAsMonorepo,
22
+ getExtrasToSetup,
23
+ getPackageManager,
24
+ getShouldInstall,
25
+ } from './shared/prompts';
26
+ import {addToTsConfig} from './shared/tsconfig';
27
+ import {addToPackageManagerWorkspaces} from './shared/package-manager';
28
+
29
+ type Arguments = ReturnType<typeof getArgv>;
30
+
31
+ export async function createApp() {
32
+ const argv = getArgv();
33
+
34
+ if (argv['--help']) {
35
+ const additionalOptions = stripIndent`
36
+ ${color.cyan(`--template`)}
37
+ The template to use for your new application. If you don’t specify a template,
38
+ this command will ask you for one instead. Must be one of the following:
39
+
40
+ - ${color.bold('basic')}, a web app with a minimal file structure
41
+ - ${color.bold('single-file')}, an entire web app in a single file
42
+ `;
43
+
44
+ printHelp({
45
+ kind: 'app',
46
+ options: additionalOptions,
47
+ packageManager: argv['--package-manager']?.toLowerCase(),
48
+ });
49
+ return;
50
+ }
51
+
52
+ const inWorkspace = fs.existsSync('quilt.workspace.ts');
53
+
54
+ const name = await getName(argv, {inWorkspace});
55
+ const directory = await getDirectory(argv, {name});
56
+ const template = await getTemplate(argv);
57
+
58
+ const createAsMonorepo = !inWorkspace && (await getCreateAsMonorepo(argv));
59
+ const shouldInstall = await getShouldInstall(argv);
60
+ const packageManager = await getPackageManager(argv);
61
+ const setupExtras = await getExtrasToSetup(argv, {inWorkspace});
62
+
63
+ const partOfMonorepo = inWorkspace || createAsMonorepo;
64
+
65
+ const appDirectory = createAsMonorepo
66
+ ? path.join(directory, 'app')
67
+ : directory;
68
+
69
+ if (fs.existsSync(directory)) {
70
+ await emptyDirectory(directory);
71
+
72
+ if (appDirectory !== directory) {
73
+ fs.mkdirSync(appDirectory, {recursive: true});
74
+ }
75
+ } else {
76
+ fs.mkdirSync(appDirectory, {recursive: true});
77
+ }
78
+
79
+ const rootDirectory = inWorkspace ? process.cwd() : directory;
80
+ const outputRoot = createOutputTarget(rootDirectory);
81
+ const appTemplate = loadTemplate(
82
+ template === 'basic' ? 'app-basic' : 'app-single-file',
83
+ );
84
+ const workspaceTemplate = loadTemplate('workspace');
85
+
86
+ // If we aren’t already in a workspace, copy the workspace files over, which
87
+ // are needed if we are making a monorepo or not.
88
+ if (!inWorkspace) {
89
+ await workspaceTemplate.copy(directory, (file) => {
90
+ // When this is a single project, we use the project’s Quilt configuration as the base.
91
+ if (file === 'quilt.workspace.ts') return createAsMonorepo;
92
+
93
+ // We need to make some adjustments to the root package.json
94
+ return file !== 'package.json';
95
+ });
96
+
97
+ // If we are creating a monorepo, we need to add the root package.json and
98
+ // package manager workspace configuration.
99
+ if (createAsMonorepo) {
100
+ const workspacePackageJson = JSON.parse(
101
+ await workspaceTemplate.read('package.json'),
102
+ );
103
+
104
+ workspacePackageJson.name = toValidPackageName(name!);
105
+
106
+ if (packageManager === 'pnpm') {
107
+ await outputRoot.write(
108
+ 'pnpm-workspace.yaml',
109
+ await format(
110
+ `
111
+ packages:
112
+ - './packages/*'
113
+ `,
114
+ {as: 'yaml'},
115
+ ),
116
+ );
117
+ } else {
118
+ workspacePackageJson.workspaces = ['packages/*'];
119
+ }
120
+
121
+ await outputRoot.write(
122
+ 'package.json',
123
+ await format(JSON.stringify(workspacePackageJson), {
124
+ as: 'json-stringify',
125
+ }),
126
+ );
127
+ } else {
128
+ const [projectPackageJson, projectTSConfig, workspacePackageJson] =
129
+ await Promise.all([
130
+ appTemplate
131
+ .read('package.json')
132
+ .then((content) => JSON.parse(content)),
133
+ appTemplate
134
+ .read('tsconfig.json')
135
+ .then((content) => JSON.parse(content)),
136
+ workspaceTemplate
137
+ .read('package.json')
138
+ .then((content) => JSON.parse(content)),
139
+ ]);
140
+
141
+ workspacePackageJson.name = toValidPackageName(name!);
142
+ workspacePackageJson.eslintConfig = projectPackageJson.eslintConfig;
143
+ workspacePackageJson.browserslist = projectPackageJson.browserslist;
144
+
145
+ const addBackToTSConfigInclude = new Set([
146
+ 'quilt.project.ts',
147
+ '*.test.ts',
148
+ '*.test.tsx',
149
+ ]);
150
+
151
+ projectTSConfig.exclude = projectTSConfig.exclude.filter(
152
+ (excluded: string) => !addBackToTSConfigInclude.has(excluded),
153
+ );
154
+
155
+ let quiltProject = await appTemplate.read('quilt.project.ts');
156
+ quiltProject = quiltProject
157
+ .replace('quiltApp', 'quiltWorkspace, quiltApp')
158
+ .replace('quiltApp(', 'quiltWorkspace(), quiltApp(');
159
+
160
+ await outputRoot.write(
161
+ 'quilt.project.ts',
162
+ await format(quiltProject, {as: 'typescript'}),
163
+ );
164
+
165
+ await outputRoot.write(
166
+ 'package.json',
167
+ await format(JSON.stringify(workspacePackageJson), {
168
+ as: 'json-stringify',
169
+ }),
170
+ );
171
+
172
+ await outputRoot.write(
173
+ 'tsconfig.json',
174
+ await format(JSON.stringify(projectTSConfig), {as: 'json'}),
175
+ );
176
+ }
177
+
178
+ if (setupExtras.has('github')) {
179
+ await loadTemplate('github').copy(directory);
180
+ }
181
+
182
+ if (setupExtras.has('vscode')) {
183
+ await loadTemplate('vscode').copy(directory);
184
+ }
185
+ }
186
+
187
+ await appTemplate.copy(appDirectory, (file) => {
188
+ // If we are in a monorepo, we can use all the template files as they are
189
+ if (file === 'quilt.project.ts' || file === 'tsconfig.json') {
190
+ return partOfMonorepo;
191
+ }
192
+
193
+ // We need to make some adjustments the project’s package.json
194
+ return file !== 'package.json';
195
+ });
196
+
197
+ if (partOfMonorepo) {
198
+ // Write the app’s package.json (the root one was already created)
199
+ const projectPackageJson = JSON.parse(
200
+ await appTemplate.read('package.json'),
201
+ );
202
+
203
+ projectPackageJson.name = path.basename(appDirectory);
204
+
205
+ await outputRoot.write(
206
+ path.join(appDirectory, 'package.json'),
207
+ await format(JSON.stringify(projectPackageJson), {
208
+ as: 'json-stringify',
209
+ }),
210
+ );
211
+
212
+ await Promise.all([
213
+ addToTsConfig(appDirectory, outputRoot),
214
+ addToPackageManagerWorkspaces(appDirectory, outputRoot, packageManager),
215
+ ]);
216
+ }
217
+
218
+ if (shouldInstall) {
219
+ process.stdout.write('\nInstalling dependencies...\n');
220
+ // TODO: better loading, handle errors
221
+ execSync(`${packageManager} install`, {cwd: rootDirectory});
222
+ process.stdout.moveCursor(0, -1);
223
+ process.stdout.clearLine(1);
224
+ console.log('Installed dependencies.');
225
+ }
226
+
227
+ const commands: string[] = [];
228
+
229
+ if (!inWorkspace && directory !== process.cwd()) {
230
+ commands.push(
231
+ `cd ${color.cyan(
232
+ relativeDirectoryForDisplay(path.relative(process.cwd(), directory)),
233
+ )} ${color.dim('# Move into your new app’s directory')}`,
234
+ );
235
+ }
236
+
237
+ if (!shouldInstall) {
238
+ commands.push(
239
+ `pnpm install ${color.dim('# Install all your dependencies')}`,
240
+ );
241
+ }
242
+
243
+ if (!inWorkspace) {
244
+ // TODO: change this condition to check if git was initialized already
245
+ commands.push(
246
+ `git init && git add -A && git commit -m "Initial commit" ${color.dim(
247
+ '# Start your git history (optional)',
248
+ )}`,
249
+ );
250
+ }
251
+
252
+ commands.push(`pnpm develop ${color.dim('# Start the development server')}`);
253
+
254
+ const whatsNext = stripIndent`
255
+ Your new app is ready to go! There’s just ${
256
+ commands.length > 1 ? 'a few more steps' : 'one more step'
257
+ } you’ll need to take
258
+ in order to start developing:
259
+ `;
260
+
261
+ console.log();
262
+ console.log(whatsNext);
263
+ console.log();
264
+ console.log(commands.map((command) => ` ${command}`).join('\n'));
265
+
266
+ const followUp = stripIndent`
267
+ Quilt can also help you build, test, lint, and type-check your new application.
268
+ You can learn more about building apps with Quilt by reading the documentation:
269
+ ${color.underline(
270
+ color.magenta(
271
+ 'https://github.com/lemonmade/quilt/tree/main/documentation',
272
+ ),
273
+ )}
274
+
275
+ Have fun! 🎉
276
+ `;
277
+
278
+ console.log();
279
+ console.log(followUp);
280
+ }
281
+
282
+ // Argument handling
283
+
284
+ function getArgv() {
285
+ const argv = arg(
286
+ {
287
+ '--yes': Boolean,
288
+ '-y': '--yes',
289
+ '--name': String,
290
+ '--template': String,
291
+ '--directory': String,
292
+ '--install': Boolean,
293
+ '--no-install': Boolean,
294
+ '--monorepo': Boolean,
295
+ '--no-monorepo': Boolean,
296
+ '--package-manager': String,
297
+ '--extras': [String],
298
+ '--no-extras': Boolean,
299
+ '--help': Boolean,
300
+ '-h': '--help',
301
+ },
302
+ {permissive: true},
303
+ );
304
+
305
+ return argv;
306
+ }
307
+
308
+ async function getName(argv: Arguments, {inWorkspace}: {inWorkspace: boolean}) {
309
+ let {'--name': name} = argv;
310
+
311
+ if (name == null) {
312
+ name = await prompt({
313
+ type: 'text',
314
+ message: 'What would you like to name your new app?',
315
+ initial: inWorkspace ? 'app' : 'my-quilt-app',
316
+ });
317
+ }
318
+
319
+ return name!;
320
+ }
321
+
322
+ async function getDirectory(argv: Arguments, {name}: {name: string}) {
323
+ let directory = path.resolve(
324
+ argv['--directory'] ?? toValidPackageName(name!),
325
+ );
326
+
327
+ while (!argv['--yes']) {
328
+ if (fs.existsSync(directory) && !(await isEmpty(directory))) {
329
+ const relativeDirectory = path.relative(process.cwd(), directory);
330
+
331
+ const empty = await prompt({
332
+ type: 'confirm',
333
+ message: `Directory ${color.bold(
334
+ relativeDirectoryForDisplay(relativeDirectory),
335
+ )} is not empty, is it safe to empty it?`,
336
+ initial: true,
337
+ });
338
+
339
+ if (empty) break;
340
+
341
+ const promptDirectory = await prompt({
342
+ type: 'text',
343
+ message: 'What directory do you want to create your new app in?',
344
+ });
345
+
346
+ directory = path.resolve(promptDirectory);
347
+ } else {
348
+ break;
349
+ }
350
+ }
351
+
352
+ return directory;
353
+ }
354
+
355
+ type Template = 'basic' | 'single-file';
356
+ const VALID_TEMPLATES = new Set<Template>(['basic', 'single-file']);
357
+
358
+ async function getTemplate(argv: Arguments) {
359
+ if (argv['--template'] && VALID_TEMPLATES.has(argv['--template'] as any)) {
360
+ return argv['--template'] as Template;
361
+ }
362
+
363
+ const template: Template = await prompt({
364
+ type: 'select',
365
+ message: 'What template would you like to use?',
366
+ hint: `Use ${color.bold('arrow keys')} to select, and ${color.bold(
367
+ 'return',
368
+ )} to submit`,
369
+ choices: [
370
+ {
371
+ title: `${color.bold(
372
+ 'The basics',
373
+ )}, a web app with a minimal file structure`,
374
+ value: 'basic',
375
+ },
376
+ {
377
+ title: `${color.bold(
378
+ 'Itty-bitty',
379
+ )}, an entire web app in a single file`,
380
+ value: 'single-file',
381
+ },
382
+ // TODO: GraphQL API
383
+ ],
384
+ });
385
+
386
+ return template;
387
+ }