Skip to content

Commit

Permalink
Replace version number hyphens with arbitrary strings (#76)
Browse files Browse the repository at this point in the history
* Allow replacing hyphens with specific strings
  • Loading branch information
Josh Malkinson authored and j-luong committed Jul 31, 2019
1 parent 2cef1d1 commit 8bb15a0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
*.log
.vscode
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,14 @@ Running rpmbuild on an npm package with a hyphen in its version number throws an
"replaceHyphens": true
}
}
```
```

Or you can explicitly tell speculate to replace hyphens with tildes `"~"`, underscores `"_"` or plus signs `"+"`:

```json
{
"spec": {
"replaceHyphens": "_"
}
}
```
15 changes: 14 additions & 1 deletion lib/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ function getReleaseNumber(release) {

function getVersionNumber({ spec, version }) {
const replaceHyphens = getValueFromSpec(spec, 'replaceHyphens', false);
const allowedReplacements = ['~', '_', '+'];
let newVersion;

return replaceHyphens ? version.replace(/-/g, '~') : version;
if (replaceHyphens === true) {
// Stay backwards compatible. Previous version was true -> tilde.
newVersion = version.replace(/-/g, '~');
} else if (typeof replaceHyphens === 'string') {
if (allowedReplacements.includes(replaceHyphens)) {
newVersion = version.replace(/-/g, replaceHyphens);
} else {
throw new Error(`replaceHyphens was given a forbidden string, so no replacement was done. Please use one of ${allowedReplacements.join('')}`);
}
}

return newVersion || version;
}

function getValueFromSpec(spec, key, fallback) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "my-cool-api",
"version": "1.1.1-pre-release-string.1",
"scripts": {
"start": "node index.js"
},
"description": "My Cool API",
"main": "index.js",
"author": "[email protected]",
"license": "MIT",
"spec": {
"replaceHyphens": "_"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
%define name my-cool-api
%define version 1.1.1_pre_release_string.1
%define release 1
%define buildroot %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

Name: %{name}
Version: %{version}
Release: %{release}
Summary: 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 my-cool-api >/dev/null || groupadd -r my-cool-api
getent passwd my-cool-api >/dev/null || useradd -r -g my-cool-api -G my-cool-api -d / -s /sbin/nologin -c "my-cool-api" my-cool-api

%install
mkdir -p %{buildroot}/usr/lib/my-cool-api
cp -r ./ %{buildroot}/usr/lib/my-cool-api
mkdir -p %{buildroot}/var/log/my-cool-api

%post
systemctl enable /usr/lib/my-cool-api/my-cool-api.service

%clean
rm -rf %{buildroot}

%files
%defattr(644, my-cool-api, my-cool-api, 755)
/usr/lib/my-cool-api
/var/log/my-cool-api
22 changes: 21 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,32 @@ describe('spec', () => {

it('replaces hyphens in the package version number with tildes if spec.replaceHyphens is true in package.json', () => {
const pkg = require('./fixtures/my-cool-api-with-hyphenated-version-and-override.json');
const expected = loadFixture('my-cool-api-with-version-hyphens-replaced.spec');
const expected = loadFixture(
'my-cool-api-with-version-hyphens-replaced.spec'
);
const spec = createSpecFile(pkg);

assert.equal(spec, expected);
});

it('replaces hyphens in the package version number with spec.replaceHyphens if it is a valid character', () => {
const pkg = require('./fixtures/my-cool-api-with-hyphenated-version-and-override-underscore.json');
const expected = loadFixture(
'my-cool-api-with-version-hyphens-replaced-underscores.spec'
);
const spec = createSpecFile(pkg);

assert.equal(spec, expected);
});

it('errors if spec.replaceHyphens is not a valid character', () => {
const pkg = require('./fixtures/my-cool-api-with-hyphenated-version-and-override-underscore.json');
pkg.spec.replaceHyphens = 'invalid';
const erroringSpecCall = createSpecFile.bind(null, pkg);

assert.throws(erroringSpecCall);
});

it('does not replace hyphens in the package version number if spec.replaceHyphens is not defined in package.json', () => {
const pkg = require('./fixtures/my-cool-api-with-hyphenated-version.json');
const expected = loadFixture('my-cool-api-with-hyphenated-version.spec');
Expand Down

0 comments on commit 8bb15a0

Please sign in to comment.