-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.ts
More file actions
423 lines (372 loc) · 11.9 KB
/
create.ts
File metadata and controls
423 lines (372 loc) · 11.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import path from 'path'
import fs from 'fs-extra'
import chalk from 'chalk'
import ora from 'ora'
import inquirer from 'inquirer'
import Mustache from 'mustache'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
interface TemplateData {
pluginName: string
pluginSlug: string
pluginId: string
githubUsername: string
authorName: string
licenseName: string
currentYear: string
isSupabaseEnabled: boolean
isMixpanelEnabled: boolean
isSentryEnabled: boolean
isNotionEnabled: boolean
}
function slugify(text: string): string {
return text
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
}
function generatePluginId(): string {
// Generate a unique 19-digit ID
// Format: timestamp (13 digits) + random (6 digits) = 19 digits total
const timestamp = Date.now().toString() // 13 digits
const random = Math.floor(Math.random() * 1000000)
.toString()
.padStart(6, '0') // 6 digits
return timestamp + random
}
export async function createPlugin(platform: string) {
console.log(chalk.cyan('\n🚀 Unoff Plugin Creator\n'))
// Always ask for plugin name
const nameAnswer = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'What is your plugin name?',
default: 'My Awesome Plugin',
validate: (input: string) => {
if (!input.trim()) {
return 'Plugin name is required'
}
return true
},
},
])
const pluginName = nameAnswer.name as string
// Ask for output directory
const dirAnswer = await inquirer.prompt([
{
type: 'input',
name: 'directory',
message: 'Where do you want to create your plugin?',
default: '.',
},
])
// Ask for GitHub username
const githubAnswers = await inquirer.prompt([
{
type: 'input',
name: 'username',
message: 'What is your GitHub username?',
default: 'octocat',
},
])
// Ask for author name
const authorAnswers = await inquirer.prompt([
{
type: 'input',
name: 'author',
message: 'What is your name (author)?',
default: 'John Doe',
},
])
// Ask for license
const licenseAnswers = await inquirer.prompt([
{
type: 'list',
name: 'license',
message: 'Choose a license:',
default: 'MIT',
choices: [
'MIT',
'Apache-2.0',
'GPL-3.0',
'BSD-3-Clause',
'ISC',
'MPL-2.0',
'LGPL-3.0',
'AGPL-3.0',
'Unlicense',
'BSD-2-Clause',
],
},
])
// Ask for external services
const servicesAnswers = await inquirer.prompt([
{
type: 'checkbox',
name: 'services',
message: 'Which external services do you want to enable?',
choices: [
{
name: 'Supabase (Database & Authentication)',
value: 'supabase',
checked: true,
},
{
name: 'Mixpanel (Analytics)',
value: 'mixpanel',
checked: true,
},
{
name: 'Sentry (Error Monitoring)',
value: 'sentry',
checked: true,
},
{
name: 'Notion (Announcements & Onboarding)',
value: 'notion',
checked: true,
},
],
},
])
const pluginSlug = slugify(pluginName)
const platformName = platform.replace('-plugin', '')
const templateDir = path.join(
__dirname,
'..',
'..',
'templates',
platformName
)
// Get current working directory with error handling
let currentDir: string
try {
currentDir = process.cwd()
} catch (error) {
console.error(
chalk.red(
"\n❌ Cannot access current directory. It may have been deleted or you don't have permissions.\n"
)
)
console.log(
chalk.yellow('💡 Tip: Navigate to a valid directory and try again.\n')
)
process.exit(1)
}
// Resolve output directory path
const outputDir = path.resolve(currentDir, dirAnswer.directory, pluginSlug)
// Check if template exists
if (!fs.existsSync(templateDir)) {
throw new Error(`Template for ${platformName} not found`)
}
// Check if output directory already exists
if (fs.existsSync(outputDir)) {
const overwrite = await inquirer.prompt([
{
type: 'confirm',
name: 'overwrite',
message: `Directory "${pluginSlug}" already exists. Overwrite?`,
default: false,
},
])
if (!overwrite.overwrite) {
console.log(chalk.yellow('\n✋ Operation cancelled\n'))
process.exit(0)
}
await fs.remove(outputDir)
}
const spinner = ora('Creating plugin...').start()
try {
// Template data for Mustache
const selectedServices = servicesAnswers.services as string[]
const templateData: TemplateData = {
pluginName,
pluginSlug,
pluginId: generatePluginId(),
githubUsername: githubAnswers.username,
authorName: authorAnswers.author as string,
licenseName: licenseAnswers.license as string,
currentYear: new Date().getFullYear().toString(),
isSupabaseEnabled: selectedServices.includes('supabase'),
isMixpanelEnabled: selectedServices.includes('mixpanel'),
isSentryEnabled: selectedServices.includes('sentry'),
isNotionEnabled: selectedServices.includes('notion'),
}
// Copy template directory excluding node_modules and dist
await fs.copy(templateDir, outputDir, {
filter: (src: string) => {
const relativePath = path.relative(templateDir, src)
const parts = relativePath.split(path.sep)
// Exclude node_modules and dist directories
return !parts.includes('node_modules') && !parts.includes('dist')
},
})
// Process all files with Mustache templating
await processDirectory(outputDir, templateData)
// Remove empty or near-empty files created by conditional Mustache templates
await removeEmptyFiles(outputDir)
// Generate .env files based on selected services
await generateEnvFiles(outputDir, templateData)
// Post-process global.config.ts to apply Mustache templating and update service flags
const globalConfigPath = path.join(outputDir, 'src', 'global.config.ts')
if (await fs.pathExists(globalConfigPath)) {
let configContent = await fs.readFile(globalConfigPath, 'utf-8')
// Apply Mustache templating to replace {{ }} placeholders
configContent = Mustache.render(configContent, templateData)
// Update service flags
configContent = configContent
.replace(
/isSupabaseEnabled: true/,
`isSupabaseEnabled: ${templateData.isSupabaseEnabled}`
)
.replace(
/isMixpanelEnabled: true/,
`isMixpanelEnabled: ${templateData.isMixpanelEnabled}`
)
.replace(
/isSentryEnabled: true/,
`isSentryEnabled: ${templateData.isSentryEnabled}`
)
.replace(
/isNotionEnabled: true/,
`isNotionEnabled: ${templateData.isNotionEnabled}`
)
await fs.writeFile(globalConfigPath, configContent, 'utf-8')
}
spinner.succeed(chalk.green('Plugin created successfully!'))
console.log(chalk.cyan('\n📦 Next steps:\n'))
console.log(chalk.white(` cd ${pluginSlug}`))
console.log(chalk.white(` npm install`))
console.log(chalk.white(` unoff build`))
console.log(chalk.cyan('\n✨ Happy coding!\n'))
} catch (error) {
spinner.fail(chalk.red('Failed to create plugin'))
throw error
}
}
async function processDirectory(dir: string, templateData: TemplateData) {
const files = await fs.readdir(dir)
for (const file of files) {
const filePath = path.join(dir, file)
const stats = await fs.stat(filePath)
if (stats.isDirectory()) {
// Skip node_modules and dist directories
if (file === 'node_modules' || file === 'dist') {
continue
}
await processDirectory(filePath, templateData)
} else {
// Process files that should be templated
const shouldTemplate = shouldProcessFile(filePath)
if (shouldTemplate) {
try {
const content = await fs.readFile(filePath, 'utf-8')
const processed = Mustache.render(content, templateData)
await fs.writeFile(filePath, processed, 'utf-8')
} catch (error) {
// Skip binary files or files that can't be read as text
console.warn(chalk.yellow(`⚠️ Skipped processing: ${file}`))
}
}
}
}
}
function shouldProcessFile(filePath: string): boolean {
const ext = path.extname(filePath)
const fileName = path.basename(filePath)
// Exclude specific files that can't handle Mustache syntax
const excludedFiles = ['global.config.ts']
if (excludedFiles.includes(fileName)) {
return false
}
// List of extensions that should be templated
// Exclude .tsx and .jsx because they contain {{ }} for JSX objects
const templateExtensions = [
'.ts',
'.js',
'.json',
'.md',
'.html',
'.css',
'.yml',
'.yaml',
'.txt',
'.xml',
]
// List of files that should be templated
const templateFiles = [
'manifest.json',
'package.json',
'README.md',
'LICENSE',
]
return templateExtensions.includes(ext) || templateFiles.includes(fileName)
}
async function removeEmptyFiles(dir: string) {
const files = await fs.readdir(dir)
for (const file of files) {
const filePath = path.join(dir, file)
const stats = await fs.stat(filePath)
if (stats.isDirectory()) {
if (file === 'node_modules' || file === 'dist') {
continue
}
await removeEmptyFiles(filePath)
} else {
// Check if file is empty or contains only whitespace
try {
const content = await fs.readFile(filePath, 'utf-8')
if (content.trim().length === 0) {
await fs.remove(filePath)
}
} catch (error) {
// Skip binary files
}
}
}
}
async function generateEnvFiles(outputDir: string, templateData: TemplateData) {
const envLocalPath = path.join(outputDir, '.env.local')
const envSentryPath = path.join(outputDir, '.env.sentry-build-plugin')
// Generate .env.local content
const envLocalContent = `
# Supabase Configuration
# Get your keys from: https://supabase.com/dashboard/project/_/settings/api
VITE_SUPABASE_URL='YOUR_SUPABASE_URL'
VITE_SUPABASE_PUBLIC_ANON_KEY='YOUR_SUPABASE_ANON_KEY'
# Sentry Configuration
# Get your DSN from: https://sentry.io/settings/projects/
VITE_SENTRY_DSN='YOUR_SENTRY_DSN'
# Mixpanel Configuration
# Get your token from: https://mixpanel.com/settings/project/
VITE_MIXPANEL_TOKEN='YOUR_MIXPANEL_TOKEN'
# Authentication & Workers
VITE_AUTH_URL='https://auth.${templateData.pluginSlug}.com'
# Workers URLs
VITE_AUTH_WORKER_URL='https://oauth.yelbolt.workers.dev'
VITE_CORS_WORKER_URL='https://cors.yelbolt.workers.dev'
VITE_ANNOUNCEMENTS_WORKER_URL='https://68e83449-announcements.yelbolt.workers.dev/'
# Notion — Content Management (Announcements & Onboarding)
# Duplicate the databases to your Notion workspace and paste their IDs below
# Create an internal integration at https://www.notion.so/my-integrations and add it to both databases
VITE_NOTION_ANNOUNCEMENTS_ID='YOUR_ANNOUNCEMENTS_DB_ID'
VITE_NOTION_ONBOARDING_ID='YOUR_ONBOARDING_DB_ID'
VITE_NOTION_API_KEY='YOUR_NOTION_API_KEY'
# LemonSqueezy (if using payments)
# You can test with this test license key: 0DE7C002-1F28-49E8-A444-B424F346416E
VITE_LEMONSQUEEZY_URL='https://api.lemonsqueezy.com/v1'
# Tolgee Translation (optional)
# Generate your auth token from: https://app.tolgee.io/account/apiKeys
VITE_TOLGEE_URL='YOUR_TOLGEE_URL'
VITE_TOLGEE_API_KEY='YOUR_TOLGEE_API_KEY'
`
await fs.writeFile(envLocalPath, envLocalContent, 'utf-8')
// Generate .env.sentry-build-plugin
const envSentryContent = `# Sentry Build Plugin Configuration
# Generate your auth token from: https://sentry.io/settings/account/api/auth-tokens/
SENTRY_AUTH_TOKEN='YOUR_SENTRY_AUTH_TOKEN'
`
await fs.writeFile(envSentryPath, envSentryContent, 'utf-8')
}