Skip to content

Commit

Permalink
feat: update progress style
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Jan 9, 2022
1 parent 7564b5c commit 1fbed03
Show file tree
Hide file tree
Showing 7 changed files with 9,592 additions and 30 deletions.
20 changes: 19 additions & 1 deletion lib/cli-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { ensureDirSync } from 'fs-extra';
import { join } from 'path';
// @ts-ignore
import progressEstimator from 'progress-estimator';
import { findPkgModuleCachePathCore, findPkgModulePathCore } from 'cache-path/lib/finder/findPkgModuleCachePath';
import { findPkgModuleCachePathCore } from 'cache-path/lib/finder/findPkgModuleCachePath';
import { __root } from '../test/__root';
import { MultiBar, Presets } from 'cli-progress';
import { gray, green, yellow } from 'ansi-colors';

export function createProgressEstimator(root: string)
{
Expand All @@ -15,4 +17,20 @@ export function createProgressEstimator(root: string)
});
}

export function createMultiBar()
{
return new MultiBar({
clearOnComplete: false,
hideCursor: true,

barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',

format: `${green('{bar}')} ${yellow('{percentage}%')} | ${gray('{duration_formatted}')} | {filename}`,

barsize: 20,

}, Presets.shades_classic);
}

export const cli_logger = createProgressEstimator(__root);
9,482 changes: 9,482 additions & 0 deletions original-plugin-raw/zh.list.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "bluelovers",
"main": "index.js",
"scripts": {
"plugin:handle-source": "ts-node scripts/plugin-handle-source.ts",
"review": "yarn run review:coverage",
"review:coverage": "yarn run lint && yarn run coverage",
"review:test": "yarn run lint && yarn run test",
Expand Down Expand Up @@ -55,8 +56,10 @@
"@bluelovers/fast-glob": "^3.0.4",
"@lazy-cjk/zh-convert": "^1.0.42",
"@yarn-tool/find-root": "^1.0.60",
"ansi-colors": "^4.1.1",
"bluebird": "^3.7.2",
"cache-path": "^2.0.29",
"cli-progress": "^3.10.0",
"crlf-normalize": "^1.0.6",
"cross-fetch": "^3.1.4",
"debug-color2": "^1.2.7",
Expand All @@ -70,6 +73,7 @@
"devDependencies": {
"@bluelovers/tsconfig": "^1.0.28",
"@types/bluebird": "^3.5.36",
"@types/cli-progress": "^3.9.2",
"@types/fs-extra": "^9.0.13",
"@types/node": "*",
"@types/node-fetch": "^2.5.12"
Expand Down
8 changes: 4 additions & 4 deletions scripts/download-original-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import fetch from 'cross-fetch';
import { Response } from 'node-fetch';
import { outputFile } from 'fs-extra';
import { join } from 'upath2';
import { __root } from '../test/__root';
import { cli_logger, createProgressEstimator } from '../lib/cli-progress';
import { cli_logger } from '../lib/cli-progress';
import { __plugin_downloaded_dir } from '../lib/const';

cli_logger(fetch('https://plugins.jetbrains.com/plugin/download?rel=true&updateId=149295')
export default cli_logger(fetch('https://plugins.jetbrains.com/plugin/download?rel=true&updateId=149295')
.then((res) => (res as any as Response).buffer())
.then(buf => {
.then(buf =>
{
return outputFile(join(__plugin_downloaded_dir, 'zh.zip'), buf)
}), `download Chinese ​(Simplified)​ Language Pack / 中文语言包`)
;
8 changes: 8 additions & 0 deletions scripts/plugin-handle-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Created by user on 2022/1/10.
*/

export default import('./download-original-plugin')
.then(() => import('./unzip'))
.then(() => import('./to-zht'))
;
32 changes: 24 additions & 8 deletions scripts/to-zht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@
* Created by user on 2022/1/9.
*/

import { cn2tw_min } from '@lazy-cjk/zh-convert/min';
import { async as FastGlob } from '@bluelovers/fast-glob/bluebird';
import { __plugin_dev_dir, __plugin_downloaded_dir_unzip } from '../lib/const';
import { outputFile, pathExists, readFile, unlink } from 'fs-extra';
import { join } from 'upath2';
import { console } from 'debug-color2';
import { cli_logger } from '../lib/cli-progress';
import { createMultiBar } from '../lib/cli-progress';
import { processText } from 'novel-segment-cli';
import { chkcrlf, CR, LF, CRLF } from 'crlf-normalize';
import { chkcrlf, CR, CRLF, LF } from 'crlf-normalize';
import { SingleBar } from 'cli-progress';

cli_logger(FastGlob([
const multibar = createMultiBar();
let bar: SingleBar;

export default FastGlob([
'**/*',
'!*.list.json',
], {
cwd: __plugin_downloaded_dir_unzip,
})
.each(async (file: string) =>
.tap((ls) =>
{
console.cyan.log(`convert to zht`);
bar = multibar.create(ls.length, 0);
})
.each(async (file: string, index) =>
{
bar.update(index + 1, { filename: file });
let fullpath_new = join(__plugin_dev_dir, file);

if (!/\.(png)$|MANIFEST\.MF/i.test(file))
Expand All @@ -40,18 +50,24 @@ cli_logger(FastGlob([

if (content_new !== content_old)
{
console.success(file);
//console.success(file);
await outputFile(fullpath_new, content_new);
return;
}
}

if (await pathExists(fullpath_new))
{
console.warn(file);
//console.warn(file);
return unlink(fullpath_new);
}
}), `convert to zht`)
})
.finally(() =>
{
bar?.update(bar.getTotal());
bar?.stop();
multibar.stop();
})
;


68 changes: 51 additions & 17 deletions scripts/unzip.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,66 @@
import JSZip from "jszip";
import { outputFile, readFile } from 'fs-extra';
import { outputFile, outputJSON, readFile } from 'fs-extra';
import { join } from 'upath2';
import { __root } from '../test/__root';
import Bluebird from 'bluebird';
import { cli_logger, createProgressEstimator } from '../lib/cli-progress';
import { __plugin_downloaded_dir, __plugin_downloaded_dir_unzip } from '../lib/const';
import { SingleBar } from 'cli-progress';
import { console } from 'debug-color2';
import { createMultiBar } from '../lib/cli-progress';

unzipLang('zh');
const multibar = createMultiBar();

export default unzipLang('zh')
.finally(() => multibar.stop())
;

function unzipLang(lang: string | 'zh')
{
return cli_logger(Bluebird.resolve(readFile(join(__plugin_downloaded_dir, `${lang}.zip`)))
.then<JSZip>(JSZip.loadAsync)
.then(async (zip) =>
{
let file = zip.file(/\.jar$/);
console.cyan.log(`unzip ${lang}.zip`);

return file[0].async('nodebuffer').then(JSZip.loadAsync)
})
.then(zip =>
let bar: SingleBar;

return Bluebird.resolve(readFile(join(__plugin_downloaded_dir, `${lang}.zip`)))
.then<JSZip>(JSZip.loadAsync)
.then(async (zip) =>
{
let file = zip.file(/\.jar$/);

if (!file.length || file.length > 1)
{
return Object.values(zip.files)
})
.each(async (file) =>
throw new Error(`files of .jar should be only one file, but get ${file}`)
}

return file[0].async('nodebuffer').then(JSZip.loadAsync)
})
.then(zip =>
{
return Object.values(zip.files)
})
.tap((ls) =>
{
bar = multibar.create(ls.length, 0);
})
.reduce(async (ls, file, index) =>
{
if (!file.dir)
{
return !file.dir && outputFile(join(__plugin_downloaded_dir_unzip, lang, file.name), await file.async('nodebuffer'))
bar.update(index + 1, { filename: file.name });
ls.push(file.name);
await outputFile(join(__plugin_downloaded_dir_unzip, lang, file.name), await file.async('nodebuffer'))
}
return ls
}, [] as string[])
.tap(ls =>
{
return outputJSON(join(__plugin_downloaded_dir_unzip, lang + '.list.json'), ls, {
spaces: 2,
})
})
.finally(() =>
{
bar?.update(bar.getTotal());
bar?.stop();
})
//.tap(console.dir)
, `unzip ${lang}.zip`)
;
}

0 comments on commit 1fbed03

Please sign in to comment.