import gulp from "gulp";
import md5 from "gulp-md5-plus";
import stylelint from "gulp-stylelint";
import cssnano from "gulp-cssnano";
import eslint from "gulp-eslint";
import sourcemaps from "gulp-sourcemaps";
import babel from "gulp-babel";
import uglify from "gulp-uglify";
import merge from "merge2";
import del from "del";
import { argv } from 'yargs';

const root = `${argv.root}/src/main`;

gulp.task("clean", () => del(`${root}/dist/web`, { force: true }));

gulp.task("resource", () => {
    return gulp.src([`${root}/web/**/*.*`, `!${root}/web/static/css/**/*.css`, `!${root}/web/static/js/**/*.js`])
        .pipe(gulp.dest(`${root}/dist/web`));
});

gulp.task("css", ["resource"], () => {
    const appCSS = gulp.src([`${root}/web/static/css/**/*.css`, `!${root}/web/static/css/lib{,/**/*.css}`])
        .pipe(sourcemaps.init())
        .pipe(stylelint({
            configFile: "stylelint.json", // see https://github.com/stylelint/stylelint-config-standard/blob/master/index.js
            reporters: [{
                formatter: "string",
                console: true
            }]
        }))
        .pipe(cssnano({
            zindex: false
        }))
        .pipe(md5(10, `${root}/dist/web/template/**/*.html`, {
            dirLevel: 2
        }))
        .pipe(sourcemaps.write("."))
        .pipe(gulp.dest(`${root}/dist/web/static/css`));

    const libCSS = gulp.src(`${root}/web/static/css/lib/**/*.css`)
        .pipe(md5(10, `${root}/dist/web/template/**/*.html`, {
            dirLevel: 2
        }))
        .pipe(gulp.dest(`${root}/dist/web/static/css/lib`));

    return merge(appCSS, libCSS);
});

gulp.task("js", ["resource"], () => {
    const appJS = gulp.src([`${root}/web/static/js/**/*.js`, `!${root}/web/static/js/lib{,/**/*.js}`])
        .pipe(eslint({
            configFile: "eslint.json",
            parserOptions: {
                "ecmaVersion": 2015
            }
        }))
        .pipe(eslint.format())
        .pipe(eslint.failAfterError())
        .pipe(sourcemaps.init())
        .pipe(babel({
            presets: ["babel-preset-es2015"].map(require.resolve)
        }))
        .pipe(uglify())
        .pipe(md5(10, `${root}/dist/web/template/**/*.html`, {
            dirLevel: 2
        }))
        .pipe(sourcemaps.write("."))
        .pipe(gulp.dest(`${root}/dist/web/static/js`));

    const libJS = gulp.src([`${root}/web/static/js/lib/**/*.js`])
        .pipe(md5(10, `${root}/dist/web/template/**/*.html`, {
            dirLevel: 2
        }))
        .pipe(gulp.dest(`${root}/dist/web/static/js/lib`));

    return merge(appJS, libJS);
});

gulp.task("build", [], () => gulp.start("resource", "css", "js"));

@@ -1128,28 +1128,16 @@
 ssnano(%7B
-%0A           
  zindex:
@@ -1142,24 +1142,16 @@
 x: false
-%0A       
  %7D))%0A   
@@ -1205,36 +1205,24 @@
 */*.html%60, %7B
-%0A           
  dirLevel: 2
@@ -1217,32 +1217,24 @@
  dirLevel: 2
-%0A       
  %7D))%0A       
@@ -1446,36 +1446,24 @@
 */*.html%60, %7B
-%0A           
  dirLevel: 2
@@ -1458,32 +1458,24 @@
  dirLevel: 2
-%0A       
  %7D))%0A       
@@ -1799,32 +1799,16 @@
 tions: %7B
-%0A               
  %22ecmaVe
@@ -1819,28 +1819,16 @@
 n%22: 2015
-%0A           
  %7D%0A     
@@ -2119,36 +2119,24 @@
 */*.html%60, %7B
-%0A           
  dirLevel: 2
@@ -2131,32 +2131,24 @@
  dirLevel: 2
-%0A       
  %7D))%0A       
@@ -2362,28 +2362,16 @@
 html%60, %7B
-%0A           
  dirLeve
@@ -2374,24 +2374,16 @@
 Level: 2
-%0A       
  %7D))%0A   
@@ -2469,32 +2469,32 @@
 S, libJS);%0A%7D);%0A%0A
+
 gulp.task(%22build
@@ -2499,12 +2499,8 @@
 ld%22,
- %5B%5D,
  () 

support babel in node pipeline