aboutsummaryrefslogtreecommitdiff
path: root/lib/template.js
blob: 991f441f46cb63721df1080814db9540a7915a34 (plain)
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
// XXX We will never use in our builds.
// XXX Original names, e. g. "@foo/bar".
const devDepsBlackList = [
    "better-npm-run",
    "doctoc",
    "eslint",
    "husky",
    "lint-staged",
    "live-server",
    "npm-run-all",
    "watch",
];

function normalize(name) {
    return name.replace('/', '-').replace('@', '').replace('.', '-');
}

function fmt(words, maxLine, glue1, glue2) {
    var line = [];
    var lines = []
    var lineLength = 0;

    for (var i = 0; i < words.length; i++) {
        const itemLength = glue1.length + words[i].length
        if (itemLength + lineLength <= maxLine || line.length === 0) {
            line.push(words[i]);
            lineLength += itemLength;
        } else {
            lines.push(line);
            line = [words[i]];
            lineLength = itemLength;
        }
    }

    lines.push(line);

    lines = lines.map((l) => {
        return l.join(glue1);
    });

    return lines.join(glue2);
}


function render({
    src,
    pkg
}) {
    const deps = pkg.dependencies || {};
    const devDeps = pkg.devDependencies || {};

    var dropDevDependencies = [];

    devDepsBlackList.forEach((d) => {
        if (devDeps[d]) {
            delete devDeps[d];
            dropDevDependencies.push(d);
        }
    });

    const npmInputs = Object.keys({...deps,
        ...devDeps
    }).sort().map(normalize);
    const args = ['buildNpmPackage', ...npmInputs];

    var source = '';

    if ('string' === typeof src) {
        if (!src.startsWith('/') && !src.startsWith('./') && !src.startsWith('../')) {
            src = `./${src}`;
        }
        source = `  src = ${src};`;
    } else {
        args.unshift(src.fetch);
        switch (src.fetch) {
            case 'fetchurl':
                source = `\
  src = ${src.fetch} {
    url = "${src.url}";
    sha256 = "${src.sha256}";
  };`;
                break;
            case 'fetchgit':
                source = `\
  src = ${src.fetch} {
    url = "${src.url}";
    rev = "${src.rev}";
    sha256 = "${src.sha256}";
  };`;
                break;
        }
    }

    return `\
{ ${fmt(args, 90, ', ', '\n, ')} }:

buildNpmPackage {
  pname = "${pkg.name}";
  version = "${pkg.version}";
${source}

  meta = {
    description = ${JSON.stringify (pkg.description || '')};
    homepage = "${pkg.homepage || ''}";
    license = "${pkg.license || ''}";
  };

  npmInputs = [
    ${fmt(npmInputs, 80, ' ', '\n    ')}
  ];

  dropDevDependencies = [
    ${fmt(dropDevDependencies.sort().map(d => `"${d}"`), 80, ' ', '\n    ')}
  ];
}
  `;
}

module.exports = render;