|
| 1 | +const vscode = require('vscode'); |
| 2 | +const fileSystem = require('fs'); |
| 3 | +const imageUtils = require('./src/image_utils'); |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {vscode.ExtensionContext} context |
| 7 | + */ |
| 8 | +const activate = (context) => { |
| 9 | + |
| 10 | + const command = 'flutter-image-assets.generate'; |
| 11 | + const commandHandler = async (args) => { |
| 12 | + const destinationPath = getDestinationPath(args); |
| 13 | + |
| 14 | + if (destinationPath) { |
| 15 | + const imagePath = await getImagePath(); |
| 16 | + |
| 17 | + // generating images for (1x, 2x, and 3x) |
| 18 | + for (let mode = 1; mode < 4; mode++) { |
| 19 | + imageUtils.scaleImageToMode(destinationPath, imagePath, mode); |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + context.subscriptions.push(vscode.commands.registerCommand(command, commandHandler)); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @param {string} args |
| 29 | + */ |
| 30 | +const getDestinationPath = (args) => { |
| 31 | + if (args) { |
| 32 | + const uri = vscode.Uri.parse(args); |
| 33 | + const path = uri.fsPath; |
| 34 | + const exists = fileSystem.existsSync(path); |
| 35 | + const isDirectoryAndExists = exists ? isDirectory(path) : false; |
| 36 | + |
| 37 | + if (isDirectoryAndExists) { |
| 38 | + return path; |
| 39 | + } else { |
| 40 | + vscode.window.showErrorMessage(`The '${path}' isn't a directory.`); |
| 41 | + } |
| 42 | + } else { |
| 43 | + vscode.window.showErrorMessage(`Can't run the command without a destination directory.`); |
| 44 | + } |
| 45 | +} |
| 46 | +/** |
| 47 | + * |
| 48 | + * @returns The path of the image file that where selected through the vscode open dialog |
| 49 | + */ |
| 50 | +const getImagePath = async () => { |
| 51 | + const options = { |
| 52 | + canSelectMany: false, |
| 53 | + openLabel: 'Select Image', |
| 54 | + filters: { 'Images': ['png', 'jpg', 'jpeg', 'JPG', 'JPEG'] } |
| 55 | + }; |
| 56 | + |
| 57 | + const uri = await vscode.window.showOpenDialog(options); |
| 58 | + if (uri && uri[0]) { |
| 59 | + return uri[0].fsPath; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * @param {string} path |
| 65 | + */ |
| 66 | +const isDirectory = (path) => { |
| 67 | + try { |
| 68 | + return fileSystem.lstatSync(path).isDirectory(); |
| 69 | + } catch (e) { |
| 70 | + return false; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// this method is called when your extension is deactivated |
| 75 | +const deactivate = () => { } |
| 76 | + |
| 77 | +module.exports = { |
| 78 | + activate, |
| 79 | + deactivate |
| 80 | +} |
0 commit comments