Skip to content

Commit

Permalink
feat(cache): 將版本資訊保存至 lib/const/version-map.json
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Feb 12, 2022
1 parent a888e26 commit 88fc62e
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const __dict_table_txt = join(__root, 'lib/static/table.txt');
export const __dict_synonym_txt = join(__root, 'lib/static/synonym.txt');

export const __dict_properties_lazy = join(__root, 'lib/static/lazy.properties');

export const __file_version_map_json = join(__root, 'lib/const/version-map.json');
20 changes: 16 additions & 4 deletions lib/const/link-of-zh-cn.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@

/**
* @default '156292'
*/
export const __plugin_zh_cn_id: string = '156292';

export const __plugin_zh_cn_id = '156292';
/**
* @default '221'
*/
export const __plugin_zh_cn_series: string = '221';
/**
* @default '221.114'
*/
export const __plugin_zh_cn_version: string = '221.114';

export const __plugin_zh_cn_version = '221.114';

export const __plugin_zh_cn_download = 'https://plugins.jetbrains.com/plugin/download?rel=true&updateId=156292';
/**
* @see https://plugins.jetbrains.com/plugin/download?rel=true&updateId=156292
*/
export const __plugin_zh_cn_download: string = 'https://plugins.jetbrains.com/plugin/download?rel=true&updateId=156292';

63 changes: 63 additions & 0 deletions lib/const/version-map.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
declare namespace version_map {
type IVersionApiResultRow = {
id: string;
link: string;
version: string;

approve: boolean;
listed: boolean;
recalculateCompatibilityAllowed: boolean;
cdate: string;
file: string;
notes: string;
since: string;
until: string;
sinceUntil: string;
channel: string;
size: number;
downloads: number;
pluginId: number;
compatibleVersions: {
GOLAND: string;
PHPSTORM: string;
DATASPELL: string;
WEBSTORM: string;
GATEWAY: string;
IDEA_COMMUNITY: string;
CLION: string;
IDEA: string;
PYCHARM_EDUCATIONAL: string;
PYCHARM_COMMUNITY: string;
PYCHARM: string;
DBE: string;
CWMGUEST: string;
RUBYMINE: string;
APPCODE: string;
IDEA_EDUCATIONAL: string;
JBCLIENT: string;
};
author: {
id: string;
name: string;
link: string;
hubLogin: string;
isJetBrains: boolean;
icon: string;
};
modules: any[];
}
type IVersionApiResult = IVersionApiResultRow[];
type IVersionDownloadMap = Record<string, string>;
type IVersionMapRecord = Record<string, IVersionApiResultRow>;
interface IVersionMap {
version_map_record: IVersionMapRecord;
series: string[];
version_download_map: IVersionDownloadMap;
series_latest_map: IVersionDownloadMap;
}
const version_map_record: IVersionMap["version_map_record"];
const series: IVersionMap["series"];
const version_download_map: IVersionMap["version_download_map"];
const series_latest_map: IVersionMap["series_latest_map"];
}
export = version_map;
78 changes: 78 additions & 0 deletions lib/util/version-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { IVersionMap } from '../const/version-map';
import { readJSON, readJSONSync} from 'fs-extra';
import { __file_version_map_json } from '../const';

let versionMap: IVersionMap;

export function _loadVersionMapSync(): IVersionMap
{
return readJSONSync(__file_version_map_json)
}

export function _loadVersionMapAsync(): Promise<IVersionMap>
{
return readJSON(__file_version_map_json)
}

export function _versionMap(): IVersionMap
{
return versionMap ??= _loadVersionMapSync()
}

export function getLatestSeries(data?: IVersionMap)
{
data ??= _versionMap();
return data.series[0]
}

export function getLatestVersion(data?: IVersionMap)
{
data ??= _versionMap();
return _getVersion(getLatestSeries(data), data)
}

export function _getVersion(series: string, data?: IVersionMap)
{
data ??= _versionMap();
return data.series_latest_map[series]
}

export function getVersionLatestInfo(data?: IVersionMap)
{
data ??= _versionMap();
return _getVersionInfoByVersion(getLatestVersion(data), data)
}

export function _getVersionInfoByVersion(version: string, data?: IVersionMap)
{
data ??= _versionMap();
return data.version_map_record[version]
}

