feat: 使用 JSON5

This commit is contained in:
lisonge 2023-12-01 11:32:40 +08:00
parent afae8c20e0
commit 60db3a92dd
5 changed files with 38 additions and 12 deletions

1
dist/gkd.json vendored

File diff suppressed because one or more lines are too long

1
dist/gkd.json5 vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -2,9 +2,9 @@
"name": "@gkd-kit/subscription",
"type": "module",
"version": "1.170.0",
"main": "./dist/gkd.json",
"unpkg": "./dist/gkd.json",
"jsdelivr": "./dist/gkd.json",
"main": "./dist/gkd.json5",
"unpkg": "./dist/gkd.json5",
"jsdelivr": "./dist/gkd.json5",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
@ -24,7 +24,7 @@
"@commitlint/cli": "^17.8.1",
"@commitlint/config-conventional": "^17.8.1",
"@commitlint/types": "^17.8.1",
"@gkd-kit/selector": "0.0.15",
"@gkd-kit/selector": "0.0.16",
"@types/lodash": "^4.14.201",
"@types/node": "^20.9.0",
"@typescript-eslint/eslint-plugin": "6.11.0",
@ -34,6 +34,7 @@
"eslint-config-prettier": "9.0.0",
"eslint-define-config": "1.24.1",
"husky": "^8.0.3",
"json5": "^2.2.3",
"lint-staged": "^15.1.0",
"lodash": "^4.17.21",
"picocolors": "^1.0.0",

View File

@ -15,8 +15,8 @@ devDependencies:
specifier: ^17.8.1
version: 17.8.1
'@gkd-kit/selector':
specifier: 0.0.15
version: 0.0.15
specifier: 0.0.16
version: 0.0.16
'@types/lodash':
specifier: ^4.14.201
version: 4.14.201
@ -44,6 +44,9 @@ devDependencies:
husky:
specifier: ^8.0.3
version: 8.0.3
json5:
specifier: 2.2.3
version: 2.2.3
lint-staged:
specifier: ^15.1.0
version: 15.1.0
@ -505,8 +508,8 @@ packages:
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
dev: true
/@gkd-kit/selector@0.0.15:
resolution: {integrity: sha512-fWFXWjfLUQ7SS9FUadEQVzq/QW/DDNAJZWJSBMRnTcHuZtS1bb0eMGS9aDa9t6hrp6YAnpoLYEkR10qzciyqTQ==}
/@gkd-kit/selector@0.0.16:
resolution: {integrity: sha512-I2/e2fYfgewaSMbkXDQz0leh35qr0XhwEsOUYjsNOQOwsg6BPnwCGwBuPVoK47TL/VmSj2NsNUCkN4fRZcvwYg==}
dev: true
/@humanwhocodes/config-array@0.11.13:
@ -1695,6 +1698,12 @@ packages:
resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==}
dev: true
/json5@2.2.3:
resolution: {integrity: sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==}
engines: {node: '>=6'}
hasBin: true
dev: true
/jsonfile@6.1.0:
resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==}
dependencies:

View File

@ -9,6 +9,7 @@ import type {
IArray,
SubscriptionConfig,
} from './types';
import JSON5 from 'json5';
const iArrayToArray = <T>(array: IArray<T> = []): T[] => {
return Array<T>().concat(array);
@ -40,15 +41,30 @@ const orderdStringify = (
JSON.stringify(Object.fromEntries(map.entries()), replacer, space) + '\n'
);
};
const orderdStringify5 = (
obj: any,
keys: string[],
replacer?: (this: any, key: string, value: any) => any,
space?: string | number,
) => {
const map = new Map<string, unknown>();
keys.forEach((k) => {
if (obj[k] === undefined) return;
map.set(k, obj[k]);
});
return (
JSON5.stringify(Object.fromEntries(map.entries()), replacer, space) + '\n'
);
};
const pkg: typeof PkgT = JSON.parse(
await fs.readFile(process.cwd() + '/package.json', 'utf-8'),
);
const pkgKeys = Object.keys(pkg);
export const writeConfig = async (config: SubscriptionConfig) => {
const gkdFp = process.cwd() + '/dist/gkd.json';
const gkdFp = process.cwd() + '/dist/gkd.json5';
const versionFp = process.cwd() + '/dist/gkd.version.json';
const oldConfig: SubscriptionConfig = JSON.parse(
const oldConfig: SubscriptionConfig = JSON5.parse(
await fs.readFile(gkdFp, 'utf-8').catch(() => '{}'),
);
@ -79,7 +95,7 @@ export const writeConfig = async (config: SubscriptionConfig) => {
);
// update gkd.json
const buffer = Buffer.from(orderdStringify(newConfig, sortKeys), 'utf-8');
const buffer = Buffer.from(orderdStringify5(newConfig, sortKeys), 'utf-8');
await fs.writeFile(gkdFp, buffer);
// update gkd.openad.json