minimaz-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +107 -0
- package/bin/cli.ts +80 -0
- package/dist/LICENSE +7 -0
- package/dist/README.md +103 -0
- package/dist/bin/cli.js +2 -0
- package/dist/package.json +42 -0
- package/package.json +57 -0
- package/src/commands/build.ts +176 -0
- package/src/commands/help.ts +33 -0
- package/src/commands/init.ts +56 -0
- package/src/commands/template.ts +146 -0
- package/src/templates/default/minimaz.config.json +26 -0
- package/src/templates/default/public/assets/.gitkeep +0 -0
- package/src/templates/default/public/favicon.ico +0 -0
- package/src/templates/default/src/index.html +80 -0
- package/src/templates/default/src/pages/about.html +46 -0
- package/src/templates/default/src/script.js +1 -0
- package/src/templates/default/src/style.css +99 -0
- package/src/templates/gitignore +2 -0
- package/src/templates/simple/minimaz.config.json +26 -0
- package/src/templates/simple/public/assets/.gitkeep +0 -0
- package/src/templates/simple/public/favicon.ico +0 -0
- package/src/templates/simple/src/index.html +15 -0
- package/src/templates/simple/src/pages/page.html +11 -0
- package/src/templates/simple/src/script.js +1 -0
- package/src/templates/simple/src/style.css +0 -0
- package/src/utils/functions.ts +161 -0
- package/src/utils/loadConfig.ts +61 -0
- package/src/utils/logService.ts +19 -0
- package/src/utils/postInstall.ts +16 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
import os from 'os'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import { log } from '../utils/logService.js'
|
|
5
|
+
import { askQuestion, listTemplates, getGlobalNodeModulesPath } from '../utils/functions.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Handles all template-related operations:
|
|
9
|
+
* - Save current folder as a global template
|
|
10
|
+
* - Update existing templates
|
|
11
|
+
* - Delete templates
|
|
12
|
+
* - Sync templates from node_modules
|
|
13
|
+
*
|
|
14
|
+
* @param targetPath - Optional path of the folder to save as template
|
|
15
|
+
* @param options - CLI flags (--list, --delete, --update, etc.)
|
|
16
|
+
*/
|
|
17
|
+
export async function template(targetPath?: string, options: any = {}): Promise<void> {
|
|
18
|
+
const templatesDir: string = path.join(os.homedir(), '.minimaz', 'templates')
|
|
19
|
+
const deleteName: string | undefined = options.delete || options.d
|
|
20
|
+
const updateName: string | undefined = options.update || options.u
|
|
21
|
+
|
|
22
|
+
if (deleteName) return await deleteTemplate(templatesDir, deleteName)
|
|
23
|
+
if (options.list || options.l) return await listTemplates(templatesDir)
|
|
24
|
+
|
|
25
|
+
// --- UPDATE MODE ---
|
|
26
|
+
if (updateName !== undefined) {
|
|
27
|
+
if (typeof updateName === 'string' && updateName.trim()) {
|
|
28
|
+
return await updateSingleTemplate(templatesDir, updateName.trim())
|
|
29
|
+
} else {
|
|
30
|
+
return await updateFromNodeModules(templatesDir)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Default action: save current folder as a template
|
|
35
|
+
await saveTemplate(templatesDir, targetPath)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Updates a single template with files from the current working directory.
|
|
40
|
+
*
|
|
41
|
+
* @param templatesDir - Global templates directory (~/.minimaz/templates)
|
|
42
|
+
* @param templateName - Name of the template to update
|
|
43
|
+
*/
|
|
44
|
+
async function updateSingleTemplate(templatesDir: string, templateName: string): Promise<void> {
|
|
45
|
+
const sourceDir: string = path.resolve(process.cwd())
|
|
46
|
+
const targetDir: string = path.join(templatesDir, templateName)
|
|
47
|
+
|
|
48
|
+
if (!await fs.pathExists(targetDir))
|
|
49
|
+
throw new Error(`Template '${templateName}' not found in ~/.minimaz/templates`)
|
|
50
|
+
|
|
51
|
+
const answer: string = await askQuestion(`❓ Update template '${templateName}' with current directory? (Y/N) `)
|
|
52
|
+
if (answer !== 'y' && answer !== '') {
|
|
53
|
+
log('info', 'Update cancelled.')
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
await fs.copy(sourceDir, targetDir, { overwrite: true })
|
|
59
|
+
log('success', `Template '${templateName}' updated from current directory.`)
|
|
60
|
+
} catch (error: any) {
|
|
61
|
+
throw new Error(`Failed to update '${templateName}': ${error.message}`)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Updates all templates from the global node_modules/minimaz/src/templates folder.
|
|
67
|
+
* This ensures the local templates are synced with the installed package.
|
|
68
|
+
*
|
|
69
|
+
* @param templatesDir - Global templates directory (~/.minimaz/templates)
|
|
70
|
+
*/
|
|
71
|
+
async function updateFromNodeModules(templatesDir: string): Promise<void> {
|
|
72
|
+
const nodeModulesPath: string = path.join(getGlobalNodeModulesPath(), 'src', 'templates')
|
|
73
|
+
|
|
74
|
+
if (!await fs.pathExists(nodeModulesPath)) throw new Error(`'node_modules/minimaz/src/templates' not found.`)
|
|
75
|
+
|
|
76
|
+
const items: string[] = await fs.readdir(nodeModulesPath)
|
|
77
|
+
|
|
78
|
+
const answer: string = await askQuestion(`⚠️ Update local templates overwriting them with defaults? (Y/N): `)
|
|
79
|
+
if (answer !== 'y' && answer !== '') {
|
|
80
|
+
log('info', 'Update cancelled.')
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
for (const item of items) {
|
|
86
|
+
const src: string = path.join(nodeModulesPath, item)
|
|
87
|
+
const dest: string = path.join(templatesDir, item)
|
|
88
|
+
await fs.copy(src, dest, { overwrite: true })
|
|
89
|
+
log('success', `Updated '${item}'`)
|
|
90
|
+
}
|
|
91
|
+
log('info', `✨ All templates and files updated successfully.`)
|
|
92
|
+
} catch (error: any) {
|
|
93
|
+
throw new Error(`Update failed: ${error.message}`)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Deletes a global template by name from ~/.minimaz/templates.
|
|
99
|
+
*
|
|
100
|
+
* @param dir - Global templates directory
|
|
101
|
+
* @param name - Template name to delete
|
|
102
|
+
*/
|
|
103
|
+
async function deleteTemplate(dir: string, name: string): Promise<void> {
|
|
104
|
+
if (!name) throw new Error('No template name specified to delete.')
|
|
105
|
+
const target: string = path.join(dir, name)
|
|
106
|
+
if (!await fs.pathExists(target)) throw new Error(`Template not found: ${name}`)
|
|
107
|
+
|
|
108
|
+
const confirm = await askQuestion(`❓ Confirm delete '${name}'? (Y/N) `)
|
|
109
|
+
if (confirm.toLowerCase() !== 'y') {
|
|
110
|
+
log('info', 'Delete cancelled.')
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
await fs.remove(target)
|
|
116
|
+
log('success', `Template '${name}' deleted.`)
|
|
117
|
+
} catch (error: any) {
|
|
118
|
+
throw new Error(`Delete error: ${error.message}`)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Saves a folder (current or specified) as a new global template.
|
|
124
|
+
*
|
|
125
|
+
* @param dir - Global templates directory (~/.minimaz/templates)
|
|
126
|
+
* @param targetPath - Optional path to save as a template
|
|
127
|
+
*/
|
|
128
|
+
async function saveTemplate(dir: string, targetPath?: string): Promise<void> {
|
|
129
|
+
let source: string = targetPath ? path.resolve(process.cwd(), targetPath) : process.cwd()
|
|
130
|
+
|
|
131
|
+
if (!await fs.pathExists(source)) {
|
|
132
|
+
log('warn', `Path not found: ${source}`)
|
|
133
|
+
const answer: string = (await askQuestion('❓ Use current directory instead? (Y/N):\t')).trim().toLowerCase()
|
|
134
|
+
if (answer !== 'y' && answer !== '') throw new Error('Operation cancelled.')
|
|
135
|
+
source = process.cwd()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
await fs.ensureDir(dir)
|
|
140
|
+
const dest: string = path.join(dir, path.basename(source))
|
|
141
|
+
await fs.copy(source, dest)
|
|
142
|
+
log('success', `Template saved to ${dest}`)
|
|
143
|
+
} catch (error: any) {
|
|
144
|
+
throw new Error(`Failed to save template: ${error.message}`)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"src": "src",
|
|
3
|
+
"dist": "dist",
|
|
4
|
+
"public": "public",
|
|
5
|
+
"minify": {
|
|
6
|
+
"html": true,
|
|
7
|
+
"css": true,
|
|
8
|
+
"js": true,
|
|
9
|
+
"ts": true
|
|
10
|
+
},
|
|
11
|
+
"replace": {
|
|
12
|
+
"../public/": "public/"
|
|
13
|
+
},
|
|
14
|
+
"styles": [
|
|
15
|
+
"style.css",
|
|
16
|
+
"style-2.css"
|
|
17
|
+
],
|
|
18
|
+
"scripts": [
|
|
19
|
+
"script.js",
|
|
20
|
+
"script-2.js"
|
|
21
|
+
],
|
|
22
|
+
"folders": {
|
|
23
|
+
"src": "",
|
|
24
|
+
"public": "public"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
File without changes
|
|
Binary file
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>Welcome to Minimaz</title>
|
|
7
|
+
<link rel="stylesheet" href="style.css" />
|
|
8
|
+
<link rel="icon" href="../public/favicon.ico" type="image/x-icon" />
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<header>
|
|
12
|
+
<nav>
|
|
13
|
+
<a class="btn active" href="#">Home</a>
|
|
14
|
+
<a class="btn" href="pages/about.html">About</a>
|
|
15
|
+
<a class="btn" href="https://github.com/zeller-dev/minimaz" target="_blank">GitHub</a>
|
|
16
|
+
</nav>
|
|
17
|
+
</header>
|
|
18
|
+
|
|
19
|
+
<main class="container">
|
|
20
|
+
<h1>🚀 Welcome to Minimaz</h1>
|
|
21
|
+
<p><strong>Minimaz</strong> is a minimal static site builder designed for speed and simplicity. It helps you:</p>
|
|
22
|
+
<ul>
|
|
23
|
+
<li>💡 Organize your files with a simple structure</li>
|
|
24
|
+
<li>⚙️ Automatically build and minify HTML, CSS, JS, and TS</li>
|
|
25
|
+
<li>🔄 Customize file inclusion order using <code>minimaz.config.json</code></li>
|
|
26
|
+
</ul>
|
|
27
|
+
|
|
28
|
+
<h2>🔧 Getting Started</h2>
|
|
29
|
+
<p>Install and use Minimaz with the following commands:</p>
|
|
30
|
+
<pre><code># 🔨 Initialize a new project
|
|
31
|
+
npx minimaz init my-site
|
|
32
|
+
|
|
33
|
+
# 🏗️ Build the site
|
|
34
|
+
npx minimaz build
|
|
35
|
+
|
|
36
|
+
# 📦 Save template or list/delete existing ones
|
|
37
|
+
npx minimaz template <path> [--list|-l] [--delete|-d]
|
|
38
|
+
|
|
39
|
+
# 💬 Show help guide
|
|
40
|
+
npx minimaz help</code></pre>
|
|
41
|
+
|
|
42
|
+
<h2>📁 Project Structure</h2>
|
|
43
|
+
<pre><code>my-site/
|
|
44
|
+
├── src/ # Source files (HTML, CSS, JS, TS)
|
|
45
|
+
├── public/ # Static assets (images, fonts, etc.)
|
|
46
|
+
├── dist/ # Output directory (auto-generated)
|
|
47
|
+
└── minimaz.config.json # Configuration file</code></pre>
|
|
48
|
+
|
|
49
|
+
<h2>⚙️ Configuration</h2>
|
|
50
|
+
<pre><code>{
|
|
51
|
+
"src": "src",
|
|
52
|
+
"dist": "dist",
|
|
53
|
+
"public": "public",
|
|
54
|
+
"minify": { "html": true, "css": true, "js": true, "ts": true },
|
|
55
|
+
"replace": { "../public/": "public/" },
|
|
56
|
+
"styles": [ "reset.css", "style.css", "theme.css" ],
|
|
57
|
+
"scripts": [ "libs/jquery.js", "utils.js", "script.js" ]
|
|
58
|
+
}</code></pre>
|
|
59
|
+
|
|
60
|
+
<h2>📤 Production</h2>
|
|
61
|
+
<p>The final output will be generated in the <code>dist/</code> folder, ready for deployment.</p>
|
|
62
|
+
|
|
63
|
+
<h2>❓ Available Commands</h2>
|
|
64
|
+
<ul>
|
|
65
|
+
<li><code>init</code> / <code>i</code> – Initialize a new project</li>
|
|
66
|
+
<li><code>build</code> / <code>b</code> – Build and minify the project</li>
|
|
67
|
+
<li><code>help</code> / <code>h</code> – Show the help message</li>
|
|
68
|
+
<li><code>template</code> / <code>t</code> – Manage templates (with <code>--list</code> and <code>--delete</code> options)</li>
|
|
69
|
+
</ul>
|
|
70
|
+
|
|
71
|
+
<p>You're ready to build fast and clean static websites. Happy coding! 🛠️</p>
|
|
72
|
+
</main>
|
|
73
|
+
|
|
74
|
+
<footer>
|
|
75
|
+
<p>© 2025 Minimaz. All rights reserved.</p>
|
|
76
|
+
</footer>
|
|
77
|
+
|
|
78
|
+
<script src="script.js"></script>
|
|
79
|
+
</body>
|
|
80
|
+
</html>
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>Welcome to Minimaz</title>
|
|
8
|
+
<link rel="stylesheet" href="../style.css" />
|
|
9
|
+
<link rel="shortcut icon" href="../../public/favicon.ico" type="image/x-icon">
|
|
10
|
+
|
|
11
|
+
</head>
|
|
12
|
+
|
|
13
|
+
<body>
|
|
14
|
+
<header>
|
|
15
|
+
<nav>
|
|
16
|
+
<a class='btn' href="../index.html">Home</a>
|
|
17
|
+
<a class='btn active' href="pages/about.html">About</a>
|
|
18
|
+
<a class='btn' target="_blank" href="https://github.com/zeller-dev/minimaz">GitHub</a>
|
|
19
|
+
</nav>
|
|
20
|
+
</header>
|
|
21
|
+
<div class="container">
|
|
22
|
+
<h1>About Minimaz</h1>
|
|
23
|
+
<p><strong>Minimaz</strong> is a minimal build tool that helps you quickly scaffold and minify HTML, CSS, and
|
|
24
|
+
JavaScript projects.</p>
|
|
25
|
+
|
|
26
|
+
<h2>Features</h2>
|
|
27
|
+
<ul>
|
|
28
|
+
<li>Zero-config HTML/CSS/JS minification</li>
|
|
29
|
+
<li>Folder structure based on <code>src</code>, <code>public</code>, and <code>dist</code></li>
|
|
30
|
+
<li>Simple CLI with <code>init</code>, <code>build</code>, and <code>help</code> commands</li>
|
|
31
|
+
</ul>
|
|
32
|
+
|
|
33
|
+
<h2>Getting Started</h2>
|
|
34
|
+
<pre><code>$ minimaz init my-project
|
|
35
|
+
$ cd my-project
|
|
36
|
+
$ minimaz build</code></pre>
|
|
37
|
+
|
|
38
|
+
<p>Learn more or contribute on <a href="#">GitHub</a>.</p>
|
|
39
|
+
</div>
|
|
40
|
+
<footer>
|
|
41
|
+
<p>© 2023 Minimaz. All rights reserved.</p>
|
|
42
|
+
</footer>
|
|
43
|
+
<script src="../script.js"></script>
|
|
44
|
+
</body>
|
|
45
|
+
|
|
46
|
+
</html>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
console.log('Minimaz is ready!')
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--primary-color: #338ca8;
|
|
3
|
+
--background-color: #222;
|
|
4
|
+
--foreground-color: #f5f5f5;
|
|
5
|
+
--code-bg: #333;
|
|
6
|
+
--code-text: #eeeeff;
|
|
7
|
+
font-size: 16px;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
* {
|
|
11
|
+
box-sizing: border-box;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
header,
|
|
15
|
+
footer {
|
|
16
|
+
background-color: var(--code-bg);
|
|
17
|
+
padding:1rem;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
body {
|
|
21
|
+
font-family: 'Segoe UI', sans-serif;
|
|
22
|
+
margin: 0;
|
|
23
|
+
background: var(--background-color);
|
|
24
|
+
color: var(--foreground-color);
|
|
25
|
+
line-height: 1.6;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.container {
|
|
29
|
+
max-width: 800px;
|
|
30
|
+
margin: auto;
|
|
31
|
+
padding: 1rem;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
h1,
|
|
35
|
+
h2,
|
|
36
|
+
h3 {
|
|
37
|
+
color: var(--primary-color);
|
|
38
|
+
margin-top: 2rem;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
p {
|
|
42
|
+
margin: 1rem 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
code {
|
|
46
|
+
background: var(--code-bg);
|
|
47
|
+
color: var(--code-text);
|
|
48
|
+
padding: 0.2rem 0.4rem;
|
|
49
|
+
border-radius: 4px;
|
|
50
|
+
font-family: monospace;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
pre {
|
|
54
|
+
background: var(--code-bg);
|
|
55
|
+
color: #ddd;
|
|
56
|
+
padding: 1rem;
|
|
57
|
+
overflow-x: auto;
|
|
58
|
+
border-radius: 6px;
|
|
59
|
+
font-family: monospace;
|
|
60
|
+
border-left: 4px solid var(--primary-color);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
a {
|
|
64
|
+
color: var(--primary-color);
|
|
65
|
+
text-decoration: none;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.btn {
|
|
69
|
+
transition: all 0.15s ease-in-out;
|
|
70
|
+
background-color: var(--primary-color);
|
|
71
|
+
border: none;
|
|
72
|
+
color: #fff;
|
|
73
|
+
padding: 10px 20px;
|
|
74
|
+
font-size: 1rem;
|
|
75
|
+
margin: .5rem .2rem;
|
|
76
|
+
cursor: pointer;
|
|
77
|
+
border-radius: 5px;
|
|
78
|
+
display: inline-block;
|
|
79
|
+
text-align: center;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.btn:hover {
|
|
83
|
+
opacity: 0.85;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.btn.active {
|
|
87
|
+
background-color: var(--code-bg);
|
|
88
|
+
color: #fff;
|
|
89
|
+
pointer-events: none;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
header {
|
|
93
|
+
font-weight: 600;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
footer p {
|
|
97
|
+
text-align: center;
|
|
98
|
+
margin: 0;
|
|
99
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"src": "src",
|
|
3
|
+
"dist": "dist",
|
|
4
|
+
"public": "public",
|
|
5
|
+
"minify": {
|
|
6
|
+
"html": true,
|
|
7
|
+
"css": true,
|
|
8
|
+
"js": true,
|
|
9
|
+
"ts": true
|
|
10
|
+
},
|
|
11
|
+
"replace": {
|
|
12
|
+
"../public/": "public/"
|
|
13
|
+
},
|
|
14
|
+
"styles": [
|
|
15
|
+
"style.css",
|
|
16
|
+
"style-2.css"
|
|
17
|
+
],
|
|
18
|
+
"scripts": [
|
|
19
|
+
"script.js",
|
|
20
|
+
"script-2.js"
|
|
21
|
+
],
|
|
22
|
+
"folders": {
|
|
23
|
+
"src": "",
|
|
24
|
+
"public": "public"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
File without changes
|
|
Binary file
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>Welcome to Minimaz</title>
|
|
8
|
+
<link rel="stylesheet" href="style.css" />
|
|
9
|
+
</head>
|
|
10
|
+
|
|
11
|
+
<body>
|
|
12
|
+
<script src="script.js"></script>
|
|
13
|
+
</body>
|
|
14
|
+
|
|
15
|
+
</html>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
console.log('Minimaz is ready!')
|
|
File without changes
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import readline from 'readline'
|
|
2
|
+
import fs from 'fs-extra'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import os from 'os'
|
|
5
|
+
import { log } from './logService.js'
|
|
6
|
+
import { execSync } from 'child_process'
|
|
7
|
+
|
|
8
|
+
// ----- Types -----
|
|
9
|
+
interface Args {
|
|
10
|
+
_: string[]
|
|
11
|
+
[key: string]: string | boolean | string[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// ----- Parse CLI Arguments -----
|
|
15
|
+
// Converts raw process arguments into structured key-value pairs
|
|
16
|
+
export function parseArgs(rawArgs: string[]): Args {
|
|
17
|
+
const args: Args = { _: [] }
|
|
18
|
+
for (let i = 0; i < rawArgs.length; i++) {
|
|
19
|
+
const arg: string = rawArgs[i]
|
|
20
|
+
if (arg.startsWith('-')) {
|
|
21
|
+
const key: string = arg.startsWith('--') ? arg.slice(2) : arg.slice(1)
|
|
22
|
+
const next: string = rawArgs[i + 1]
|
|
23
|
+
const hasValue: boolean = !!next && !next.startsWith('-')
|
|
24
|
+
args[key] = hasValue ? next : true
|
|
25
|
+
if (hasValue) i++
|
|
26
|
+
} else {
|
|
27
|
+
args._.push(arg)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return args
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ----- Ask Question from CLI -----
|
|
34
|
+
// Prompts the user with a question and returns the input
|
|
35
|
+
export function askQuestion(query: string): Promise<string> {
|
|
36
|
+
return new Promise(resolve => {
|
|
37
|
+
const rl: readline.Interface = readline.createInterface({
|
|
38
|
+
input: process.stdin,
|
|
39
|
+
output: process.stdout
|
|
40
|
+
})
|
|
41
|
+
rl.question(query, answer => {
|
|
42
|
+
rl.close()
|
|
43
|
+
resolve(answer.trim())
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ----- List Templates -----
|
|
49
|
+
// Displays all available global templates in the given directory
|
|
50
|
+
export async function listTemplates(dir: string): Promise<void> {
|
|
51
|
+
if (!await fs.pathExists(dir)) {
|
|
52
|
+
log('info', 'No templates directory found.')
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const templates: string[] = await fs.readdir(dir)
|
|
57
|
+
if (templates.length === 0) log('info', 'No global templates available.')
|
|
58
|
+
else {
|
|
59
|
+
log('info', 'Available global templates:')
|
|
60
|
+
templates.forEach(t => log('info', `- ${t}`))
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ----- Apply Replacements -----
|
|
65
|
+
// Replaces all occurrences of keys in content with their corresponding values
|
|
66
|
+
export function applyReplacements(content: string, replacements: Record<string, string> = {}): string {
|
|
67
|
+
for (const [from, to] of Object.entries(replacements)) {
|
|
68
|
+
content = content.split(from).join(to)
|
|
69
|
+
}
|
|
70
|
+
return content
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ----- Read File with Replacements -----
|
|
74
|
+
// Reads a file and applies string replacements if provided
|
|
75
|
+
export async function getFile(srcPath: string, replace?: Record<string, string>): Promise<string> {
|
|
76
|
+
try {
|
|
77
|
+
let file: string = await fs.readFile(srcPath, 'utf-8')
|
|
78
|
+
if (replace) file = applyReplacements(file, replace)
|
|
79
|
+
return file
|
|
80
|
+
} catch (error: any) {
|
|
81
|
+
log('error', `Failed to read file ${srcPath}: ${error.message}`)
|
|
82
|
+
return ''
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ----- Global Node Modules Path -----
|
|
87
|
+
// Returns the path to global node_modules depending on the platform
|
|
88
|
+
|
|
89
|
+
export function getGlobalNodeModulesPath(): string {
|
|
90
|
+
try {
|
|
91
|
+
const prefix = execSync('npm config get prefix', { encoding: 'utf-8' }).trim();
|
|
92
|
+
if (!prefix) throw new Error('Empty prefix');
|
|
93
|
+
return process.platform === 'win32'
|
|
94
|
+
? path.join(prefix, 'node_modules', 'minimaz-cli')
|
|
95
|
+
: path.join(prefix, 'lib', 'node_modules', 'minimaz-cli');
|
|
96
|
+
} catch {
|
|
97
|
+
// fallback
|
|
98
|
+
return process.platform === 'win32'
|
|
99
|
+
? path.join(process.env.APPDATA || '', 'npm', 'node_modules', 'minimaz-cli')
|
|
100
|
+
: '/usr/local/lib/node_modules/minimaz-cli';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
// ----- Create Global Templates Folder -----
|
|
106
|
+
// Creates ~/.minimaz/templates and copies default templates if folder is empty
|
|
107
|
+
export async function createGlobalDir(): Promise<void> {
|
|
108
|
+
const minimazDir = path.join(os.homedir(), '.minimaz')
|
|
109
|
+
const globalTemplatesDir = path.join(minimazDir, 'templates')
|
|
110
|
+
const defaultTemplatesDir = path.join(getGlobalNodeModulesPath(), 'src', 'templates')
|
|
111
|
+
const settingsPath = path.join(minimazDir, 'settings.json')
|
|
112
|
+
|
|
113
|
+
console.log(defaultTemplatesDir)
|
|
114
|
+
|
|
115
|
+
// ----- Ensure node_modules path exists (fallback for portable setups) -----
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
// ----- Ensure minimaz dir exists -----
|
|
119
|
+
await fs.ensureDir(minimazDir)
|
|
120
|
+
|
|
121
|
+
// ----- Create settings.json if missing -----
|
|
122
|
+
if (!await fs.pathExists(settingsPath)) {
|
|
123
|
+
const defaultSettings = {
|
|
124
|
+
createdAt: new Date().toISOString(),
|
|
125
|
+
templatesPath: globalTemplatesDir,
|
|
126
|
+
npmGlobalPath: getGlobalNodeModulesPath()
|
|
127
|
+
}
|
|
128
|
+
await fs.outputJson(settingsPath, defaultSettings, { spaces: 2 })
|
|
129
|
+
log('success', `Created settings.json at ${settingsPath}`)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ----- Check if templates folder exists -----
|
|
133
|
+
const exists = await fs.pathExists(globalTemplatesDir)
|
|
134
|
+
const isEmpty = exists ? (await fs.readdir(globalTemplatesDir)).length === 0 : true
|
|
135
|
+
|
|
136
|
+
if (!exists) {
|
|
137
|
+
await fs.ensureDir(globalTemplatesDir)
|
|
138
|
+
log('success', 'Created global templates directory.')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ----- Skip copy if not empty -----
|
|
142
|
+
if (!isEmpty) {
|
|
143
|
+
log('info', 'Global templates directory not empty. Skipping copy.')
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const templates: string[] = await fs.readdir(defaultTemplatesDir)
|
|
148
|
+
|
|
149
|
+
console.log(templates)
|
|
150
|
+
// ----- Copy default templates -----
|
|
151
|
+
for (const name of await fs.readdir(defaultTemplatesDir)) {
|
|
152
|
+
await fs.copy(path.join(defaultTemplatesDir, name), path.join(globalTemplatesDir, name))
|
|
153
|
+
log('success', `Copied template '${name}'.`)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
log('success', 'Default templates setup completed.')
|
|
157
|
+
} catch (error: any) {
|
|
158
|
+
log('error', `Failed to create global templates directory: ${error.message}`)
|
|
159
|
+
throw error
|
|
160
|
+
}
|
|
161
|
+
}
|