export function _getVersionInfoBySeries(series: string, data?: IVersionMap)
{
data ??= _versionMap();
return _getVersionInfoByVersion(_getVersion(series, data), data)
}

export function getVersionLatestDownload(data?: IVersionMap)
{
data ??= _versionMap();
return _getVersionDownloadByVersion(getLatestSeries(data), data)
}

export function _getVersionDownloadByVersion(version: string, data?: IVersionMap)
{
data ??= _versionMap();
return data.version_download_map[version]
}

/**
* @example
* generateDownloadLink(156292)
* // => https://plugins.jetbrains.com/plugin/download?rel=true&updateId=156292
*/
export function generateDownloadLink(id: string | number)
{
return `https://plugins.jetbrains.com/plugin/download?rel=true&updateId=${id}`
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"posttest": "yarn run build:sort",
"prebuild": "ts-node scripts/update-meta.ts",
"build": "ts-node scripts/plugin-build-install-file.ts",
"build:commit": "git add --all ./original-plugin ./original-plugin-raw ./plugin-dev-out ./plugin-dev-raw ./lib/static ./test/__snapshots__ ./lib/const/link-of-zh-cn.ts && git commit -m \"build(release): update build\" ./original-plugin ./original-plugin-raw ./plugin-dev-out ./plugin-dev-raw ./lib/static ./test/__snapshots__ ./lib/const/link-of-zh-cn.ts & echo build:commit",
"build:commit": "ts-node ./scripts/git/build-commit.ts & echo build:commit",
"build:dts": "ynpx dts-bundle-generator -o ./dist/index.d.ts ./src/index.ts --no-banner & echo build:dts",
"build:dts:copy": "copy .\\src\\index.d.ts .\\dist\\index.d.ts & echo build:dts",
"build:dts:tsc": "tsc --emitDeclarationOnly --declaration --noEmit false",
Expand All @@ -34,7 +34,7 @@
"ci:install": "yarn install --frozen-lockfile && yarn add -W typescript@next jest ts-jest ts-node ynpx lerna yarn-tool",
"ci:build": "yarn run plugin:build && yarn run build:commit",
"ci:postpublish": "ts-node scripts/ci-postpublish.ts && yarn run postpublish:git:tag & echo ci:postpublish",
"_ci:postpublish": "yarn run postpublish:changelog && yarn run postpublish:git:commit && yarn run postpublish:git:tag & echo ci:postpublish",
"_ci:postpublish": "yarn run postpublish:changelog && yarn run postpublish:git:commit && yarn run postpublish:git:tag & echo ci:postpublish",
"ci:git:config": "git config --local core.autocrlf false && git config --local core.ignorecase false && git config --local include.path ../.gitconfig && git config --local core.autocrlf false && git config --local core.ignorecase false",
"ci:git:fetch:remote:tag": "git fetch --tag -f",
"npm:publish": "npm publish",
Expand Down Expand Up @@ -69,6 +69,7 @@
},
"dependencies": {
"@bluelovers/fast-glob": "^3.0.4",
"@bluelovers/string-natural-compare": "^2.0.5",
"@git-lazy/spawn": "^1.0.28",
"@git-lazy/tag": "^1.0.9",
"@lazy-cjk/zh-convert": "^1.0.44",
Expand Down Expand Up @@ -98,6 +99,7 @@
"novel-segment": "^2.7.87",
"novel-segment-cli": "^1.1.105",
"progress-estimator": "^0.3.0",
"semver": "^7.3.5",
"tslib": "^2.3.1",
"upath2": "^3.1.12"
},
Expand All @@ -112,6 +114,7 @@
"@types/micromatch": "^4.0.2",
"@types/node": "*",
"@types/node-fetch": "<3 >=2.5.12",
"@types/semver": "^7.3.9",
"@yarn-tool/changelog": "^1.0.66",
"git-diff-from": "^1.1.25",
"gitlog2": "^5.0.21"
Expand Down
142 changes: 128 additions & 14 deletions scripts/fetch-latest-version-of-zh-cn.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,148 @@
import { outputFile } from 'fs-extra';
import { outputFile, outputJSON, readJSON } from 'fs-extra';
import { join } from 'upath2';
import { __root } from '../test/__root';
import fetch from 'cross-fetch';
import { __plugin_zh_cn_id } from '../lib/const/link-of-zh-cn';
import { console } from 'debug-color2';
import Bluebird from 'bluebird';
import { __file_version_map_json } from '../lib/const';
import { array_unique_overwrite } from 'array-hyper-unique';
import type { IVersionApiResult, IVersionMap } from '../lib/const/version-map';
import { maxSatisfying } from 'semver';
import { naturalCompare, createNew } from '@bluelovers/string-natural-compare';
import {
_getVersionDownloadByVersion, _getVersionInfoBySeries,
_getVersionInfoByVersion,
_loadVersionMapAsync, generateDownloadLink,
getLatestSeries,
} from '../lib/util/version-map';

