Browse Source

cli: Fix npm warning by using configured default license (#2929)

Mike MacCana 1 year ago
parent
commit
054850c614
3 changed files with 27 additions and 4 deletions
  1. 1 0
      CHANGELOG.md
  2. 20 2
      cli/src/lib.rs
  3. 6 2
      cli/src/rust_template.rs

+ 1 - 0
CHANGELOG.md

@@ -19,6 +19,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 
 - lang: Eliminate variable allocations that build up stack space for token extension code generation ([#2913](https://github.com/coral-xyz/anchor/pull/2913)).
 - ts: Fix incorrect `maxSupportedTransactionVersion` in `AnchorProvider.send*()` methods ([#2922](https://github.com/coral-xyz/anchor/pull/2922)).
+- cli: Use npm's configured default license for new projects make with `anchor init` ([#2929](https://github.com/coral-xyz/anchor/pull/2929)).
 
 ### Breaking
 

+ 20 - 2
cli/src/lib.rs

@@ -655,6 +655,22 @@ fn restore_toolchain(restore_cbs: RestoreToolchainCallbacks) -> Result<()> {
     Ok(())
 }
 
+/// Get the system's default license - what 'npm init' would use.
+fn get_npm_init_license() -> Result<String> {
+    let npm_init_license_output = std::process::Command::new("npm")
+        .arg("config")
+        .arg("get")
+        .arg("init-license")
+        .output()?;
+
+    if !npm_init_license_output.status.success() {
+        return Err(anyhow!("Failed to get npm init license"));
+    }
+
+    let license = String::from_utf8(npm_init_license_output.stdout)?;
+    Ok(license.trim().to_string())
+}
+
 fn process_command(opts: Opts) -> Result<()> {
     match opts.command {
         Command::Init {
@@ -919,11 +935,13 @@ fn init(
     // Build the migrations directory.
     fs::create_dir_all("migrations")?;
 
+    let license = get_npm_init_license()?;
+
     let jest = TestTemplate::Jest == test_template;
     if javascript {
         // Build javascript config
         let mut package_json = File::create("package.json")?;
-        package_json.write_all(rust_template::package_json(jest).as_bytes())?;
+        package_json.write_all(rust_template::package_json(jest, license).as_bytes())?;
 
         let mut deploy = File::create("migrations/deploy.js")?;
 
@@ -934,7 +952,7 @@ fn init(
         ts_config.write_all(rust_template::ts_config(jest).as_bytes())?;
 
         let mut ts_package_json = File::create("package.json")?;
-        ts_package_json.write_all(rust_template::ts_package_json(jest).as_bytes())?;
+        ts_package_json.write_all(rust_template::ts_package_json(jest, license).as_bytes())?;
 
         let mut deploy = File::create("migrations/deploy.ts")?;
         deploy.write_all(rust_template::ts_deploy_script().as_bytes())?;

+ 6 - 2
cli/src/rust_template.rs

@@ -394,10 +394,11 @@ describe("{}", () => {{
     )
 }
 
-pub fn package_json(jest: bool) -> String {
+pub fn package_json(jest: bool, license: String) -> String {
     if jest {
         format!(
             r#"{{
+  "license": "{license}",
   "scripts": {{
     "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
     "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
@@ -415,6 +416,7 @@ pub fn package_json(jest: bool) -> String {
     } else {
         format!(
             r#"{{
+  "license": "{license}",
   "scripts": {{
     "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
     "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
@@ -433,10 +435,11 @@ pub fn package_json(jest: bool) -> String {
     }
 }
 
-pub fn ts_package_json(jest: bool) -> String {
+pub fn ts_package_json(jest: bool, license: String) -> String {
     if jest {
         format!(
             r#"{{
+  "license": "{license}",              
   "scripts": {{
     "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
     "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
@@ -458,6 +461,7 @@ pub fn ts_package_json(jest: bool) -> String {
     } else {
         format!(
             r#"{{
+  "license": "{license}",  
   "scripts": {{
     "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
     "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"