Skip to content

Commit df36ab5

Browse files
authored
feat(create-tsdown): migrate from Gugustinette/create-tsdown (#535)
1 parent feecadf commit df36ab5

File tree

15 files changed

+287
-58
lines changed

15 files changed

+287
-58
lines changed

docs/guide/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bun add -D typescript
6464

6565
### Starter Templates {#starter-templates}
6666

67-
To get started even faster, you can use the [create-tsdown](https://github.com/gugustinette/create-tsdown) CLI, which provides a set of starter templates for building pure TypeScript libraries, as well as frontend libraries like React and Vue.
67+
To get started even faster, you can use the [create-tsdown](https://github.com/rolldown/tsdown/tree/main/packages/create-tsdown) CLI, which provides a set of starter templates for building pure TypeScript libraries, as well as frontend libraries like React and Vue.
6868

6969
::: code-group
7070

docs/zh-CN/guide/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bun add -D typescript
6464

6565
### 起步模板 {#starter-templates}
6666

67-
为了更快速地开始,您可以使用 [create-tsdown](https://github.com/gugustinette/create-tsdown) CLI,它提供了一系列起步模板,适用于构建纯 TypeScript 库以及如 React、Vue 等前端库。
67+
为了更快速地开始,您可以使用 [create-tsdown](https://github.com/rolldown/tsdown/tree/main/packages/create-tsdown) CLI,它提供了一系列起步模板,适用于构建纯 TypeScript 库以及如 React、Vue 等前端库。
6868

6969
::: code-group
7070

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default sxzz({
44
pnpm: true,
55
})
66
.append({
7-
files: ['examples/**'],
7+
files: ['templates/**'],
88
rules: {
99
'pnpm/json-enforce-catalog': 'off',
1010
},

examples/simple/package.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/simple/src/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/simple/tsdown.config.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"test": "vitest",
5454
"typecheck": "tsc --noEmit",
5555
"format": "prettier --cache --write .",
56-
"release": "bumpp",
56+
"release": "bumpp -r",
5757
"prepublishOnly": "pnpm run build",
5858
"docs:dev": "pnpm -C docs run dev",
5959
"docs:build": "pnpm -C docs run build",
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "create-tsdown",
3+
"version": "0.0.3",
4+
"description": "Create a new tsdown project",
5+
"type": "module",
6+
"license": "MIT",
7+
"homepage": "https://github.com/rolldown/tsdown/tree/main/packages/create#readme",
8+
"bugs": {
9+
"url": "https://github.com/rolldown/tsdown/issues"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/rolldown/tsdown.git",
14+
"directory": "packages/create-tsdown"
15+
},
16+
"author": "Kevin Deng <sxzz@sxzz.moe>",
17+
"funding": "https://github.com/sponsors/sxzz",
18+
"files": [
19+
"dist"
20+
],
21+
"main": "./dist/index.mjs",
22+
"module": "./dist/index.mjs",
23+
"types": "./dist/index.d.mts",
24+
"exports": {
25+
".": "./dist/index.mjs",
26+
"./run": "./dist/run.mjs",
27+
"./package.json": "./package.json"
28+
},
29+
"typesVersions": {
30+
"*": {
31+
"*": [
32+
"./dist/*",
33+
"./*"
34+
]
35+
}
36+
},
37+
"bin": {
38+
"create-tsdown": "./dist/run.js"
39+
},
40+
"publishConfig": {
41+
"access": "public"
42+
},
43+
"scripts": {},
44+
"dependencies": {
45+
"@clack/prompts": "catalog:prod",
46+
"cac": "catalog:prod",
47+
"giget": "catalog:prod",
48+
"package-manager-detector": "catalog:prod"
49+
},
50+
"engines": {
51+
"node": ">=20.19.0"
52+
}
53+
}

packages/create-tsdown/src/cli.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import process from 'node:process'
2+
import { log } from '@clack/prompts'
3+
import { cac } from 'cac'
4+
import { version } from '../package.json'
5+
import { create, type Options } from './index'
6+
7+
const cli = cac('create-tsdown')
8+
cli.help().version(version)
9+
10+
cli
11+
.command('[path]', 'Create a tsdown project', {
12+
ignoreOptionDefaultValue: true,
13+
allowUnknownOptions: true,
14+
})
15+
.option(
16+
'-t, --template <template>',
17+
'Available templates: default, minimal, vue, react, solid',
18+
{ default: 'default' },
19+
)
20+
.action((path: string | undefined, options: Options) => create(path, options))
21+
22+
export async function runCLI(): Promise<void> {
23+
cli.parse(process.argv, { run: false })
24+
25+
try {
26+
await cli.runMatchedCommand()
27+
} catch (error) {
28+
log.error(String(error))
29+
process.exit(1)
30+
}
31+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import process from 'node:process'
2+
import { styleText } from 'node:util'
3+
import {
4+
cancel,
5+
intro,
6+
isCancel,
7+
outro,
8+
select,
9+
spinner,
10+
text,
11+
} from '@clack/prompts'
12+
import { downloadTemplate } from 'giget'
13+
import { getUserAgent } from 'package-manager-detector'
14+
15+
export interface Options {
16+
template?: 'default' | 'minimal' | 'vue' | 'react' | 'solid'
17+
path?: string
18+
}
19+
20+
export type ResolvedOptions = Required<Options>
21+
22+
/**
23+
* Create a tsdown project.
24+
*/
25+
export async function create(
26+
path: string | undefined,
27+
options: Options,
28+
): Promise<void> {
29+
intro(`Creating a tsdown project...`)
30+
31+
const resolved = await resolveOptions({ ...options, path })
32+
33+
const s = spinner()
34+
s.start('Cloning the template...')
35+
36+
await downloadTemplate(`gh:sxzz/tsdown-templates/${resolved.template}`, {
37+
dir: resolved.path,
38+
})
39+
40+
s.stop('Template cloned')
41+
42+
const pm = getUserAgent() || 'npm'
43+
outro(
44+
`Done! Now run:\n` +
45+
` ${styleText('green', `cd ${resolved.path}`)}\n` +
46+
` ${styleText('green', `${pm} install`)}\n` +
47+
` ${styleText('green', `${pm} run build`)}\n\n` +
48+
`For more information, visit: ${styleText('underline', `https://tsdown.dev/`)}`,
49+
)
50+
}
51+
52+
/**
53+
* Resolve the user options and configs
54+
* @param options The user options
55+
* @returns The resolved options
56+
*/
57+
export async function resolveOptions(
58+
options: Options,
59+
): Promise<ResolvedOptions> {
60+
let path: Options['path'] | symbol = options.path
61+
62+
if (!path) {
63+
const defaultPath = './my-tsdown-package'
64+
path =
65+
(await text({
66+
message: 'What is the name of your package?',
67+
placeholder: defaultPath,
68+
})) || defaultPath
69+
if (isCancel(path)) {
70+
cancel('Operation cancelled.')
71+
process.exit(1)
72+
}
73+
}
74+
75+
let template: Options['template'] | symbol = options.template
76+
if (template) {
77+
if (!['default', 'minimal', 'vue', 'react', 'solid'].includes(template)) {
78+
throw new Error(
79+
`Invalid template "${template}". Available templates: default, vue, react, solid`,
80+
)
81+
}
82+
} else {
83+
template = await select({
84+
message: 'Which template do you want to use?',
85+
options: [
86+
{ value: 'default', label: 'Default' },
87+
{ value: 'minimal', label: 'Minimal' },
88+
{ value: 'vue', label: 'Vue' },
89+
{ value: 'react', label: 'React' },
90+
{ value: 'solid', label: 'Solid' },
91+
],
92+
initialValue: 'default',
93+
})
94+
95+
if (isCancel(template)) {
96+
cancel('Operation cancelled.')
97+
process.exit(1)
98+
}
99+
}
100+
101+
return {
102+
path,
103+
template,
104+
} satisfies ResolvedOptions
105+
}

0 commit comments

Comments
 (0)