You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.0 KiB

  1. var fs = require('fs'),
  2. path = require('path'),
  3. exec = require('child_process').exec,
  4. less = require('less'),
  5. sourcePath, targetPath;
  6. // sourcePath = 'src/styles.less';
  7. // targetPath = 'src/css/';
  8. sourcePath = 'src/app/theme'; // less檔來源路徑
  9. targetPath = 'src/assets/themes'; // 轉換為css後存檔路徑
  10. cssPath = 'assets/themes'; // 專案存取css路徑
  11. //自訂路徑:获取命令行中的路径
  12. process.argv.forEach(function (val, index, array) {
  13. console.log(val);
  14. if (index == 2) {
  15. sourcePath = val;
  16. }
  17. if (index == 3) {
  18. targetPath = val;
  19. }
  20. if (index == 4) {
  21. cssPath = val;
  22. }
  23. })
  24. var lessc = function (rootPath, targetPath, cssPath) {
  25. //**簡易測試寫法 */
  26. // //判断文件是否为less文件
  27. // if (path.extname(rootPath) === ".less") {
  28. // var currentFilePath = path.resolve(rootPath);
  29. // var newFilePath = path.resolve(targetPath, path.basename(currentFilePath, '.less') + ".css");
  30. // if (!fs.existsSync(targetPath)) {
  31. // fs.mkdirSync(targetPath);
  32. // }
  33. // // 方法一:用less render
  34. // less.render(fs.readFileSync(currentFilePath).toString(), {
  35. // filename: path.resolve(currentFilePath),
  36. // }, function (e, output) {
  37. // if (e) {
  38. // console.log(e);
  39. // }
  40. // fs.writeFileSync(newFilePath, output.css);
  41. // });
  42. // ////方法二:使用命令列lessc,但錯誤訊息不知如何列出
  43. // // exec("lessc -x" + currentFilePath + " > " + newFilePath);
  44. // }
  45. //**簡易測試寫法END */
  46. //取得当前绝对路径
  47. rootPath = path.resolve(rootPath);
  48. //目标路径绝对路径
  49. targetPath = path.resolve(targetPath);
  50. //判断目录是否存在
  51. fs.exists(rootPath, function (exists) {
  52. //路径存在
  53. if (exists) {
  54. var jsonArr=[];
  55. //获取当前路径下的所有文件和路径名
  56. var childArray = fs.readdirSync(rootPath);
  57. if (childArray.length) {
  58. var id = '';
  59. var themePath = '';
  60. var jsonFilePath = path.resolve(targetPath, "themeJson.json");
  61. for (var i = 0; i < childArray.length; i++) {
  62. var currentFilePath = path.resolve(rootPath, childArray[i]);
  63. var currentTargetPath = path.resolve(targetPath, childArray[i])
  64. //读取文件信息
  65. var stats = fs.statSync(currentFilePath);
  66. //若是目录则递归调用
  67. if (stats.isDirectory()) {
  68. //lessc(currentFilePath, currentTargetPath);
  69. } else {
  70. //判断文件是否为less文件
  71. if (path.extname(currentFilePath) === ".less") {
  72. var newFilePath = path.resolve(targetPath, path.basename(currentFilePath, '.less') + ".css");
  73. id = path.basename(currentFilePath, '.less');
  74. themePath = cssPath + '/' + id + '.css';
  75. if (!fs.existsSync(targetPath)) {
  76. fs.mkdirSync(targetPath);
  77. }
  78. // 方法一:用less render
  79. (function(currentFilePath, newFilePath, id, themePath) {
  80. less.render(fs.readFileSync(currentFilePath).toString(), {
  81. filename: path.resolve(currentFilePath),
  82. javascriptEnabled: true
  83. }, function (e, output) {
  84. if (e) {
  85. console.log(e); // 錯誤訊息
  86. }
  87. console.log(newFilePath);
  88. jsonArr.push({"id":id, "path":themePath});
  89. fs.writeFileSync(newFilePath, output.css); // 寫入css
  90. fs.writeFileSync(jsonFilePath,JSON.stringify(jsonArr)); // 產生css名稱及路徑
  91. });
  92. })(currentFilePath, newFilePath, id, themePath);
  93. ////方法二:使用命令列lessc,但錯誤訊息不知如何列出
  94. // exec("lessc -x " + currentFilePath + " > " + newFilePath);
  95. }
  96. }
  97. }
  98. }
  99. } else {
  100. console.log("directory is not exists");
  101. }
  102. });
  103. }
  104. lessc(sourcePath, targetPath, cssPath);