1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::Package;

/// create the package json matching specs for repo
pub fn generate_package_json(package: &mut Package, source: &bool) -> String {
    let keywords = package
        .keywords
        .clone()
        .unwrap_or(Vec::new())
        .iter()
        .map(|word| format!(r#""{}""#, word))
        .collect::<Vec<String>>()
        .join(",");
    let authors = package.authors.clone().unwrap_or(Vec::new());
    let license = package.license.clone().unwrap_or("ISC".to_string());
    let repository = package.repository.clone().unwrap_or("".to_string());
    let homepage = package.homepage.clone().unwrap_or("".to_string());
    let description = package.description.clone().unwrap_or("".to_string());

    let authors = if authors.is_empty() {
        "".to_string()
    } else {
        let first = authors.first().unwrap();
        format!(r#""author": "{}","#, first)
    };

    let bugs = if repository.is_empty() {
        "".to_string()
    } else {
        format!(
            r#",
  "bugs": {{
    "url": "{}/issues"
  }}"#,
            repository
        )
    };

    let repository = if repository.is_empty() {
        "".to_string()
    } else {
        format!(
            r#"
  "repository": {{
    "type": "git",
    "url": "{}.git"
  }},"#,
            repository
        )
    };

    let description = if description.is_empty() {
        println!("Description is optional on the Cargo.toml, but nice to add to let others know what the package is about.");
        "".to_string()
    } else {
        format!(r#""description": "{}","#, description)
    };

    let files = if source == &true {
        ""
    } else {
        r#",
  "files": [
    "pre-install.js",
    "start.js",
    "uninstall.js",
    "README.md",
    "LICENSE"
  ]"#
    };

    let homepage = if homepage.is_empty() {
        "".to_string()
    } else {
        format!(
            r#",
  "homepage": "{}""#,
            homepage
        )
    };

    let license = if license.is_empty() {
        "".to_string()
    } else {
        format!(
            r#"
  "license": "{}""#,
            license
        )
    };

    format!(
        r#"{{
  "name": "{}",
  "version": "{}",
  {}
  "main": "start.js",
  "directories": {{
    "test": "tests"
  }},
  "scripts": {{
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "node ./pre-install.js",
    "uninstall": "node ./uninstall.js"
  }},{}
  "keywords": [{}],
  {}{}{}{}{}
}}
    "#,
        package.name,
        package.version,
        description,
        repository,
        keywords,
        authors,
        license,
        bugs,
        homepage,
        files
    )
}