Skip to content

Commit

Permalink
fix(cli): fix broken --album on Windows.
Browse files Browse the repository at this point in the history
Extract folder names via system function to avoid the difference between / and \ on Windows.
  • Loading branch information
fky2015 committed Jun 25, 2024
1 parent 5912fcc commit b41de15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
19 changes: 19 additions & 0 deletions cli/src/commands/asset.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { platform } from 'node:os';
import { UploadOptionsDto, getAlbumName } from 'src/commands/asset';
import { describe, expect, it } from 'vitest';

describe('Unit function tests', () => {
it('should return a non-undefined value', () => {
if (platform() === 'win32') {
// This is meaningless for Unix systems.
expect(getAlbumName(String.raw`D:\test\Filename.txt`, {} as UploadOptionsDto)).toBe('test');
}
expect(getAlbumName('D:/parentfolder/test/Filename.txt', {} as UploadOptionsDto)).toBe('test');
});

it('has higher priority to return `albumName` in `options`', () => {
expect(getAlbumName('/parentfolder/test/Filename.txt', { albumName: 'example' } as UploadOptionsDto)).toBe(
'example',
);
});
});
11 changes: 6 additions & 5 deletions cli/src/commands/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Presets, SingleBar } from 'cli-progress';
import { chunk } from 'lodash-es';
import { Stats, createReadStream } from 'node:fs';
import { stat, unlink } from 'node:fs/promises';
import os from 'node:os';
import path, { basename } from 'node:path';
import { BaseOptions, authenticate, crawl, sha1 } from 'src/utils';

Expand All @@ -25,7 +24,7 @@ const s = (count: number) => (count === 1 ? '' : 's');
type AssetBulkUploadCheckResults = Array<AssetBulkUploadCheckResult & { id: string }>;
type Asset = { id: string; filepath: string };

interface UploadOptionsDto {
export interface UploadOptionsDto {
recursive?: boolean;
ignore?: string;
dryRun?: boolean;
Expand Down Expand Up @@ -346,7 +345,9 @@ const updateAlbums = async (assets: Asset[], options: UploadOptionsDto) => {
}
};

const getAlbumName = (filepath: string, options: UploadOptionsDto) => {
const folderName = os.platform() === 'win32' ? filepath.split('\\').at(-2) : filepath.split('/').at(-2);
return options.albumName ?? folderName;
// `filepath` valid format:
// - Windows: `D:\\test\\Filename.txt` or `D:/test/Filename.txt`
// - Unix: `/test/Filename.txt`
export const getAlbumName = (filepath: string, options: UploadOptionsDto) => {
return options.albumName ?? path.basename(path.dirname(filepath));
};

0 comments on commit b41de15

Please sign in to comment.