-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (117 loc) · 3.7 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const program = require('commander');
const recursive = require("recursive-readdir");
const targz = require('targz');
const tmp = require('tmp');
const YAML = require('yaml');
const ignore = require('ignore');
function toUnityPosixPath(pathStr) {
let result = pathStr.split(path.sep).join(path.posix.sep);
debug(`toPosixPath: ${result}`);
if (result.indexOf("Assets/") !== 0) {
result = "Assets/" + result;
}
return result;
}
let directory = null;
program
.arguments('<directory>')
.option('-o, --output <output filename>', 'The output package name', 'Assets.unitypackage')
.option('--overwrite', 'Overwrite output file if exists', false)
.option('-x, --exclude <filter>', 'Exclude files/folders using comma separated string (uses .gitignore spec)')
.option('-p, --prepend <directory>', 'Prepend directory to \'pathname\' in asset package')
.option('-d, --debug', 'Debug script', false)
.action(function (dir) {
directory = dir || ".";
})
.parse(process.argv);
directory = directory || ".";
let ig = ignore();
if (program.exclude) {
let exclude = program.exclude;
ig = ignore().add(exclude.split(',').filter(item => item.trim() !== ''));
}
let debug = function () {
if (program.debug) {
console.log(...arguments);
}
}
if (fs.existsSync(program.output)) {
if (program.overwrite) {
fs.unlinkSync(program.output);
} else {
console.log(`Output file '${program.output}' exists. Use --overwrite to overwrite existing file.`);
process.exit(1);
}
}
let ignoreFunction = function (file, stats) {
let filePath = path.relative(directory, file);
let ignored = ig.ignores(filePath);
if (ignored) {
debug("Excluding file", file);
}
return ignored;
}
recursive(directory, [ignoreFunction], function (err, files) {
if (err) {
console.log("Asset packaging failed", err);
process.exit(1);
}
let onDiskFiles = {};
files.forEach(file => {
if (file.endsWith('.meta')) {
let original = file.substr(0, file.length - 5);
let currentItems = onDiskFiles[original];
let items = { ...currentItems, meta: file };
onDiskFiles[original] = items;
} else {
let currentItems = onDiskFiles[file];
let items = { ...currentItems, asset: file };
onDiskFiles[file] = items;
}
});
let temp = tmp.dirSync();
let tempDirectory = temp.name;
debug(`Working temp directory is ${tempDirectory}.`);
for (var file in onDiskFiles) {
let data = onDiskFiles[file];
if (!data.meta) {
debug(`Skipping ${file}. Not .meta file found.`);
continue;
}
const metaFile = fs.readFileSync(data.meta, 'utf8')
let meta = YAML.parse(metaFile);
let itemDirectory = path.join(tempDirectory, meta.guid);
fs.mkdirSync(itemDirectory);
debug(`\n\n${JSON.stringify(meta)}\n\n`);
let fullPath = [];
if (program.prepend) {
fullPath.push(program.prepend);
}
fullPath.push(path.relative(directory, file));
let filePath = toUnityPosixPath(path.join(...fullPath));
let pathname = path.join(itemDirectory, 'pathname').replace();
debug(`Writing ${filePath} to ${pathname}`);
fs.writeFileSync(pathname, filePath);
let assetMeta = path.join(itemDirectory, 'asset.meta');
debug(`Meta Copying '${data.meta}' to ${assetMeta}`);
fs.copyFileSync(data.meta, assetMeta);
if (data.asset) {
let asset = path.join(itemDirectory, 'asset');
debug(`Copying '${data.asset}' to ${asset}`);
fs.copyFileSync(data.asset, asset);
} else {
debug(`No asset to copy for ${file}`);
}
}
targz.compress({ src: tempDirectory, dest: program.output }, function (err) {
if (err) {
console.log("Asset packaging failed", err);
process.exit(1);
} else {
console.log(`Asset package '${program.output}' created successfully.`);
}
});
});