Skip to content

Commit

Permalink
refactor: code splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Feb 12, 2022
1 parent 88fc62e commit 80fade5
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 50 deletions.
File renamed without changes.
36 changes: 36 additions & 0 deletions lib/git/commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { crossSpawnGitAsync } from '@git-lazy/spawn';
import { opts } from './_config';
import { ITSArrayListMaybeReadonly, ITSValueOrArrayMaybeReadonly } from 'ts-type/lib/type/base';
import { ISpawnGitAsyncOptions } from '@git-lazy/spawn/lib/types';

export function lazyCommitFiles(files: ITSValueOrArrayMaybeReadonly<string>, commitMessage: string, config?: {
addFlags?: ITSArrayListMaybeReadonly<string>,
options?: ISpawnGitAsyncOptions,
})
{
files = [files].flat();
config ??= {};

const options: ISpawnGitAsyncOptions = {
...opts,
...config.options,
};

const addFlags = config.addFlags ?? [];

return crossSpawnGitAsync('git', [
'add',
...addFlags,
...files,
], options)
.then(() =>
{
return crossSpawnGitAsync('git', [
'commit',
'-m',
commitMessage,
...files,
], options)
})
;
}
13 changes: 13 additions & 0 deletions lib/util/download-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fetch from 'cross-fetch';
import { Response } from 'node-fetch';
import { outputFile } from 'fs-extra';

export function downloadPlugin(link: string, output_file: string)
{
return fetch(link)
.then((res) => (res as any as Response).buffer())
.then(buf =>
{
return outputFile(output_file, buf)
})
}
13 changes: 13 additions & 0 deletions lib/util/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Bluebird from 'bluebird';
import { resolve } from 'upath2';
import { __root } from '../../test/__root';

export function _lazyImport<T>(target: string, dir = __root): Bluebird<T>
{
return Bluebird.resolve(import(resolve(dir, target))).then(m => m.default ?? m)
}

export function _lazyImportWithDelay<T>(target: string, dir = __root, delay?: number): Bluebird<T>
{
return _lazyImport<T>(target, dir).delay(delay || 2000)
}
11 changes: 2 additions & 9 deletions scripts/download-original-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import fetch from 'cross-fetch';
import { Response } from 'node-fetch';
import { outputFile } from 'fs-extra';
import { join } from 'upath2';
import { cli_logger } from '../lib/cli-progress';
import { __plugin_downloaded_dir } from '../lib/const';
import { __plugin_zh_cn_download, __plugin_zh_cn_id, __plugin_zh_cn_version } from '../lib/const/link-of-zh-cn';
import { downloadPlugin } from '../lib/util/download-plugin';

export default cli_logger(fetch(__plugin_zh_cn_download)
.then((res) => (res as any as Response).buffer())
.then(buf =>
{
return outputFile(join(__plugin_downloaded_dir, 'zh.zip'), buf)
}), `download Chinese ​(Simplified)​ Language Pack / 中文语言包\nid: ${__plugin_zh_cn_id}\nversion: ${__plugin_zh_cn_version}`)
export default cli_logger(downloadPlugin(__plugin_zh_cn_download, join(__plugin_downloaded_dir, 'zh.zip')), `download Chinese ​(Simplified)​ Language Pack / 中文语言包\nid: ${__plugin_zh_cn_id}\nversion: ${__plugin_zh_cn_version}`)
;
51 changes: 26 additions & 25 deletions scripts/git/build-commit.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { crossSpawnGitAsync } from '@git-lazy/spawn';
import { opts } from './_config';
import { lazyCommitFiles } from '../../lib/git/commit';
import { _lazyImportWithDelay } from '../../lib/util/import';
import Bluebird from 'bluebird';

const list = [
'./original-plugin',
'./original-plugin-raw',
'./plugin-dev-out',
'./plugin-dev-raw',
'./lib/static',
'./test/__snapshots__',
'./lib/const/link-of-zh-cn.ts',
] as const;

export default crossSpawnGitAsync('git', [
'add',
'--all',
...list,
], opts)
export default Bluebird.mapSeries([
'./commit-version-map.ts',
'./commit-link-of-zh-cn.ts',
] as const, lazyImport)
.then(() =>
{
return crossSpawnGitAsync('git', [
'commit',
'-m',
'build(release): update build',
...list,
], opts)
})
;
const list = [
'./original-plugin',
'./original-plugin-raw',
'./plugin-dev-out',
'./plugin-dev-raw',
'./lib/static',
'./test/__snapshots__',
'./lib/const/link-of-zh-cn.ts',
] as const;

return lazyCommitFiles(list, 'build(release): update build', {
addFlags: ['--all'],
})
});

function lazyImport(target: string)
{
return _lazyImportWithDelay(target, __dirname)
}
5 changes: 5 additions & 0 deletions scripts/git/commit-link-of-zh-cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { lazyCommitFiles } from '../../lib/git/commit';

export default lazyCommitFiles('./lib/const/link-of-zh-cn.ts', 'build(cache): update link-of-zh-cn.ts')
.catch(() => void 0)
;
17 changes: 2 additions & 15 deletions scripts/git/commit-version-map.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { crossSpawnGitAsync } from '@git-lazy/spawn';
import { opts } from './_config';
import { lazyCommitFiles } from '../../lib/git/commit';

export default crossSpawnGitAsync('git', [
'add',
'./lib/const/version-map.json',
], opts)
.then(() =>
{
return crossSpawnGitAsync('git', [
'commit',
'-m',
'build(cache): update version-map',
'./lib/const/version-map.json',
], opts)
})
export default lazyCommitFiles('./lib/const/version-map.json', 'build(cache): update version-map')
.catch(() => void 0)
;
3 changes: 2 additions & 1 deletion scripts/plugin-handle-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Bluebird from 'bluebird';
import { resolve } from 'upath2';
import { _lazyImport, _lazyImportWithDelay } from '../lib/util/import';

export default Bluebird.mapSeries([
'./fetch-latest-version-of-zh-cn',
Expand All @@ -15,5 +16,5 @@ export default Bluebird.mapSeries([

function lazyImport(target: string)
{
return Bluebird.resolve(import(resolve(__dirname, target))).then(m => m.default ?? m).delay(2000)
return _lazyImportWithDelay(target, __dirname)
}

0 comments on commit 80fade5

Please sign in to comment.