forked from odrick/free-tex-packer-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (118 loc) · 4.04 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const argv = require('optimist').argv;
const chalk = require('chalk');
const texturePacker = require('free-tex-packer-core');
const appInfo = require('./package.json');
function isExists(path) {
return fs.existsSync(path);
}
function fixPath(path) {
return path.trim().split('\\').join('/');
}
function getNameFromPath(path) {
return path.trim().split('/').pop();
}
function isFolder(path) {
if(isExists(path)) {
return fs.statSync(path).isDirectory();
}
else {
path = fixPath(path);
let name = getNameFromPath(path);
let parts = name.split('.');
return parts.length === 1;
}
}
function getFolderFilesList(dir, base = '', list = []) {
let files = fs.readdirSync(dir);
for(let file of files) {
let p = path.resolve(dir, file);
if(isFolder(p) && p.toUpperCase().indexOf('__MACOSX') < 0) {
list = getFolderFilesList(p, base + file + '/', list);
}
else {
list.push({
name: (base ? base : '') + file,
path: p
});
}
}
return list;
}
function loadImages(images, files) {
for(let image of images) {
try {
files.push({path: image.name, contents: fs.readFileSync(image.path)});
}
catch(e) {
}
}
}
console.log(chalk.yellowBright("Free Texture Packer CLI v" + appInfo.version));
let projectPath = argv.project;
if(!projectPath) {
console.log(chalk.redBright('Choose project, using --project argument'));
process.exit();
}
fs.readFile(projectPath, (err, content) => {
if(err) {
console.error(err.toString());
return;
}
content = content.toString();
let project = null;
try {
project = JSON.parse(content);
}
catch(e) {
console.log(chalk.redBright('Unsupported project format ' + projectPath));
process.exit();
}
let outputPath = argv.output;
if(!outputPath) {
outputPath = project.savePath;
}
if(!outputPath) {
outputPath = path.dirname(projectPath);
}
let files = [];
loadImages(project.images, files);
for(let folder of project.folders) {
if(isExists(folder)) {
let list = getFolderFilesList(folder, getNameFromPath(folder) + '/');
loadImages(list, files);
}
}
let options = project.packOptions;
//Map exporters to core format
if(options.exporter === 'JSON (hash)') options.exporter = 'JsonHash';
if(options.exporter === 'JSON (array)') options.exporter = 'JsonArray';
if(options.exporter === 'XML') options.exporter = 'XML';
if(options.exporter === 'css (modern)') options.exporter = 'Css';
if(options.exporter === 'css (old)') options.exporter = 'OldCss';
if(options.exporter === 'pixi.js') options.exporter = 'Pixi';
if(options.exporter === 'Phaser (hash)') options.exporter = 'PhaserHash';
if(options.exporter === 'Phaser (array)') options.exporter = 'PhaserArray';
if(options.exporter === 'Phaser 3') options.exporter = 'Phaser3';
if(options.exporter === 'Spine') options.exporter = 'Spine';
if(options.exporter === 'cocos2d') options.exporter = 'Cocos2d';
if(options.exporter === 'UnrealEngine') options.exporter = 'Unreal';
if(options.exporter === 'Starling') options.exporter = 'Starling';
if(options.exporter === 'Unity3D') options.exporter = 'Unity3D';
if(options.exporter === 'custom') {
console.log(chalk.redBright('CLI does not support a custom exporter'));
process.exit();
}
options.appInfo = appInfo;
console.log(chalk.white('Start packing ') + chalk.magentaBright(projectPath));
texturePacker(files, options, (files) => {
for(let file of files) {
let out = path.resolve(outputPath, file.name);
console.log(chalk.white('Writing ') + chalk.greenBright(out));
fs.writeFileSync(out, file.buffer);
}
console.log(chalk.yellowBright("Done"));
});
});