Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·143 lines (143 loc) · 5.83 KB
/
Copy pathindex.js
File metadata and controls
executable file
·143 lines (143 loc) · 5.83 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
#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const commander_1 = require("commander");
const prompts_1 = __importDefault(require("prompts"));
process.on("SIGINT", () => process.exit(0));
process.on("SIGTERM", () => process.exit(0));
const pathArray = new Array();
const add = new commander_1.Command();
add.name("add");
add.description('Generate a new template or component');
add.command('add');
add.action(async () => {
const message = `Where do you want to generate this component?
Your current location is: ${process.cwd()}`;
getDirChoices();
});
async function getDirChoices() {
const choices = fs.readdirSync(path.resolve(process.cwd(), ...pathArray)).map((item) => {
const currPath = path.resolve(path.resolve(process.cwd(), ...pathArray), item);
const fileStats = fs.lstatSync(currPath);
if (fileStats.isDirectory() && item.substring(0, 1) !== '.' && item !== 'node_modules') {
return {
title: item,
value: item
};
}
}).filter((item) => item !== undefined);
choices.unshift({
title: 'Current Path',
value: 'Current Path'
});
if (pathArray.length > 0) {
choices.push({
value: '..',
title: '..'
});
}
const { choice } = await prompts_1.default.prompt([
{
type: 'select',
name: 'choice',
message: 'Please select a directory or path to add the Kalendi Widget',
choices: choices,
},
]);
if (choice === "Current Path") {
const pwd = process.cwd();
const templatePath = path.resolve(pwd, ...pathArray); // The base path (i.e. where the directory will be created)
// If the Kalendi-Widget already exists in current path
const pathIfDirectoryExists = path.join(templatePath, 'Kalendi-Widget');
if (fs.existsSync(pathIfDirectoryExists)) {
const { value } = await prompts_1.default.prompt({
type: 'toggle',
name: 'value',
message: 'Kalendi-Widget seems to already exist in the path. You are about to overwrite the existing component, are you sure about this choice?',
initial: true,
active: 'yes',
inactive: 'no'
});
if (value) { // User clicked on yes
fs.rmSync(pathIfDirectoryExists, { recursive: true, force: true });
console.log(`Generating Kalendi Widget at ${templatePath}`);
const srcPath = path.resolve(__dirname, 'src'); // Get the path to the src directory with all the files
fs.mkdirSync(path.resolve(templatePath, 'Kalendi-Widget'), { recursive: true }); // Create the directory
copyDirectory(path.join(templatePath, 'Kalendi-Widget'), srcPath);
}
else {
getDirChoices();
}
}
else {
console.log(`Generating Kalendi Widget at ${templatePath}`);
const srcPath = path.resolve(__dirname, 'src'); // Get the path to the src directory with all the files
fs.mkdirSync(path.resolve(templatePath, 'Kalendi-Widget'), { recursive: true }); // Create the directory
copyDirectory(path.join(templatePath, 'Kalendi-Widget'), srcPath);
}
}
else if (choice === "..") {
pathArray.pop();
getDirChoices();
}
else {
pathArray.push(choice);
getDirChoices();
}
}
function copyDirectory(templatePath, sourcePath) {
const entries = fs.readdirSync(sourcePath, { withFileTypes: true });
// Loop through all files in the directory
for (const entry of entries) {
const itemPath = path.join(sourcePath, entry.name); // The path of the current Item (a File or a Directory)
const destPath = path.join(templatePath, entry.name); // Where it will go
//console.log(itemPath, destPath)
if (entry.isDirectory()) {
//console.log(itemPath, destPath)
fs.mkdirSync(path.resolve(templatePath, entry.name), { recursive: true }); // Create the directory
copyDirectory(destPath, itemPath);
}
else {
fs.copyFileSync(itemPath, destPath);
}
}
}
async function main() {
//const packageInfo = await getPackageInfo()
const program = new commander_1.Command();
program.name("Kalendi-Widget @ DataBlock");
program.description("Add Kalendi Widget to your frontend");
program.version("1.0.0");
program.addCommand(add);
program.parse();
}
main();
//# sourceMappingURL=index.js.map