directoryUtilities.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. import fileSystem = require('fs');
  6. import vscode = require('vscode');
  7. import path = require('path');
  8. /**
  9. *
  10. * @param path destination path
  11. */
  12. export function CreateDirectory(
  13. dest: string,
  14. mode: string | number | null | undefined = undefined
  15. ): boolean {
  16. try {
  17. if (fileSystem.existsSync(dest)) {
  18. if (fileSystem.lstatSync(dest).isDirectory()) {
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. }
  24. if (!path) {
  25. return false;
  26. }
  27. let parent = path.dirname(dest);
  28. if (!CreateDirectory(parent, mode)) {
  29. return false;
  30. }
  31. fileSystem.mkdirSync(dest, mode);
  32. return true;
  33. } catch (error) {
  34. vscode.window.showErrorMessage(error as string);
  35. return false;
  36. }
  37. }
  38. export function CopyFiles(src: string, dest: string, flags?: number): boolean {
  39. try {
  40. fileSystem.copyFileSync(src, dest);
  41. return true;
  42. } catch (error) {
  43. vscode.window.showErrorMessage(error as string);
  44. return false;
  45. }
  46. }
  47. export function WriteIntoFile(path: string, data: string): void {
  48. try {
  49. fileSystem.writeFileSync(path, data, null);
  50. } catch (err) {
  51. vscode.window.showErrorMessage(err as string);
  52. }
  53. }
  54. export function ReadFromFile(path: string): string {
  55. try {
  56. let data = fileSystem.readFileSync(path, { encoding: 'utf-8' });
  57. return data as string;
  58. } catch (err) {
  59. vscode.window.showErrorMessage(err as string);
  60. return '';
  61. }
  62. }
  63. export function WriteIntoFileAsync(
  64. path: string,
  65. data: string,
  66. callback: fileSystem.NoParamCallback
  67. ): void {
  68. try {
  69. fileSystem.writeFile(path, data, callback);
  70. } catch (err) {
  71. vscode.window.showErrorMessage(err as string);
  72. return;
  73. }
  74. }
  75. export function CheckIfDirectoryExist(path: string): boolean {
  76. try {
  77. if (fileSystem.existsSync(path)) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. } catch (err) {
  83. vscode.window.showErrorMessage(err as string);
  84. return false;
  85. }
  86. }