Skip to content

Commit b544559

Browse files
committed
feat: support remove package
1 parent cbd2fca commit b544559

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Import Maps Command Line Interface
22

3-
A command-line utility for managing [JavaScript import maps](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) dependencies.
3+
A command-line utility for managing [JavaScript import maps](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) dependencies.
44

55
<img width="80%" alt="Screenshot 2023-04-01 at 00 54 27" src="https://user-images.githubusercontent.com/1680157/229265234-f5120ec8-28cb-4cce-9303-444b076ec032.png">
66

@@ -10,7 +10,6 @@ This utility uses the [esm.sh](https://esm.sh) CDN to fetch package information
1010

1111
To install `im-cli` globally, run the following command:
1212

13-
1413
```bash
1514
npm install -g im-cli
1615
```
@@ -45,12 +44,12 @@ To update the `lodash` package to the latest version, run:
4544
im update lodash
4645
```
4746

48-
### Deleting a package
47+
### Removing a package
4948

5049
To remove the `lodash` package from your import map JSON file, run:
5150

5251
```bash
53-
im delete lodash
52+
im remove lodash
5453
```
5554

5655
### Displaying help

im.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"imports": {
3-
"lodash": "https://esm.sh/[email protected]",
4-
"moment": "https://esm.sh/[email protected]"
3+
"lodash": "https://esm.sh/[email protected]"
54
}
65
}

lib/im-cli.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,23 @@ async function addOrUpdatePackage(
6060
)
6161
}
6262

63-
function deletePackage(packageName: string): void {
64-
// ...
63+
function removePackage(packageName: string): void {
64+
if (!fs.existsSync(importMapFile)) {
65+
console.error("im.json file not found.")
66+
return
67+
}
68+
69+
const fileContent = fs.readFileSync(importMapFile, "utf8")
70+
const importMap = JSON.parse(fileContent)
71+
72+
if (!importMap.imports[packageName]) {
73+
console.error(`Package ${packageName} not found in im.json.`)
74+
return
75+
}
76+
77+
delete importMap.imports[packageName]
78+
fs.writeFileSync(importMapFile, JSON.stringify(importMap, null, 2))
79+
console.log(`Package ${packageName} removed from im.json`)
6580
}
6681

6782
function displayHelp(): void {
@@ -73,7 +88,7 @@ function displayHelp(): void {
7388
" im update <package-name> Update a package in the import map"
7489
)
7590
console.log(
76-
" im delete <package-name> Delete a package from the import map"
91+
" im remove <package-name> Remove a package from the import map"
7792
)
7893
console.log(
7994
" im help Display this help message"
@@ -88,9 +103,9 @@ function displayHelp(): void {
88103
await addOrUpdatePackage(packageArg, command === "update")
89104
}
90105
break
91-
case "delete":
106+
case "remove":
92107
if (packageArg) {
93-
deletePackage(packageArg)
108+
removePackage(packageArg)
94109
}
95110
break
96111
case "help":

0 commit comments

Comments
 (0)