Skip to content

Commit

Permalink
Fix: names for plugins (#7)
Browse files Browse the repository at this point in the history
* Fix: names for plugins

* Fix: paths and logger
  • Loading branch information
Zyie authored Apr 19, 2024
1 parent 37edac2 commit d7c1e53
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 82 deletions.
6 changes: 3 additions & 3 deletions packages/cache-buster/test/cacheBuster.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
});
13 changes: 7 additions & 6 deletions packages/core/src/Asset.ts
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/AssetCache.ts
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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 });
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/AssetIgnore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import minimatch from 'minimatch';
import { relative } from 'upath';
import { path } from './utils/path';

export interface AssetIgnoreOptions
{
Expand Down Expand Up @@ -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++)
{
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
10 changes: 5 additions & 5 deletions packages/core/src/AssetWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 === '.')
{
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class LoggerClass

public info(message: string)
{
return;
this.report({
type: 'log',
level: 'info',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/applySettingToAsset.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/utils/checkExt.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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);
}
10 changes: 5 additions & 5 deletions packages/core/src/utils/createNewAssetAt.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -49,5 +49,5 @@ function createNewFilePath(asset: Asset, newFileName: string, outputBase?: strin
newFileName = stripTags(newFileName);
}

return joinSafe(outputDir, newFileName);
return path.joinSafe(outputDir, newFileName);
}
4 changes: 2 additions & 2 deletions packages/core/src/utils/logAssetGraph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { basename } from 'upath';
import type { Asset } from '../Asset';
import { path } from './path';

const stateColorMap = {
normal: 'white',
Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from 'upath';
import upath from 'upath';
export const path = upath;
10 changes: 5 additions & 5 deletions packages/core/src/utils/swapExt.ts
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 4 additions & 6 deletions packages/ffmpeg/src/ffmpeg.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -108,13 +108,11 @@ async function convert(ffmpegOptions: FfmpegData, input: string, output: string,
});
}

let nameIndex = 0;

export function ffmpeg(defaultOptions: FfmpegOptions): AssetPipe<FfmpegOptions>
{
return {
folder: false,
name: defaultOptions.name ?? `ffmpeg-${++nameIndex}`,
name: 'ffmpeg',
defaultOptions,
test(asset: Asset, options)
{
Expand All @@ -128,7 +126,7 @@ export function ffmpeg(defaultOptions: FfmpegOptions): AssetPipe<FfmpegOptions>
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, '');

Expand Down
5 changes: 3 additions & 2 deletions packages/json/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'>;

Expand Down Expand Up @@ -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];
}
}
Expand Down
Loading

0 comments on commit d7c1e53

Please sign in to comment.