From ce884066e0ce02bf4f6012885011b5464c2ff82a Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 7 Apr 2022 09:14:38 -0400 Subject: [PATCH] cli: add prettier to new project js/ts template (#1741) --- .github/workflows/tests.yaml | 2 +- CHANGELOG.md | 1 + cli/src/lib.rs | 12 ++++++------ cli/src/template.rs | 26 ++++++++++++++++++++++++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 3168e0d3d..a890c8a0a 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -317,7 +317,7 @@ jobs: name: anchor-binary path: ~/.cargo/bin/ - - run: cd "$(mktemp -d)" && anchor init hello-anchor && cd hello-anchor && anchor test + - run: cd "$(mktemp -d)" && anchor init hello-anchor && cd hello-anchor && anchor test && yarn lint:fix - uses: ./.github/actions/git-diff/ test-programs: diff --git a/CHANGELOG.md b/CHANGELOG.md index 092ffc041..f4888f8b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi * ts: Add view functions ([#1695](https://github.com/project-serum/anchor/pull/1695)). * avm: New `avm update` command to update the Anchor CLI to the latest version ([#1670](https://github.com/project-serum/anchor/pull/1670)). * cli: Update js/ts templates to use new `program.methods` syntax ([#1732](https://github.com/project-serum/anchor/pull/1732)). +* cli: Workspaces created with `anchor init` now come with the `prettier` formatter and scripts included ([#1741](https://github.com/project-serum/anchor/pull/1741)). ### Fixes diff --git a/cli/src/lib.rs b/cli/src/lib.rs index ca82cdd91..6306da221 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -521,16 +521,16 @@ fn init(cfg_override: &ConfigOverride, name: String, javascript: bool, no_git: b ); cfg.programs.insert(Cluster::Localnet, localnet); let toml = cfg.to_string(); - let mut file = File::create("Anchor.toml")?; - file.write_all(toml.as_bytes())?; + fs::write("Anchor.toml", toml)?; // Build virtual manifest. - let mut virt_manifest = File::create("Cargo.toml")?; - virt_manifest.write_all(template::virtual_manifest().as_bytes())?; + fs::write("Cargo.toml", template::virtual_manifest())?; // Initialize .gitignore file - let mut virt_manifest = File::create(".gitignore")?; - virt_manifest.write_all(template::git_ignore().as_bytes())?; + fs::write(".gitignore", template::git_ignore())?; + + // Initialize .prettierignore file + fs::write(".prettierignore", template::prettier_ignore())?; // Build the program. fs::create_dir("programs")?; diff --git a/cli/src/template.rs b/cli/src/template.rs index 378d023dc..1b7455e9f 100644 --- a/cli/src/template.rs +++ b/cli/src/template.rs @@ -214,12 +214,17 @@ describe("{}", () => {{ pub fn package_json() -> String { format!( r#"{{ + "scripts": {{ + "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w", + "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check" + }}, "dependencies": {{ "@project-serum/anchor": "^{0}" }}, "devDependencies": {{ "chai": "^4.3.4", - "mocha": "^9.0.3" + "mocha": "^9.0.3", + "prettier": "^2.6.2" }} }} "#, @@ -230,6 +235,10 @@ pub fn package_json() -> String { pub fn ts_package_json() -> String { format!( r#"{{ + "scripts": {{ + "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w", + "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check" + }}, "dependencies": {{ "@project-serum/anchor": "^{0}" }}, @@ -240,7 +249,8 @@ pub fn ts_package_json() -> String { "@types/bn.js": "^5.1.0", "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", - "typescript": "^4.3.5" + "typescript": "^4.3.5", + "prettier": "^2.6.2" }} }} "#, @@ -300,6 +310,18 @@ test-ledger "# } +pub fn prettier_ignore() -> &'static str { + r#" +.anchor +.DS_Store +target +node_modules +dist +build +test-ledger +"# +} + pub fn node_shell( cluster_url: &str, wallet_path: &str,