-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.js
More file actions
110 lines (96 loc) · 3.51 KB
/
templates.js
File metadata and controls
110 lines (96 loc) · 3.51 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
'use strict';
const fs = require('fs');
const path = require('path');
const colors = require('colors');
const findConfig = require('find-config');
const inquirer = require('inquirer');
module.exports = class ThemeBuilder {
constructor(firstName, lastName) {
this.questions = [{
name: 'theme-name',
type: 'input',
message: 'Theme Name:',
validate: (input) => {
if (/[A-Za-z]/.test(input)) return true;
else return 'Theme name may only include letters.';
}
}];
}
generate(name) {
if(!name){
this.ask();
}
else {
this.setUpGen(name);
}
}
validateName(name){
if (/[A-Za-z]/.test(name)) {
this.setUpGen(name)
}
else {
console.log('Error: Name Must Only Contain Letters underscores or -'.bold.red);
}
}
ask() {
inquirer.prompt(this.questions)
.then(answers => {
var themeName = answers['theme-name'];
this.setUpGen(themeName);
});
}
setUpGen(themeName){
const CURR_DIR = process.cwd();
const templatePath = `${__dirname}/templates/theme-template`;
themeName = themeName.toLowerCase();
const projectRoot = findConfig('package.json', { dir: CURR_DIR }).replace('package.json','');
console.log(`*==== Generating ${themeName} Files ====*`);
try {
fs.mkdirSync(`${projectRoot}/themes/${themeName}`);
this.createDirectoryContents(templatePath, themeName, projectRoot);
}
catch (e) {
if (e.code == 'EEXIST') {
console.log(`Theme Named ${themeName} Already Exists`.red.bold);
}
}
}
createDirectoryContents(templatePath, themeName, projectRoot) {
//read template source files
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach(file => {
const origFilePath = `${templatePath}/${file}`;
const filename = file.replace("TEMP", themeName);
const stats = fs.statSync(origFilePath);
const writePath = `${projectRoot}themes/${themeName}/${filename}`;
// test to see if file or directory
if (stats.isFile()) {
// Add component name to file names
// Read tmeplate file
const contents = fs.readFileSync(origFilePath, 'utf8');
// Create new file with the componet name filled in
fs.writeFile(writePath, this.replaceContent(contents, themeName), function (err) {
if (err) {
return console.error(`Error Writing: ${filename}`);
}
else {
console.log(`${'Added:'.bold.green} ${filename}`);
}
});
}
});
try {
fs.mkdirSync(`${projectRoot}themes/${themeName}/images`);
console.log(`${'Added:'.bold.green} /images`);
fs.mkdirSync(`${projectRoot}themes/${themeName}/icons`);
console.log(`${'Added:'.bold.green} /icons`);
}
catch (e) {
console.log(`Subdirectory Create Fail`.red.bold, e);
}
}
replaceContent(content, componentName){
let temp = content.replace(':REPLACE:', componentName);
return temp;
}
}