I do see similar questions but no actual answer. The problem with mine it keeps adding a d%3A
on the path. I don’t know why it is doing that. I feel if it wouldn’t do that I can finish my extension.
Yes I was working from it with a tutorial but I can’t figure out the path situation. I know other questions are out there but their’s are actually different.
my extension.js
JavaScript
x
63
63
1
// The module 'vscode' contains the VS Code extensibility API
2
// Import the module and reference it with the alias vscode in your code below
3
const vscode = require('vscode');
4
const fs = require('fs');
5
const path = require('path')
6
7
// this method is called when your extension is activated
8
// your extension is activated the very first time the command is executed
9
10
/**
11
* @param {vscode.ExtensionContext} context
12
*/
13
function activate(context) {
14
15
// Use the console to output diagnostic information (console.log) and errors (console.error)
16
// This line of code will only be executed once when your extension is activated
17
console.log('Congratulations, your extension "alsbp" is now active!');
18
19
// The command has been defined in the package.json file
20
// Now provide the implementation of the command with registerCommand
21
// The commandId parameter must match the command field in package.json
22
let disposable = vscode.commands.registerCommand(
23
'alsbp.createBoilerPlate',
24
function () {
25
const htmlContent = `
26
<!DOCTYPE html>
27
<html lang="en">
28
<head>
29
<meta charset="UTF-8">
30
<meta http-equiv="X-UA-Compatible" content="IE=edge">
31
<meta name="viewport" content="width=device-width, initial-scale=1.0">
32
<title>Your Title</title>
33
<link rel="stylesheet" href="./app.css">
34
</head>
35
<body>
36
<script src="./app.js"></script>
37
</body>
38
</html>
39
`;
40
41
const folderPath = vscode.workspace.workspaceFolders[0].uri.toString().split(":")[1];
42
console.log(folderPath)
43
fs.writeFile(path.join(folderPath, "index.html"), htmlContent, err =>{
44
if(err){
45
console.error(err);
46
return vscode.window.showErrorMessage("Failed dude just failed")
47
};
48
vscode.window.showInformationMessage("Created boiler plate")
49
}
50
);
51
});
52
53
context.subscriptions.push(disposable);
54
}
55
56
// this method is called when your extension is deactivated
57
function deactivate() {}
58
59
module.exports = {
60
activate,
61
deactivate
62
}
63
package.json
JavaScript
1
42
42
1
{
2
"name": "alsbp",
3
"displayName": "AlsBoilerPlates",
4
"description": "generate a html js and css file and have the emmet link app.js and css file",
5
"version": "0.0.1",
6
"engines": {
7
"vscode": "^1.58.0"
8
},
9
"categories": [
10
"Other"
11
],
12
"activationEvents": [
13
"onCommand:alsbp.createBoilerPlate"
14
],
15
"main": "./extension.js",
16
"contributes": {
17
"commands": [
18
{
19
"command": "alsbp.createBoilerPlate",
20
"title": "Create Al's Boiler Plates"
21
}
22
]
23
},
24
"scripts": {
25
"lint": "eslint .",
26
"pretest": "npm run lint",
27
"test": "node ./test/runTest.js"
28
},
29
"devDependencies": {
30
"@types/vscode": "^1.58.0",
31
"@types/glob": "^7.1.3",
32
"@types/mocha": "^8.2.2",
33
"@types/node": "14.x",
34
"eslint": "^7.27.0",
35
"glob": "^7.1.7",
36
"mocha": "^8.4.0",
37
"typescript": "^4.3.2",
38
"vscode-test": "^1.5.2"
39
}
40
}
41
42
Advertisement
Answer
I had to look at the path:
I had this originally
JavaScript
1
2
1
const folderPath = vscode.workspace.workspaceFolders[0].uri.toString().split(":")[1];
2
and changed it to this
JavaScript
1
2
1
const folderPath = vscode.workspace.workspaceFolders[0].uri['fsPath']
2
I forgot about being able to use the [] because of mapping etc..