Skip to content

Commit

Permalink
feat: update script
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Jan 8, 2022
1 parent ceef5ed commit 4c5a909
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 25 deletions.
3 changes: 3 additions & 0 deletions lib/cli-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from 'path';
// @ts-ignore
import progressEstimator from 'progress-estimator';
import { findPkgModuleCachePathCore, findPkgModulePathCore } from 'cache-path/lib/finder/findPkgModuleCachePath';
import { __root } from '../test/__root';

export function createProgressEstimator(root: string)
{
Expand All @@ -13,3 +14,5 @@ export function createProgressEstimator(root: string)
storagePath,
});
}

export const cli_logger = createProgressEstimator(__root);
7 changes: 7 additions & 0 deletions lib/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { join } from 'upath2';
import { __root } from '../test/__root';

export const __plugin_downloaded_dir = join(__root, 'original-plugin');
export const __plugin_downloaded_dir_unzip = join(__root, 'original-plugin-raw');

export const __plugin_dev_dir = join(__root, 'plugin-dev-raw');
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"bluebird": "^3.7.2",
"cache-path": "^2.0.29",
"cross-fetch": "^3.1.4",
"debug-color2": "^1.2.7",
"fs-extra": "^10.0.0",
"jszip": "^3.7.1",
"progress-estimator": "^0.3.0",
Expand Down
11 changes: 5 additions & 6 deletions scripts/download-original-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import fetch from 'cross-fetch';
import { Response } from 'node-fetch';
import { outputFile } from 'fs-extra';
import { join } from 'path';
import { join } from 'upath2';
import { __root } from '../test/__root';
import { createProgressEstimator } from '../lib/cli-progress';
import { cli_logger, createProgressEstimator } from '../lib/cli-progress';
import { __plugin_downloaded_dir } from '../lib/const';

const logger = createProgressEstimator(__root);

logger(fetch('https://plugins.jetbrains.com/plugin/download?rel=true&updateId=149295')
cli_logger(fetch('https://plugins.jetbrains.com/plugin/download?rel=true&updateId=149295')
.then((res) => (res as any as Response).buffer())
.then(buf => {
return outputFile(join(__root, 'original-plugin', 'zh.zip'), buf)
return outputFile(join(__plugin_downloaded_dir, 'zh.zip'), buf)
}), `download Chinese ​(Simplified)​ Language Pack / 中文语言包`)
;
50 changes: 50 additions & 0 deletions scripts/to-zht.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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';

cli_logger(FastGlob([
'**/*',
], {
cwd: __plugin_downloaded_dir_unzip,
})
.each(async (file: string) =>
{
let fullpath_new = join(__plugin_dev_dir, file);

if (!/\.(png)$/i.test(file))
{
let fullpath = join(__plugin_downloaded_dir_unzip, file);

const content_old = await readFile(fullpath).then(content => content.toString());
let content_new = cn2tw_min(content_old);

if (/META-INF\/plugin\.xml$/i.test(file))
{
content_new = content_new.replace(/<name>.+<\/name>/, `<name>Chinese (Traditional) Language Pack / 中文語言包</name>`);
}

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

if (await pathExists(fullpath_new))
{
console.warn(file);
return unlink(fullpath_new);
}
}), `convert to zht`)
;


44 changes: 25 additions & 19 deletions scripts/unzip.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import JSZip from "jszip";
import { outputFile, readFile } from 'fs-extra';
import { join } from 'path';
import { join } from 'upath2';
import { __root } from '../test/__root';
import Bluebird from 'bluebird';
import { createProgressEstimator } from '../lib/cli-progress';
import { cli_logger, createProgressEstimator } from '../lib/cli-progress';
import { __plugin_downloaded_dir, __plugin_downloaded_dir_unzip } from '../lib/const';

const logger = createProgressEstimator(__root);
unzipLang('zh');

logger(Bluebird.resolve(readFile(join(__root, 'original-plugin', 'zh.zip')))
.then<JSZip>(JSZip.loadAsync)
.then(async (zip) => {
let file = zip.file(/\.jar$/);

return file[0].async('nodebuffer').then(JSZip.loadAsync)
})
.then(zip => {
return Object.values(zip.files)
})
.each(async (file) => {
return !file.dir && outputFile(join(__root, 'original-plugin-raw', 'zh', file.name), await file.async('nodebuffer'))
})
//.tap(console.dir)
, `unzip zh.zip`)
;
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$/);

return file[0].async('nodebuffer').then(JSZip.loadAsync)
})
.then(zip =>
{
return Object.values(zip.files)
})
.each(async (file) =>
{
return !file.dir && outputFile(join(__plugin_downloaded_dir_unzip, lang, file.name), await file.async('nodebuffer'))
})
//.tap(console.dir)
, `unzip ${lang}.zip`)
;
}

0 comments on commit 4c5a909

Please sign in to comment.