Skip to content

Commit

Permalink
Fix npm warning by using configured default license
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemaccana committed Apr 25, 2024
1 parent 81c8c55 commit 699d94a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
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()?;

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

0 comments on commit 699d94a

Please sign in to comment.