Skip to content

Commit da72e42

Browse files
jdwilkin4WillHutt
andauthored
feat: updating CLI options (#1280)
Co-authored-by: WillHutt <whutt1994@gmail.com>
1 parent 0eef0e3 commit da72e42

2 files changed

Lines changed: 67 additions & 12 deletions

File tree

packages/create-starter/src/index.ts

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import { trackSelectedKit } from './metrics';
1111
const STARTER_KITS_JSON_URL = 'https://raw.githubusercontent.com/thisdot/starter.dev/main/starter-kits.json';
1212
const EXCLUDED_PACKAGE_JSON_FIELDS = ['hasShowcase'];
1313

14+
type Package = {
15+
scripts: {
16+
dev: string;
17+
start: string;
18+
};
19+
};
20+
1421
export async function main() {
1522
console.log(`\n${bold('Welcome to starter.dev!')} ${gray('(create-starter)')}`);
1623

@@ -55,11 +62,21 @@ export async function main() {
5562
},
5663
]);
5764

58-
if (!options.kit || !options.name) {
65+
const packageOptions = await prompts([
66+
{
67+
type: 'autocomplete',
68+
name: 'packageManager',
69+
message: 'Which package manager would you like to use?',
70+
choices: packageSelection(options.kit).map((pm) => ({ title: pm, value: pm })),
71+
suggest: (input, choices) => Promise.resolve(choices.filter((c) => c.title.includes(input))),
72+
},
73+
]);
74+
75+
if (!options.kit || !options.name || !packageOptions) {
5976
process.exit(1);
6077
}
6178

62-
const [createSelectedKitResult] = await Promise.allSettled([createStarter(options), trackSelectedKit(options.kit)]);
79+
const [createSelectedKitResult] = await Promise.allSettled([createStarter(options, packageOptions), trackSelectedKit(options.kit)]);
6380

6481
if (createSelectedKitResult.status === 'rejected') {
6582
const err = createSelectedKitResult.reason;
@@ -68,9 +85,23 @@ export async function main() {
6885
}
6986
}
7087

71-
async function createStarter(options: prompts.Answers<'name' | 'kit'>): Promise<void> {
88+
function packageSelection(selection: string): Array<string> {
89+
let packageOptions;
90+
switch (selection) {
91+
case 'deno-oak-denodb':
92+
packageOptions = ['deno'];
93+
break;
94+
default:
95+
packageOptions = ['npm', 'pnpm', 'yarn'];
96+
}
97+
return packageOptions;
98+
}
99+
100+
async function createStarter(options: prompts.Answers<'name' | 'kit'>, packageOptions: prompts.Answers<'packageManager'>): Promise<void> {
72101
const repoPath = `thisdot/starter.dev/starters/${options.kit}`;
73102
const destPath = path.join(process.cwd(), options.name);
103+
const packageManager = packageOptions.packageManager;
104+
const kit = options.kit;
74105

75106
const emitter = degit(repoPath, {
76107
cache: false,
@@ -98,13 +129,32 @@ async function createStarter(options: prompts.Answers<'name' | 'kit'>): Promise<
98129
await initNodeProject(packageJsonPath, destPath, options);
99130
}
100131

101-
await initGitRepo(destPath);
132+
const packageCommand = `https://raw.githubusercontent.com/thisdot/starter.dev/main/starters/${kit}/${packageManager === 'deno' ? 'deno' : 'package'}.json`;
133+
const res = await fetch(packageCommand);
134+
let packageJSON: Package;
135+
136+
if (res.ok) {
137+
packageJSON = (await res.json()) as Package;
138+
} else {
139+
throw new Error();
140+
}
141+
142+
const startAction = packageJSON?.scripts?.dev !== undefined ? 'dev' : 'start';
143+
144+
await initGitRepo(destPath, packageManager);
102145
console.log(bold(green('✔') + ' Done!'));
103146
console.log('\nNext steps:');
104147
console.log(` ${bold(cyan(`cd ${options.name}`))}`);
105148

106149
if (packageJsonExists) {
107-
console.log(` ${bold(cyan('npm install'))} (or pnpm install, yarn, etc)`);
150+
console.log(` ${bold(cyan(`${packageManager} ${startAction}`))}`);
151+
} else if (packageManager === 'deno') {
152+
console.log(` ${bold(cyan('Make sure you have Docker & docker-compose installed on your machine'))}`);
153+
console.log(` ${bold(cyan('Create a .env file and copy the contents of .env.example into it'))}`);
154+
console.log(` ${bold(cyan('Run deno task start-db to start the local PostgreSQL and Redis'))}`);
155+
console.log(` ${bold(cyan('Run deno task start-web to start the development server'))}`);
156+
console.log(` ${bold(cyan('Open your browser to http://localhost:3333/health to see the API running'))}`);
157+
console.log(` ${bold(cyan('Proceed to the Seeding chapter to seed the database with some sample data'))}`);
108158
}
109159
} catch (err: unknown) {
110160
throw new Error('Failed to initialize the starter kit. This probably means that you provided an invalid kit name.');

packages/create-starter/src/utils.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ export async function fileExists(path: string) {
1515
}
1616
}
1717

18-
export async function initGitRepo(path: string) {
18+
export async function initGitRepo(path: string, packageManager: string) {
1919
return new Promise((resolve, reject) => {
20-
exec(`git init ${path}`, (err) => {
21-
if (err) {
22-
reject(err);
23-
} else {
24-
resolve(undefined);
20+
exec(
21+
`cd ${path} && git init && ${packageManager} ${
22+
packageManager === 'deno' ? 'cache --lock=deno.lock --lock-write deps.ts' : 'install'
23+
} && git add . && git commit -m 'init commit generated by starter.dev CLI' && cd ../`,
24+
(err) => {
25+
if (err) {
26+
reject(err);
27+
} else {
28+
resolve(undefined);
29+
}
2530
}
26-
});
31+
);
2732
});
2833
}
2934

0 commit comments

Comments
 (0)