From d7c1e530ab8215e7e7efa5eeef5c874b886c78d1 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:00:51 +0100 Subject: [PATCH] Fix: names for plugins (#7) * Fix: names for plugins * Fix: paths and logger --- .../cache-buster/test/cacheBuster.test.ts | 6 ++--- packages/core/src/Asset.ts | 13 +++++----- packages/core/src/AssetCache.ts | 4 +-- packages/core/src/AssetIgnore.ts | 4 +-- packages/core/src/AssetPack.ts | 12 ++++----- packages/core/src/AssetWatcher.ts | 10 ++++---- packages/core/src/index.ts | 2 +- packages/core/src/logger/Logger.ts | 1 - .../core/src/utils/applySettingToAsset.ts | 4 +-- packages/core/src/utils/checkExt.ts | 10 ++++---- packages/core/src/utils/createNewAssetAt.ts | 10 ++++---- packages/core/src/utils/logAssetGraph.ts | 4 +-- packages/core/src/utils/path.ts | 3 ++- packages/core/src/utils/swapExt.ts | 10 ++++---- packages/ffmpeg/src/ffmpeg.ts | 10 +++----- packages/json/src/index.ts | 5 ++-- packages/manifest/src/pixiManifest.ts | 25 ++++++++----------- .../mipmap-compress/src/mipmapCompress.ts | 2 +- .../mipmap-compress/src/spineAtlasMipmap.ts | 2 +- .../texture-packer/src/packer/createJsons.ts | 6 ++--- .../src/packer/createTextureData.ts | 3 ++- packages/texture-packer/src/texturePacker.ts | 6 ++--- .../src/texturePackerCacheBuster.ts | 4 +-- packages/webfont/src/webfont.ts | 4 +-- 24 files changed, 78 insertions(+), 82 deletions(-) diff --git a/packages/cache-buster/test/cacheBuster.test.ts b/packages/cache-buster/test/cacheBuster.test.ts index fda0c56..dca7e5f 100644 --- a/packages/cache-buster/test/cacheBuster.test.ts +++ b/packages/cache-buster/test/cacheBuster.test.ts @@ -1,4 +1,4 @@ -import { AssetPack, joinSafe } from '@play-co/assetpack-core'; +import { AssetPack, path } from '@play-co/assetpack-core'; import { existsSync, readFileSync } from 'fs-extra'; import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test'; import { cacheBuster, crc32 } from '../src'; @@ -38,12 +38,12 @@ describe('CacheBuster', () => await assetpack.run(); - const originalPath = joinSafe('.testInput', testName, 'ttf.ttf'); + const originalPath = path.joinSafe('.testInput', testName, 'ttf.ttf'); const buffer = readFileSync(originalPath); const hash = crc32(buffer); - expect(existsSync(joinSafe('.testOutput', testName, `ttf-${hash}.ttf`))).toBe(true); + expect(existsSync(path.joinSafe('.testOutput', testName, `ttf-${hash}.ttf`))).toBe(true); }); }); diff --git a/packages/core/src/Asset.ts b/packages/core/src/Asset.ts index 5b59a94..e41e309 100644 --- a/packages/core/src/Asset.ts +++ b/packages/core/src/Asset.ts @@ -1,7 +1,8 @@ -import { basename, dirname, extname } from 'upath'; import { extractTagsFromFileName } from './utils/extractTagsFromFileName'; import { getHash } from './utils/getHash'; import { readFileSync } from 'fs-extra'; +import { Logger } from './logger/Logger'; +import { path } from './utils/path'; export interface AssetOptions { @@ -106,7 +107,7 @@ export class Asset { if (this.isFolder) { - console.warn('[Assetpack] folders should not have buffers!'); + Logger.warn('[Assetpack] folders should not have buffers!. Contact the developer of Assetpack'); } if (!this._buffer) @@ -128,7 +129,7 @@ export class Asset { if (this.isFolder) { - console.warn('[Assetpack] folders should not have hashes'); + Logger.warn('[Assetpack] folders should not have hashes. Contact the developer of the Assetpack'); } this._hash ??= getHash(this.buffer ?? this.path); @@ -138,17 +139,17 @@ export class Asset get filename() { - return basename(this.path); + return path.basename(this.path); } get directory() { - return dirname(this.path); + return path.dirname(this.path); } get extension() { - return extname(this.path); + return path.extname(this.path); } get rootAsset() diff --git a/packages/core/src/AssetCache.ts b/packages/core/src/AssetCache.ts index 82f06c8..df0627d 100644 --- a/packages/core/src/AssetCache.ts +++ b/packages/core/src/AssetCache.ts @@ -1,6 +1,6 @@ -import { joinSafe } from 'upath'; import type { Asset } from './Asset'; import { ensureDirSync, readJSONSync, writeJSONSync } from 'fs-extra'; +import { path } from './utils/path'; export interface AssetCacheOptions { @@ -42,7 +42,7 @@ export class AssetCache this._serializeAsset(asset, schema.assets, true); // get root dir in node - ensureDirSync(joinSafe('.assetpack')); + ensureDirSync(path.joinSafe('.assetpack')); writeJSONSync(`.assetpack/${this._cacheName}.json`, schema, { spaces: 4 }); } diff --git a/packages/core/src/AssetIgnore.ts b/packages/core/src/AssetIgnore.ts index 5959378..3985fbe 100644 --- a/packages/core/src/AssetIgnore.ts +++ b/packages/core/src/AssetIgnore.ts @@ -1,5 +1,5 @@ import minimatch from 'minimatch'; -import { relative } from 'upath'; +import { path } from './utils/path'; export interface AssetIgnoreOptions { @@ -28,7 +28,7 @@ export class AssetIgnore _ignoreHash[fullPath] = false; if (_ignore.length > 0) { - const relativePath = relative(this._basePath, fullPath); + const relativePath = path.relative(this._basePath, fullPath); for (let i = 0; i < _ignore.length; i++) { diff --git a/packages/core/src/AssetPack.ts b/packages/core/src/AssetPack.ts index 11d28f9..5d616fd 100644 --- a/packages/core/src/AssetPack.ts +++ b/packages/core/src/AssetPack.ts @@ -5,12 +5,12 @@ import { AssetCache } from './AssetCache'; import { AssetWatcher } from './AssetWatcher'; import type { AssetSettings } from './pipes/PipeSystem'; import { PipeSystem } from './pipes/PipeSystem'; -import { isAbsolute, join, normalizeSafe } from 'upath'; import { finalCopyPipe } from './pipes/finalCopyPipe'; import type { AssetPackConfig } from './config'; import objectHash from 'object-hash'; import { Logger } from './logger/Logger'; import { promiseAllConcurrent } from './utils/promiseAllConcurrent'; +import { path } from './utils/path'; export class AssetPack { @@ -232,14 +232,14 @@ function _deleteAsset(asset: Asset) } } -function normalizePath(path: string) +function normalizePath(pth: string) { - path = normalizeSafe(path); + pth = path.normalizeSafe(pth); - if (!isAbsolute(path)) + if (!path.isAbsolute(pth)) { - path = normalizeSafe(join(process.cwd(), path)); + pth = path.normalizeSafe(path.join(process.cwd(), pth)); } - return path; + return pth; } diff --git a/packages/core/src/AssetWatcher.ts b/packages/core/src/AssetWatcher.ts index 16fd7d9..bfadbf1 100644 --- a/packages/core/src/AssetWatcher.ts +++ b/packages/core/src/AssetWatcher.ts @@ -3,10 +3,10 @@ import { Asset } from './Asset'; import chokidar from 'chokidar'; import type { CachedAsset } from './AssetCache'; import { syncAssetsWithCache } from './utils/syncAssetsWithCache'; -import { dirname, joinSafe } from 'upath'; import { AssetIgnore } from './AssetIgnore'; import type { AssetSettings } from './pipes/PipeSystem'; import { applySettingToAsset } from './utils/applySettingToAsset'; +import { path } from './utils/path'; export interface AssetWatcherOptions { @@ -217,7 +217,7 @@ export class AssetWatcher this._assetHash[file] = asset; - const parentAsset = this._assetHash[dirname(file)]; + const parentAsset = this._assetHash[path.dirname(file)]; parentAsset.addChild(asset); } @@ -272,7 +272,7 @@ export class AssetWatcher files.forEach((file) => { - const fullPath = joinSafe(asset.path, file); + const fullPath = path.joinSafe(asset.path, file); if (fullPath.includes('DS_Store')) return; @@ -302,7 +302,7 @@ export class AssetWatcher private _ensureDirectory(dirPath: string) { - const parentPath = dirname(dirPath); + const parentPath = path.dirname(dirPath); if (parentPath === this._entryPath || parentPath === '.') { @@ -323,7 +323,7 @@ export class AssetWatcher asset.state = 'added'; - const parentAsset = this._assetHash[dirname(parentPath)]; + const parentAsset = this._assetHash[path.dirname(parentPath)]; parentAsset.addChild(asset); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 164254c..ca38f25 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,7 +1,7 @@ // export * from './AssetPack'; // export * from './Cache'; // export * from './config'; -// export * from './logger/Logger'; +export * from './logger/Logger'; // export * from './Plugin'; // export * from './Processor'; // export * from './utils'; diff --git a/packages/core/src/logger/Logger.ts b/packages/core/src/logger/Logger.ts index 42aa4ea..6485f36 100644 --- a/packages/core/src/logger/Logger.ts +++ b/packages/core/src/logger/Logger.ts @@ -32,7 +32,6 @@ class LoggerClass public info(message: string) { - return; this.report({ type: 'log', level: 'info', diff --git a/packages/core/src/utils/applySettingToAsset.ts b/packages/core/src/utils/applySettingToAsset.ts index de4faec..ee11aa4 100644 --- a/packages/core/src/utils/applySettingToAsset.ts +++ b/packages/core/src/utils/applySettingToAsset.ts @@ -1,12 +1,12 @@ import type { Asset } from '../Asset'; import type { AssetSettings } from '../pipes/PipeSystem'; -import { relative } from './path'; import minimatch from 'minimatch'; import { merge } from './merge'; +import { path } from './path'; export function applySettingToAsset(asset: Asset, settings: AssetSettings[], entryPath: string) { - const relativePath = relative(entryPath, asset.path); + const relativePath = path.relative(entryPath, asset.path); let assetOptions; let metaData; diff --git a/packages/core/src/utils/checkExt.ts b/packages/core/src/utils/checkExt.ts index 2e9f4ab..5127ea3 100644 --- a/packages/core/src/utils/checkExt.ts +++ b/packages/core/src/utils/checkExt.ts @@ -1,13 +1,13 @@ -import { extname } from 'upath'; +import { path } from './path'; -export function checkExt(path: string, ...ext: string[]) +export function checkExt(pth: string, ...ext: string[]) { - if (typeof path !== 'string') + if (typeof pth !== 'string') { return false; } - if (path.length === 0) + if (pth.length === 0) { return false; } @@ -17,7 +17,7 @@ export function checkExt(path: string, ...ext: string[]) return true; } - const pathExtname = extname(path).toLowerCase(); + const pathExtname = path.extname(pth).toLowerCase(); return ext.some((e) => e.toLowerCase() === pathExtname); } diff --git a/packages/core/src/utils/createNewAssetAt.ts b/packages/core/src/utils/createNewAssetAt.ts index 2f08e61..50e0ad4 100644 --- a/packages/core/src/utils/createNewAssetAt.ts +++ b/packages/core/src/utils/createNewAssetAt.ts @@ -1,5 +1,5 @@ -import { joinSafe, relative } from 'upath'; import { Asset } from '../Asset'; +import { path } from './path'; import { stripTags } from './stripTags'; export function createNewAssetAt(asset: Asset, newFileName: string, outputBase?: string, shouldStripTags?: boolean) @@ -29,17 +29,17 @@ function createNewFilePath(asset: Asset, newFileName: string, outputBase?: strin const originalDir = original.directory; - const relativePath = relative(original.rootAsset.path, originalDir); + const relativePath = path.relative(original.rootAsset.path, originalDir); let outputDir: string; if (outputBase) { - outputDir = joinSafe(outputBase, relativePath); + outputDir = path.joinSafe(outputBase, relativePath); } else { - outputDir = joinSafe('.assetpack', asset.transformName, relativePath); + outputDir = path.joinSafe('.assetpack', asset.transformName, relativePath); } if (shouldStripTags) @@ -49,5 +49,5 @@ function createNewFilePath(asset: Asset, newFileName: string, outputBase?: strin newFileName = stripTags(newFileName); } - return joinSafe(outputDir, newFileName); + return path.joinSafe(outputDir, newFileName); } diff --git a/packages/core/src/utils/logAssetGraph.ts b/packages/core/src/utils/logAssetGraph.ts index 0b1436f..6b3810c 100644 --- a/packages/core/src/utils/logAssetGraph.ts +++ b/packages/core/src/utils/logAssetGraph.ts @@ -1,5 +1,5 @@ -import { basename } from 'upath'; import type { Asset } from '../Asset'; +import { path } from './path'; const stateColorMap = { normal: 'white', @@ -22,7 +22,7 @@ const colors = { export function logAssetGraph(asset: Asset, indent = '') { // get file name.. - const baseName = basename(asset.path); + const baseName = path.basename(asset.path); log(`${indent}|- ${baseName}: ${asset.state}`, stateColorMap[asset.state] as keyof typeof colors); diff --git a/packages/core/src/utils/path.ts b/packages/core/src/utils/path.ts index 21efdc1..5eebd75 100644 --- a/packages/core/src/utils/path.ts +++ b/packages/core/src/utils/path.ts @@ -1 +1,2 @@ -export * from 'upath'; +import upath from 'upath'; +export const path = upath; diff --git a/packages/core/src/utils/swapExt.ts b/packages/core/src/utils/swapExt.ts index 937021b..d9a68a2 100644 --- a/packages/core/src/utils/swapExt.ts +++ b/packages/core/src/utils/swapExt.ts @@ -1,16 +1,16 @@ -import { extname } from 'upath'; +import { path } from './path'; /** * Convenience function that Takes a path and swaps the extension * with the new extension provided * - * @param path - The path to swap the extension of + * @param pth - The path to swap the extension of * @param newExt - The new extension to use * @returns - The path with the new extension */ -export function swapExt(path: string, newExt: string) +export function swapExt(pth: string, newExt: string) { - const pathExt = extname(path); + const pathExt = path.extname(pth); - return path.replace(pathExt, newExt); + return pth.replace(pathExt, newExt); } diff --git a/packages/ffmpeg/src/ffmpeg.ts b/packages/ffmpeg/src/ffmpeg.ts index f276684..1153234 100644 --- a/packages/ffmpeg/src/ffmpeg.ts +++ b/packages/ffmpeg/src/ffmpeg.ts @@ -1,5 +1,5 @@ import type { AssetPipe, Asset } from '@play-co/assetpack-core'; -import { checkExt, createNewAssetAt, extname, dirname } from '@play-co/assetpack-core'; +import { checkExt, createNewAssetAt, path } from '@play-co/assetpack-core'; import fluentFfmpeg from 'fluent-ffmpeg'; import ffmpegPath from '@ffmpeg-installer/ffmpeg'; import { copyFileSync, ensureDir } from 'fs-extra'; @@ -64,7 +64,7 @@ async function convert(ffmpegOptions: FfmpegData, input: string, output: string, let hasOutput = false; const command = fluentFfmpeg(); - await ensureDir(dirname(output)); + await ensureDir(path.dirname(output)); // add each format to the command as an output ffmpegOptions.formats.forEach((format) => @@ -108,13 +108,11 @@ async function convert(ffmpegOptions: FfmpegData, input: string, output: string, }); } -let nameIndex = 0; - export function ffmpeg(defaultOptions: FfmpegOptions): AssetPipe { return { folder: false, - name: defaultOptions.name ?? `ffmpeg-${++nameIndex}`, + name: 'ffmpeg', defaultOptions, test(asset: Asset, options) { @@ -128,7 +126,7 @@ export function ffmpeg(defaultOptions: FfmpegOptions): AssetPipe async transform(asset: Asset, options) { // merge options with defaults - const extension = extname(asset.path); + const extension = path.extname(asset.path); const baseFileName = asset.filename.replace(extension, ''); diff --git a/packages/json/src/index.ts b/packages/json/src/index.ts index 721947c..102b882 100644 --- a/packages/json/src/index.ts +++ b/packages/json/src/index.ts @@ -1,5 +1,5 @@ import type { AssetPipe, Asset, PluginOptions } from '@play-co/assetpack-core'; -import { checkExt, createNewAssetAt } from '@play-co/assetpack-core'; +import { checkExt, createNewAssetAt, Logger } from '@play-co/assetpack-core'; export type JsonOptions = PluginOptions<'nc'>; @@ -37,7 +37,8 @@ export function json(_options: JsonOptions = {}): AssetPipe } catch (e) { - // Logger.warn(`[json] Failed to parse json file: ${asset.path}`); + Logger.warn(`[json] Failed to compress json file: ${asset.path}`); + return [asset]; } } diff --git a/packages/manifest/src/pixiManifest.ts b/packages/manifest/src/pixiManifest.ts index a1dae20..75bdeb1 100644 --- a/packages/manifest/src/pixiManifest.ts +++ b/packages/manifest/src/pixiManifest.ts @@ -3,11 +3,7 @@ import { type Asset, type AssetPipe, type PipeSystem, - basename, - joinSafe, - relative, - trimExt, - dirname + path } from '@play-co/assetpack-core'; import { writeJSON } from 'fs-extra'; @@ -44,13 +40,12 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe { - const newFileName = dirname(options.output) === '.' - ? joinSafe(pipeSystem.outputPath, options.output) : options.output; + const newFileName = path.dirname(options.output) === '.' + ? path.joinSafe(pipeSystem.outputPath, options.output) : options.output; const defaultBundle = { name: 'default', @@ -101,16 +96,16 @@ function collectAssets( getTexturePackedAssets(finalAssets).forEach((pages, pageIndex) => { bundleAssets.push({ - alias: getShortNames(stripTags(relative(entryPath, `${asset.path}-${pageIndex}`)), options), - src: pages.map((finalAsset) => relative(outputPath, finalAsset.path)) + alias: getShortNames(stripTags(path.relative(entryPath, `${asset.path}-${pageIndex}`)), options), + src: pages.map((finalAsset) => path.relative(outputPath, finalAsset.path)) }); }); } else { bundleAssets.push({ - alias: getShortNames(stripTags(relative(entryPath, asset.path)), options), - src: finalAssets.map((finalAsset) => relative(outputPath, finalAsset.path)) + alias: getShortNames(stripTags(path.relative(entryPath, asset.path)), options), + src: finalAssets.map((finalAsset) => path.relative(outputPath, finalAsset.path)) }); } } @@ -152,9 +147,9 @@ function getShortNames(name: string, options: PixiManifestOptions) allNames.push(name); /* eslint-disable @typescript-eslint/no-unused-expressions */ - trimExtensions && allNames.push(trimExt(name)); - createShortcuts && allNames.push(basename(name)); - createShortcuts && trimExtensions && allNames.push(trimExt(basename(name))); + trimExtensions && allNames.push(path.trimExt(name)); + createShortcuts && allNames.push(path.basename(name)); + createShortcuts && trimExtensions && allNames.push(path.trimExt(path.basename(name))); /* eslint-enable @typescript-eslint/no-unused-expressions */ return allNames; diff --git a/packages/mipmap-compress/src/mipmapCompress.ts b/packages/mipmap-compress/src/mipmapCompress.ts index 91f913d..752bb2a 100644 --- a/packages/mipmap-compress/src/mipmapCompress.ts +++ b/packages/mipmap-compress/src/mipmapCompress.ts @@ -91,7 +91,7 @@ export function mipmapCompress(_options: MipmapCompressOptions = {}): AssetPipe< return { folder: true, - name: 'mip-compress', + name: 'mipmap-compress', defaultOptions, test(asset: Asset, options) { diff --git a/packages/mipmap-compress/src/spineAtlasMipmap.ts b/packages/mipmap-compress/src/spineAtlasMipmap.ts index ed9fbef..12507dc 100644 --- a/packages/mipmap-compress/src/spineAtlasMipmap.ts +++ b/packages/mipmap-compress/src/spineAtlasMipmap.ts @@ -19,7 +19,7 @@ export function spineAtlasMipmap(_options?: SpineOptions): AssetPipe) { @@ -53,7 +54,7 @@ export async function createTextureData(options: Required) let result = await sharpImage.toBuffer({ resolveWithObject: true }).catch((error) => { // eslint-disable-next-line max-len - console.warn(`[Assetpack][packTextures] Failed to process texture: ${texture.path} - ${error}, using empty pixel texture instead.`); + Logger.warn(`[Assetpack][packTextures] Failed to process texture: ${texture.path} - ${error}, using empty pixel texture instead.`); return { data: null, info: null }; }); diff --git a/packages/texture-packer/src/texturePacker.ts b/packages/texture-packer/src/texturePacker.ts index 6823e9e..5535fb0 100644 --- a/packages/texture-packer/src/texturePacker.ts +++ b/packages/texture-packer/src/texturePacker.ts @@ -1,5 +1,5 @@ import type { PluginOptions, Asset, AssetPipe } from '@play-co/assetpack-core'; -import { createNewAssetAt, stripTags, relative } from '@play-co/assetpack-core'; +import { createNewAssetAt, stripTags, path, Logger } from '@play-co/assetpack-core'; import { readFile } from 'fs-extra'; import glob from 'glob-promise'; import type { PackTexturesOptions, TexturePackerFormat } from './packer/packTextures'; @@ -42,7 +42,7 @@ function checkForTexturePackerShortcutClashes( if (clashes.length > 0) { // eslint-disable-next-line max-len - console.warn(`[Assetpack][texturePacker] Texture Packer Shortcut clash detected for between ${clashes.join(', ')}. This means that 'nameStyle' is set to 'short' and different sprite sheets have frames that share the same name. Please either rename the files or set 'nameStyle' in the texture packer options to 'relative'`); + Logger.warn(`[Assetpack][texturePacker] Texture Packer Shortcut clash detected for between ${clashes.join(', ')}. This means that 'nameStyle' is set to 'short' and different sprite sheets have frames that share the same name. Please either rename the files or set 'nameStyle' in the texture packer options to 'relative'`); } } @@ -113,7 +113,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe