Skip to content

Commit

Permalink
Refactor cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed May 6, 2022
1 parent 57f8392 commit f3ae9b3
Show file tree
Hide file tree
Showing 19 changed files with 505 additions and 231 deletions.
23 changes: 21 additions & 2 deletions dist/cli/batch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runBatchTagger = void 0;
const fs_1 = require("fs");
Expand All @@ -11,8 +30,8 @@ const runBatchTagger = async (folder) => {
.filter(dir => dir.isDirectory())
.map(dir => dir.name);
const albumCount = albums.length;
const { CliTagger } = await Promise.resolve().then(() => require('./tagger'));
const ora = await Promise.resolve().then(() => require('ora'));
const { CliTagger } = await Promise.resolve().then(() => __importStar(require('./tagger')));
const { default: ora } = await Promise.resolve().then(() => __importStar(require('ora')));
for (let index = 0; index < albumCount; index++) {
try {
const album = (0, default_album_name_1.getDefaultAlbumName)(albums[index]);
Expand Down
25 changes: 22 additions & 3 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const options_1 = require("./options");
const readline_1 = require("../core/readline");
const default_album_name_1 = require("./default-album-name");
let spinner;
const runTagger = async (album) => {
const ora = await Promise.resolve().then(() => require('ora'));
const { default: ora } = await Promise.resolve().then(() => __importStar(require('ora')));
if (!spinner) {
spinner = ora({
text: '搜索中',
Expand All @@ -16,14 +35,14 @@ const runTagger = async (album) => {
}
}).start();
}
const { CliTagger } = await Promise.resolve().then(() => require('./tagger'));
const { CliTagger } = await Promise.resolve().then(() => __importStar(require('./tagger')));
const tagger = new CliTagger(options_1.cliOptions, options_1.metadataConfig, spinner);
await tagger.run(album);
process.exit();
};
const defaultAlbumName = (0, default_album_name_1.getDefaultAlbumName)();
if (options_1.cliOptions.batch) {
Promise.resolve().then(() => require('./batch')).then(({ runBatchTagger }) => {
Promise.resolve().then(() => __importStar(require('./batch'))).then(({ runBatchTagger }) => {
runBatchTagger(options_1.cliOptions.batch);
});
}
Expand Down
117 changes: 93 additions & 24 deletions dist/cli/options.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,114 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.metadataConfig = exports.lyricConfig = exports.cliOptions = void 0;
const commandLineArgs = require("command-line-args");
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const core_config_1 = require("../core/core-config");
const debug_1 = require("../core/debug");
const config_file_1 = require("./config-file");
const options = commandLineArgs([
{ name: 'cover', alias: 'c', type: Boolean, defaultValue: false },
{ name: 'debug', alias: 'd', type: Boolean, defaultValue: false },
{ name: 'source', alias: 's', type: String, defaultValue: 'thb-wiki' },
{ name: 'lyric', alias: 'l', type: Boolean, defaultValue: false },
{ name: 'batch', alias: 'b', type: String, defaultValue: '' },
{ name: 'separator', type: String, defaultValue: core_config_1.DefaultMetadataSeparator },
{ name: 'timeout', type: Number, defaultValue: 30 },
{ name: 'retry', type: Number, defaultValue: 3 },
{ name: 'lyric-type', alias: 't', type: String },
{ name: 'lyric-output', alias: 'o', type: String },
{ name: 'no-lyric-time', alias: 'T', type: Boolean, defaultValue: false },
{ name: 'no-interactive', alias: 'I', type: Boolean, defaultValue: false },
]);
const options = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
.option('cover', {
alias: 'c',
type: 'boolean',
default: false,
description: '是否将封面保存为独立文件',
})
.option('debug', {
alias: 'd',
type: 'boolean',
default: false,
description: '是否启用调试模式, 输出更杂碎的日志',
})
.option('source', {
alias: 's',
type: 'string',
default: 'thb-wiki',
choices: ['thb-wiki', 'local-mp3', 'local-json'],
description: '设置数据源',
})
.option('lyric', {
alias: 'l',
type: 'boolean',
default: false,
description: '是否启用歌词写入 (会增加运行时间)',
})
.option('lyric-type', {
alias: 'lt',
type: 'string',
default: 'original',
choices: ['original', 'translated', 'mixed'],
description: '歌词类型, 可以选择原文/译文/混合模式',
})
.option('lyric-output', {
alias: 'lo',
type: 'string',
default: 'metadata',
choices: ['metadata', 'lrc'],
description: '歌词输出方式, 可以选择写入歌曲元数据或者保存为 lrc 文件',
})
.option('translation-separator', {
alias: 'ts',
type: 'string',
default: ' // ',
description: '指定混合歌词模式下, 使用的分隔符',
})
.option('lyric-time', {
alias: 'lt',
type: 'boolean',
default: true,
description: '是否启用歌词时轴',
})
.option('batch', {
alias: 'b',
type: 'string',
description: '是否使用批量模式, 参数为开始批量运行的路径',
})
.option('separator', {
type: 'string',
default: core_config_1.DefaultMetadataSeparator,
description: '指定 mp3 元数据的分隔符',
})
.option('timeout', {
type: 'number',
default: 30,
description: '指定一次运行的超时时间',
})
.option('retry', {
type: 'number',
default: 3,
description: '指定超时后自动重试的最大次数',
})
.option('interactive', {
alias: 'i',
type: 'boolean',
default: true,
description: '是否允许交互',
})
.parseSync();
(0, debug_1.setDebug)(options.debug);
const configFile = (0, config_file_1.loadConfigFile)();
if (configFile !== null) {
(0, debug_1.log)('config file: ', configFile);
const { lyric, ...restConfig } = configFile;
if (lyric !== undefined) {
if (options['lyric-output'] === undefined) {
options['lyric-output'] = lyric.output;
if (options.lyricOutput === undefined) {
options.lyricOutput = lyric.output;
}
if (options['lyric-type'] === undefined) {
options['lyric-type'] = lyric.type;
if (options.lyricType === undefined) {
options.lyricType = lyric.type;
}
options['translation-separator'] = lyric.translationSeparator;
options.translationSeparator = lyric.translationSeparator;
}
Object.assign(options, restConfig);
}
const lyric = {
type: options['lyric-type'] || 'original',
output: options['lyric-output'] || 'metadata',
time: !options['no-lyric-time'],
translationSeparator: options['translation-separator'] || ' // '
type: options.lyricType,
output: options.lyricOutput,
time: options.lyricTime,
translationSeparator: options.translationSeparator,
};
const metadata = {
lyric: options.lyric ? lyric : undefined,
Expand Down
35 changes: 27 additions & 8 deletions dist/cli/tagger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliTagger = void 0;
const fs_1 = require("fs");
Expand Down Expand Up @@ -55,20 +74,20 @@ class CliTagger {
const json = (0, fs_1.readFileSync)((0, path_1.resolve)(this.workingDir, localMetadata), { encoding: 'utf8' });
(0, debug_1.log)('localJson get');
(0, debug_1.log)(json);
const { localJson } = await Promise.resolve().then(() => require('../core/metadata/local-json/local-json'));
const { localJson } = await Promise.resolve().then(() => __importStar(require('../core/metadata/local-json/local-json')));
return localJson.normalize(JSON.parse(json), await this.getLocalCover());
}
async downloadMetadata(album, cover) {
const { sourceMappings } = await Promise.resolve().then(() => require(`../core/metadata/source-mappings`));
const { sourceMappings } = await Promise.resolve().then(() => __importStar(require(`../core/metadata/source-mappings`)));
const metadataSource = sourceMappings[this.cliOptions.source];
metadataSource.config = this.metadataConfig;
this.metadataSource = metadataSource;
return await this.metadataSource.getMetadata(album, cover);
}
async createFiles(metadata) {
const { readdirSync, renameSync } = await Promise.resolve().then(() => require('fs'));
const { dirname } = await Promise.resolve().then(() => require('path'));
const { writerMappings } = await Promise.resolve().then(() => require('../core/writer/writer-mappings'));
const { readdirSync, renameSync } = await Promise.resolve().then(() => __importStar(require('fs')));
const { dirname } = await Promise.resolve().then(() => __importStar(require('path')));
const { writerMappings } = await Promise.resolve().then(() => __importStar(require('../core/writer/writer-mappings')));
const fileTypes = Object.keys(writerMappings);
const fileTypeFilter = (file) => fileTypes.some(type => file.endsWith(type));
const dir = readdirSync(this.workingDir).sort(leadingNumberSort);
Expand Down Expand Up @@ -100,7 +119,7 @@ class CliTagger {
return targetFiles;
}
async writeMetadataToFile(metadata, targetFiles) {
const { writerMappings } = await Promise.resolve().then(() => require('../core/writer/writer-mappings'));
const { writerMappings } = await Promise.resolve().then(() => __importStar(require('../core/writer/writer-mappings')));
for (let i = 0; i < targetFiles.length; i++) {
const file = targetFiles[i];
(0, debug_1.log)(file);
Expand All @@ -120,7 +139,7 @@ class CliTagger {
// }))
const coverBuffer = metadata[0].coverImage;
if (this.cliOptions.cover && coverBuffer) {
const imageType = await Promise.resolve().then(() => require('image-type'));
const { default: imageType } = await Promise.resolve().then(() => __importStar(require('image-type')));
const type = imageType(coverBuffer);
if (type !== null) {
const coverFilename = (0, path_1.resolve)(this.workingDir, `cover.${type.ext}`);
Expand Down Expand Up @@ -183,7 +202,7 @@ class CliTagger {
});
}
async run(album) {
const { sourceMappings } = await Promise.resolve().then(() => require(`../core/metadata/source-mappings`));
const { sourceMappings } = await Promise.resolve().then(() => __importStar(require(`../core/metadata/source-mappings`)));
const metadataSource = sourceMappings[this.cliOptions.source];
const noInteractive = this.cliOptions['no-interactive'];
if (!metadataSource) {
Expand Down
23 changes: 21 additions & 2 deletions dist/core/exists.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePath = void 0;
const resolvePath = async (path) => {
const { resolve } = await Promise.resolve().then(() => require('path'));
const { existsSync } = await Promise.resolve().then(() => require('fs'));
const { resolve } = await Promise.resolve().then(() => __importStar(require('path')));
const { existsSync } = await Promise.resolve().then(() => __importStar(require('fs')));
const localSourcePath = resolve(path).replace(/\\/g, '/');
if (!existsSync(localSourcePath)) {
throw new Error('路径不存在');
Expand Down
5 changes: 4 additions & 1 deletion dist/core/metadata/local-json/local-json.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.localJson = exports.LocalJson = void 0;
const metadata_source_1 = require("../metadata-source");
const fs_1 = require("fs");
const exists_1 = require("../../exists");
const axios_1 = require("axios");
const axios_1 = __importDefault(require("axios"));
class LocalJson extends metadata_source_1.MetadataSource {
/**
* Normalize JSON data from file:
Expand Down
23 changes: 21 additions & 2 deletions dist/core/metadata/local-mp3/local-mp3.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.localMp3 = exports.LocalMp3 = void 0;
const metadata_source_1 = require("../metadata-source");
const fs_1 = require("fs");
const exists_1 = require("../../exists");
const proxy_1 = require("../../proxy");
const id3 = require("../../node-id3");
const id3 = __importStar(require("../../node-id3"));
const dirFilter = (path, predicate) => {
return (0, fs_1.readdirSync)(path, { withFileTypes: true })
.filter(predicate)
Expand All @@ -16,7 +35,7 @@ class LocalMp3 extends metadata_source_1.MetadataSource {
return (0, exists_1.resolvePath)(localSource);
}
async getMultipleDiscFiles(path) {
const { join } = await Promise.resolve().then(() => require('path'));
const { join } = await Promise.resolve().then(() => __importStar(require('path')));
const subFolders = dirFilter(path, it => it.isDirectory() && /^Disc (\d+)/.test(it.name));
const mp3Filter = (it) => it.isFile() && it.name.endsWith('.mp3');
if (subFolders.length > 0) {
Expand Down
5 changes: 4 additions & 1 deletion dist/core/metadata/thb-wiki/lyrics/thb-wiki-lyrics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadLyrics = void 0;
const axios_1 = require("axios");
const axios_1 = __importDefault(require("axios"));
const jsdom_1 = require("jsdom");
const debug_1 = require("../../../debug");
const lyric_parser_1 = require("./lyric-parser");
Expand Down
Loading

0 comments on commit f3ae9b3

Please sign in to comment.