Don't write .eslintrc.json files (#5218)

* Factor out daml2ts utilities

changelog_begin
changelog_end

* Stop writing .eslintrc.json files into daml2ts packages

changelog_begin
changelog_end
This commit is contained in:
Shayne Fletcher 2020-03-26 14:42:59 -04:00 committed by GitHub
parent 5c913106fc
commit 8bb9f4b5c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 71 deletions

View File

@ -123,6 +123,7 @@ da_haskell_test(
"//:sdk-version-hs-lib",
"//compiler/damlc/daml-opts:daml-opts-types",
"//daml-assistant/daml-helper:daml-helper-lib",
"//language-support/ts/codegen/tests:daml2ts-test-helpers",
"//libs-haskell/bazel-runfiles",
"//libs-haskell/da-hs-base",
],

View File

@ -31,13 +31,11 @@ import Test.Main
import Test.Tasty
import Test.Tasty.HUnit
import qualified Web.JWT as JWT
import qualified Data.ByteString.Lazy as BSL
import qualified Data.HashMap.Strict as HMS
import Data.Aeson
import DA.Directory
import DA.Bazel.Runfiles
import DA.Daml.Helper.Run
import DA.Test.Daml2TsUtils
import SdkVersion
main :: IO ()
@ -524,12 +522,7 @@ codegenTests codegenDir damlTypes = testGroup "daml codegen" (
createDirectoryIfMissing True "generated"
withCurrentDirectory "generated" $ do
copyDirectory damlTypes "daml-types"
BSL.writeFile "package.json" $ encode $
object
[ "private" .= True
, "workspaces" .= [T.pack lang]
, "resolutions" .= HMS.fromList ([("@daml/types", "file:daml-types")] :: [(T.Text, T.Text)])
]
writeRootPackageJson Nothing [lang]
callCommandQuiet $
unwords [ "daml", "codegen", lang
, darFile ++ maybe "" ("=" ++) namespace

View File

@ -19,7 +19,6 @@ import qualified "zip-archive" Codec.Archive.Zip as Zip
import qualified Data.Map as Map
import Data.Aeson hiding (Options)
import Data.Aeson.Encode.Pretty
import Data.Hashable
import Control.Monad.Extra
import DA.Daml.LF.Ast
@ -43,25 +42,19 @@ import qualified DA.Daml.Project.Types as DATypes
-- Referenced from 'writePackageJson'. Lifted here for easy
-- maintenance.
data ConfigConsts = ConfigConsts
{ pkgDependencies :: HMS.HashMap NpmPackageName NpmPackageVersion
, pkgDevDependencies :: HMS.HashMap NpmPackageName NpmPackageVersion
{ pkgDependencies :: HMS.HashMap T.Text T.Text
, pkgDevDependencies :: HMS.HashMap T.Text T.Text
}
configConsts :: T.Text -> ConfigConsts
configConsts sdkVersion = ConfigConsts
{ pkgDependencies = HMS.fromList
[ (NpmPackageName "@mojotech/json-type-validation", NpmPackageVersion "^3.1.0")
, (NpmPackageName "@daml/types", NpmPackageVersion sdkVersion)
[ ("@mojotech/json-type-validation", "^3.1.0")
, ("@daml/types", sdkVersion)
]
, pkgDevDependencies = HMS.fromList
[ (NpmPackageName "typescript", NpmPackageVersion "~3.7.3")
[ ("typescript", "~3.7.3")
]
}
newtype NpmPackageName = NpmPackageName {unNpmPackageName :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON, ToJSONKey)
newtype NpmPackageVersion = NpmPackageVersion {unNpmPackageVersion :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON)
data Options = Options
{ optInputDars :: [FilePath]
@ -203,7 +196,6 @@ daml2ts Daml2TsParams {..} = do
-- Now write package metadata.
writeIndexTs packageSrcDir pkgId modules
writeTsConfig packageDir
writeEsLintConfig packageDir
writePackageJson packageDir sdkVersion scope dependencies
pure (pkgName, dependencies)
where
@ -632,35 +624,14 @@ writeTsConfig dir =
, "include" .= (["src/**/*.ts"] :: [T.Text])
]
writeEsLintConfig :: FilePath -> IO ()
writeEsLintConfig dir =
BSL.writeFile (dir </> ".eslintrc.json") $ encodePretty esLintConfig
where
esLintConfig :: Value
esLintConfig = object
[ "parser" .= ("@typescript-eslint/parser" :: T.Text)
, "parserOptions" .= object [("project", "./tsconfig.json")]
, "plugins" .= (["@typescript-eslint"] :: [T.Text])
, "extends" .= (
[ "eslint:recommended"
, "plugin:@typescript-eslint/eslint-recommended"
, "plugin:@typescript-eslint/recommended"
, "plugin:@typescript-eslint/recommended-requiring-type-checking"
] :: [T.Text])
, "rules" .= object
[ ("@typescript-eslint/explicit-function-return-type", "off")
, ("@typescript-eslint/no-inferrable-types", "off")
]
]
writePackageJson :: FilePath -> SdkVersion -> Scope -> [Dependency] -> IO ()
writePackageJson packageDir sdkVersion (Scope scope) depends =
BSL.writeFile (packageDir </> "package.json") $
encodePretty (packageJson (NpmPackageName name) (NpmPackageVersion version) dependencies)
encodePretty (packageJson name version dependencies)
where
version = versionToText sdkVersion
name = packageNameOfPackageDir packageDir
dependencies = HMS.fromList [ (NpmPackageName pkg, NpmPackageVersion version)
dependencies = HMS.fromList [ (pkg, version)
| d <- depends
, let pkgName = unDependency d
, let pkg = scope <> "/" <> pkgName
@ -671,7 +642,7 @@ writePackageJson packageDir sdkVersion (Scope scope) depends =
where
package = T.pack $ takeFileName packageDir
packageJson :: NpmPackageName -> NpmPackageVersion -> HMS.HashMap NpmPackageName NpmPackageVersion -> Value
packageJson :: T.Text -> T.Text -> HMS.HashMap T.Text T.Text -> Value
packageJson name version dependencies = object
[ "private" .= True
, "name" .= name

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
load("//rules_daml:daml.bzl", "daml_compile")
load("//bazel_tools:haskell.bzl", "da_haskell_test")
load("//bazel_tools:haskell.bzl", "da_haskell_library", "da_haskell_test")
load("@build_environment//:configuration.bzl", "sdk_version")
load("@os_info//:os_info.bzl", "is_windows")
@ -109,6 +109,25 @@ sh_test(
],
) if not is_windows else None
da_haskell_library(
name = "daml2ts-test-helpers",
srcs = ["src/DA/Test/Daml2TsUtils.hs"],
hackage_deps = [
"aeson",
"base",
"bytestring",
"filepath",
"hashable",
"text",
"unordered-containers",
],
src_strip_prefix = "src",
visibility = ["//visibility:public"],
deps = [
"//libs-haskell/da-hs-base",
],
)
# daml2ts
# =======
#
@ -152,6 +171,7 @@ da_haskell_test(
src_strip_prefix = "src",
visibility = ["//visibility:public"],
deps = [
":daml2ts-test-helpers",
"//compiler/daml-lf-ast",
"//libs-haskell/bazel-runfiles",
"//libs-haskell/da-hs-base",

View File

@ -13,34 +13,28 @@ import System.Exit
import DA.Bazel.Runfiles
import qualified DA.Daml.LF.Ast.Version as LF
import DA.Directory
import DA.Test.Daml2TsUtils
import Data.Maybe
import Data.List.Extra
import qualified Data.Text.Extended as T
import qualified Data.ByteString.Lazy as BSL
import qualified Data.HashMap.Strict as HMS
import Data.Aeson
import Data.Hashable
import Test.Tasty
import Test.Tasty.HUnit
-- Referenced from the DAVL test. Lifted here for easy
-- maintenance.
data ConfigConsts = ConfigConsts
{ pkgDevDependencies :: HMS.HashMap NpmPackageName NpmPackageVersion }
{ pkgDevDependencies :: HMS.HashMap T.Text T.Text }
configConsts :: ConfigConsts
configConsts = ConfigConsts
{ pkgDevDependencies = HMS.fromList
[ (NpmPackageName "eslint", NpmPackageVersion "^6.7.2")
, (NpmPackageName "@typescript-eslint/eslint-plugin", NpmPackageVersion "2.11.0")
, (NpmPackageName "@typescript-eslint/parser", NpmPackageVersion "2.11.0")
[ ("eslint", "^6.7.2")
, ("@typescript-eslint/eslint-plugin", "2.11.0")
, ("@typescript-eslint/parser", "2.11.0")
]
}
newtype NpmPackageName = NpmPackageName {unNpmPackageName :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON, ToJSONKey)
newtype NpmPackageVersion = NpmPackageVersion {unNpmPackageVersion :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON)
main :: IO ()
main = do
@ -218,22 +212,38 @@ tests damlTypes yarn damlc daml2ts davl = testGroup "daml2ts tests"
step "eslint..."
withCurrentDirectory daml2tsDir $ do
pkgs <- (\\ ["package.json", "node_modules"]) <$> listDirectory daml2tsDir
BSL.writeFile "package.json" $ encode (
BSL.writeFile "package.json" $ encode $
object
[ "private" .= True
, "devDependencies" .= pkgDevDependencies configConsts
, "workspaces" .= pkgs
, "name" .= ("daml2ts" :: T.Text)
, "version" .= ("0.0.0" :: T.Text)
])
]
BSL.writeFile ".eslintrc.json" $ encode $
object
[ "parser" .= ("@typescript-eslint/parser" :: T.Text)
, "parserOptions" .= object [("project", "./tsconfig.json")]
, "plugins" .= (["@typescript-eslint"] :: [T.Text])
, "extends" .= (
[ "eslint:recommended"
, "plugin:@typescript-eslint/eslint-recommended"
, "plugin:@typescript-eslint/recommended"
, "plugin:@typescript-eslint/recommended-requiring-type-checking"
] :: [T.Text])
, "rules" .= object
[ ("@typescript-eslint/explicit-function-return-type", "off")
, ("@typescript-eslint/no-inferrable-types", "off")
]
]
callProcessSilent yarn ["install", "--pure-lockfile"]
callProcessSilent yarn ["workspaces", "run", "eslint", "--ext", ".ts", "--max-warnings", "0", "src/"]
callProcessSilent yarn ["workspaces", "run", "eslint", "-c", ".." </> ".eslintrc.json", "--ext", ".ts", "--max-warnings", "0", "src/"]
]
where
setupYarnEnvironment :: IO ()
setupYarnEnvironment = do
copyDirectory damlTypes "daml-types"
writePackageJson
writeRootPackageJson Nothing ["daml2ts"]
buildProject :: [String] -> IO ()
buildProject args = callProcessSilent damlc (["build"] ++ args)
@ -263,15 +273,6 @@ tests damlTypes yarn damlc daml2ts davl = testGroup "daml2ts tests"
[" - " ++ dependency | dependency <- dependencies] ++
["build-options: [--target=" <> LF.renderVersion ver <> "]" | Just ver <- [mbLfVersion]]
writePackageJson :: IO ()
writePackageJson = BSL.writeFile "package.json" $ encode packageJson
where
packageJson = object
[ "private" .= True
, "workspaces" .= (["daml2ts"] :: [T.Text])
, "resolutions" .= HMS.fromList ([("@daml/types", "file:daml-types")] :: [(T.Text, T.Text)])
]
assertTsFileExists :: FilePath -> String -> IO ()
assertTsFileExists proj file = do
assertFileExists (proj </> "src" </> file <.> "ts")

View File

@ -0,0 +1,20 @@
-- Copyright (c) 2020 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
module DA.Test.Daml2TsUtils (writeRootPackageJson) where
import qualified Data.Text.Extended as T
import qualified Data.ByteString.Lazy as BSL
import qualified Data.HashMap.Strict as HMS
import Data.Aeson
import System.FilePath
-- The need for this utility comes up in at least the assistant
-- integration and daml2ts tests.
writeRootPackageJson :: Maybe FilePath -> [String] -> IO ()
writeRootPackageJson dir workspaces =
BSL.writeFile (maybe "package.json" (</> "package.json") dir) $ encode $
object
[ "private" .= True
, "workspaces" .= map T.pack workspaces
, "resolutions" .= HMS.fromList ([ ("@daml/types", "file:daml-types") ] :: [(T.Text, T.Text)])
]