<commit_before>// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import AxiomError from 'axiom/core/error';
import Completer from 'axiom/core/completer';
import Path from 'axiom/fs/path';

/** @typedef JsExecuteContext$$module$axiom$fs$js$execute_context */
var JsExecuteContext;

/**
 * @param {JsExecuteContext} cx
 * @return {void}
 */
export var main = function(cx) {
  cx.ready();
  // We might need to read from stdin: pause it so no data is lost.
  cx.stdin.pause();

  var list = cx.getArg('_', []);
  if (!list.length || cx.getArg('help')) {
    cx.stdout.write([
      'usage: cat <file>|-...',
      'Reads files sequentially, writing them to the standard output.',
      'The file operands are processed in command-line order.',
      'If file is a single dash (`-`) or absent, cat reads from the standard',
      'input.'
    ].join('\r\n') + '\r\n');
    cx.closeOk();
    return;
  }

  var fileSystem = cx.fileSystemManager;
  var errors = false;
  var readStdin = false;
  var stdinReader;

  var catNext = function() {
    if (!list.length) {
      return null;
    }

    /** @type {Promise} */
    var promise;
    /** @type {string} */
    var pathSpec = list.shift();
  
    if (pathSpec === '-') {
      if (!stdinReader) {
        stdinReader = new Completer();
        var stdinInput = '';
        cx.stdin.onData.addListener(function(data) {
          stdinInput += data;
        });
        cx.stdin.onEnd.listenOnce(function() {
          stdinReader.resolve(stdinInput);
        });
      }
      promise = stdinReader.promise;
    } else {
      promise = fileSystem.readFile(Path.abs(cx.getPwd(), pathSpec))
        .then(function(result) {
          return result.data;
        });
    }

    return promise.then(function(data) {
      cx.stdout.write(data);
      return catNext();
    }).catch(function(e) {
      errors = true;
      cx.stdout.write('cat ' + pathSpec + ': ' + e.toString() + '\n');
      return catNext();
    });
  };

  catNext().then(function() {
    if (errors) {
      cx.closeError(new AxiomError.Runtime('Some files could not be read'));
    } else {
      cx.closeOk();
    }
  });

  // Delay resuming on purpose to support possible multiple occurances of '-':
  //   `echo something | cat - foo.txt - bar.txt`.
  if (stdinReader) {
    cx.stdin.resume();
  }
};

export default main;

main.signature = {
  'help|h': '?',
  "_": '@'
};<commit_msg>Fix case `echo abc | cat foo.txt -`, which used to hang<commit_after>// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import AxiomError from 'axiom/core/error';
import Completer from 'axiom/core/completer';
import Path from 'axiom/fs/path';

/** @typedef JsExecuteContext$$module$axiom$fs$js$execute_context */
var JsExecuteContext;

/**
 * @param {JsExecuteContext} cx
 * @return {void}
 */
export var main = function(cx) {
  cx.ready();

  var list = cx.getArg('_', []);
  if (!list.length || cx.getArg('help')) {
    cx.stdout.write([
      'usage: cat <file>|-...',
      'Reads files sequentially, writing them to the standard output.',
      'The file operands are processed in command-line order.',
      'If file is a single dash (`-`) or absent, cat reads from the standard',
      'input.'
    ].join('\r\n') + '\r\n');
    cx.closeOk();
    return;
  }

  var errors = false;
  var stdinReader = new Completer();

  if (list.indexOf('-') !== -1) {
    var stdinInput = '';
    cx.stdin.pause();
    cx.stdin.onData.addListener(function(data) {
      stdinInput += data;
    });
    cx.stdin.onEnd.listenOnce(function() {
      stdinReader.resolve(stdinInput);
    });
    cx.stdin.resume();
  }

  var catNext = function() {
    if (!list.length) {
      return null;
    }

    /** @type {Promise} */
    var promise;
    /** @type {string} */
    var pathSpec = list.shift();
  
    if (pathSpec === '-') {
      promise = stdinReader.promise;
    } else {
      promise = cx.fileSystemManager.readFile(Path.abs(cx.getPwd(), pathSpec))
        .then(function(result) {
          return result.data;
        });
    }

    return promise.then(function(data) {
      cx.stdout.write(data);
      return catNext();
    }).catch(function(e) {
      errors = true;
      cx.stdout.write('cat ' + pathSpec + ': ' + e.toString() + '\n');
      return catNext();
    });
  };

  catNext().then(function() {
    if (errors) {
      cx.closeError(new AxiomError.Runtime('Some files could not be read'));
    } else {
      cx.closeOk();
    }
  });
};

export default main;

main.signature = {
  'help|h': '?',
  "_": '@'
};
{'fraction_non_alphanumeric': 0.1516973125884017, 'fraction_numerical': 0.003005657708628006, 'mean_word_length': 2.6520335700451905, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7087608', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1760, 'n_words': 514}