@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,286 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { execSync } from 'child_process';
4
+ import { s as stripIndent, c as cyan_1, b as bold_1, p as printHelp, g as getCreateAsMonorepo, a as getShouldInstall, d as getPackageManager, e as getExtrasToSetup, f as dim_1, u as underline_1, m as magenta_1, h as arg_1, i as prompt } from './index.mjs';
5
+ import { e as emptyDirectory, t as toValidPackageName, f as format, l as loadTemplate, a as addToTsConfig, b as addToPackageManagerWorkspaces, r as relativeDirectoryForDisplay, i as isEmpty, c as createOutputTarget } from './package-manager.mjs';
6
+ import 'tty';
7
+ import 'url';
8
+ import 'readline';
9
+ import 'events';
10
+
11
+ let _ = t => t,
12
+ _t,
13
+ _t2,
14
+ _t3;
15
+ async function createApp() {
16
+ const argv = getArgv();
17
+
18
+ if (argv['--help']) {
19
+ var _argv$PackageManag;
20
+
21
+ const additionalOptions = stripIndent(_t || (_t = _`
22
+ ${0}
23
+ The template to use for your new application. If you don’t specify a template,
24
+ this command will ask you for one instead. Must be one of the following:
25
+
26
+ - ${0}, a web app with a minimal file structure
27
+ - ${0}, an entire web app in a single file
28
+ `), cyan_1(`--template`), bold_1('basic'), bold_1('single-file'));
29
+ printHelp({
30
+ kind: 'app',
31
+ options: additionalOptions,
32
+ packageManager: (_argv$PackageManag = argv['--package-manager']) === null || _argv$PackageManag === void 0 ? void 0 : _argv$PackageManag.toLowerCase()
33
+ });
34
+ return;
35
+ }
36
+
37
+ const inWorkspace = fs.existsSync('quilt.workspace.ts');
38
+ const name = await getName(argv, {
39
+ inWorkspace
40
+ });
41
+ const directory = await getDirectory(argv, {
42
+ name
43
+ });
44
+ const template = await getTemplate(argv);
45
+ const createAsMonorepo = !inWorkspace && (await getCreateAsMonorepo(argv));
46
+ const shouldInstall = await getShouldInstall(argv);
47
+ const packageManager = await getPackageManager(argv);
48
+ const setupExtras = await getExtrasToSetup(argv, {
49
+ inWorkspace
50
+ });
51
+ const partOfMonorepo = inWorkspace || createAsMonorepo;
52
+ const appDirectory = createAsMonorepo ? path.join(directory, 'app') : directory;
53
+
54
+ if (fs.existsSync(directory)) {
55
+ await emptyDirectory(directory);
56
+
57
+ if (appDirectory !== directory) {
58
+ fs.mkdirSync(appDirectory, {
59
+ recursive: true
60
+ });
61
+ }
62
+ } else {
63
+ fs.mkdirSync(appDirectory, {
64
+ recursive: true
65
+ });
66
+ }
67
+
68
+ const rootDirectory = inWorkspace ? process.cwd() : directory;
69
+ const outputRoot = createOutputTarget(rootDirectory);
70
+ const appTemplate = loadTemplate(template === 'basic' ? 'app-basic' : 'app-single-file');
71
+ const workspaceTemplate = loadTemplate('workspace'); // If we aren’t already in a workspace, copy the workspace files over, which
72
+ // are needed if we are making a monorepo or not.
73
+
74
+ if (!inWorkspace) {
75
+ await workspaceTemplate.copy(directory, file => {
76
+ // When this is a single project, we use the project’s Quilt configuration as the base.
77
+ if (file === 'quilt.workspace.ts') return createAsMonorepo; // We need to make some adjustments to the root package.json
78
+
79
+ return file !== 'package.json';
80
+ }); // If we are creating a monorepo, we need to add the root package.json and
81
+ // package manager workspace configuration.
82
+
83
+ if (createAsMonorepo) {
84
+ const workspacePackageJson = JSON.parse(await workspaceTemplate.read('package.json'));
85
+ workspacePackageJson.name = toValidPackageName(name);
86
+
87
+ if (packageManager === 'pnpm') {
88
+ await outputRoot.write('pnpm-workspace.yaml', await format(`
89
+ packages:
90
+ - './packages/*'
91
+ `, {
92
+ as: 'yaml'
93
+ }));
94
+ } else {
95
+ workspacePackageJson.workspaces = ['packages/*'];
96
+ }
97
+
98
+ await outputRoot.write('package.json', await format(JSON.stringify(workspacePackageJson), {
99
+ as: 'json-stringify'
100
+ }));
101
+ } else {
102
+ const [projectPackageJson, projectTSConfig, workspacePackageJson] = await Promise.all([appTemplate.read('package.json').then(content => JSON.parse(content)), appTemplate.read('tsconfig.json').then(content => JSON.parse(content)), workspaceTemplate.read('package.json').then(content => JSON.parse(content))]);
103
+ workspacePackageJson.name = toValidPackageName(name);
104
+ workspacePackageJson.eslintConfig = projectPackageJson.eslintConfig;
105
+ workspacePackageJson.browserslist = projectPackageJson.browserslist;
106
+ let quiltProject = await appTemplate.read('quilt.project.ts');
107
+ quiltProject = quiltProject.replace('quiltApp', 'quiltWorkspace, quiltApp').replace('quiltApp(', 'quiltWorkspace(), quiltApp(');
108
+ await outputRoot.write('quilt.project.ts', await format(quiltProject, {
109
+ as: 'typescript'
110
+ }));
111
+ await outputRoot.write('package.json', await format(JSON.stringify(workspacePackageJson), {
112
+ as: 'json-stringify'
113
+ }));
114
+ await outputRoot.write('tsconfig.json', await format(JSON.stringify(projectTSConfig), {
115
+ as: 'json'
116
+ }));
117
+ }
118
+
119
+ if (setupExtras.has('github')) {
120
+ await loadTemplate('github').copy(directory);
121
+ }
122
+
123
+ if (setupExtras.has('vscode')) {
124
+ await loadTemplate('vscode').copy(directory);
125
+ }
126
+ }
127
+
128
+ await appTemplate.copy(appDirectory, file => {
129
+ // If we are in a monorepo, we can use all the template files as they are
130
+ if (file === 'quilt.project.ts' || file === 'tsconfig.json') {
131
+ return partOfMonorepo;
132
+ } // We need to make some adjustments the project’s package.json
133
+
134
+
135
+ return file !== 'package.json';
136
+ });
137
+
138
+ if (partOfMonorepo) {
139
+ // Write the app’s package.json (the root one was already created)
140
+ const projectPackageJson = JSON.parse(await appTemplate.read('package.json'));
141
+ projectPackageJson.name = path.basename(appDirectory);
142
+ await outputRoot.write(path.join(appDirectory, 'package.json'), await format(JSON.stringify(projectPackageJson), {
143
+ as: 'json-stringify'
144
+ }));
145
+ await Promise.all([addToTsConfig(appDirectory, outputRoot), addToPackageManagerWorkspaces(appDirectory, outputRoot, packageManager)]);
146
+ }
147
+
148
+ if (shouldInstall) {
149
+ process.stdout.write('\nInstalling dependencies...\n'); // TODO: better loading, handle errors
150
+
151
+ execSync(`${packageManager} install`, {
152
+ cwd: rootDirectory
153
+ });
154
+ process.stdout.moveCursor(0, -1);
155
+ process.stdout.clearLine(1);
156
+ console.log('Installed dependencies.');
157
+ }
158
+
159
+ const commands = [];
160
+
161
+ if (!inWorkspace && directory !== process.cwd()) {
162
+ commands.push(`cd ${cyan_1(relativeDirectoryForDisplay(path.relative(process.cwd(), directory)))} ${dim_1('# Move into your new app’s directory')}`);
163
+ }
164
+
165
+ if (!shouldInstall) {
166
+ commands.push(`pnpm install ${dim_1('# Install all your dependencies')}`);
167
+ }
168
+
169
+ if (!inWorkspace) {
170
+ // TODO: change this condition to check if git was initialized already
171
+ commands.push(`git init && git add -A && git commit -m "Initial commit" ${dim_1('# Start your git history (optional)')}`);
172
+ }
173
+
174
+ commands.push(`pnpm develop ${dim_1('# Start the development server')}`);
175
+ const whatsNext = stripIndent(_t2 || (_t2 = _`
176
+ Your new app is ready to go! There’s just ${0} you’ll need to take
177
+ in order to start developing:
178
+ `), commands.length > 1 ? 'a few more steps' : 'one more step');
179
+ console.log();
180
+ console.log(whatsNext);
181
+ console.log();
182
+ console.log(commands.map(command => ` ${command}`).join('\n'));
183
+ const followUp = stripIndent(_t3 || (_t3 = _`
184
+ Quilt can also help you build, test, lint, and type-check your new application.
185
+ You can learn more about building apps with Quilt by reading the documentation:
186
+ ${0}
187
+
188
+ Have fun! 🎉
189
+ `), underline_1(magenta_1('https://github.com/lemonmade/quilt/tree/main/documentation')));
190
+ console.log();
191
+ console.log(followUp);
192
+ } // Argument handling
193
+
194
+ function getArgv() {
195
+ const argv = arg_1({
196
+ '--yes': Boolean,
197
+ '-y': '--yes',
198
+ '--name': String,
199
+ '--template': String,
200
+ '--directory': String,
201
+ '--install': Boolean,
202
+ '--no-install': Boolean,
203
+ '--monorepo': Boolean,
204
+ '--no-monorepo': Boolean,
205
+ '--package-manager': String,
206
+ '--extras': [String],
207
+ '--no-extras': Boolean,
208
+ '--help': Boolean,
209
+ '-h': '--help'
210
+ }, {
211
+ permissive: true
212
+ });
213
+ return argv;
214
+ }
215
+
216
+ async function getName(argv, {
217
+ inWorkspace
218
+ }) {
219
+ let {
220
+ '--name': name
221
+ } = argv;
222
+
223
+ if (name == null) {
224
+ name = await prompt({
225
+ type: 'text',
226
+ message: 'What would you like to name your new app?',
227
+ initial: inWorkspace ? 'app' : 'my-quilt-app'
228
+ });
229
+ }
230
+
231
+ return name;
232
+ }
233
+
234
+ async function getDirectory(argv, {
235
+ name
236
+ }) {
237
+ var _argv$Directory;
238
+
239
+ let directory = path.resolve((_argv$Directory = argv['--directory']) !== null && _argv$Directory !== void 0 ? _argv$Directory : toValidPackageName(name));
240
+
241
+ while (!argv['--yes']) {
242
+ if (fs.existsSync(directory) && !(await isEmpty(directory))) {
243
+ const relativeDirectory = path.relative(process.cwd(), directory);
244
+ const empty = await prompt({
245
+ type: 'confirm',
246
+ message: `Directory ${bold_1(relativeDirectoryForDisplay(relativeDirectory))} is not empty, is it safe to empty it?`,
247
+ initial: true
248
+ });
249
+ if (empty) break;
250
+ const promptDirectory = await prompt({
251
+ type: 'text',
252
+ message: 'What directory do you want to create your new app in?'
253
+ });
254
+ directory = path.resolve(promptDirectory);
255
+ } else {
256
+ break;
257
+ }
258
+ }
259
+
260
+ return directory;
261
+ }
262
+
263
+ const VALID_TEMPLATES = new Set(['basic', 'single-file']);
264
+
265
+ async function getTemplate(argv) {
266
+ if (argv['--template'] && VALID_TEMPLATES.has(argv['--template'])) {
267
+ return argv['--template'];
268
+ }
269
+
270
+ const template = await prompt({
271
+ type: 'select',
272
+ message: 'What template would you like to use?',
273
+ hint: `Use ${bold_1('arrow keys')} to select, and ${bold_1('return')} to submit`,
274
+ choices: [{
275
+ title: `${bold_1('The basics')}, a web app with a minimal file structure`,
276
+ value: 'basic'
277
+ }, {
278
+ title: `${bold_1('Itty-bitty')}, an entire web app in a single file`,
279
+ value: 'single-file'
280
+ } // TODO: GraphQL API
281
+ ]
282
+ });
283
+ return template;
284
+ }
285
+
286
+ export { createApp };