Skip to content

Commit

Permalink
feat: 改善 cli 互動訊息
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Feb 16, 2022
1 parent f9edbb4 commit ce526c3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
28 changes: 23 additions & 5 deletions bin/download-series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { cliSelectSeries } from '../lib/cli/version-map';
import { cli_logger } from '../lib/cli-progress';
import { downloadPlugin, generateDownloadMessage } from '../lib/util/download-plugin';
import { join } from 'upath2';
import { basename, join } from 'upath2';
import { __plugin_downloaded_dir } from '../lib/const';
import {
_getVersion,
Expand All @@ -13,8 +13,9 @@ import {
} from '../lib/util/version-map';
import { console, chalkByConsole } from 'debug-color2';
import { pathExists } from 'fs-extra';
import { prompt } from 'enquirer';

cliSelectSeries()
export default cliSelectSeries()
.then(async (result) =>
{
const series = result.series;
Expand All @@ -23,18 +24,35 @@ cliSelectSeries()

const file = join(__plugin_downloaded_dir, `zh-${info.version}.zip`);

const bool = await pathExists(file);
let bool = await pathExists(file);

if (bool)
{
await prompt<{
force: boolean,
}>({
name: 'force',
type: 'confirm',
message: chalkByConsole((chalk) =>
{
return chalk.red(`檔案 ${chalk.cyan(basename(file))} 已經存在,是否強制下載?`)
}, console),
}).then(result =>
{
bool = !result.force;
});
}

const msg = generateDownloadMessage(info, !bool);

if (bool)
{
console.warn(`檔案已經存在,忽略下載`);
console.log(msg);
return
}

console.info(link);

return cli_logger(downloadPlugin(link, file), msg)
return cli_logger(downloadPlugin(link, file, true), msg)
})
;
2 changes: 1 addition & 1 deletion lib/cli/version-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function cliSelectSeries()
}>({
name: 'series',
type: 'select',
message: 'Select IDE series?',
message: '請選擇 IDE 版本系列:',
choices: _getSeries().map(value =>
{
const version = _getVersion(value);
Expand Down
20 changes: 18 additions & 2 deletions lib/util/download-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import fetch from 'cross-fetch';
import { Response } from 'node-fetch';
import { outputFile } from 'fs-extra';
import { outputFile, pathExists } from 'fs-extra';
import { generateDownloadLink } from './version-map';
import { IVersionApiResultRow } from '../const/version-map';
import { console, chalkByConsole } from 'debug-color2';
import { basename } from 'upath2';

export function downloadPlugin(link: string, output_file: string)
export async function downloadPlugin(link: string, output_file: string, force?: boolean)
{
if (!force)
{
let name = basename(output_file);

if (/^\w+-\d+/i.test(name))
{
const bool = await pathExists(output_file);

if (bool)
{
return;
}
}
}

return fetch(link)
.then((res) => (res as any as Response).buffer())
.then(buf =>
Expand Down

0 comments on commit ce526c3

Please sign in to comment.