const { Service, checkGitStatus, executeScriptAsync } = require('clean-scripts')
const { watch } = require('watch-then-execute')

const tsFiles = `"packages/@(core|vue|react|angular)/@(src|demo)/**/*.@(ts|tsx)" "spec/**/*.ts" "screenshots/**/*.ts"`
const lessFiles = `"packages/core/src/**/*.less" "packages/core/demo/**/*.less"`
const jsFiles = `"*.config.js" "spec/**/*.config.js"`
const excludeTsFiles = `"packages/@(core|vue|react|angular)/@(src|demo)/**/*.d.ts"`

const vueTemplateCommand = `file2variable-cli --config packages/vue/src/file2variable.config.js`
const tscSrcCommand = [
  `tsc -p packages/core/src`,
  `tsc -p packages/vue/src`,
  `tsc -p packages/react/src`
]
const tscDemoCommand = [
  `tsc -p packages/core/demo`,
  `tsc -p packages/vue/demo`,
  `tsc -p packages/react/demo`
]
const webpackCommand = `webpack`
const revStaticCommand = `rev-static`
const cssCommand = [
  `lessc packages/core/src/grid.less -sm=on > packages/core/src/grid.css`,
  `postcss packages/core/src/grid.css -o packages/core/dist/grid.css`,
  `cleancss packages/core/dist/grid.css ./packages/core/node_modules/perfect-scrollbar/css/perfect-scrollbar.css -o packages/core/dist/grid.min.css`,
  `lessc packages/core/demo/index.less -sm=on > packages/core/demo/index.css`,
  `cleancss packages/core/dist/grid.min.css packages/core/demo/index.css ./node_modules/github-fork-ribbon-css/gh-fork-ribbon.css -o packages/core/demo/index.bundle.css`
]

module.exports = {
  build: [
    {
      js: [
        vueTemplateCommand,
        tscSrcCommand,
        tscDemoCommand,
        webpackCommand
      ],
      css: cssCommand,
      clean: `rimraf "packages/@(core|vue|react|angular)/demo/**/@(*.bundle-*.js|*.bundle-*.css)"`
    },
    revStaticCommand
  ],
  lint: {
    ts: `tslint ${tsFiles} --exclude ${excludeTsFiles}`,
    js: `standard ${jsFiles}`,
    less: `stylelint ${lessFiles}`,
    export: `no-unused-export ${tsFiles} ${lessFiles} --exclude ${excludeTsFiles}`,
    commit: `commitlint --from=HEAD~1`,
    markdown: `markdownlint README.md`
  },
  test: [
    'tsc -p spec',
    'karma start spec/karma.config.js',
    () => checkGitStatus()
  ],
  fix: {
    ts: `tslint --fix ${tsFiles}`,
    js: `standard --fix ${jsFiles}`,
    less: `stylelint --fix ${lessFiles}`
  },
  watch: {
    template: `${vueTemplateCommand} --watch`,
    src: `${tscSrcCommand} --watch`,
    demo: `${tscDemoCommand} --watch`,
    webpack: `${webpackCommand} --watch`,
    less: () => watch(['packages/core/src/*.less', 'packages/core/demo/*.less'], [], () => executeScriptAsync(cssCommand)),
    rev: `${revStaticCommand} --watch`
  },
  screenshot: [
    new Service(`http-server -p 8000`),
    `tsc -p screenshots`,
    `node screenshots/index.js`
  ]
}

@@ -564,24 +564,28 @@
 s%60%0Aconst tsc
+Core
 SrcCommand =
@@ -576,36 +576,32 @@
 CoreSrcCommand =
- %5B%0A 
  %60tsc -p package
@@ -607,27 +607,49 @@
 es/core/src%60
-,%0A 
+%0Aconst tscVueSrcCommand =
  %60tsc -p pac
@@ -662,19 +662,43 @@
 vue/src%60
-,%0A 
+%0Aconst tscReactSrcCommand =
  %60tsc -p
@@ -718,17 +718,16 @@
 ct/src%60%0A
-%5D
 %0Aconst t
@@ -724,24 +724,28 @@
 %60%0A%0Aconst tsc
+Core
 DemoCommand 
@@ -745,20 +745,16 @@
 ommand =
- %5B%0A 
  %60tsc -p
@@ -769,27 +769,50 @@
 s/core/demo%60
-,%0A 
+%0Aconst tscVueDemoCommand =
  %60tsc -p pac
@@ -826,19 +826,44 @@
 ue/demo%60
-,%0A 
+%0Aconst tscReactDemoCommand =
  %60tsc -p
@@ -884,17 +884,16 @@
 t/demo%60%0A
-%5D
 %0Aconst w
@@ -1612,16 +1612,20 @@
      tsc
+Core
 SrcComma
@@ -1636,30 +1636,192 @@
         
-tscDemoCommand
+%7B%0A          tscVueSrcCommand,%0A          tscReactSrcCommand%0A        %7D,%0A        tscCoreDemoCommand,%0A        %7B%0A          tscVueDemoCommand,%0A          tscReactDemoCommand%0A        %7D
 ,%0A      
@@ -2550,16 +2550,26 @@
     
-t
+vueT
 emplate
+Command
 : %60$
@@ -2607,19 +2607,33 @@
     
-src
+tscCoreSrcCommand
 : %60$%7Btsc
 SrcC
@@ -2628,16 +2628,20 @@
 : %60$%7Btsc
+Core
 SrcComma
@@ -2662,20 +2662,262 @@
     
-demo: %60$%7Btsc
+tscVueSrcCommand: %60$%7BtscVueSrcCommand%7D --watch%60,%0A    tscReactSrcCommand: %60$%7BtscReactSrcCommand%7D --watch%60,%0A    tscCoreDemoCommand: %60$%7BtscCoreDemoCommand%7D --watch%60,%0A    tscVueDemoCommand: %60$%7BtscVueDemoCommand%7D --watch%60,%0A    tscReactDemoCommand: %60$%7BtscReact
 Demo

improve scripts