@@ -7,41 +7,63 @@ import { PackageJson } from 'type-fest'
77const { name } = require ( '../package.json' ) as PackageJson
88const paths = envPaths ( name , { suffix : '' } )
99
10+ export type AddedAsset = { file : string ; disabled : boolean }
11+
1012export const dataFile = path . join ( paths . data , 'added-assets.json' )
1113
12- type AddedAssets = string [ ]
14+ export let CURRENT_ASSETS : AddedAsset [ ] = [ ]
1315
14- export let CURRENT_ASSETS : AddedAssets = [ ]
16+ // 兼容之前 string[] 的数据
17+ type CompatibleJson = ( string | AddedAsset ) [ ]
18+ function toAssets ( compatibleJSON : CompatibleJson ) {
19+ return compatibleJSON . map ( ( item ) => {
20+ if ( typeof item === 'string' ) return { file : item , disabled : false }
21+ return item
22+ } )
23+ }
1524
1625export function read ( ) {
17- let json = fse . readJSONSync ( dataFile , { throws : false } ) as AddedAssets
26+ const json = fse . readJSONSync ( dataFile , { throws : false } ) as CompatibleJson
1827
1928 if ( ! json ) {
20- json = [ ] as AddedAssets
21- write ( json )
29+ write ( [ ] )
30+ return [ ]
2231 }
2332
24- CURRENT_ASSETS = json
25- return json
33+ return ( CURRENT_ASSETS = toAssets ( json ) )
2634}
2735
2836// read on start
2937consola . info ( '[vsc-custom]: using data file: %s' , dataFile )
3038read ( )
3139
32- export function write ( data : AddedAssets ) {
40+ export function write ( data : AddedAsset [ ] ) {
3341 CURRENT_ASSETS = data
3442 fse . outputJSONSync ( dataFile , data )
3543}
3644
37- export function add ( item : string ) {
38- const items = read ( )
39-
40- if ( items . includes ( item ) ) {
41- items . splice ( items . indexOf ( item ) , 1 )
45+ const _remove = ( file : string ) => {
46+ const index = CURRENT_ASSETS . findIndex ( ( i ) => i . file === file )
47+ if ( index !== - 1 ) {
48+ CURRENT_ASSETS . splice ( index , 1 )
4249 }
50+ }
51+
52+ export function add ( file : string ) {
53+ // rm
54+ _remove ( file )
55+
56+ // add
57+ CURRENT_ASSETS . push ( { file, disabled : false } )
58+
59+ // persist
60+ write ( CURRENT_ASSETS )
61+ }
62+
63+ export function remove ( file : string ) {
64+ // rm
65+ _remove ( file )
4366
44- items . push ( item )
45- write ( items )
46- return items
67+ // persist
68+ write ( CURRENT_ASSETS )
4769}
0 commit comments