Skip to content

Commit 791748a

Browse files
committed
feat: save disabled state
1 parent a82e65d commit 791748a

File tree

7 files changed

+92
-26
lines changed

7 files changed

+92
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## v0.6.0 2023-03-31
4+
5+
- save disabled state in `vsc-custom ls` command
6+
- new command: rm added via `vsc-custom rm` command
7+
38
## v0.5.0 2022-12-08
49

510
- warn on local file not exist

src/cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cli, Builtins } from 'clipanion'
1+
import { Builtins, Cli } from 'clipanion'
22
import { PackageJson } from 'type-fest'
33
const { version, name, bin } = require('../package') as PackageJson
44

@@ -17,6 +17,9 @@ cli.register(Builtins.DefinitionsCommand)
1717
import { AddCommand } from './commands/add'
1818
cli.register(AddCommand)
1919

20+
import { RemoveCommand } from './commands/remove'
21+
cli.register(RemoveCommand)
22+
2023
import { UpdateCommand } from './commands/update'
2124
cli.register(UpdateCommand)
2225

src/commands/add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ export class AddCommand extends Command {
4848
add(file)
4949
await applyData()
5050

51-
consola.log('[vsc-custom]: embed file success %s', file)
51+
consola.success('[vsc-custom]: embed file success %s', file)
5252
}
5353
}

src/commands/common.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import cheerio, { CheerioAPI } from 'cheerio'
22
import consola from 'consola'
3+
import debugFactory from 'debug'
34
import fse from 'fs-extra'
45
import path from 'path'
56
import pmap from 'promise.map'
67
import { APP_DIR, DATA_ATTR_NAME, HTML_FILE } from '../config'
78
import { CURRENT_ASSETS } from '../data'
89
import { checkChecksum, chown, getContent } from '../utils'
910

11+
const debug = debugFactory('vsc-custom:common')
12+
1013
export async function prepare() {
1114
let ok = true
1215
try {
@@ -42,14 +45,19 @@ export async function applyData() {
4245

4346
// update contents
4447
const listData = await pmap(
45-
CURRENT_ASSETS,
46-
async (file) => {
48+
CURRENT_ASSETS.filter((x) => !x.disabled),
49+
async ({ file }) => {
4750
const content = await getContent(file)
4851
return { file, content }
4952
},
5053
5
5154
)
5255

56+
debug(
57+
'after filter out disabled: %O',
58+
listData.map((x) => x.file)
59+
)
60+
5361
// create new tags
5462
for (let { file, content } of listData) {
5563
const ext = path.extname(file)

src/commands/list.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Command, Usage } from 'clipanion'
22
import inquirer from 'inquirer'
3-
import { CURRENT_ASSETS, write } from '../data'
3+
import { AddedAsset, CURRENT_ASSETS, write } from '../data'
44
import { applyData } from './common'
55

66
export class ListCommand extends Command {
@@ -12,20 +12,19 @@ export class ListCommand extends Command {
1212
}
1313

1414
async execute() {
15-
const list = CURRENT_ASSETS
1615
const { selectedIndex } = await inquirer.prompt([
1716
{
1817
type: 'checkbox',
1918
message: 'choose from list',
2019
name: 'selectedIndex',
21-
choices: CURRENT_ASSETS.map((item, index) => {
22-
return { name: item, value: index, checked: true }
20+
choices: CURRENT_ASSETS.map((asset, index) => {
21+
return { name: asset.file, value: index, checked: !asset.disabled }
2322
}),
2423
},
2524
])
2625

27-
const newList = CURRENT_ASSETS.filter((item, index) => {
28-
return selectedIndex.includes(index)
26+
const newList: AddedAsset[] = CURRENT_ASSETS.map((item, index) => {
27+
return { ...item, disabled: !selectedIndex.includes(index) }
2928
})
3029
write(newList)
3130

src/commands/remove.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Command, Option, Usage } from 'clipanion'
2+
import consola from 'consola'
3+
import path from 'path'
4+
import { remove } from '../data'
5+
import { isUrl } from '../utils'
6+
import { applyData } from './common'
7+
8+
export class RemoveCommand extends Command {
9+
static paths = [['remove'], ['rm']]
10+
11+
static usage: Usage = {
12+
category: '',
13+
description: `remove previously added file or url`,
14+
details: '',
15+
examples: [[`remove file`, `$0 remove ./custom.css`]],
16+
}
17+
18+
file = Option.String({ required: true })
19+
20+
async execute() {
21+
let { file } = this
22+
if (!isUrl(file)) file = path.resolve(file)
23+
24+
remove(file)
25+
await applyData()
26+
27+
consola.success('[vsc-custom]: remove file success %s', file)
28+
}
29+
}

src/data.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,63 @@ import { PackageJson } from 'type-fest'
77
const { name } = require('../package.json') as PackageJson
88
const paths = envPaths(name, { suffix: '' })
99

10+
export type AddedAsset = { file: string; disabled: boolean }
11+
1012
export 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

1625
export 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
2937
consola.info('[vsc-custom]: using data file: %s', dataFile)
3038
read()
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

Comments
 (0)