-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-post.js
More file actions
113 lines (99 loc) · 2.93 KB
/
Copy pathnew-post.js
File metadata and controls
113 lines (99 loc) · 2.93 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
import fs from 'fs/promises';
import path from 'path';
import prompts from 'prompts';
import chalk from 'chalk';
import slugify from 'slugify';
const POSTS_DIR = path.join(process.cwd(), 'posts');
async function main() {
console.log(chalk.bold.blue('📝 Create a New Blog Post'));
const response = await prompts([
{
type: 'text',
name: 'title',
message: 'What is the title of your post?',
validate: (value) => (value.length > 0 ? true : 'Title is required'),
},
{
type: 'text',
name: 'slug',
message: 'What is the slug for your post?',
initial: (prev) => slugify(prev, { lower: true, strict: true }),
validate: (value) => {
if (!value) return 'Slug is required';
if (!/^[a-z0-9-]+$/.test(value))
return 'Slug must be kebab-case (lowercase, numbers, hyphens)';
return true;
},
},
{
type: 'text',
name: 'description',
message: 'Brief description of the post:',
},
{
type: 'select',
name: 'category',
message: 'Select a category:',
choices: [
{ title: 'Dev', value: 'Dev' },
{ title: 'Life', value: 'Life' },
{ title: 'Travel', value: 'Travel' },
{ title: 'Review', value: 'Review' },
{ title: 'Insight', value: 'Insight' },
{ title: 'Essay', value: 'Essay' },
],
initial: 0,
},
{
type: 'list',
name: 'tags',
message: 'Enter tags (comma separated):',
separator: ',',
},
]);
if (!response.title || !response.slug) {
console.log(chalk.yellow('Operation cancelled.'));
return;
}
const { title, slug, description, category, tags } = response;
const date = new Date().toISOString().split('T')[0];
const targetDir = path.join(POSTS_DIR, slug);
try {
// Check if directory exists
try {
await fs.access(targetDir);
console.error(
chalk.red(`Error: Directory posts/${slug} already exists!`)
);
process.exit(1);
} catch {
// Directory doesn't exist, proceed
}
await fs.mkdir(targetDir, { recursive: true });
// Create meta.json
const metaData = {
title,
description,
date,
category,
tags: tags.map((t) => t.trim()).filter(Boolean),
};
await fs.writeFile(
path.join(targetDir, 'meta.json'),
JSON.stringify(metaData, null, 2)
);
// Create index.mdx
const mdxContent = `
## Introduction
Write your post content here...
`;
await fs.writeFile(path.join(targetDir, 'index.mdx'), mdxContent.trim());
console.log(chalk.green(`\n✅ Successfully created new post!`));
console.log(`📂 Directory: ${chalk.cyan(`posts/${slug}`)}`);
console.log(`📄 Meta: ${chalk.cyan(`posts/${slug}/meta.json`)}`);
console.log(`📝 Content: ${chalk.cyan(`posts/${slug}/index.mdx`)}\n`);
} catch (error) {
console.error(chalk.red('Failed to create post:'), error);
}
}
main();