diff --git a/lib/files.js b/lib/files.js index 673d7c7..9c9df74 100644 --- a/lib/files.js +++ b/lib/files.js @@ -4,13 +4,13 @@ const path = require('path'); module.exports = { serviceFile: function (root, pkg) { - return path.resolve(root, pkg.name + '.service'); + return path.resolve(root, this.packageFileName(pkg) + '.service'); }, specsDirectory: function (root) { return path.resolve(root, 'SPECS'); }, specFile: function (root, pkg) { - return path.resolve(root, pkg.name + '.spec'); + return path.resolve(root, this.packageFileName(pkg) + '.spec'); }, sourcesDirectory: function (root) { return path.resolve(root, 'SOURCES'); @@ -18,6 +18,12 @@ module.exports = { sourcesArchive: function (root, pkg) { const sourcesDirectory = this.sourcesDirectory(root); - return path.resolve(sourcesDirectory, pkg.name + '.tar.gz'); + return path.resolve(sourcesDirectory, this.packageFileName(pkg) + '.tar.gz'); + }, + packageFileName: function (pkg) { + return pkg.name[0] === '@' + // scoped packages get special treatment + ? pkg.name.substr(1).replace(/\//g, '-') + : pkg.name; } }; diff --git a/lib/serviceProperties.js b/lib/serviceProperties.js index 049a9ca..74297b0 100644 --- a/lib/serviceProperties.js +++ b/lib/serviceProperties.js @@ -1,6 +1,7 @@ 'use strict'; const truncate = require('./truncate'); +const files = require('./files'); function convertToKeyValueFromSpec(spec, prop) { if (spec && prop in spec) { @@ -13,8 +14,8 @@ function convertToKeyValueFromSpec(spec, prop) { module.exports = function (pkg) { return Object.assign( { - name: pkg.name, - username: truncate(pkg.name), + name: files.packageFileName(pkg), + username: truncate(files.packageFileName(pkg)), description: pkg.description, environment: convertToKeyValueFromSpec(pkg.spec, 'environment'), serviceOptions: convertToKeyValueFromSpec(pkg.spec, 'serviceOptions') diff --git a/test/fixtures/my-scoped-cool-api.json b/test/fixtures/my-scoped-cool-api.json new file mode 100644 index 0000000..3721aee --- /dev/null +++ b/test/fixtures/my-scoped-cool-api.json @@ -0,0 +1,11 @@ +{ + "name": "@scoped/my-cool-api", + "version": "1.1.1", + "scripts": { + "start": "node index.js" + }, + "description": "My Cool API", + "main": "index.js", + "author": "bob@example.com", + "license": "MIT" +} diff --git a/test/fixtures/my-scoped-cool-api.service b/test/fixtures/my-scoped-cool-api.service new file mode 100644 index 0000000..6528616 --- /dev/null +++ b/test/fixtures/my-scoped-cool-api.service @@ -0,0 +1,16 @@ +[Unit] +Description=My Cool API +After=network.target nss-lookup.target + +[Service] +ExecStart=/usr/bin/npm start +WorkingDirectory=/usr/lib/scoped-my-cool-api +Restart=always +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=scoped-my-cool-api +User=scoped-my-cool-api +Group=scoped-my-cool-api + +[Install] +WantedBy=multi-user.target diff --git a/test/fixtures/my-scoped-cool-api.spec b/test/fixtures/my-scoped-cool-api.spec new file mode 100644 index 0000000..7adba06 --- /dev/null +++ b/test/fixtures/my-scoped-cool-api.spec @@ -0,0 +1,47 @@ +%define name scoped-my-cool-api +%define version 1.1.1 +%define release 1 +%define buildroot %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) + +Name: %{name} +Version: %{version} +Release: %{release} +Summary: scoped-my-cool-api + +Group: Installation Script +License: MIT +Source: %{name}.tar.gz +BuildRoot: %{buildroot} +Requires: nodejs +BuildRequires: nodejs +AutoReqProv: no + +%description +My Cool API + +%prep +%setup -q -c -n %{name} + +%build +npm prune --production +npm rebuild + +%pre +getent group scoped-my-cool-api >/dev/null || groupadd -r scoped-my-cool-api +getent passwd scoped-my-cool-api >/dev/null || useradd -r -g scoped-my-cool-api -G scoped-my-cool-api -d / -s /sbin/nologin -c "scoped-my-cool-api" scoped-my-cool-api + +%install +mkdir -p %{buildroot}/usr/lib/scoped-my-cool-api +cp -r ./ %{buildroot}/usr/lib/scoped-my-cool-api +mkdir -p %{buildroot}/var/log/scoped-my-cool-api + +%post +systemctl enable /usr/lib/scoped-my-cool-api/scoped-my-cool-api.service + +%clean +rm -rf %{buildroot} + +%files +%defattr(644, scoped-my-cool-api, scoped-my-cool-api, 755) +/usr/lib/scoped-my-cool-api +/var/log/scoped-my-cool-api diff --git a/test/service.js b/test/service.js index 2077f6c..20eb721 100644 --- a/test/service.js +++ b/test/service.js @@ -36,4 +36,12 @@ describe('service', () => { assert.equal(service, expected); }); + + it('creates a service file from a scoped package.json', () => { + const pkg = require('./fixtures/my-scoped-cool-api'); + const expected = loadFixture('my-scoped-cool-api.service'); + const service = createServiceFile(pkg); + + assert.equal(service, expected); + }); }); diff --git a/test/spec.js b/test/spec.js index 9034c60..5551366 100644 --- a/test/spec.js +++ b/test/spec.js @@ -90,4 +90,12 @@ describe('spec', () => { const spec = createSpecFile(pkg); assert.equal(spec, expected); }); + + it('creates a spec file from a scoped package.json', () => { + const pkg = require('./fixtures/my-scoped-cool-api'); + const expected = loadFixture('my-scoped-cool-api.spec'); + const spec = createSpecFile(pkg); + assert.equal(spec, expected); + }); + });