package io.wcm.maven.plugins.nodejs.mojo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

import org.apache.maven.plugin.logging.Log;

/**
 * Logs the output of the nodejs process
 */
final class NodejsOutputStreamHandler extends Thread {

  private static final Pattern ERROR_LOG_PATTERN = Pattern.compile(".*(ERROR|FAILED|ERR|npm error).*");
  private static final Pattern WARNING_LOG_PATTERN = Pattern.compile(".*(warn).*", Pattern.CASE_INSENSITIVE);

  private final InputStream inputStream;
  private final Log logger;

  public NodejsOutputStreamHandler(InputStream inputStream, Log logger) {
    this.inputStream = inputStream;
    this.logger = logger;
  }

  @Override
  public void run(){
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    try {
      while((line = reader.readLine()) != null) {
        if (ERROR_LOG_PATTERN.matcher(line).matches()) {
          logger.error(line);
        }
        else if (WARNING_LOG_PATTERN.matcher(line).matches()) {
          logger.warn(line);
        }
        else {
          logger.info(line);
        }
      }
    } catch (IOException e) {
      logger.error(e.getMessage());
    }
  }
}

@@ -1,8 +1,636 @@
+/*%0A * #%25L%0A * wcm.io%0A * %25%25%0A * Copyright (C) 2014 wcm.io%0A * %25%25%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A * #L%25%0A */%0A
 package 
@@ -1425,16 +1425,17 @@
 id run()
+ 
 %7B%0A    Bu
@@ -1550,16 +1550,17 @@
    while
+ 
 ((line =

add file header