javascript - 从 js 文件中获取对象作为字符串读取并使用 gulp 处理成单个文件

标签 javascript node.js ecmascript-6 gulp

我正在尝试使用 Gulp 读取 TypographyJS 文件,以构建用于轻松下载相关 Google 字体的字体列表。

到目前为止,我的任务如下:

gulp.task('buildFontListFile', function (event) {
  return gulp.src(`node_modules/+(typography-theme*)/dist/index.js`)
    .pipe(debug({title: 'typography-theme:'}))
    .pipe(gulp.dest('gulpTest/fonts'));
});

所以我正确地定位了我需要的文件,但目前只是将副本转储到新目录中。我正在阅读的文件如下所示:

'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});


var theme = {
  title: 'Noriega',
  baseFontSize: '18px',
  baseLineHeight: 1.61,
  headerFontFamily: ['Lato', 'sans-serif'],
  bodyFontFamily: ['Lato', 'sans-serif'],
  bodyWeight: 400,
  headerWeight: 700,
  boldWeight: 700,
  googleFonts: [{
    name: 'Lato',
    styles: ['400', '700']
  }],
  scaleRatio: 1.618
};
exports.default = theme;

我需要做的是从默认导出中获取 googleFonts 键,并将每个条目处理为如下所示的行:

Lato:400,700&subset=latin

以这种格式输出 key 的内容应该很容易,但读取文件的内容是我目前无法弄清楚的。

在最终输出如下所示的文件之前,读取这些文件的内容并进行一些处理的最佳方法是什么?

Lato:400,700&subset=latin
Source+Sans+Pro:200,400,700&subset=latin,latin-ext

最佳答案

试试这个:

const gulp = require("gulp");
const glob = require('glob');
const fs = require('fs');

gulp.task('default', function () {

  // like gulp.src but makes it easy to work on individual files one at a time
  glob("node_modules/+(typography-theme*)/dist/index.js", function (er, files) {

    // used later to avoid an extra newline after the last entry added
    const numFiles = files.length;
    let thisFileNum = 1;

    files.forEach(file => {

      const contents = fs.readFileSync(file, "utf8");

      // look for "name: 'Lato',"
      let fontName = contents.match(/name:\s*'(.*)'/);

      // look for "styles: ['400', '700']"
      let fontStyle = contents.match(/styles:\s*\['(.*)'\]/);

      // get rid of the ' and , in fontStyle [the regexp returns ""'400', '700']""
      fontStyle = fontStyle[1].replace(/,|'/g, "");

      // in fontNames like "Souce Sans Pro", replace the spaces with +'s
      fontName = fontName[1].replace(/\s+/g, "+");

      // in fontStyles like "200 400 700", replace the spaces with ,'s
      fontStyle = fontStyle.replace(/\s+/g, ",");

      // now build the next line to append to the file 
      let line = `${fontName}:${fontStyle}&subset=latin`;
      // I don't know where you got the 'latin-ext' on one of your fonts

      // if the last file, omit the newline
      if (thisFileNum === numFiles) {
        fs.appendFileSync('modified.txt', line);
      }
      else {
        fs.appendFileSync('modified.txt', line + '\n');
      }

      thisFileNum++;
    });
  });
});

我不知道新文件条目的顺序是否得到保证。

关于javascript - 从 js 文件中获取对象作为字符串读取并使用 gulp 处理成单个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51119685/

相关文章:

node.js - 如何在 Sequelize 中使用 bulkDelete 截断表和级联?

javascript - 有什么理由不使用 ES6 类?

javascript - Nodejs中的回调错误参数命名约定

javascript - 三个 js : I wanted to achieve something shown in the image, 我该怎么做,我试过 BoxGeomatry 和 planeGeomatry 也不一样

javascript - 如何关闭覆盖屏幕的div

javascript - 如何打印span标签中变量的值?

javascript - 客户端js获取node.js API调用

javascript - 用于 es6(弱) map 、集合的正确 React 组件 PropTypes

JavaScript:了解生成器和生成序列的使用

javascript - 每个循环语法的 typescript 错误