-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
272 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { CliOptions } from './options' | ||
|
||
export abstract class CliCommandBase { | ||
workingDir = '.' | ||
constructor(public cliOptions: CliOptions) {} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Metadata } from '../core' | ||
import { log } from '../core/debug' | ||
import { simplifyMetadataInfo } from '../core/metadata/normalize/normalize' | ||
import { CliCommandBase } from './command-base' | ||
import { CliOptions, metadataConfig } from './options' | ||
|
||
const handleBufferStringify = (key: string, value: any) => { | ||
const isBuffer = typeof value === 'object' && value !== null && value.type === 'Buffer' | ||
if (isBuffer) { | ||
return `<Buffer length=${value.data?.length ?? 0}>` | ||
} | ||
return value | ||
} | ||
const dumpCover = async (metadatas: Metadata[]) => { | ||
const { writeFileSync } = await import('fs') | ||
const { resolve } = await import('path') | ||
const metadata = metadatas.find(m => m.coverImage) | ||
if (!metadata) { | ||
return | ||
} | ||
const { default: imageType } = await import('image-type') | ||
const type = imageType(metadata.coverImage) | ||
if (!type) { | ||
return | ||
} | ||
const coverFilename = resolve(process.cwd(), `cover.${type.ext}`) | ||
log('cover file', coverFilename) | ||
writeFileSync(coverFilename, metadata.coverImage) | ||
} | ||
|
||
export class CliDumper extends CliCommandBase { | ||
constructor(cliOptions: CliOptions) { | ||
super(cliOptions) | ||
} | ||
|
||
async run() { | ||
const { glob } = await import('glob') | ||
const { extname, resolve } = await import('path') | ||
const { writeFileSync, readFileSync } = await import('fs') | ||
const { readerMappings } = await import('../core/reader/reader-mappings') | ||
const globTypes = Object.keys(readerMappings) | ||
.map(readerType => readerType.replace(/^\./, '')) | ||
.join('|') | ||
const files = ( | ||
await glob(`./**/*.@(${globTypes})`, { posix: true, cwd: this.workingDir }) | ||
).sort() | ||
log({ globTypes }) | ||
log(files) | ||
if (files.length === 0) { | ||
console.log('没有找到能够提取的音乐文件') | ||
return | ||
} | ||
const results: { metadata: Metadata; rawTag: any }[] = await Promise.all( | ||
files.map(async file => { | ||
const type = extname(file) | ||
const reader = readerMappings[type] | ||
reader.config = metadataConfig | ||
const buffer = readFileSync(resolve(this.workingDir, file)) | ||
const rawTag = await reader.readRaw(buffer) | ||
const metadata = await reader.read(rawTag) | ||
return { | ||
rawTag, | ||
metadata, | ||
} | ||
}), | ||
) | ||
|
||
const metadatas = results.map(it => it.metadata) | ||
const rawTags = results.map(it => it.rawTag) | ||
await simplifyMetadataInfo({ | ||
metadatas, | ||
}) | ||
writeFileSync( | ||
resolve(this.workingDir, 'metadata.json'), | ||
JSON.stringify( | ||
metadatas.map(({ coverImage, ...restParts }) => { | ||
return restParts | ||
}), | ||
undefined, | ||
2, | ||
), | ||
) | ||
if (this.cliOptions.debug) { | ||
writeFileSync( | ||
resolve(this.workingDir, 'metadata.debug.json'), | ||
JSON.stringify(rawTags, handleBufferStringify, 2), | ||
) | ||
} | ||
if (this.cliOptions.cover) { | ||
await dumpCover(metadatas) | ||
} | ||
} | ||
} |
Oops, something went wrong.