lib/file/chown.js
function chown(file, uid, gid, options) {
  if (!isPlatform('unix')) return;

  if (_.isObject(uid)) {
    options = gid;
    const ownerInfo = uid;
    uid = (ownerInfo.uid || ownerInfo.owner || ownerInfo.user || ownerInfo.username);
    gid = (ownerInfo.gid || ownerInfo.group);
  }
  options = _.sanitize(options, {abortOnError: true, recursive: false});
  let recurse = false;
  try {
    if (uid) uid = getUid(uid, {refresh: false});
    if (gid) gid = getGid(gid, {refresh: false});
    if (!exists(file)) throw new Error(`Path '${file}' does not exists`);
    recurse = options.recursive && isDirectory(file);
  } catch (e) {
    if (options.abortOnError) {
      throw e;
    } else {
      return;
    }
  }
  if (recurse) {
    _.each(listDirContents(file, {includeTopDir: true}), function(f) {
      _chown(f, uid, gid, options);
    });
  } else {
    _chown(file, uid, gid, options);
  }
}
function chown(file, uid, gid, options) {
  if (!isPlatform('unix')) return;

  if (_.isObject(uid)) {
    options = gid;
    const ownerInfo = uid;
    uid = (ownerInfo.uid || ownerInfo.owner || ownerInfo.user || ownerInfo.username);
    gid = (ownerInfo.gid || ownerInfo.group);
  }
  options = _.sanitize(options, {abortOnError: true, recursive: false});
  let recurse = false;
  try {
    if (uid) uid = getUid(uid, {refresh: false});
    if (gid) gid = getGid(gid, {refresh: false});
    if (!exists(file)) throw new Error(`Path '${file}' does not exists`);
    recurse = options.recursive && isDirectory(file);
  } catch (e) {
    if (options.abortOnError) {
      throw e;
    } else {
      return;
    }
  }
  if (recurse) {
    _.each(listDirContents(file, {includeTopDir: true}), function(f) {
      _chown(f, uid, gid, options);
    });
  } else {
    _chown(file, uid, gid, options);
  }
}
function chown ( file , uid , gid , options ) { if ( ! isPlatform ( 'unix' ) ) return ; if ( _ . isObject ( uid ) ) { options = gid ; const ownerInfo = uid ; uid = ( ownerInfo . uid || ownerInfo . owner || ownerInfo . user || ownerInfo . username ) ; gid = ( ownerInfo . gid || ownerInfo . group ) ; } options = _ . sanitize ( options , { abortOnError : true , recursive : false } ) ; let recurse = false ; try { if ( uid ) uid = getUid ( uid , { refresh : false } ) ; if ( gid ) gid = getGid ( gid , { refresh : false } ) ; if ( ! exists ( file ) ) throw new Error ( ` ${ file } ` ) ; recurse = options . recursive && isDirectory ( file ) ; } catch ( e ) { if ( options . abortOnError ) { throw e ; } else { return ; } } if ( recurse ) { _ . each ( listDirContents ( file , { includeTopDir : true } ) , function ( f ) { _chown ( f , uid , gid , options ) ; } ) ; } else { _chown ( file , uid , gid , options ) ; } }
Change file owner
@function $file~chown
@param {string} file - File which owner and grop will be modified
@param {string|number} uid - Username or uid number to set. Can be provided as null
@param {string|number} gid - Group or gid number to set. Can be provided as null
@param {Object} [options]
@param {boolean} [options.abortOnError=true] - Throw an error if the function fails to execute
@param {boolean} [options.recursive=false] - Change directory owner recursively
@example
// Set the owner of 'conf/my.cnf' to 'mysql'
$file.chown('conf/my.cnf', 'mysql');
@example
// Change 'plugins' directory owner user and group recursively
$file.chown('plugins', 'daemon', 'daemon', {recursive: true});
Change file owner