moe-file.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of Moeditor.
  3. *
  4. * Copyright (c) 2016 Menci <huanghaorui301@gmail.com>
  5. * Copyright (c) 2015 Thomas Brouard (for codes from Abricotine)
  6. *
  7. * Moeditor is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Moeditor is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Moeditor. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. 'use strict';
  21. const fs = require('fs'),
  22. mime = require('mime');
  23. // const lineSeparators=['\r\n','\r'];
  24. // let fileLineSeparators={};
  25. // function normalizeLineEndings(str) {
  26. // if (!str) return str;
  27. // return str.replace(/\r\n|\r/g, '\n');
  28. // }
  29. // function restoreLineEndings(fileName,str) {
  30. // if (!str) return str;
  31. //
  32. // let sep=fileLineSeparators[fileName]
  33. // if(sep===undefined){return str;}
  34. //
  35. // return str.replace(/\n/g,sep);
  36. // }
  37. class MoeditorFile {
  38. static isFile(fileName) {
  39. try {
  40. return fs.statSync(fileName).isFile();
  41. } catch (e) {
  42. return false;
  43. }
  44. }
  45. static isTextFile(fileName) {
  46. try {
  47. if (!fs.statSync(fileName).isFile()) return false;
  48. var type = mime.lookup(fileName);
  49. return type && type.substr(0, 4) === 'text';
  50. } catch (e) {
  51. return false;
  52. }
  53. }
  54. static isDirectory(fileName) {
  55. try {
  56. return fs.lstatSync(fileName).isDirectory();
  57. } catch (e) {
  58. return false;
  59. }
  60. }
  61. static read(fileName, empty) {
  62. try {
  63. // for(let i in lineSeparators){
  64. // let sep=lineSeparators[i];
  65. // if(content.indexOf(sep)>=0){
  66. // fileLineSeparators[fileName]=sep;break;
  67. // }
  68. // }
  69. // return normalizeLineEndings(content);
  70. return fs.readFileSync(fileName,"utf8");
  71. } catch(e) {
  72. return empty;
  73. }
  74. }
  75. static write(fileName, content) {
  76. // content=restoreLineEndings(fileName,content)
  77. return fs.writeFileSync(fileName, content);
  78. }
  79. static writeAsync(fileName, content, cb) {
  80. // content=restoreLineEndings(fileName,content)
  81. return fs.writeFile(fileName, content, cb);
  82. }
  83. static remove(fileName) {
  84. try {
  85. fs.unlinkSync(fileName);
  86. } catch(e) {
  87. ;
  88. }
  89. }
  90. }
  91. module.exports = MoeditorFile;