Haskell Twitch高级文件观察器 DSL执行 2 次

问题描述

我想使用 Twitch 包将“central”目录中的任何 javascript 文件复制到“back”和“front”目录。这是代码

{-# LANGUAGE OverloadedStrings #-}


module Main where

import Data.List
  ( isPrefixOf,tails,findindex
  )
import System.Directory 
  ( createDirectoryIfMissing,removeDirectoryRecursive,copyFile
  )
import System.Directory.Recursive 
  ( getSubdirsRecursive,getFilesRecursive
  )
import Twitch
  ( defaultMain,(|>)
  )


main :: IO ()
main = do

  putStrLn "Haskell works <(^u^)>"

  -- copy central folder to back and front,and setup recopy when central changes
  copyCentral
  defaultMain $ do 
    "./central/**/*.js" |> copyFiletoBackAndFront




copyCentral :: IO ()
copyCentral = do

  createDirectoryIfMissing False "./back/src/central/" 
  removeDirectoryRecursive "./back/src/central"
  createDirectoryIfMissing False "./front/src/central/" 
  removeDirectoryRecursive "./front/src/central"

  centralFiles <- getFilesRecursive "./central/"
  centralDirs  <- getSubdirsRecursive "./central/"

  mapM_ (\d -> createDirectoryIfMissing True $ "./back/src" ++ tail d) centralDirs
  mapM_ (\d -> createDirectoryIfMissing True $ "./front/src" ++ tail d) centralDirs
  mapM_ (\f -> copyFile f $ "./back/src" ++ tail f) centralFiles
  mapM_ (\f -> copyFile f $ "./front/src" ++ tail f) centralFiles


copyFiletoBackAndFront :: FilePath -> IO ()
copyFiletoBackAndFront absolutePath = 
    maybe 
      (putStrLn "Error in the file path")
      getRelativePathAndcopy
      (findindex (isPrefixOf "central") (tails absolutePath))
  where
    getRelativePathAndcopy n = do
      let relativePath = drop n absolutePath
      copyFile relativePath $ "./back/src/"  ++ relativePath
      copyFile relativePath $ "./front/src/" ++ relativePath
      putStrLn $ "copied file " ++ relativePath ++ " to back and front"

令人惊讶的是它有效!但我得到 putStrLn 输出 2 次:

copied file central\models\Board\Board.js to back and front
copied file central\models\Board\Board.js to back and front

而且我怀疑可能是因为程序运行在 2 个线程中,因为有时我会得到隔行输出

CCooppiieedd  ffiillee  cceennttrraall\\mmooddeellss\\BBooaarrdd\\BBooaarrdd..jjss  ttoo  bbaacckk  aanndd  ffrroonntt

copiCeodp ifeidl ef icleen tcreanlt\rmaold\emlosd\eBlosa\rBdo\abroda\rBdo.ajrsd .tjos  btaoc kb aacnkd  afnrdo nftr
ont

当我运行 cabal v2-repl 时也会发生这种情况,我只是尝试了 cabal v2-run myprogram &,但它根本不起作用:(

请问您对这两个问题有什么帮助吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)