-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.js
81 lines (68 loc) · 2.39 KB
/
create.js
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
const fs = require("fs");
const path = require("path");
const templatePath = `${__dirname}/template`;
const dirDist = `${__dirname}/modules`;
const { pascalCase, camelCase, hyphenate } = require("./string-utilities");
const transformToPascalCase = (x, newValue) =>
x.replace(/{pascalCase}/g, pascalCase(newValue));
const transformToCamelCase = (x, newValue) =>
x.replace(/{camelCase}/g, camelCase(newValue));
const transformToHyphenate = (x, newValue) =>
x.replace(/{hyphenate}/g, hyphenate(newValue));
const isDirectory = file => fs.lstatSync(file).isDirectory();
const isFile = file => fs.lstatSync(file).isFile();
const readingFile = (name, newLocation, fileName) => (err, fileContent) => {
if (err) throw err;
if (name !== undefined) {
let newFileName = transformToHyphenate(fileName, name);
let newFileContent = "";
newFileContent = transformToPascalCase(fileContent, name);
newFileContent = transformToCamelCase(newFileContent, name);
newFileContent = transformToHyphenate(newFileContent, name);
fs.writeFile(`${newLocation}/${newFileName}`, newFileContent, function(
err
) {
if (err) throw err;
console.log(`The file ${newFileName} was saved!`);
});
} else {
return;
}
};
const readingDir = (currentPath, newLocation, name) => (err, files) => {
if (err) throw err;
files.map(file => {
const newCurrentPath = `${currentPath}/${file}`;
if (isDirectory(newCurrentPath)) {
const newFolderLocation = `${newLocation}/${file}`;
if (!fs.existsSync(newFolderLocation)) {
fs.mkdirSync(newFolderLocation);
}
fs.readdir(
`${newCurrentPath}`,
readingDir(newCurrentPath, newFolderLocation, name)
);
} else if (isFile(newCurrentPath)) {
fs.readFile(
`${newCurrentPath}`,
"utf8",
readingFile(name, newLocation, file)
);
}
});
};
module.exports = (type, name) => {
const newPath = `${dirDist}/${type}`;
if (!fs.existsSync(newPath)) {
fs.mkdirSync(newPath);
}
const currentPath = `${templatePath}/${type}`;
const newDirName = hyphenate(name);
const newLocation = `${newPath}/${newDirName}`;
if (!fs.existsSync(newLocation)) {
fs.mkdirSync(newLocation);
}
fs.readdir(`${currentPath}`, readingDir(currentPath, newLocation, name));
console.log(`You can find all files here: ${newLocation}`);
console.log("_________________________");
};