export default fetch('https://plugins.jetbrains.com/api/plugins/13710/updates?channel=&size=1')
const _myNaturalCompare = createNew({
desc: true,
})

export default Bluebird.resolve(fetch('https://plugins.jetbrains.com/api/plugins/13710/updates?channel=&size=5'))
.then((res) =>
{
return res.json();
}).then((data: {
id: string,
link: string,
version: string,
}[]) =>
return res.json() as any as IVersionApiResult;
})
.then(async (data) =>
{

const oldMap: IVersionMap = await _loadVersionMapAsync()
.catch(() => void 0)
.then((map: IVersionMap) =>
{
// @ts-ignore
map ??= {} as IVersionMap;
map.version_map_record ??= {};
map.version_download_map ??= {};
map.series ??= [];
map.series_latest_map ??= {};

return map
})
;

const _version_map_list2: IVersionApiResult = array_unique_overwrite(data.concat(Object.values(oldMap.version_map_record)));

let version_map_record = _version_map_list2.reduce((a, row) =>
{

a[row.version] ??= row;

return a
}, {} as IVersionMap["version_map_record"]);

let series_latest_map = Object.keys(version_map_record)
.sort(_myNaturalCompare)
.reduce((series_latest_map, v) =>
{

const sv = v.split('.')[0];

const version = series_latest_map[sv] = maxSatisfying([
v + '.0',
(series_latest_map[sv] ?? v) + '.0',
], `*`, {
loose: true,
})
.replace(/\.0$/, '')
;

series_latest_map[sv] = version;

return series_latest_map
}, {} as IVersionMap["series_latest_map"]);

const series: IVersionMap["series"] = Object.keys(series_latest_map)
.sort(_myNaturalCompare)
;

const version_download_map: IVersionMap["version_download_map"] = {};

version_map_record = series
.reduce((a, sv) =>
{

const version = series_latest_map[sv];

const row = version_map_record[version];

version_download_map[row.version] = generateDownloadLink(row.id);

a[version] = row;

return a
}, {} as IVersionMap["version_map_record"])
;

const record: IVersionMap = {
version_map_record,
version_download_map,
series,
series_latest_map,
};

await outputJSON(__file_version_map_json, record, {
spaces: 2,
});

return record
})
.then((data) =>
{
const id = data[0].id.toString();
const version = data[0].version.toString();
const sv = getLatestSeries(data);
const row = _getVersionInfoBySeries(sv, data);

const id = row.id.toString();
const version = row.version.toString();

if (__plugin_zh_cn_id !== id)
{
console.success(`發現新版本 #${id} version: ${version}`);

return outputFile(join(__root, 'lib/const/link-of-zh-cn.ts'), `
const __plugin_zh_cn_download = _getVersionDownloadByVersion(version, data);

export const __plugin_zh_cn_id = '${id}';
return outputFile(join(__root, 'lib/const/link-of-zh-cn.ts'), `
/**
* @default '${id}'
*/
export const __plugin_zh_cn_id: string = '${id}';
export const __plugin_zh_cn_version = '${version}';
/**
* @default '${sv}'
*/
export const __plugin_zh_cn_series: string = '${sv}';
/**
* @default '${version}'
*/
export const __plugin_zh_cn_version: string = '${version}';
export const __plugin_zh_cn_download = 'https://plugins.jetbrains.com/plugin/download?rel=true&updateId=${id}';
/**
* @see ${__plugin_zh_cn_download}
*/
export const __plugin_zh_cn_download: string = '${__plugin_zh_cn_download}';
`);
}
Expand Down
8 changes: 8 additions & 0 deletions scripts/git/_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import { __root } from '../../test/__root';

export const opts = {
cwd: __root,
throwError: true,
printStderr: true,
} as const;
Loading

0 comments on commit 88fc62e

Please sign in to comment.