@remvst/asset-catalog 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -9,6 +9,7 @@ const utils_1 = require("./utils");
9
9
  const yargs_1 = __importDefault(require("yargs/yargs"));
10
10
  const helpers_1 = require("yargs/helpers");
11
11
  const path_1 = require("path");
12
+ const audiosprite_1 = __importDefault(require("audiosprite"));
12
13
  async function main() {
13
14
  const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
14
15
  .options({
@@ -20,9 +21,9 @@ async function main() {
20
21
  },
21
22
  'assetDir': {
22
23
  type: 'string',
23
- default: './package.json',
24
+ default: '.',
24
25
  alias: 'a',
25
- describe: 'package.json file to use for the version',
26
+ describe: 'Asset directory where all the PNGs are located',
26
27
  },
27
28
  'wav': {
28
29
  type: 'boolean',
@@ -39,6 +40,19 @@ async function main() {
39
40
  default: true,
40
41
  describe: 'Include .mp3 files',
41
42
  },
43
+ 'outSpritesheet': {
44
+ type: 'string',
45
+ required: false,
46
+ describe: 'Path to the exported spritesheet (without the extension)',
47
+ },
48
+ 'spritesheetExcludeCategory': {
49
+ type: 'string',
50
+ array: true,
51
+ required: false,
52
+ alias: 'x',
53
+ default: [],
54
+ describe: 'Exclude certain categories from the spritesheet',
55
+ },
42
56
  })
43
57
  .argv;
44
58
  const extensions = [];
@@ -54,7 +68,8 @@ async function main() {
54
68
  }
55
69
  catch (e) { }
56
70
  const files = await (0, utils_1.allFiles)(argv.assetDir);
57
- const sounds = files.filter(file => extensions.indexOf((0, path_1.extname)(file)) >= 0);
71
+ const sounds = files.filter(file => extensions.indexOf((0, path_1.extname)(file)) >= 0)
72
+ .filter(file => (0, path_1.basename)(file, (0, path_1.extname)(file)) !== 'sprites');
58
73
  const defs = new Map();
59
74
  for (const sound of sounds) {
60
75
  const category = (0, utils_1.categoryPath)(argv.assetDir, sound).join('_');
@@ -68,6 +83,48 @@ async function main() {
68
83
  }
69
84
  defs.get(category).get(filenameWithoutExt).add(sound);
70
85
  }
86
+ const spriteSounds = new Map();
87
+ let spritesheetJson = null;
88
+ if (argv.outSpritesheet) {
89
+ for (const category of defs.keys()) {
90
+ let isCategoryExcluded = false;
91
+ for (const exclusion of argv.spritesheetExcludeCategory) {
92
+ if (category.indexOf(exclusion) !== -1) {
93
+ isCategoryExcluded = true;
94
+ break;
95
+ }
96
+ }
97
+ if (isCategoryExcluded) {
98
+ continue;
99
+ }
100
+ for (const filenameWithoutExt of defs.get(category).keys()) {
101
+ const fileSet = defs.get(category).get(filenameWithoutExt);
102
+ const files = Array.from(fileSet);
103
+ const pickedFile = files.find(file => (0, path_1.extname)(file) === '.wav') ||
104
+ files.find(file => (0, path_1.extname)(file) === '.ogg') ||
105
+ files.find(file => (0, path_1.extname)(file) === '.mp3');
106
+ if (!pickedFile) {
107
+ throw new Error('Unable to pick file for sprite in ' + category);
108
+ }
109
+ spriteSounds.set(category + '/' + filenameWithoutExt, pickedFile);
110
+ }
111
+ }
112
+ spritesheetJson = await new Promise((resolve, reject) => {
113
+ const options = {
114
+ output: argv.outSpritesheet,
115
+ format: 'howler',
116
+ export: extensions.map(ext => ext.slice(1)).join(','),
117
+ };
118
+ (0, audiosprite_1.default)(Array.from(spriteSounds.values()), options, (err, res) => {
119
+ if (err) {
120
+ reject(err);
121
+ }
122
+ else {
123
+ resolve(res);
124
+ }
125
+ });
126
+ });
127
+ }
71
128
  const imports = [];
72
129
  const definitions = [];
73
130
  const funcs = [];
@@ -76,10 +133,38 @@ async function main() {
76
133
  soundDefinition += ' constructor(\n';
77
134
  soundDefinition += ' readonly basename: string,\n';
78
135
  soundDefinition += ' readonly files: string[],\n';
136
+ soundDefinition += ' readonly sprite: string | null,\n';
79
137
  soundDefinition += ' readonly averageFileSize: number,\n';
80
138
  soundDefinition += ' ) {}\n';
81
139
  soundDefinition += '}\n';
82
140
  definitions.push(soundDefinition);
141
+ if (argv.outSpritesheet) {
142
+ const modifiedSpritesheetJson = { ...spritesheetJson };
143
+ modifiedSpritesheetJson.urls = extensions.map(ext => argv.outSpritesheet + ext);
144
+ let spriteSheetFunc = 'export function sound_spritesheet() {\n';
145
+ spriteSheetFunc += ' return {\n';
146
+ spriteSheetFunc += ' urls: [\n';
147
+ const idealOrder = ['.ogg', '.mp3', '.wav'];
148
+ const sortedExtensions = extensions.sort((a, b) => idealOrder.indexOf(a) - idealOrder.indexOf(b));
149
+ for (const extension of sortedExtensions) {
150
+ const importName = 'sprites_' + extension.slice(1);
151
+ imports.push(`import ${importName} from '${(0, path_1.relative)((0, path_1.dirname)(argv.outFile), argv.outSpritesheet + extension).replace(/\\/g, '/')}';`);
152
+ spriteSheetFunc += ` ${importName},\n`;
153
+ }
154
+ let totalFileSize = 0;
155
+ for (const extension of sortedExtensions) {
156
+ const soundFile = argv.outSpritesheet + extension;
157
+ const stats = await fs_1.promises.stat(soundFile);
158
+ totalFileSize += stats.size;
159
+ }
160
+ const averageFileSize = Math.round(totalFileSize / files.length);
161
+ spriteSheetFunc += ' ],\n';
162
+ spriteSheetFunc += ` sprite: ${JSON.stringify(spritesheetJson.sprite)},\n`;
163
+ spriteSheetFunc += ` averageFileSize: ${averageFileSize},\n`;
164
+ spriteSheetFunc += ' }\n';
165
+ spriteSheetFunc += '};\n';
166
+ funcs.push(spriteSheetFunc);
167
+ }
83
168
  for (const category of defs.keys()) {
84
169
  let func = `export function sound_${category}(): SoundDefinition[] {\n`;
85
170
  func += ` return [\n`;
@@ -101,7 +186,9 @@ async function main() {
101
186
  totalFileSize += stats.size;
102
187
  }
103
188
  const averageFileSize = Math.round(totalFileSize / files.length);
189
+ const hasSprite = spriteSounds.get(category + '/' + filenameWithoutExt);
104
190
  func += ' ],\n';
191
+ func += ` ${JSON.stringify(hasSprite ? filenameWithoutExt : null)},\n`;
105
192
  func += ` ${averageFileSize},\n`;
106
193
  func += ' ),\n';
107
194
  }
package/lib/utils.d.ts CHANGED
@@ -2,4 +2,4 @@ export declare function allFiles(path: string): Promise<string[]>;
2
2
  export declare function sanitize(string: string): string;
3
3
  export declare function camelize(str: string): string;
4
4
  export declare function lowerCamelize(str: string): string;
5
- export declare function categoryPath(assetDir: string, png: string): string[];
5
+ export declare function categoryPath(assetDir: string, filepath: string): string[];
package/lib/utils.js CHANGED
@@ -34,8 +34,8 @@ function lowerCamelize(str) {
34
34
  return camelized.slice(0, 1).toLowerCase() + camelized.slice(1);
35
35
  }
36
36
  exports.lowerCamelize = lowerCamelize;
37
- function categoryPath(assetDir, png) {
38
- const dir = (0, path_1.dirname)(png);
37
+ function categoryPath(assetDir, filepath) {
38
+ const dir = (0, path_1.dirname)(filepath);
39
39
  const trimmedDir = (0, path_1.relative)(assetDir, dir);
40
40
  return trimmedDir
41
41
  .split(/[\/\\]/g)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remvst/asset-catalog",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "bin": {
6
6
  "generate-image-catalog": "./lib/generate-image-catalog.js",
@@ -9,19 +9,21 @@
9
9
  "scripts": {
10
10
  "build": "rm -rf lib && tsc && chmod +x lib/generate-image-catalog.js lib/generate-sound-catalog.js",
11
11
  "test:images": "ts-node src/generate-image-catalog.ts --assetDir=./testData --outFile=testOut/images.ts --outSpritesheet=testOut/spritesheet.png",
12
- "test:sounds": "ts-node src/generate-sound-catalog.ts --assetDir=./testData/sounds --outFile=testOut/sounds.ts",
12
+ "test:sounds": "ts-node src/generate-sound-catalog.ts --assetDir=./testData/sounds --outFile=testOut/sounds.ts --outSpritesheet=testData/sounds/sprites --spritesheetExcludeCategory=jump",
13
13
  "test": "npm run test:images && npm run test:sounds",
14
14
  "prepublishOnly": "npm i && npm run build"
15
15
  },
16
16
  "author": "Rémi Vansteelandt",
17
17
  "license": "UNLICENSED",
18
18
  "dependencies": {
19
+ "audiosprite": "^0.7.2",
19
20
  "bin-pack": "^1.0.2",
20
21
  "canvas": "^2.11.2",
21
22
  "image-size": "^1.0.2",
22
23
  "yargs": "^17.7.2"
23
24
  },
24
25
  "devDependencies": {
26
+ "@types/audiosprite": "^0.7.3",
25
27
  "@types/bin-pack": "^1.0.3",
26
28
  "@types/node": "^18.11.5",
27
29
  "@types/yargs": "^17.0.32",
@@ -4,7 +4,8 @@ import { promises as fs } from 'fs';
4
4
  import { sanitize, allFiles, categoryPath } from './utils';
5
5
  import yargs from 'yargs/yargs';
6
6
  import { hideBin } from 'yargs/helpers';
7
- import { dirname, extname, basename, relative } from 'path';
7
+ import { dirname, extname, basename, relative, parse } from 'path';
8
+ import audiosprite from 'audiosprite';
8
9
 
9
10
  async function main() {
10
11
  const argv = await yargs(hideBin(process.argv))
@@ -17,9 +18,9 @@ async function main() {
17
18
  },
18
19
  'assetDir': {
19
20
  type: 'string',
20
- default: './package.json',
21
+ default: '.',
21
22
  alias: 'a',
22
- describe: 'package.json file to use for the version',
23
+ describe: 'Asset directory where all the PNGs are located',
23
24
  },
24
25
  'wav': {
25
26
  type: 'boolean',
@@ -36,6 +37,19 @@ async function main() {
36
37
  default: true,
37
38
  describe: 'Include .mp3 files',
38
39
  },
40
+ 'outSpritesheet': {
41
+ type: 'string',
42
+ required: false,
43
+ describe: 'Path to the exported spritesheet (without the extension)',
44
+ },
45
+ 'spritesheetExcludeCategory': {
46
+ type: 'string',
47
+ array: true,
48
+ required: false,
49
+ alias: 'x',
50
+ default: [] as string[],
51
+ describe: 'Exclude certain categories from the spritesheet',
52
+ },
39
53
  })
40
54
  .argv;
41
55
 
@@ -51,7 +65,8 @@ async function main() {
51
65
 
52
66
 
53
67
  const files = await allFiles(argv.assetDir);
54
- const sounds = files.filter(file => extensions.indexOf(extname(file)) >= 0);
68
+ const sounds = files.filter(file => extensions.indexOf(extname(file)) >= 0)
69
+ .filter(file => basename(file, extname(file)) !== 'sprites');
55
70
 
56
71
  const defs = new Map<string, Map<string, Set<string>>>();
57
72
 
@@ -71,6 +86,53 @@ async function main() {
71
86
  defs.get(category)!.get(filenameWithoutExt)!.add(sound);
72
87
  }
73
88
 
89
+ const spriteSounds: Map<string, string> = new Map();
90
+
91
+ let spritesheetJson: any = null;
92
+ if (argv.outSpritesheet) {
93
+ for (const category of defs.keys()) {
94
+ let isCategoryExcluded = false;
95
+ for (const exclusion of argv.spritesheetExcludeCategory) {
96
+ if (category.indexOf(exclusion) !== -1) {
97
+ isCategoryExcluded = true;
98
+ break;
99
+ }
100
+ }
101
+ if (isCategoryExcluded) {
102
+ continue;
103
+ }
104
+
105
+ for (const filenameWithoutExt of defs.get(category)!.keys()) {
106
+ const fileSet = defs.get(category)!.get(filenameWithoutExt)!;
107
+
108
+ const files = Array.from(fileSet);
109
+ const pickedFile = files.find(file => extname(file) === '.wav') ||
110
+ files.find(file => extname(file) === '.ogg') ||
111
+ files.find(file => extname(file) === '.mp3');
112
+ if (!pickedFile) {
113
+ throw new Error('Unable to pick file for sprite in ' + category);
114
+ }
115
+ spriteSounds.set(category + '/' + filenameWithoutExt, pickedFile);
116
+ }
117
+ }
118
+
119
+ spritesheetJson = await new Promise((resolve, reject) => {
120
+ const options: audiosprite.Option = {
121
+ output: argv.outSpritesheet,
122
+ format: 'howler',
123
+ export: extensions.map(ext => ext.slice(1)).join(','),
124
+ };
125
+
126
+ audiosprite(Array.from(spriteSounds.values()), options, (err, res) => {
127
+ if (err) {
128
+ reject(err);
129
+ } else {
130
+ resolve(res);
131
+ }
132
+ });
133
+ });
134
+ }
135
+
74
136
  const imports = [];
75
137
  const definitions = [];
76
138
  const funcs = [];
@@ -80,11 +142,47 @@ async function main() {
80
142
  soundDefinition += ' constructor(\n';
81
143
  soundDefinition += ' readonly basename: string,\n';
82
144
  soundDefinition += ' readonly files: string[],\n';
145
+ soundDefinition += ' readonly sprite: string | null,\n';
83
146
  soundDefinition += ' readonly averageFileSize: number,\n';
84
147
  soundDefinition += ' ) {}\n';
85
148
  soundDefinition += '}\n';
86
149
  definitions.push(soundDefinition);
87
150
 
151
+ if (argv.outSpritesheet) {
152
+ const modifiedSpritesheetJson = { ...spritesheetJson };
153
+ modifiedSpritesheetJson.urls = extensions.map(ext => argv.outSpritesheet + ext);
154
+
155
+ let spriteSheetFunc = 'export function sound_spritesheet() {\n';
156
+ spriteSheetFunc += ' return {\n';
157
+ spriteSheetFunc += ' urls: [\n';
158
+
159
+
160
+ const idealOrder = ['.ogg', '.mp3', '.wav'];
161
+ const sortedExtensions = extensions.sort((a, b) => idealOrder.indexOf(a) - idealOrder.indexOf(b));
162
+ for (const extension of sortedExtensions) {
163
+ const importName = 'sprites_' + extension.slice(1);
164
+ imports.push(`import ${importName} from '${relative(dirname(argv.outFile), argv.outSpritesheet + extension).replace(/\\/g, '/')}';`);
165
+
166
+ spriteSheetFunc += ` ${importName},\n`;
167
+ }
168
+
169
+ let totalFileSize = 0;
170
+ for (const extension of sortedExtensions) {
171
+ const soundFile = argv.outSpritesheet + extension;
172
+ const stats = await fs.stat(soundFile);
173
+ totalFileSize += stats.size;
174
+ }
175
+
176
+ const averageFileSize = Math.round(totalFileSize / files.length);
177
+
178
+ spriteSheetFunc += ' ],\n';
179
+ spriteSheetFunc += ` sprite: ${JSON.stringify(spritesheetJson.sprite)},\n`;
180
+ spriteSheetFunc += ` averageFileSize: ${averageFileSize},\n`;
181
+ spriteSheetFunc += ' }\n';
182
+ spriteSheetFunc += '};\n';
183
+ funcs.push(spriteSheetFunc);
184
+ }
185
+
88
186
  for (const category of defs.keys()) {
89
187
  let func = `export function sound_${category}(): SoundDefinition[] {\n`;
90
188
  func += ` return [\n`;
@@ -111,7 +209,10 @@ async function main() {
111
209
 
112
210
  const averageFileSize = Math.round(totalFileSize / files.length);
113
211
 
212
+ const hasSprite = spriteSounds.get(category + '/' + filenameWithoutExt)!;
213
+
114
214
  func += ' ],\n';
215
+ func += ` ${JSON.stringify(hasSprite ? filenameWithoutExt : null)},\n`;
115
216
  func += ` ${averageFileSize},\n`;
116
217
  func += ' ),\n';
117
218
  }
package/src/utils.ts CHANGED
@@ -34,8 +34,8 @@ export function lowerCamelize(str: string): string {
34
34
  return camelized.slice(0, 1).toLowerCase() + camelized.slice(1);
35
35
  }
36
36
 
37
- export function categoryPath(assetDir: string, png: string): string[] {
38
- const dir = dirname(png);
37
+ export function categoryPath(assetDir: string, filepath: string): string[] {
38
+ const dir = dirname(filepath);
39
39
  const trimmedDir = relative(assetDir, dir);
40
40
  return trimmedDir
41
41
  .split(/[\/\\]/g)