summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/test-tools/wamr-ide/VSCode-Extension/src/utilities/directoryUtilities.ts
blob: 0efbea5d905a87ef323dbe2e4e680e61e4843ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

import fileSystem = require('fs');
import vscode = require('vscode');
import path = require('path');
import os = require('os');
import request = require('request');
import yauzl = require('yauzl');

/**
 *
 * @param path destination path
 */
export function createDirectory(
    dest: string,
    mode: string | number | null | undefined = undefined
): boolean {
    try {
        if (fileSystem.existsSync(dest)) {
            if (fileSystem.lstatSync(dest).isDirectory()) {
                return true;
            } else {
                return false;
            }
        }

        if (!path) {
            return false;
        }

        const parent = path.dirname(dest);
        if (!createDirectory(parent, mode)) {
            return false;
        }

        fileSystem.mkdirSync(dest, mode);
        return true;
    } catch (error) {
        vscode.window.showErrorMessage(error as string);
        return false;
    }
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function copyFiles(src: string, dest: string, flags?: number): boolean {
    try {
        fileSystem.copyFileSync(src, dest);
        return true;
    } catch (error) {
        vscode.window.showErrorMessage(error as string);
        return false;
    }
}

export function writeIntoFile(path: string, data: string): void {
    try {
        fileSystem.writeFileSync(path, data, null);
    } catch (err) {
        vscode.window.showErrorMessage(err as string);
    }
}

export function readFromFile(path: string): string {
    try {
        const data = fileSystem.readFileSync(path, { encoding: 'utf-8' });
        return data as string;
    } catch (err) {
        vscode.window.showErrorMessage(err as string);
        return '';
    }
}

export function writeIntoFileAsync(
    path: string,
    data: string,
    callback: fileSystem.NoParamCallback
): void {
    try {
        fileSystem.writeFile(path, data, callback);
    } catch (err) {
        vscode.window.showErrorMessage(err as string);
        return;
    }
}

export function checkIfPathExists(path: string): boolean {
    try {
        if (fileSystem.existsSync(path)) {
            return true;
        } else {
            return false;
        }
    } catch (err) {
        vscode.window.showErrorMessage(err as string);
        return false;
    }
}

export function checkIfDirectoryExists(path: string): boolean {
    const doesPathExist = checkIfPathExists(path);
    if (doesPathExist) {
        return fileSystem.lstatSync(path).isDirectory();
    }
    return false;
}

export function checkIfFileExists(path: string): boolean {
    const doesPathExist = checkIfPathExists(path);
    if (doesPathExist) {
        return fileSystem.lstatSync(path).isFile();
    }
    return false;
}

export function checkFolderName(folderName: string): boolean {
    let invalidCharacterArr: string[] = [];
    let valid = true;

    if (folderName.length > 255) {
        valid = false;
    }

    if (os.platform() === 'win32') {
        invalidCharacterArr = ['\\', '/', ':', '?', '*', '"', '|', '<', '>'];
    } else if (os.platform() === 'linux' || os.platform() === 'darwin') {
        invalidCharacterArr = ['/'];
    }

    invalidCharacterArr.forEach(function (c) {
        if (folderName.indexOf(c) !== -1) {
            valid = false;
        }
    });

    return valid;
}

export function downloadFile(
    url: string,
    destinationPath: string
): Promise<void> {
    return new Promise((resolve, reject) => {
        const file = fileSystem.createWriteStream(destinationPath);
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        const stream = request(url, undefined, (error, response, body) => {
            if (response.statusCode !== 200) {
                reject(
                    new Error(
                        `Download from ${url} failed with ${response.statusMessage}`
                    )
                );
            }
        }).pipe(file);
        stream.on('close', resolve);
        stream.on('error', reject);
    });
}

export function unzipFile(
    sourcePath: string,
    getDestinationFileName: (entryName: string) => string
): Promise<string[]> {
    return new Promise((resolve, reject) => {
        const unzippedFilePaths: string[] = [];
        yauzl.open(
            sourcePath,
            { lazyEntries: true },
            function (error, zipfile) {
                if (error) {
                    reject(error);
                    return;
                }
                zipfile.readEntry();
                zipfile.on('entry', function (entry) {
                    // This entry is a directory so skip it
                    if (/\/$/.test(entry.fileName)) {
                        zipfile.readEntry();
                        return;
                    }

                    zipfile.openReadStream(entry, function (error, readStream) {
                        if (error) {
                            reject(error);
                            return;
                        }
                        readStream.on('end', () => zipfile.readEntry());
                        const destinationFileName = getDestinationFileName(
                            entry.fileName
                        );
                        fileSystem.mkdirSync(
                            path.dirname(destinationFileName),
                            { recursive: true }
                        );

                        const file =
                            fileSystem.createWriteStream(destinationFileName);
                        readStream.pipe(file).on('error', reject);
                        unzippedFilePaths.push(destinationFileName);
                    });
                });
                zipfile.on('end', function () {
                    zipfile.close();
                    resolve(unzippedFilePaths);
                });
            }
        );
    });
}