Skip to content

Commit

Permalink
refactor(cli): code splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Jul 25, 2022
1 parent 88084fa commit 1e26ab6
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 271 deletions.
19 changes: 4 additions & 15 deletions bin/build-series.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
#!/usr/bin/env node

import { _lazyImportCore } from '../lib/util/import';
import { unzipLang } from '../lib/build/unzip';
import { basename } from 'upath2';
import { convertLang } from '../lib/build/to-zht';
import { replaceProperties } from '../lib/build/properties-replace';
import { packPluginJar } from '../lib/build/pack-plugin-jar';
import { _handleBuild } from '../lib/cli/handle-build';

export default _lazyImportCore(import('./download-series'))
.then(async (result) => {

const name = result.name ?? basename(result.file, '.zip');

await unzipLang(name);
await convertLang(name);
await replaceProperties(name);
await packPluginJar(name);

})
.then(_handleBuild)
;
261 changes: 5 additions & 256 deletions bin/download-series.ts
Original file line number Diff line number Diff line change
@@ -1,260 +1,9 @@
#!/usr/bin/env node

import { cliSelectSeries, cliSelectVersion } from '../lib/cli/version-map';
import { cli_logger } from '../lib/cli-progress';
import { downloadPlugin, generateDownloadMessage } from '../lib/util/download-plugin';
import { basename, join } from 'upath2';
import { __plugin_downloaded_dir } from '../lib/const';
import {
_getVersion,
_getVersionDownloadByVersion,
_getVersionInfoBySeries,
_getVersionInfoByVersion,
getLatestSeries,
} from '../lib/util/version-map';
import { console, chalkByConsole } from 'debug-color2';
import { copy, copyFile, pathExists } from 'fs-extra';
import { prompt } from 'enquirer';
import yargs from 'yargs';
import { argvDownload } from '../lib/cli/argv-download';
import { _handleArgv, _handleArgvResult } from '../lib/cli/handle-argv';

export default yargs
.version(false)
.option('series', {
alias: ['s'],
desc: `IDE 版本系列`,
string: true,
})
.option('version', {
alias: ['v'],
desc: `IDE 版本`,
string: true,
})
.option('force', {
alias: ['f'],
desc: `忽略已存在下載檔案`,
boolean: true,
})
.option('all', {
alias: ['A'],
desc: `額外顯示非該系列最新版本`,
boolean: true,
})
.option('source', {
boolean: true,
})
.option('slient', {
boolean: true,
})
.parseAsync()
.then(async (argv) =>
{
let {
series,
force,
version,
source,
slient,
} = argv;

if (version?.length)
{
if (_getVersionDownloadByVersion(version)?.length)
{
series = void 0
}
else
{
if (slient)
{
throw new RangeError(`目標版本 ${version} 不存在!`)
}

version = void 0;
}
}

if (series?.length)
{
if (series === 'latest')
{
series = getLatestSeries();
}
else if (!_getVersion(series)?.length)
{
if (slient)
{
throw new RangeError(`目標系列 ${series} 不存在!`)
}

series = void 0
}
}

if (!series?.length)
{
if (slient)
{
throw new RangeError(`請指定版本或系列!`)
}

const all = argv.all ?? await prompt<{
all: boolean,
}>({
name: 'all',
type: 'confirm',
message: chalkByConsole((chalk) =>
{
return chalk.red(`是否額外顯示非該系列最新版本?`)
}, console),
}).then(result =>
{
return result.all;
});

return (all ? cliSelectVersion : cliSelectSeries)()
.then(result =>
{
return {
...result,
force,
source,
slient,
}
})
}

return {
series,
version,
force,
source,
slient,
}
})
.then((result: {
series?: string,
version?: string,
force: boolean,
source: boolean,
slient: boolean,
}) =>
{
let {
series,
version,
force,
} = result;

if (version?.length)
{
series = void 0;
}
else
{
version = void 0;
}

if (!series?.length)
{
series = void 0;
}
else
{
version = void 0;
}

return {
...result,
series,
version,
force,
}
})
.then(async (result) =>
{
const {
series,
version,
} = result;

const info = version?.length ? _getVersionInfoByVersion(version) : _getVersionInfoBySeries(series);

const link = _getVersionDownloadByVersion(info.version);

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

const ret = {
series,
version: info.version,
link,
info,
file,
};

let bool = await pathExists(file);

if (bool && typeof result.force === 'boolean')
{
bool = !result.force;
console.warn(chalkByConsole((chalk) =>
{
return `檔案 ${chalk.cyan(basename(file))} 已經存在,但將強制下載`
}, console))
}
else if (bool && !result.slient)
{
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.log(msg);
}
else
{
console.info(link);

await cli_logger(downloadPlugin(link, file, true), msg)
}

let name: string;

if (result.source)
{
name = 'zh';

let target = join(__plugin_downloaded_dir, `${name}.zip`);

console.warn(chalkByConsole((chalk) =>
{
return `複製 ${chalk.cyan(basename(file))}\n  => ${chalk.cyan(basename(target))}`
}, console))

await copy(ret.file, target, {
overwrite: true,
errorOnExist: false,
preserveTimestamps: true,
dereference: true,
})
}

return {
...ret,
name,
}
})
export default argvDownload()
.then(_handleArgv)
.then(_handleArgvResult)
;
36 changes: 36 additions & 0 deletions lib/cli/argv-download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import yargs from 'yargs';
import isInteractive from 'is-interactive';

export function argvDownload()
{
return yargs
.version(false)
.option('series', {
alias: ['s'],
desc: `IDE 版本系列`,
string: true,
})
.option('version', {
alias: ['v'],
desc: `IDE 版本`,
string: true,
})
.option('force', {
alias: ['f'],
desc: `忽略已存在下載檔案`,
boolean: true,
})
.option('all', {
alias: ['A'],
desc: `額外顯示非該系列最新版本`,
boolean: true,
})
.option('source', {
boolean: true,
})
.option('disable-interactive', {
boolean: true,
default: !isInteractive(),
})
.parseAsync()
}
Loading

0 comments on commit 1e26ab6

Please sign in to comment.