|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 | const path = require('path'); |
3 | 3 | import * as fs from 'fs'; |
4 | | -const templatePath = path.join(__dirname, '../templates'); |
5 | 4 | import * as _ from 'lodash'; |
6 | 5 |
|
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); |
11 | 8 | 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 | +} |
19 | 14 |
|
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 | +} |
29 | 19 |
|
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 | +} |
49 | 24 |
|
50 | | - }) |
| 25 | +export function activate(context: vscode.ExtensionContext) { |
| 26 | + const templatePath = path.join(__dirname, '../templates'); |
51 | 27 |
|
| 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 | + } |
52 | 49 | }) |
53 | | - |
54 | 50 | context.subscriptions.push(disposable); |
55 | 51 | } |
56 | 52 |
|
|
0 commit comments