Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix npm warning by using configured default license #2929

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved

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 {
Expand Down Expand Up @@ -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")?;

Expand All @@ -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())?;
Expand Down
8 changes: 6 additions & 2 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,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"
Expand All @@ -413,6 +414,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"
Expand All @@ -431,10 +433,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"
Expand All @@ -456,6 +459,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"
Expand Down
Loading