-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
130 lines (93 loc) · 3.47 KB
/
index.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
var PSD = require('psd');
var fs = require('fs');
var Mustache = require('mustache');
var gulp = require('gulp');
var sass = require('gulp-sass');//为了编译sass, 不需要的话可以不引
var config = require('./cfg');
var _util = {
deleteFolderRecursive: function(path) {
var _this = this;
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index) {
var curPath = path + '/' + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
_this.deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
},
buildJSON: function(list) {
return {
"block-list": list
}
},
stringObj: function(obj, seprator) {
var strs = [],
result;
for (var key in obj) {
strs.push(key + ':' + obj[key]);
}
result = strs.join(seprator);
return result + seprator;
}
}
var file = process.argv[2] || config.imgFile;
var cssList = [],
prefix = config.prefix ,
nameType = config.nameType ,
cssTPL = fs.readFileSync(config.tpl.css).toString() ,
htmlTPL = fs.readFileSync(config.tpl.html).toString() ,
outpath = config.outpath ;
fs.existsSync(outpath) || fs.mkdirSync(outpath);
cutPSD();
function cutPSD() {
var start = new Date();
console.log(file)
//var filterReg = new RegExp("[^A-z]");
var pathName = file.slice(0, -4);
var path = outpath + pathName;
var imgPath = path;
//文件系统创建 file system creation
fs.existsSync(path) && _util.deleteFolderRecursive(path);
fs.existsSync(path) || fs.mkdirSync(path);
fs.existsSync(imgPath) || fs.mkdirSync(imgPath);
PSD.open(file).then(function(psd) {
psd.tree().descendants().forEach(function(node, i) {
if (node.isGroup()) {
return true;
}
if (node.visible()) {
node.name = nameType === 'index'? i : node.name ;
cssList.push({
"prefix": prefix,
"name": node.name,
"top": node.get("top"),
"left": node.get("left"),
"width": node.get("width"),
"height": node.get("height")
});
node.saveAsPng(imgPath + "/" + prefix + '-' + node.name + ".png").catch(function(err) {
console.log(err.stack);
});
}
});
}).then(function() {
var blockJSON = _util.buildJSON(cssList.reverse());
fs.writeFile(path + '/block.scss', Mustache.render(cssTPL, blockJSON) ,(err) => {
if (err) throw err;
console.log('样式生成完毕!' + ((new Date()) - start) + "ms");
//sass to css
gulp.src(path + '/block.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(path));
});
fs.writeFileSync(path + '/block.html', Mustache.render(htmlTPL, blockJSON));
console.log('页面生成完毕!' + ((new Date()) - start) + "ms");
console.log("Finished in " + ((new Date()) - start) + "ms");
}).catch(function(err) {
console.log(err.stack);
});
}