@dhayalesh/create-ktern-docker 1.0.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/bin/cli.js +102 -0
- package/package.json +15 -0
- package/templates/Dockerfile.template +8 -0
package/bin/cli.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const inquirer = require('inquirer');
|
|
6
|
+
const ejs = require('ejs');
|
|
7
|
+
const { promisify } = require('util');
|
|
8
|
+
|
|
9
|
+
const copyFile = promisify(fs.copyFile);
|
|
10
|
+
const mkdir = promisify(fs.mkdir);
|
|
11
|
+
const readdir = promisify(fs.readdir);
|
|
12
|
+
const stat = promisify(fs.stat);
|
|
13
|
+
const writeFile = promisify(fs.writeFile);
|
|
14
|
+
const readFile = promisify(fs.readFile);
|
|
15
|
+
|
|
16
|
+
async function copyTemplate(src, dest, data) {
|
|
17
|
+
const stats = await stat(src);
|
|
18
|
+
if (stats.isDirectory()) {
|
|
19
|
+
if (!fs.existsSync(dest)) {
|
|
20
|
+
await mkdir(dest, { recursive: true });
|
|
21
|
+
}
|
|
22
|
+
const files = await readdir(src);
|
|
23
|
+
for (const file of files) {
|
|
24
|
+
await copyTemplate(path.join(src, file), path.join(dest, file), data);
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
let content = await readFile(src, 'utf8');
|
|
28
|
+
// Simple template replacement if no EJS variables are found or if we want to use EJS
|
|
29
|
+
try {
|
|
30
|
+
content = ejs.render(content, data);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
// If EJS fails (e.g. invalid syntax in non-template files), just use raw content
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Remove .template or .tmpl suffix if present in destination filename
|
|
36
|
+
const finalDest = dest.replace(/\.template$/, '').replace(/\.tmpl$/, '');
|
|
37
|
+
await writeFile(finalDest, content);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function run() {
|
|
42
|
+
console.log('š Welcome to the KTERN Generator!');
|
|
43
|
+
|
|
44
|
+
const questions = [
|
|
45
|
+
{
|
|
46
|
+
type: 'input',
|
|
47
|
+
name: 'name',
|
|
48
|
+
message: 'What is the project name?',
|
|
49
|
+
validate: (input) => input.length > 0 || 'Project name is required',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
type: 'input',
|
|
53
|
+
name: 'directory',
|
|
54
|
+
message: 'In which directory should the project be created?',
|
|
55
|
+
default: (answers) => path.join(process.cwd(), answers.name),
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
type: 'number',
|
|
59
|
+
name: 'port',
|
|
60
|
+
message: 'What port should the service run on?',
|
|
61
|
+
default: 3000,
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
const answers = await inquirer.prompt(questions);
|
|
66
|
+
const targetDir = path.resolve(answers.directory);
|
|
67
|
+
|
|
68
|
+
if (fs.existsSync(targetDir)) {
|
|
69
|
+
const { overwrite } = await inquirer.prompt([
|
|
70
|
+
{
|
|
71
|
+
type: 'confirm',
|
|
72
|
+
name: 'overwrite',
|
|
73
|
+
message: `Directory ${targetDir} already exists. Overwrite?`,
|
|
74
|
+
default: false,
|
|
75
|
+
},
|
|
76
|
+
]);
|
|
77
|
+
if (!overwrite) {
|
|
78
|
+
console.log('Exiting...');
|
|
79
|
+
process.exit(0);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
console.log(`\nCreating project "${answers.name}" in ${targetDir}...`);
|
|
84
|
+
|
|
85
|
+
const templateDir = path.join(__dirname, '../templates');
|
|
86
|
+
await copyTemplate(templateDir, targetDir, {
|
|
87
|
+
name: answers.name,
|
|
88
|
+
port: answers.port,
|
|
89
|
+
// Add more default variables if needed
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
console.log('\nā
Project created successfully!');
|
|
93
|
+
console.log(`\nNext steps:`);
|
|
94
|
+
console.log(` cd ${targetDir}`);
|
|
95
|
+
console.log(` npm install`);
|
|
96
|
+
console.log(` npm start`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
run().catch((err) => {
|
|
100
|
+
console.error('ā Error:', err.message);
|
|
101
|
+
process.exit(1);
|
|
102
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dhayalesh/create-ktern-docker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Standalone KTERN docker generator",
|
|
5
|
+
"bin": {
|
|
6
|
+
"create-ktern-docker": "./bin/cli.js"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"inquirer": "^8.2.4",
|
|
10
|
+
"ejs": "^3.1.8"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "restricted"
|
|
14
|
+
}
|
|
15
|
+
}
|