Skip to content

Commit

Permalink
fix: quote values if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Felten authored and Markus Felten committed Dec 23, 2018
1 parent 36bef7c commit b59980d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
46 changes: 29 additions & 17 deletions src/npm-pkgbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import { promisify } from 'util';
import { finished } from 'stream';
import fs from "fs";

function quote(v)
{
if(v === undefined) return '';

if(Array.isArray(v)) {
return '(' + v.map(x=>quote(x)).join(',') + ')';
}
if(typeof v === 'number' || v instanceof Number) return v;

return v.match(/^\w+$/) ? v : "'" + v + "'";
}

export async function npm2pkgbuild(dir, out, options = {}) {
const installdir = options.installdir || '/';

Expand All @@ -20,32 +32,32 @@ export async function npm2pkgbuild(dir, out, options = {}) {
const properties = {
url: pkg.homepage,
pkgdesc: pkg.description,
license: pkg.license,
license: [pkg.license],
pkgrel: 1,
pkgname : pkg.name
arch: 'any',
pkgver: pkg.version.replace(/[\w\-]+$/,''),
pkgname : pkg.name,
arch: ['any'],
makedependes : 'git',
dependes: 'nodejs',
source: repo,
md5sums : 'SKIP',
install: options.installHook
source: [repo],
md5sums : ['SKIP'],
install: options.installHook,
groups: [],
optdepends: [],
provides: [],
conflicts: [],
replaces: [],
backup: [],
options: [],
noextract: [],
validpgpkeys: []
};

out.write(
`# ${pkg.contributors.map((c,i) => `${i?'Contributor':'Maintainer'}: ${c.name} <${c.email}>`).join('\n# ')}
pkgver=${pkg.version.replace(/[\w\-]+$/,'')}
${Object.keys(properties).filter(k=>properties[k]!==undefined).map(k=>`${k}=${quote(properties[k])}`).join('\n')}
epoch=
groups=()
optdepends=()
provides=()
conflicts=()
replaces=()
backup=()
options=()
${Object.keys(properties).filter(k=>properties[k]!==undefined).map(k=> `${k}='${properties[k]}'`).join('\n')}
changelog=
noextract=()
validpgpkeys=()
pkgver() {
cd "$pkgname"
Expand Down
6 changes: 3 additions & 3 deletions tests/simple-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import test from "ava";
import { join } from "path";
import { WritableStreamBuffer } from 'stream-buffers';
import { WritableStreamBuffer } from 'stream-buffers';

import { npm2pkgbuild } from "../src/npm-pkgbuild";

test("npm2pkgbuild", async t => {
const ws = new WritableStreamBuffer({ initialSize: 10240 });
const ws = new WritableStreamBuffer({ initialSize: 10240 });

await npm2pkgbuild(join(__dirname, "..", "tests", "fixtures"), ws);

t.regex(ws.getContentsAsString('utf8'), /source=\(git/);
t.regex(ws.getContentsAsString('utf8'), /source=\('git/);
});

0 comments on commit b59980d

Please sign in to comment.