Skip to content

Commit

Permalink
feat(create-tnf): support argv.packageManager
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 26, 2024
1 parent 7f75211 commit 8c78d69
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
11 changes: 8 additions & 3 deletions create-tnf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ Create a new TNF project.
## Usage

```bash
$ npm create tnf
# Create a new project with the given name and template
$ npm create tnf <project-name> --template=<template-name>
$ npm create tnf <project-name> <options>
$ pnpm create tnf <project-name> <options>
$ yarn create tnf <project-name> <options>
```

## Options

- `--package-manager`: Use the specified package manager.
- `--template`: Use the specified template.

## LICENSE

MIT
1 change: 1 addition & 0 deletions create-tnf/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Examples:
cwd: cwd,
name: argv._[0] as string | undefined,
template: argv.template,
packageManager: argv.packageManager,
})
.then(() => {
p.outro('Create success!');
Expand Down
24 changes: 12 additions & 12 deletions create-tnf/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as p from './clack/prompt/index';

const CANCEL_TEXT = 'Operation cancelled.';

type NpmClient = 'pnpm' | 'yarn' | 'npm';
type PackageManager = 'pnpm' | 'yarn' | 'npm';

const templates = {
minimal: {
Expand All @@ -25,12 +25,12 @@ export async function create({
cwd,
name,
template,
npmClient,
packageManager,
}: {
cwd: string;
name?: string;
template?: string;
npmClient?: NpmClient;
packageManager?: PackageManager;
}) {
const templatesPath = path.join(__dirname, '../templates');
const templateList = fs
Expand Down Expand Up @@ -82,16 +82,16 @@ export async function create({
throw new Error(CANCEL_TEXT);
}

const selectedNpmClient =
npmClient ||
const selectedPackageManager =
packageManager ||
(await p.select({
message: 'Which npm client would you like?',
options: ['pnpm', 'yarn', 'npm'].map((client) => ({
value: client,
label: client,
})),
}));
if (p.isCancel(selectedNpmClient)) {
if (p.isCancel(selectedPackageManager)) {
throw new Error(CANCEL_TEXT);
}

Expand All @@ -111,23 +111,23 @@ export async function create({
copySpinner.stop(`Copied template ${selectedTemplate}`);

const installTask = p.taskLog(
`Installing dependencies with ${selectedNpmClient}...`,
`Installing dependencies with ${selectedPackageManager}...`,
);
const args = selectedNpmClient === 'yarn' ? [] : ['install'];
const args = selectedPackageManager === 'yarn' ? [] : ['install'];
try {
await execa(selectedNpmClient, args, {
await execa(selectedPackageManager, args, {
cwd: projectPath,
onData: (data) => {
installTask.text = data;
},
});
} catch (error) {
installTask.fail(
`Failed to install dependencies with ${selectedNpmClient}`,
`Failed to install dependencies with ${selectedPackageManager}`,
);
throw error;
}
installTask.success(`Installed dependencies with ${selectedNpmClient}`);
installTask.success(`Installed dependencies with ${selectedPackageManager}`);

const syncTask = p.taskLog('Setting up project...');
try {
Expand All @@ -147,7 +147,7 @@ export async function create({
`
1: ${pc.bold(pc.cyan(`cd ${projectName}`))}
2: ${pc.bold(pc.cyan(`git init && git add -A && git commit -m "Initial commit"`))} (optional)
3: ${pc.bold(pc.cyan(devCommands[selectedNpmClient as keyof typeof devCommands]))}
3: ${pc.bold(pc.cyan(devCommands[selectedPackageManager as keyof typeof devCommands]))}
To close the dev server, hit ${pc.bold(pc.cyan('Ctrl+C'))}
`.trim(),
Expand Down
5 changes: 3 additions & 2 deletions create-tnf/templates/minimal/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"private": true,
"scripts": {
"dev": "tnf dev",
"build": "tnf build",
"dev": "tnf dev",
"postinstall": "tnf sync",
"preview": "tnf preview",
"postinstall": "tnf sync"
"sync": "tnf sync"
},
"dependencies": {
"@types/react": "^19.0.2",
Expand Down

0 comments on commit 8c78d69

Please sign in to comment.