Dashboard sipadu mbip
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

File.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. let os = require('os');
  2. let md5 = require('md5');
  3. let path = require('path');
  4. let fs = require('fs-extra');
  5. let Terser = require('terser');
  6. let UglifyCss = require('clean-css');
  7. class File {
  8. /**
  9. * Create a new instance.
  10. *
  11. * @param {string} filePath
  12. */
  13. constructor(filePath) {
  14. this.absolutePath = path.resolve(filePath);
  15. this.filePath = this.relativePath();
  16. this.segments = this.parse();
  17. }
  18. /**
  19. * Static constructor.
  20. *
  21. * @param {string} file
  22. */
  23. static find(file) {
  24. return new File(file);
  25. }
  26. /**
  27. * Get the size of the file.
  28. */
  29. size() {
  30. return fs.statSync(this.path()).size;
  31. }
  32. /**
  33. * Determine if the given file exists.
  34. *
  35. * @param {string} file
  36. */
  37. static exists(file) {
  38. return fs.existsSync(file);
  39. }
  40. /**
  41. * Delete/Unlink the current file.
  42. */
  43. delete() {
  44. if (fs.existsSync(this.path())) {
  45. fs.unlinkSync(this.path());
  46. }
  47. }
  48. /**
  49. * Get the name of the file.
  50. */
  51. name() {
  52. return this.segments.file;
  53. }
  54. /**
  55. * Get the name of the file, minus the extension.
  56. */
  57. nameWithoutExtension() {
  58. return this.segments.name;
  59. }
  60. /**
  61. * Get the extension of the file.
  62. */
  63. extension() {
  64. return this.segments.ext;
  65. }
  66. /**
  67. * Get the absolute path to the file.
  68. */
  69. path() {
  70. return this.absolutePath;
  71. }
  72. /**
  73. * Get the relative path to the file, from the project root.
  74. */
  75. relativePath() {
  76. return path.relative(Mix.paths.root(), this.path());
  77. }
  78. /**
  79. * Get the absolute path to the file, minus the extension.
  80. */
  81. pathWithoutExtension() {
  82. return this.segments.pathWithoutExt;
  83. }
  84. /**
  85. * Force the file's relative path to begin from the public path.
  86. *
  87. * @param {string|null} publicPath
  88. */
  89. forceFromPublic(publicPath) {
  90. publicPath = publicPath || Config.publicPath;
  91. if (!this.relativePath().startsWith(publicPath)) {
  92. return new File(path.join(publicPath, this.relativePath()));
  93. }
  94. return this;
  95. }
  96. /**
  97. * Get the path to the file, starting at the project's public dir.
  98. *
  99. * @param {string|null} publicPath
  100. */
  101. pathFromPublic(publicPath) {
  102. publicPath = publicPath || Config.publicPath;
  103. let extra = this.filePath.startsWith(publicPath) ? publicPath : '';
  104. return this.path().replace(Mix.paths.root(extra), '');
  105. }
  106. /**
  107. * Get the base directory of the file.
  108. */
  109. base() {
  110. return this.segments.base;
  111. }
  112. /**
  113. * Determine if the file is a directory.
  114. */
  115. isDirectory() {
  116. return this.segments.isDir;
  117. }
  118. /**
  119. * Determine if the path is a file, and not a directory.
  120. */
  121. isFile() {
  122. return this.segments.isFile;
  123. }
  124. /**
  125. * Write the given contents to the file.
  126. *
  127. * @param {string} body
  128. */
  129. write(body) {
  130. if (typeof body === 'object') {
  131. body = JSON.stringify(body, null, 4);
  132. }
  133. body = body + os.EOL;
  134. fs.writeFileSync(this.absolutePath, body);
  135. return this;
  136. }
  137. /**
  138. * Read the file's contents.
  139. */
  140. read() {
  141. return fs.readFileSync(this.path(), 'utf8');
  142. }
  143. /**
  144. * Calculate the proper version hash for the file.
  145. */
  146. version() {
  147. return md5(this.read()).substr(0, 20);
  148. }
  149. /**
  150. * Create all nested directories.
  151. */
  152. makeDirectories() {
  153. fs.ensureDirSync(this.base());
  154. return this;
  155. }
  156. /**
  157. * Copy the current file to a new location.
  158. *
  159. * @param {string} destination
  160. */
  161. copyTo(destination) {
  162. fs.copySync(this.path(), destination);
  163. return this;
  164. }
  165. /**
  166. * Minify the file, if it is CSS or JS.
  167. */
  168. minify() {
  169. if (this.extension() === '.js') {
  170. this.write(
  171. Terser.minify(this.read(), Config.terser.terserOptions).code
  172. );
  173. }
  174. if (this.extension() === '.css') {
  175. this.write(
  176. new UglifyCss(Config.cleanCss).minify(this.read()).styles
  177. );
  178. }
  179. return this;
  180. }
  181. /**
  182. * Rename the file.
  183. *
  184. * @param {string} to
  185. */
  186. rename(to) {
  187. to = path.join(this.base(), to);
  188. fs.renameSync(this.path(), to);
  189. return new File(to);
  190. }
  191. /**
  192. * It can append to the current path.
  193. *
  194. * @param {string} append
  195. */
  196. append(append) {
  197. return new File(path.join(this.path(), append));
  198. }
  199. /**
  200. * Determine if the file path contains the given text.
  201. *
  202. * @param {string} text
  203. */
  204. contains(text) {
  205. return this.path().includes(text);
  206. }
  207. /**
  208. * Parse the file path.
  209. */
  210. parse() {
  211. let parsed = path.parse(this.absolutePath);
  212. return {
  213. path: this.filePath,
  214. absolutePath: this.absolutePath,
  215. pathWithoutExt: path.join(parsed.dir, `${parsed.name}`),
  216. isDir: !parsed.ext && !parsed.name.endsWith('*'),
  217. isFile: !!parsed.ext,
  218. name: parsed.name,
  219. ext: parsed.ext,
  220. file: parsed.base,
  221. base: parsed.dir
  222. };
  223. }
  224. }
  225. module.exports = File;