yo generator 对象 方法

templatePath : 源路径,相对于根源路径
destinationPath : 目的路径,相对于根目的路径
destinationRoot : 根目的路径,可以通过destinationRoot('XX')来设置根目的路径
sourceRoot : 根源路径,可以通过sourceRoot(‘XX’)来设置根源路径


resolved : 当前index.js
appname :当前generator 名字
props : 当前prompt 存储变量
copy :复制方法
function copy(source, destination, process) {
  var file = this._prepCopy(source, destination, process);

  try {
    file.body = this.engine(file.body, this);
  } catch (err) {
    // this happens in some cases when trying to copy a JS file like lodash/underscore
    // (conflicting the templating engine)
  }
  this.fs.copy(file.source, file.destination, {
    process: function () {
      return file.body;
    }
  });

  return this;
}



this.copy 执行prepCopy之后调用this.fs.copy方法处理.

_prepCopy : 预复制方法,主要处理一些路径等方面的初始化
function _prepCopy(source, destination, process) {
  destination = destination || source;

  if (typeof destination === 'function') {
    process = destination;
    destination = source;
  }

  source = isPathAbsolute(source) ? source : path.join(this.sourceRoot(), source);
  destination = isPathAbsolute(destination) ? destination : path.join(this.destinationRoot(), destination);

  var encoding = null;
  var body = fs.readFileSync(source);
  var isText = istextorbinary.isTextSync(undefined, body);

  if (isText) {
    body = body.toString();

    if (typeof process === 'function') {
      body = process(body, source, destination, {
        encoding: encoding
      });
    }
  }

  return {
    body: body,
    encoding: encoding,
    destination: destination,
    source: source
  };
}


template : 复制方法,先使用engine进行模板parser
function template(source, destination, data, options) {
  if (!destination || !isPathAbsolute(destination)) {
    destination = path.join(
      this.destinationRoot(),
      this.engine(destination || source, data || this, options)
    );
  }

  if (!isPathAbsolute(source)) {
    source = path.join(
      this.sourceRoot(),
      this.engine(source, data || this, options)
    );
  }

  var body = this.engine(this.fs.read(source), data || this, options);

  // Using copy to keep the file mode of the previous file
  this.fs.copy(source, destination, {
    process: function () {
      return body;
    }
  });

  return this;
}



directory : 复制目录方法
function directory(source, destination, process, bulk) {
  // Only add sourceRoot if the path is not absolute
  var root = isPathAbsolute(source) ? source : path.join(this.sourceRoot(), source);
  var files = glob.sync('**', { dot: true, nodir: true, cwd: root });

  destination = destination || source;

  if (typeof destination === 'function') {
    process = destination;
    destination = source;
  }

  var cp = this.copy;

  if (bulk) {
    cp = this.bulkCopy;
  }

  // get the path relative to the template root, and copy to the relative destination
  for (var i in files) {
    var dest = path.join(destination, files[i]);
    cp.call(this, path.join(root, files[i]), dest, process);
  }

  return this;
}


runInstall : install 所安装依赖
installDependencies : install 所安装依赖
bowerInstall : install using bower
npmInstall : install using npm 
spawnCommand : 执行其他命令
prompt : 
invoke : 
composeWith : 调用sub-generator 或者其他generator






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值