Browse Source

cli: add prettier to new project js/ts template (#1741)

Paul 3 years ago
parent
commit
ce884066e0
4 changed files with 32 additions and 9 deletions
  1. 1 1
      .github/workflows/tests.yaml
  2. 1 0
      CHANGELOG.md
  3. 6 6
      cli/src/lib.rs
  4. 24 2
      cli/src/template.rs

+ 1 - 1
.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:

+ 1 - 0
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
 

+ 6 - 6
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")?;

+ 24 - 2
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,