forked from gfpaiva/jussitb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fs.js
177 lines (129 loc) · 5.41 KB
/
Fs.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
const { readFile, writeFile, existsSync, mkdirSync, readFileSync, renameSync } = require('fs');
const ncp = require('ncp').ncp;
ncp.limit = 16;
const PROJECTDIR = process.cwd();
const DIRNAME = __dirname;
class Fs {
constructor() {
this.templatePaths = {
controller: `${DIRNAME}/templates/controller/CONTORLLERNAME.js`,
module: `${DIRNAME}/templates/module/MODULENAME.js`,
page: {
root: `${DIRNAME}/templates/page`,
html: `${DIRNAME}/templates/page/0-PAGENAME.html`,
script: `${DIRNAME}/templates/page/scripts/PAGENAME.js`,
scss: `${DIRNAME}/templates/page/styles/PAGENAME.scss`,
},
project: `${DIRNAME}/templates/project`
};
this.srcPaths = {
controller: `${PROJECTDIR}\\src\\Scripts\\controllers`,
module: `${PROJECTDIR}\\src\\Scripts\\modules`,
page: `${PROJECTDIR}\\src\\Pages`,
project: {
root: `${PROJECTDIR}`,
style: project => `${PROJECTDIR}\\${project}\\src\\Styles`,
script: project => `${PROJECTDIR}\\${project}\\src\\Scripts`,
HTML: project => `${PROJECTDIR}\\${project}\\src\\01 - HTML Templates`,
SUB: project => `${PROJECTDIR}\\${project}\\src\\01 - HTML Templates\\Sub Templates`,
SHELF: project => `${PROJECTDIR}\\${project}\\src\\02 - Shelves Templates`,
pkg: project => `${PROJECTDIR}\\${project}\\package.json`,
gulp: project => `${PROJECTDIR}\\${project}\\gulpfile.js`,
config: project => `${PROJECTDIR}\\${project}\\config.js`,
},
};
};
createJsFile ( { name, overview }, type ) {
return new Promise((resolve) => {
readFile(this.templatePaths[type], 'utf8', (err, data) => {
if(err) throw new Error(err);
const createdFile = `${this.srcPaths[type]}\\${name}.js`;
// if(existsSync(createdFile)) return reject(`File: ${createdFile} alredy exists`);
const result = data
.replace(/CONTORLLERNAME|MODULENAME/gm, name)
.replace(/FILEOVERVIEW/gm, overview);
writeFile(createdFile, result, 'utf8', function (err) {
if(err) throw new Error(err);
return resolve(createdFile);
});
});
});
}
createPage( { name, account, overview } ) {
return new Promise((resolve) => {
const pagePath = `${this.srcPaths.page}\\${name}`;
// if(existsSync(pagePath)) return reject(`Page: ${pagePath} alredy exists`);
mkdirSync(pagePath);
mkdirSync(`${pagePath}\\images`);
mkdirSync(`${pagePath}\\scripts`);
mkdirSync(`${pagePath}\\styles`);
const htmlFile = readFileSync(this.templatePaths.page.html, 'utf8')
.replace(/PAGENAME/gm, name)
.replace(/ACCOUNT/gm, account);
const JsFile = readFileSync(this.templatePaths.page.script, 'utf8')
.replace(/PAGENAME/gm, name)
.replace(/FILEOVERVIEW/gm, overview);
const ScssFile = readFileSync(this.templatePaths.page.scss, 'utf8').replace(/PAGENAME/gm, name);
this._writeFilePromise(`${pagePath}\\0-${name}.html`, htmlFile)
.then(() => this._writeFilePromise(`${pagePath}\\scripts\\${name}.js`, JsFile))
.then(() => this._writeFilePromise(`${pagePath}\\styles\\${name}.scss`, ScssFile))
.then(() => resolve(pagePath));
});
}
createProject( { name, account } ) {
return new Promise((resolve) => {
const projectPath = `${this.srcPaths.project.root}\\${name}`;
mkdirSync(projectPath);
let pkgFile,
gulpFile,
cfgFile;
this._copyPastePromise(this.templatePaths.project, projectPath)
.then(() => {
renameSync(`${this.srcPaths.project.style(name)}\\PROJECTACCOUNTNAME-style.scss`, `${this.srcPaths.project.style(name)}\\${account}-style.scss`);
renameSync(`${this.srcPaths.project.script(name)}\\PROJECTACCOUNTNAME-app.js`, `${this.srcPaths.project.script(name)}\\${account}-app.js`);
return;
})
.then(() => {
pkgFile = readFileSync(this.srcPaths.project.pkg(name), 'utf8').replace(/PROJECTACCOUNTNAME/gm, account);
gulpFile = readFileSync(this.srcPaths.project.gulp(name), 'utf8').replace(/PROJECTACCOUNTNAME/gm, account);
cfgFile = readFileSync(this.srcPaths.project.config(name), 'utf8').replace(/PROJECTACCOUNTNAME/gm, account);
return;
})
.then(() => this._writeFilePromise(this.srcPaths.project.pkg(name), pkgFile))
.then(() => this._writeFilePromise(this.srcPaths.project.gulp(name), gulpFile))
.then(() => this._writeFilePromise(this.srcPaths.project.config(name), cfgFile))
.then(() => resolve(projectPath))
});
}
createProjectHTML(templateList, templateType, projectFolderName) {
return templateList.map(template => this._writeFilePromise(`${this.srcPaths.project[templateType](projectFolderName)}\\${template}.html`, ''));
}
fillProjectHTML(contents) {
return contents.map(content => this._writeFilePromise(content.file, content.html));
}
_copyPastePromise(src, dest) {
return new Promise((resolve) => {
ncp(src, dest, err => {
if(err) throw new Error(err);
resolve(true);
});
});
};
_writeFilePromise(file, content) {
return new Promise((resolve, reject) => {
writeFile(file, content, 'utf8', function (err) {
if(err) throw new Error(err);
resolve(file);
});
});
};
checkCreate( cmd, type ) {
return new Promise((resolve, reject) => {
const createdFile = type === 'page' ? `${this.srcPaths.page}\\${cmd.name}` : (type === 'project' ? `${this.srcPaths.project.root}\\${cmd.name}` :`${this.srcPaths[type]}\\${cmd.name}.js`);
if(existsSync(createdFile)) return reject(`${createdFile} alredy exists`);
return resolve(cmd);
})
}
}
module.exports = Fs;