Skip to content

Commit 6082ba1

Browse files
committed
first release
1 parent 35e5240 commit 6082ba1

File tree

3 files changed

+122
-100
lines changed

3 files changed

+122
-100
lines changed

README.md

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,34 @@
1-
# FUA README
1+
# FUA
22

3-
This is the README for your extension "FUA". After writing up a brief description, we recommend including the following sections.
3+
FUA is a "Find and Remove Unused Assets" VS Code extension.
4+
You can check and remove/delete your project's unused assets/images.
45

56
## Features
67

7-
Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file.
8+
### Supported Extensions
89

9-
For example if there is an image subfolder under your extension project workspace:
10+
#### Supported Asset File Extensions
1011

11-
\!\[feature X\]\(images/feature-x.png\)
12+
- .jpg
13+
- .jpeg
14+
- .png
15+
- .svg
16+
- .gif
1217

13-
> Tip: Many popular extensions utilize animations. This is an excellent way to show off your extension! We recommend short, focused animations that are easy to follow.
18+
#### Supported Component File Extensions
1419

15-
## Requirements
20+
- .html
21+
- .css
22+
- .js
23+
- .jsx
1624

17-
If you have any requirements or dependencies, add a section describing those and how to install and configure them.
25+
## Using
1826

19-
## Extension Settings
20-
21-
Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
22-
23-
For example:
24-
25-
This extension contributes the following settings:
26-
27-
* `myExtension.enable`: Enable/disable this extension.
28-
* `myExtension.thing`: Set to `blah` to do something.
29-
30-
## Known Issues
31-
32-
Calling out known issues can help limit users opening duplicate issues against your extension.
33-
34-
## Release Notes
35-
36-
Users appreciate release notes as you update your extension.
37-
38-
### 1.0.0
39-
40-
Initial release of ...
41-
42-
### 1.0.1
43-
44-
Fixed issue #.
45-
46-
### 1.1.0
47-
48-
Added features X, Y, and Z.
49-
50-
---
51-
52-
## Working with Markdown
53-
54-
You can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts:
55-
56-
* Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux)
57-
* Toggle preview (`Shift+Cmd+V` on macOS or `Shift+Ctrl+V` on Windows and Linux)
58-
* Press `Ctrl+Space` (Windows, Linux, macOS) to see a list of Markdown snippets
59-
60-
## For more information
61-
62-
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
63-
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)
27+
- Open a workspace/folder in VS Code.
28+
- Sure that your workspace has "src" folder (because FUA working on src file).
29+
- Press 'F1' key and enter `Find Unused Assets` command.
30+
- After a short period of time You will see the `UnusedAssets.txt` file. You can check this file and delete the lines you don't want.
31+
- Lastly, Press 'F1' key and enter `Remove Unused Assets` command.
32+
- After a short period of time your unused assets will be removed.
6433

6534
**Enjoy!**

extension.js

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const vscode = require("vscode");
2+
const { open, readFile, access, constants, unlink } = require("fs/promises");
23

34
/**
45
* @param {vscode.ExtensionContext} context
@@ -19,10 +20,35 @@ async function activate(context) {
1920

2021
async function findUnusedAssets() {
2122
if (await isFindable()) {
22-
vscode.window.showInformationMessage("works!");
23-
let workspacePath = vscode.workspace.workspaceFolders[0].uri.path;
24-
let workspaceFsPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
25-
// Continue with your logic here
23+
const assets = await getAssets();
24+
vscode.window.showInformationMessage("Total Asset Count: " + assets.length);
25+
const components = await getComponents();
26+
vscode.window.showInformationMessage("Total Component Count: " + components.length);
27+
let unusedAssetPaths = [];
28+
29+
for (let i = 0; i < assets.length; i++) {
30+
let fileName = getFileName(assets[i]);
31+
if (!(await isFileUsing(fileName, components))) {
32+
unusedAssetPaths.push(assets[i].fsPath);
33+
}
34+
}
35+
await createUnusedAssetsFile(unusedAssetPaths);
36+
vscode.window.showInformationMessage(
37+
"Done! Unused Asset Files Count: " + unusedAssetPaths.length.toString()
38+
);
39+
}
40+
}
41+
42+
async function removeUnusedAssets() {
43+
if (await isRemovable()) {
44+
const file = await open(getUnusedAssetsFilePath());
45+
const unusedAssets = (await readFile(file)).toString().split("\n");
46+
for (let i = 0; i < unusedAssets.length; i++) {
47+
unlink(unusedAssets[i]);
48+
}
49+
await file.close();
50+
unlink(getUnusedAssetsFilePath());
51+
vscode.window.showInformationMessage("Done! All unused assets were deleted.");
2652
}
2753
}
2854

@@ -32,6 +58,76 @@ async function isFindable() {
3258
} else return false;
3359
}
3460

61+
async function isRemovable() {
62+
if (isWorkspaceOpen() && (await hasSrcFolder()) && (await hasUnusedAssetsFile())) return true;
63+
else return false;
64+
}
65+
66+
async function hasUnusedAssetsFile() {
67+
try {
68+
await access(getUnusedAssetsFilePath(), constants.R_OK);
69+
return true;
70+
} catch (error) {
71+
vscode.window.showErrorMessage(
72+
"The UnusedAssets.txt file not found. Please run 'Find Unused Assets' command first."
73+
);
74+
return false;
75+
}
76+
}
77+
78+
async function createUnusedAssetsFile(assetPaths) {
79+
try {
80+
const file = await open(getUnusedAssetsFilePath(), "w");
81+
await file.writeFile("");
82+
if (assetPaths.length > 0) {
83+
await file.appendFile(assetPaths[0]);
84+
for (let i = 1; i < assetPaths.length; i++) {
85+
await file.appendFile("\n");
86+
await file.appendFile(assetPaths[i]);
87+
}
88+
}
89+
await file.close();
90+
} catch (error) {
91+
vscode.window.showErrorMessage(
92+
"Something went wrong when creating UnusedAssets.txt file: \n" + error.message
93+
);
94+
}
95+
}
96+
97+
async function isFileUsing(fileName, components) {
98+
let isUsing = false;
99+
try {
100+
for (let i = 0; i < components.length; i++) {
101+
const file = await open(components[i].fsPath, "r");
102+
if ((await readFile(file)).toString().includes(fileName)) {
103+
isUsing = true;
104+
await file.close();
105+
break;
106+
}
107+
await file.close();
108+
}
109+
} catch (error) {
110+
vscode.window.showErrorMessage("Got an error trying to check the file using: " + error.message);
111+
}
112+
return isUsing;
113+
}
114+
115+
function getUnusedAssetsFilePath() {
116+
return vscode.Uri.joinPath(vscode.workspace.workspaceFolders[0].uri, "UnusedAssets.txt").fsPath;
117+
}
118+
119+
function getFileName(uri) {
120+
return uri.path.split("/").pop();
121+
}
122+
123+
async function getAssets() {
124+
return await vscode.workspace.findFiles("src/**/*.{png,jpg,jpeg,gif,svg}");
125+
}
126+
127+
async function getComponents() {
128+
return await vscode.workspace.findFiles("src/**/*.{html,css,js,jsx}");
129+
}
130+
35131
async function hasSrcFolder() {
36132
const files = await vscode.workspace.findFiles("src/**", "", 1);
37133
if (!files || files.length === 0) {
@@ -47,8 +143,6 @@ function isWorkspaceOpen() {
47143
} else return true;
48144
}
49145

50-
async function removeUnusedAssets() {}
51-
52146
function deactivate() {}
53147

54148
module.exports = {

vsc-extension-quickstart.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)