aboutsummaryrefslogtreecommitdiff
path: root/lib/template.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/template.js')
-rw-r--r--lib/template.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/template.js b/lib/template.js
new file mode 100644
index 0000000..ce05473
--- /dev/null
+++ b/lib/template.js
@@ -0,0 +1,89 @@
+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 || {};
+ 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 = "${pkg.description || ''}";
+ homepage = "${pkg.homepage || ''}";
+ license = "${pkg.license || ''}";
+ };
+
+ npmInputs = [
+ ${fmt(npmInputs, 80, ' ', '\n ')}
+ ];
+}
+ `;
+}
+
+module.exports = render;