-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathcreate.ts
More file actions
84 lines (67 loc) · 2.42 KB
/
create.ts
File metadata and controls
84 lines (67 loc) · 2.42 KB
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
import path from 'node:path';
import * as p from '@clack/prompts';
import { pmFromUserAgent, pmInstall } from './utils/pm.ts';
import { cancelExit, confirmStep, rgbText } from './utils/prompts.ts';
import { copyTemplate, prepareDirectory } from './utils/files.ts';
import { getPackageName, getProjectDirectory } from './utils/inputs.ts';
import { detect, resolveCommand } from 'package-manager-detector';
const DEFAULT_PROJECT_DIR = 'tgpu-project';
const PROJECT_TEMPLATES = [
{
value: 'vite-simple',
label: rgbText('Vite (Simple)', 175, 105, 245),
},
];
export async function createProject(cwd: string) {
p.intro('Creating a new TypeGPU project.');
const projectDir = await getProjectDirectory(DEFAULT_PROJECT_DIR);
const root = await prepareDirectory(cwd, projectDir);
const packageName = await getPackageName(projectDir);
const projectTemplate = await p.select({
message: 'Select a template:',
options: PROJECT_TEMPLATES,
});
if (p.isCancel(projectTemplate)) {
cancelExit();
}
p.log.step(`Scaffolding project in ${projectDir}...`);
const templateDir = path.resolve(
import.meta.dirname,
'../templates',
`template-${projectTemplate}`,
);
copyTemplate(templateDir, root, packageName);
p.log.success(`Scaffolded project at ${projectDir}.`);
const detected = await detect({ cwd });
const pm = detected?.agent ?? pmFromUserAgent(process.env.npm_config_user_agent);
const shouldInstall = await confirmStep(`Install dependencies with ${pm}?`, true);
if (shouldInstall) {
process.chdir(root);
pmInstall(pm);
}
const cdPath = path.relative(cwd, root);
const installCmd = resolveCommand(pm, 'install', []);
const runCmd = resolveCommand(pm, 'run', ['dev']);
const steps: string[] = [];
const shouldCd = (!shouldInstall && !!installCmd) || !!runCmd || !!cdPath;
if (shouldCd && cdPath) {
steps.push(` cd ${cdPath}`);
}
if (!shouldInstall && installCmd) {
steps.push(` ${installCmd.command} ${installCmd.args.join(' ')}`);
}
if (runCmd) {
steps.push(` ${runCmd.command} ${runCmd.args.join(' ')}`);
}
let msg = 'Done!\n';
if (steps.length > 0) {
msg += ` To get started run:\n\n`;
msg += steps.join('\n');
msg += `\n\n`;
msg += `\
Note: If you are using VS Code or Cursor, you may need to run
“TypeScript: Select TypeScript Version” and choose
“Use Workspace Version” to enable tsover.`;
}
p.outro(msg);
}