Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions packages/create-starter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import { trackSelectedKit } from './metrics';
const STARTER_KITS_JSON_URL = 'https://raw.githubusercontent.com/thisdot/starter.dev/main/starter-kits.json';
const EXCLUDED_PACKAGE_JSON_FIELDS = ['hasShowcase'];

type Package = {
scripts: {
dev: string;
start: string;
};
};

export async function main() {
console.log(`\n${bold('Welcome to starter.dev!')} ${gray('(create-starter)')}`);

Expand Down Expand Up @@ -55,11 +62,21 @@ export async function main() {
},
]);

if (!options.kit || !options.name) {
const packageOptions = await prompts([
{
type: 'autocomplete',
name: 'packageManager',
message: 'Which package manager would you like to use?',
choices: packageSelection(options.kit).map((pm) => ({ title: pm, value: pm })),
suggest: (input, choices) => Promise.resolve(choices.filter((c) => c.title.includes(input))),
Comment thread
WillHutt marked this conversation as resolved.
},
]);

if (!options.kit || !options.name || !packageOptions) {
process.exit(1);
}

const [createSelectedKitResult] = await Promise.allSettled([createStarter(options), trackSelectedKit(options.kit)]);
const [createSelectedKitResult] = await Promise.allSettled([createStarter(options, packageOptions), trackSelectedKit(options.kit)]);

if (createSelectedKitResult.status === 'rejected') {
const err = createSelectedKitResult.reason;
Expand All @@ -68,9 +85,23 @@ export async function main() {
}
}

async function createStarter(options: prompts.Answers<'name' | 'kit'>): Promise<void> {
function packageSelection(selection: string): Array<string> {
let packageOptions;
switch (selection) {
case 'deno-oak-denodb':
packageOptions = ['deno'];
break;
default:
packageOptions = ['npm', 'pnpm', 'yarn'];
}
return packageOptions;
}

async function createStarter(options: prompts.Answers<'name' | 'kit'>, packageOptions: prompts.Answers<'packageManager'>): Promise<void> {
const repoPath = `thisdot/starter.dev/starters/${options.kit}`;
const destPath = path.join(process.cwd(), options.name);
const packageManager = packageOptions.packageManager;
const kit = options.kit;

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

await initGitRepo(destPath);
const packageCommand = `https://raw.githubusercontent.com/thisdot/starter.dev/main/starters/${kit}/${packageManager === 'deno' ? 'deno' : 'package'}.json`;
const res = await fetch(packageCommand);
let packageJSON: Package;

if (res.ok) {
packageJSON = (await res.json()) as Package;
} else {
throw new Error();
}

const startAction = packageJSON?.scripts?.dev !== undefined ? 'dev' : 'start';

await initGitRepo(destPath, packageManager);
console.log(bold(green('✔') + ' Done!'));
console.log('\nNext steps:');
console.log(` ${bold(cyan(`cd ${options.name}`))}`);
Comment on lines 146 to 147
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Dustin wanted to update the language here to be this based on the slack thread disucssion:

oh update next steps to say "npm start" (or equivalent based on package manager selected" instead of "npm install"

I double check with him on that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the kit is created we would still be outside of the newly created directory though and would still need to cd into it.


if (packageJsonExists) {
console.log(` ${bold(cyan('npm install'))} (or pnpm install, yarn, etc)`);
console.log(` ${bold(cyan(`${packageManager} ${startAction}`))}`);
} else if (packageManager === 'deno') {
console.log(` ${bold(cyan('Make sure you have Docker & docker-compose installed on your machine'))}`);
console.log(` ${bold(cyan('Create a .env file and copy the contents of .env.example into it'))}`);
console.log(` ${bold(cyan('Run deno task start-db to start the local PostgreSQL and Redis'))}`);
console.log(` ${bold(cyan('Run deno task start-web to start the development server'))}`);
console.log(` ${bold(cyan('Open your browser to http://localhost:3333/health to see the API running'))}`);
console.log(` ${bold(cyan('Proceed to the Seeding chapter to seed the database with some sample data'))}`);
}
} catch (err: unknown) {
throw new Error('Failed to initialize the starter kit. This probably means that you provided an invalid kit name.');
Expand Down
19 changes: 12 additions & 7 deletions packages/create-starter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ export async function fileExists(path: string) {
}
}

export async function initGitRepo(path: string) {
export async function initGitRepo(path: string, packageManager: string) {
return new Promise((resolve, reject) => {
exec(`git init ${path}`, (err) => {
if (err) {
reject(err);
} else {
resolve(undefined);
exec(
`cd ${path} && git init && ${packageManager} ${
packageManager === 'deno' ? 'cache --lock=deno.lock --lock-write deps.ts' : 'install'
} && git add . && git commit -m 'init commit generated by starter.dev CLI' && cd ../`,
Comment on lines +21 to +23

Check warning

Code scanning / CodeQL

Shell command built from environment values

This shell command depends on an uncontrolled [absolute path](1).
(err) => {
if (err) {
reject(err);
} else {
resolve(undefined);
}
}
});
);
});
}

Expand Down