varlet源码复盘
正在加载今日诗词....
2022-05-03

所使用到的npm包盘点

  • fs-extra

    • outputFileSync和writeFileSync基本相同,除了当父目录不存在的时候会自动创建, file必须是文件路径

      const fs = require('fs-extra')
      
      const file = '/tmp/this/path/does/not/exist/file.txt'
      fs.outputFileSync(file, 'hello!')
      
      const data = fs.readFileSync(file, 'utf8')
      console.log(data) // => hello!
      
    • removeSync, 删除文件或者目录,如果目录为空则什么事情都不做

      const fs = require('fs-extra')
      
      // remove file
      fs.removeSync('/tmp/myfile')
      
      fs.removeSync('/home/jprichardson') // I just deleted my entire HOME directory.
      
    • readdirSync, 同步读取目录内容

  • lodash

    • merge

      该方法类似_.assign, 除了它递归合并 sources 来源对象自身和继承的可枚举属性到 object 目标对象。如果目标值存在,被解析为undefinedsources 来源对象属性将被跳过。数组和普通对象会递归合并,其他对象和值会被直接分配覆盖。源对象从从左到右分配。后续的来源对象属性会覆盖之前分配的属性。

      Note: 这方法会改变对象 object.

      var object = {
        'a': [{ 'b': 2 }, { 'd': 4 }]
      };
       
      var other = {
        'a': [{ 'c': 3 }, { 'e': 5 }]
      };
       
      _.merge(object, other);
      // => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
      
  • webfoot

  • execa https://www.npmjs.com/package/execa

  • @vue/compiler-sfc https://www.npmjs.com/package/@vue/compiler-sfc

javascript相关盘点

  • json

    • JSON.stringify

      // 加上2确保写入文件的时候,json不会压缩在一起
      JSON.stringify(mergedConfig, null, 2)
      
  • Array.filter(Boolean) 过滤掉false的元素 (空字符串, 0 , false)

nodejs相关源码盘点

  • require

    • 删除require的缓存, 保证修改配置文件的时候,能够加载到最新的内容

       delete require.cache[require.resolve(VNPN_CONFIG)]
      
  • Promise

  • path

    • path.relative()方法用于根据当前工作目录查找从给定路径到另一个路径的相对路径。如果两个给定路径相同,则它将解析为零长度的字符串。

      // Node.js program to demonstrate the     
      // path.relative() method  
           
      // Import the path module 
      const path = require('path'); 
         
      path1 = path.relative("geeks/website", "geeks/index.html"); 
      console.log(path1) 
         
      path2 = path.relative("users/admin", "admin/files/website"); 
      console.log(path2) 
         
      // When both the paths are same 
      // It returns blank string 
      path3 = path.relative("users/admin", "users/admin"); 
      console.log(path3)
      

其他

Copyright © 2022 @filway

  • ☀️
  • 🌑