-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (155 loc) · 5.66 KB
/
index.js
File metadata and controls
186 lines (155 loc) · 5.66 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
186
#!/usr/bin/env node
/* eslint-disable no-console */
import fs from 'fs-extra'
import path from 'path'
import prompts from 'prompts'
import chalk from 'chalk'
import { execa } from 'execa'
import { fileURLToPath } from 'url'
// eslint-disable-next-line import/extensions
import runPrettierFormat from './lib/format.js'
const sourceRoot = path.dirname(fileURLToPath(import.meta.url))
const wayfindpcPkgVersion = JSON.parse(
fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8')
).version
const configFiles = [
'.eslintrc.json',
'.eslintignore',
'.prettierrc.json',
'.prettierignore',
'.gitattributes',
'.vscode/settings.json'
]
async function main() {
if (process.argv.includes('--version')) {
console.log(`wayfindpc v${wayfindpcPkgVersion}`)
process.exit(0)
}
if (process.argv[2] === 'format') {
console.log(chalk.cyan('🔧 Formatting project with ESLint and Prettier...'))
await runPrettierFormat()
process.exit(0)
}
if (process.argv[2] === 'lint') {
console.log(chalk.cyan('🔍 Checking project with ESLint and Prettier...'))
try {
await execa('npx', ['eslint', '.'], { stdio: 'inherit' })
await execa('npx', ['prettier', '--check', '.'], { stdio: 'inherit' })
} catch (err) {
if (err.exitCode === 2 && /No files matching/.test(err.stderr || '')) {
console.warn(chalk.yellow('⚠️ No matching files to format with ESLint.'))
} else {
console.error(chalk.red('❌ Formatting failed:'), err.message || err)
process.exit(1)
}
}
process.exit(0)
}
const rawArgs = process.argv.slice(2)
const force = rawArgs.includes('--force')
const nameArg = rawArgs.find((arg) => !arg.startsWith('--'))
const cwd = process.cwd()
const pkgJsonPath = path.join(cwd, 'package.json')
const hasPkg = fs.existsSync(pkgJsonPath)
let pkgName = nameArg
let pkg = {}
if (hasPkg) {
if (pkgName) {
console.log(
chalk.yellow(`⚠️ Found existing package.json. Maintaining the current name.`)
)
}
pkg = await fs.readJSON(pkgJsonPath)
pkgName = pkg.name || path.basename(cwd)
} else {
if (!pkgName) {
const response = await prompts([
{
type: 'text',
name: 'pkgName',
message: 'Package name:',
initial: 'my-project',
validate: (name) => /^[a-zA-Z0-9-_]+$/.test(name) || 'Invalid package name'
}
])
pkgName = response.pkgName
if (!pkgName) {
console.error(chalk.red('❌ No package name provided.'))
process.exit(1)
}
}
pkg.name = pkgName
pkg.version = '1.0.0'
pkg.type = 'module'
pkg.scripts = {}
pkg.devDependencies = {}
console.log(chalk.cyan(`Creating new package.json as "${pkgName}"`))
}
if (pkg.scripts?.format && pkg.scripts.format !== 'wayfindpc format') {
console.log(
chalk.yellow(`⚠️ Overwriting existing "format" script: "${pkg.scripts.format}"`)
)
}
if (pkg.scripts?.format && pkg.scripts.lint !== 'wayfindpc lint') {
console.log(chalk.yellow(`⚠️ Overwriting existing "lint" script: "${pkg.scripts.lint}"`))
}
pkg.scripts = pkg.scripts || {}
pkg.scripts.format = 'wayfindpc format'
pkg.scripts.lint = 'wayfindpc lint'
pkg.devDependencies = {
...pkg.devDependencies,
wayfindpc: `^${wayfindpcPkgVersion}`
}
await fs.writeJSON(pkgJsonPath, pkg, { spaces: 4 })
await fs.ensureDir(path.join(cwd, '.vscode'))
console.log(chalk.cyan('\nWriting fresh config files...'))
const skipped = []
const written = []
// eslint-disable-next-line no-restricted-syntax
for (const filename of configFiles) {
const src = path.join(sourceRoot, filename)
const dest = path.join(cwd, filename)
const exists = fs.existsSync(dest)
if (exists && !force) {
skipped.push(filename)
// eslint-disable-next-line no-continue
continue
}
// eslint-disable-next-line no-await-in-loop
await fs.copy(src, dest)
written.push(filename)
}
if (written.length > 0) {
console.log(chalk.green(`✅ Created: ${written.join(', ')}`))
}
if (skipped.length > 0) {
console.log(chalk.yellow(`⚠️ Skipped: ${skipped.join(', ')}`))
console.log(chalk.gray(' Use --force to overwrite existing files.'))
}
console.log(chalk.yellow('\nInstalling dependencies...\n'))
try {
await execa('npm', ['install'], { cwd, stdio: 'inherit' })
} catch (err) {
console.error(
chalk.red('❌ Failed to install dependencies.'),
chalk.gray('Try running `npm install` manually.')
)
process.exit(1)
}
console.log(chalk.greenBright('\n🎉 Project setup complete!'))
console.log(
chalk.gray('Run ') + chalk.cyan('npm run format') + chalk.gray(' to format your code.')
)
console.log(
chalk.gray('Run ') + chalk.cyan('npm run lint') + chalk.gray(' to lint your code.\n')
)
console.log(`\n ${chalk.yellow('Tip:')}`)
console.log(
chalk.gray('If ESLint squiggles are not showing in VS Code, open the Command Palette') +
chalk.gray(' (') +
chalk.cyan('Ctrl+Shift+P') +
chalk.gray(') and run ') +
chalk.cyan('"ESLint: Restart ESLint Server".\n')
)
}
main()