aptechka 0.11.3 → 0.11.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aptechka",
3
- "version": "0.11.3",
3
+ "version": "0.11.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/denisavitski/aptechka.git"
@@ -1,4 +1,4 @@
1
1
  import { FaviconSource } from './types';
2
2
  import { Output } from '../utils';
3
3
 
4
- export declare function generateFavicon(source: FaviconSource): Promise<Output>;
4
+ export declare function generateFavicon(source: Omit<FaviconSource, 'type'>): Promise<Output>;
@@ -1,3 +1,3 @@
1
1
  import { SequenceSource } from './types';
2
2
 
3
- export declare function generateSequence(source: SequenceSource): Promise<import('../utils').Output>;
3
+ export declare function generateSequence(source: Omit<SequenceSource, 'type'>): Promise<import('../utils').Output>;
@@ -1,4 +1,4 @@
1
1
  import { SpriteSource } from './types';
2
2
  import { Output } from '../utils';
3
3
 
4
- export declare function generateSprite(source: SpriteSource): Promise<Output>;
4
+ export declare function generateSprite(source: Omit<SpriteSource, 'type'>): Promise<Output>;
@@ -7,4 +7,4 @@ export { optimize, type OptimizeOptions, type OptimizeProgressCallback, type Opt
7
7
  export { optimizeImage } from './optimizeImage';
8
8
  export { optimizer } from './optimizer';
9
9
  export { optimizeVideo } from './optimizeVideo';
10
- export { type SourceSettings, type Source, type SkipSource, type ImageSource, type VideoSource, type FaviconSource, type SpriteSource, type SequenceSource, type KnownSource, ALLOWED_IMAGE_EXTENSIONS, ALLOWED_VIDEO_EXTENSIONS, } from './types';
10
+ export { type SourceDefaultSettings, type Source, type SkipSource, type ImageSource, type VideoSource, type FaviconSource, type SpriteSource, type SequenceSource, type KnownSource, ALLOWED_IMAGE_EXTENSIONS, ALLOWED_VIDEO_EXTENSIONS, } from './types';
@@ -1,7 +1,7 @@
1
1
  import { mkdir, readFile, readdir } from "fs/promises";
2
2
  import { dirname, join, sep, extname } from "path";
3
3
  import Ffmpeg from "fluent-ffmpeg";
4
- import { getTmpPath, outputFile, getFolderFiles, removeExtension, clear, getBuffer, replaceExtension, outputFiles } from "../utils/index.js";
4
+ import { getTmpPath, outputFile, getFolderFiles, removeExtension, clear, getBuffer, clamp, replaceExtension, outputFiles } from "../utils/index.js";
5
5
  import favicons from "favicons";
6
6
  import { parse } from "node-html-parser";
7
7
  import { statSync } from "fs";
@@ -247,6 +247,7 @@ async function inputFiles({
247
247
  settings: {
248
248
  quality: 80,
249
249
  scale: 1,
250
+ fps: 0,
250
251
  destinationPath,
251
252
  ...(_e = settings == null ? void 0 : settings.video) == null ? void 0 : _e.call(settings, { destinationPath })
252
253
  }
@@ -263,6 +264,12 @@ async function inputFiles({
263
264
  }
264
265
  return sources;
265
266
  }
267
+ function getNumberSetting(value, min, max) {
268
+ if (typeof value === "number") {
269
+ return clamp(value, min, max);
270
+ }
271
+ return value;
272
+ }
266
273
  async function optimizeImage(source) {
267
274
  const { settings } = source;
268
275
  const content = await getBuffer(source.content);
@@ -272,24 +279,23 @@ async function optimizeImage(source) {
272
279
  const width = meta.width;
273
280
  const height = meta.height;
274
281
  const output = [];
282
+ const scale = getNumberSetting(settings.scale, 0, 1);
283
+ const quality = getNumberSetting(settings.quality, 0, 100);
275
284
  if (width && height) {
276
- if (settings == null ? void 0 : settings.scale) {
277
- image.resize(
278
- Math.floor(width * settings.scale),
279
- Math.floor(height * settings.scale)
280
- );
285
+ if (scale) {
286
+ image.resize(Math.floor(width * scale), Math.floor(height * scale));
281
287
  }
282
288
  }
283
289
  if (ext === ".jpg" || ext === ".jpeg") {
284
290
  image.jpeg({
285
291
  mozjpeg: true,
286
- quality: settings == null ? void 0 : settings.quality
292
+ quality
287
293
  });
288
294
  } else if (ext === ".png") {
289
295
  image.png({
290
296
  compressionLevel: 9,
291
297
  adaptiveFiltering: true,
292
- quality: settings == null ? void 0 : settings.quality,
298
+ quality,
293
299
  effort: 8
294
300
  });
295
301
  }
@@ -300,7 +306,7 @@ async function optimizeImage(source) {
300
306
  });
301
307
  if (settings == null ? void 0 : settings.webp) {
302
308
  const buffer2 = await image.webp({
303
- quality: settings.quality,
309
+ quality,
304
310
  effort: 5
305
311
  }).toBuffer();
306
312
  output.push({
@@ -323,17 +329,20 @@ async function optimizeImage(source) {
323
329
  async function optimizeVideo(source) {
324
330
  const { settings } = source;
325
331
  const buffer = await getBuffer(source.content);
332
+ const quality = getNumberSetting((settings == null ? void 0 : settings.quality) || 80, 0, 100);
333
+ const fps = getNumberSetting((settings == null ? void 0 : settings.fps) || 0, 0, 300);
334
+ const scale = getNumberSetting((settings == null ? void 0 : settings.scale) || 1, 0, 1);
326
335
  return FFmpeg({
327
336
  inputPath: settings.destinationPath,
328
337
  fileContent: buffer,
329
338
  instructions: (command) => {
330
339
  command.addOutputOption(
331
- `-crf ${Math.round((100 - ((settings == null ? void 0 : settings.quality) || 80)) * 51 / 100)}`
340
+ `-crf ${Math.round((100 - quality) * 51 / 100)}`
332
341
  );
333
- if (settings == null ? void 0 : settings.fps) {
334
- command.fps(settings.fps);
342
+ if (fps) {
343
+ command.fps(fps);
335
344
  }
336
- command.size(`${((settings == null ? void 0 : settings.scale) || 1) * 100}%`);
345
+ command.size(`${scale * 100}%`);
337
346
  }
338
347
  });
339
348
  }
@@ -1,4 +1,4 @@
1
1
  import { ImageSource } from './types';
2
2
  import { Output } from '../utils';
3
3
 
4
- export declare function optimizeImage(source: ImageSource): Promise<Output>;
4
+ export declare function optimizeImage(source: Omit<ImageSource, 'type'>): Promise<Output>;
@@ -1,3 +1,3 @@
1
1
  import { VideoSource } from './types';
2
2
 
3
- export declare function optimizeVideo(source: VideoSource): Promise<import('../utils').Output>;
3
+ export declare function optimizeVideo(source: Omit<VideoSource, 'type'>): Promise<import('../utils').Output>;
@@ -1,13 +1,16 @@
1
1
  import { FaviconOptions } from 'favicons';
2
2
 
3
- export type SourceSettings = {
3
+ export type SourceDefaultSettings = {
4
4
  [key: string]: any;
5
5
  };
6
- export interface Source<B = Buffer | File, T extends string = string, S extends SourceSettings = {}> {
6
+ export type SourceSetting = {
7
+ value: any;
8
+ };
9
+ export type Source<B = Buffer | File, T extends string = string, S extends SourceDefaultSettings = {}> = {
7
10
  content: B;
8
11
  type: T;
9
12
  settings: S;
10
- }
13
+ };
11
14
  export type SkipSource = Source<Buffer | File, 'skip', {
12
15
  destinationPath: string;
13
16
  }>;
@@ -0,0 +1 @@
1
+ export declare function getNumberSetting<T extends number | undefined>(value: T, min: number, max: number): number | T;
@@ -1,4 +1,5 @@
1
1
  export { getBuffer } from './buffer';
2
2
  export { removeExtension, replaceExtension, getTmpPath } from './path';
3
3
  export { outputFile, outputFiles, clear, getFolderFiles } from './fs';
4
+ export { clamp } from './math';
4
5
  export type { WriteFileData, OutputItem, Output } from './types';
@@ -49,7 +49,11 @@ async function getFolderFiles(folderPath) {
49
49
  }
50
50
  return files;
51
51
  }
52
+ function clamp(number, min = 0, max = 0) {
53
+ return Math.max(min, Math.min(number, max));
54
+ }
52
55
  export {
56
+ clamp,
53
57
  clear,
54
58
  getBuffer,
55
59
  getFolderFiles,
@@ -0,0 +1 @@
1
+ export declare function clamp(number: number, min?: number, max?: number): number;