From d013913ab48a8ee664e84d74daf52211b7c9411d Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Wed, 2 Oct 2019 10:43:46 +0200 Subject: Initial version 0.1.0 --- lib/template.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/template.js (limited to 'lib/template.js') 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; -- cgit v1.2.3