_check-assets.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. module.exports = function (){
  2. var log = console.log
  3. var path = require('path')
  4. var fs = require('fs')
  5. var _ = require('../node_modules/grunt-contrib-compress/node_modules/archiver/node_modules/lodash')
  6. var cwd = path.join(__dirname, '..')
  7. var source = path.join(cwd, 'src', 'css')
  8. var target = path.join(cwd, 'src')
  9. var targetIgnore = ['.svn', 'css', 'data', 'js', 'partial', 'sass', '.html']
  10. var errMsg
  11. // 1. sync read source dir, get [sourceList]
  12. // [calcSource] by sourceList
  13. // 2. sync read target dir, get [targetList]
  14. // [calcTarget] by targetList
  15. // 3. pull calcSource's item out, which exist in [calcTarget]
  16. // if calcSource still have item, means some url not found, then format message
  17. // main
  18. var calcSource = getCalcSource(source)
  19. var calcTarget = listFiles(target, targetIgnore)
  20. _.each(calcTarget, function(t){
  21. _.pull(calcSource, t)
  22. })
  23. calcSource = _.map(calcSource, function(s){
  24. return path.relative(cwd, s)
  25. })
  26. errMsg = showErrorMsg(calcSource)
  27. return errMsg;
  28. function getCalcSource(source){
  29. var sourceList = getSourceList(source)
  30. var calcSource = sourceList.map(function(p){
  31. return path.join(source, p)
  32. })
  33. return calcSource
  34. }
  35. function getSourceList(source){
  36. var sourceList = []
  37. var reg = /url\(["']?(?!http[s]?)([\w\d\s!.\/\-\_]*\.[\w?#]+)["']?\)/gm
  38. var files = listFiles(source)
  39. _.each(files, function(f){
  40. var text = fs.readFileSync(f, 'utf8')
  41. while(match = reg.exec(text)){
  42. sourceList.push(match[1])
  43. }
  44. })
  45. sourceList = _clearRepeat(sourceList)
  46. return sourceList
  47. }
  48. // utils
  49. function showErrorMsg(arr){
  50. var len = +arr.length, msg;
  51. if(len>0){
  52. msg = arr.join('\n')
  53. return 'files no found: '+'\n'+msg;
  54. }
  55. return false;
  56. }
  57. // excldList: 1)suffix file; 2)dir
  58. function listFiles (f, excldList) {
  59. var filelist = [], dirlist = [];
  60. var dir = fs.lstatSync(f).isDirectory() ? f : path.dirname(f);
  61. var filelistTemp = fs.readdirSync(dir).filter(function (file) {
  62. return fs.statSync(path.join(dir, file)).isFile() && !_hasItem(excldList, file);
  63. }).map(function(file){
  64. return path.join(dir, file)
  65. })
  66. filelist = filelist.concat(filelistTemp)
  67. var dirlist = fs.readdirSync(dir).filter(function (file) {
  68. return fs.statSync(path.join(dir, file)).isDirectory() && !_hasItem(excldList, file);
  69. }).map(function(file){
  70. return path.join(dir, file)
  71. }).forEach(function(f){
  72. var filelistTemp = listFiles.call(this, f, excldList)
  73. filelist = filelist.concat(filelistTemp)
  74. })
  75. return filelist;
  76. };
  77. function _hasItem(arr, item){
  78. arr = arr||[]
  79. var result = false
  80. arr.forEach(function(e){
  81. if(e === item){result=true}
  82. })
  83. return result
  84. }
  85. function _clearRepeat(arr){
  86. var result = []
  87. for (var i = arr.length - 1; i >= 0; i--) {
  88. if(!_hasItem(result, arr[i])){
  89. result.push(arr[i])
  90. }
  91. };
  92. return result
  93. }
  94. }