Skip to content

Commit a6cd9ed

Browse files
committed
better code
1 parent 9aff321 commit a6cd9ed

1 file changed

Lines changed: 38 additions & 42 deletions

File tree

src/extension.ts

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
11
import * as vscode from 'vscode';
22
const path = require('path');
33
import * as fs from 'fs';
4-
const templatePath = path.join(__dirname, '../templates');
54
import * as _ from 'lodash';
65

7-
export function activate(context: vscode.ExtensionContext) {
8-
9-
console.log('Congratulations, your extension "dockerfiletemplate" is now active!');
10-
6+
function GetTemplates(templatePath: fs.PathLike): Array<Array<string>> {
7+
const files = fs.readdirSync(templatePath);
118
const templates: Array<string> = [];
12-
let allFiles: Array<string>;
13-
fs.readdir(templatePath, function (err:any, files:Array<string>) {
14-
if (err) {
15-
return console.log('Unable to scan directory: ' + err);
16-
}
17-
allFiles = files;
18-
files.filter(e => e.includes(".dockerignore") ? false : true).forEach(file => {
9+
files.filter(e => e.includes(".dockerignore") ? false : true).forEach(file => {
10+
templates.push(_.trimEnd(file, ".dockerfile"));
11+
})
12+
return [templates, files];
13+
}
1914

20-
templates.push(_.trimEnd(file, ".dockerfile"))
21-
})
22-
23-
});
24-
25-
let disposable = vscode.commands.registerCommand('dockerfiletemplate.generatedockerfile', () => {
26-
const { workspace: { workspaceFolders } } = vscode;
27-
console.log(workspaceFolders)
28-
const workspaceFolder = workspaceFolders ? workspaceFolders[0].uri.fsPath : null;
15+
function CreateDockerFile(option: any , templatePath: String, workspaceFolder: String): void {
16+
const data = fs.readFileSync(`${templatePath}/${option}.dockerfile`);
17+
fs.writeFileSync(`${workspaceFolder}/Dockerfile`, data);
18+
}
2919

30-
vscode.window.showQuickPick(templates).then(option => {
31-
if (!option) return;
32-
allFiles.forEach((file) => {
33-
// check if a ignore file exist
34-
if (file === `.dockerignore ${option}`) {
35-
vscode.window.showInformationMessage("A .dockerignore file exist for this template , do you want to use it ?", "yes", "no").then(response => {
36-
if (response === "yes") {
37-
fs.readFile(`${templatePath}/.dockerignore ${option}`, (err: any , data: Buffer) => {
38-
console.log(data)
39-
fs.writeFileSync (`${workspaceFolder}/.dockerignore`, data)
40-
})
41-
}
42-
})
43-
}
44-
fs.readFile(`${templatePath}/${option}.dockerfile`, (err: any , data: Buffer) => {
45-
console.log(`${workspaceFolder}/Dockerfile`)
46-
fs.writeFileSync (`${workspaceFolder}/Dockerfile`, data);
47-
})
48-
})
20+
function CreateIgnoreDockerFile(option: any , templatePath: String, workspaceFolder: String): void {
21+
const data = fs.readFileSync(`${templatePath}/.dockerignore ${option}`);
22+
fs.writeFileSync(`${workspaceFolder}/.dockerignore`, data);
23+
}
4924

50-
})
25+
export function activate(context: vscode.ExtensionContext) {
26+
const templatePath = path.join(__dirname, '../templates');
5127

28+
const [ templates, allFiles ] :Array<Array<string>> = GetTemplates(templatePath);
29+
let disposable = vscode.commands.registerCommand('dockerfiletemplate.generatedockerfile', () => {
30+
const { workspace: { workspaceFolders } } = vscode;
31+
const workspaceFolder = workspaceFolders ? workspaceFolders[0].uri.fsPath : null;
32+
if(workspaceFolder){
33+
vscode.window.showQuickPick(templates).then(option => {
34+
if (!option) return;
35+
allFiles.forEach((file) => {
36+
// check if a ignore file exist
37+
if (file === `.dockerignore ${option}`) {
38+
vscode.window.showInformationMessage("A .dockerignore file exist for this template , do you want to use it ?", "yes", "no").then(response => {
39+
if (response === "yes") CreateIgnoreDockerFile(option , templatePath, workspaceFolder)
40+
})
41+
}
42+
CreateDockerFile(option, templatePath, workspaceFolder)
43+
})
44+
})
45+
}
46+
else {
47+
vscode.window.showInformationMessage("workspaceFolder not found")
48+
}
5249
})
53-
5450
context.subscriptions.push(disposable);
5551
}
5652

0 commit comments

Comments
 (0)