-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
185 lines (160 loc) · 6.2 KB
/
create.js
File metadata and controls
185 lines (160 loc) · 6.2 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
import { program } from 'commander'
import chalk from 'chalk'
import inquirer from 'inquirer'
import fs from 'fs-extra'
import path from 'path'
import ora from 'ora'
import validatePackageName from 'validate-npm-package-name'
import { exec } from 'child_process'
import { promisify } from 'util'
const execAsync = promisify(exec)
const spinner = ora()
program
.name('create-tauri-app')
.description('Create a new Tauri 2 + React + TypeScript project')
.argument('[project-name]', 'Project name')
.option('-f, --force', 'Overwrite target directory if it exists')
.action(async (projectName, options) => {
try {
// If no project name provided, ask for it
if (!projectName) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'projectName',
message: 'Project name:',
validate: input => {
if (!input) return 'Project name is required'
const validation = validatePackageName(input)
if (!validation.validForNewPackages) {
return `Invalid project name: ${validation.errors[0]}`
}
return true
},
},
])
projectName = answers.projectName
}
// Validate project name
const validation = validatePackageName(projectName)
if (!validation.validForNewPackages) {
console.error(chalk.red(`Error: Invalid project name: ${validation.errors[0]}`))
process.exit(1)
}
const targetDir = path.resolve(process.cwd(), projectName)
// Check if directory exists
if (fs.existsSync(targetDir)) {
if (!options.force) {
const { overwrite } = await inquirer.prompt([
{
type: 'confirm',
name: 'overwrite',
message: `Directory "${projectName}" already exists. Overwrite?`,
default: false,
},
])
if (!overwrite) {
console.log(chalk.yellow('Operation cancelled'))
process.exit(0)
}
}
spinner.start('Cleaning existing directory...')
await fs.remove(targetDir)
spinner.succeed('Directory cleaned')
}
// Create project directory
spinner.start('Creating project directory...')
await fs.ensureDir(targetDir)
spinner.succeed('Project directory created')
// Get template directory
const currentFileUrl = new URL(import.meta.url)
let templateDir = path.dirname(currentFileUrl.pathname)
// Fix Windows path issue (remove leading slash)
if (process.platform === 'win32' && templateDir.startsWith('/')) {
templateDir = templateDir.substring(1)
}
const templatePath = path.join(templateDir, 'template')
// Copy template files
spinner.start('Copying template files...')
await fs.copy(templatePath, targetDir, {
filter: src => {
// Skip node_modules and .git directories
const basename = path.basename(src)
return basename !== 'node_modules' && basename !== '.git'
},
})
// Make husky hooks executable
const huskyDir = path.join(targetDir, '.husky')
if (await fs.pathExists(huskyDir)) {
const hooks = await fs.readdir(huskyDir)
for (const hook of hooks) {
const hookPath = path.join(huskyDir, hook)
const stat = await fs.stat(hookPath)
if (stat.isFile()) {
await fs.chmod(hookPath, '755')
}
}
}
spinner.succeed('Template files copied')
// Update package.json
spinner.start('Updating package.json...')
const packageJsonPath = path.join(targetDir, 'package.json')
const packageJson = await fs.readJson(packageJsonPath)
packageJson.name = projectName
packageJson.version = '0.1.0'
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 })
spinner.succeed('package.json updated')
// Update tauri.conf.json
spinner.start('Updating Tauri configuration...')
const tauriConfPath = path.join(targetDir, 'src-tauri', 'tauri.conf.json')
const tauriConf = await fs.readJson(tauriConfPath)
tauriConf.productName = projectName
tauriConf.version = '0.1.0'
tauriConf.identifier = `com.${projectName.toLowerCase()}.${projectName.toLowerCase()}`
tauriConf.app.windows[0].title = projectName
await fs.writeJson(tauriConfPath, tauriConf, { spaces: 2 })
spinner.succeed('Tauri configuration updated')
// Update Cargo.toml
spinner.start('Updating Cargo.toml...')
const cargoPath = path.join(targetDir, 'src-tauri', 'Cargo.toml')
let cargoContent = await fs.readFile(cargoPath, 'utf8')
cargoContent = cargoContent.replace(
/name = "tauri-app"/,
`name = "${projectName.replace(/-/g, '_')}"`
)
await fs.writeFile(cargoPath, cargoContent)
spinner.succeed('Cargo.toml updated')
// Remove template-specific files
const filesToRemove = ['create-package.json', 'create.js']
// Initialize git repository and install husky
spinner.start('Initializing Git repository...')
await fs.ensureDir(path.join(targetDir, '.git'))
await execAsync('git init', { cwd: targetDir })
spinner.succeed('Git repository initialized')
// Install dependencies (this will also run husky install)
spinner.start('Installing dependencies...')
await execAsync('pnpm install', { cwd: targetDir })
spinner.succeed('Dependencies installed')
for (const file of filesToRemove) {
const filePath = path.join(targetDir, file)
if (await fs.pathExists(filePath)) {
await fs.remove(filePath)
}
}
console.log()
console.log(chalk.green('✨ Project created successfully!'))
console.log()
console.log(chalk.cyan('Next steps:'))
console.log()
console.log(chalk.gray(` cd ${projectName}`))
console.log(chalk.gray(' pnpm tauri dev'))
console.log()
console.log(chalk.yellow('Happy coding! 🚀'))
} catch (error) {
spinner.fail('Error occurred')
console.error(chalk.red(error.message))
process.exit(1)
}
})
program.parse()