Skip to content

Commit

Permalink
Allow custom names with --name flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tamsingreen committed Sep 13, 2016
1 parent 1bb116b commit bcf5c28
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,13 @@ By default speculate will set the RPM release number to 1, if you want to overri
```sh
speculate --release=7
```

### Custom Name

By default speculate will set the name from `package.json`, if you want to override this you can do so by using the `--name` flag:

```sh
speculate --name=my-cool-api
```

This is useful if you are using private NPM packages which start with an `@`.
3 changes: 2 additions & 1 deletion bin/speculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ var name = projectPkg.name;
program
.version(commandPkg.version)
.option('-r --release <release>', 'Specify release number of package')
.option('-n --name <name>', 'Specify custom name for package')
.parse(process.argv);

clean(cwd, projectPkg);
generate(cwd, projectPkg, program.release, function (err) {
generate(cwd, projectPkg, program.release, program.name, function (err) {
if (err) {
console.error('Error:', err.message);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
return path.resolve(root, 'SOURCES');
},
sourcesArchive: function (root, pkg) {
var sourcesDirectory = this.sourcesDirectory(root, pkg);
var sourcesDirectory = this.sourcesDirectory(root);

return path.resolve(sourcesDirectory, pkg.name + '.tar.gz');
}
Expand Down
18 changes: 14 additions & 4 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,25 @@ function generateSpecFile(root, pkg, release) {
fs.writeFileSync(specFilePath, specFileContents);
}

module.exports = function (root, pkg, release, cb) {
function addCustomFieldsToPackage(pkg, customName) {
if (customName) {
return Object.assign({}, pkg, { name: customName });
}

return pkg;
}

module.exports = function (root, pkg, release, customName, cb) {

var customPackage = addCustomFieldsToPackage(pkg, customName);
var specsDirectory = files.specsDirectory(root);
var sourcesDirectory = files.sourcesDirectory(root);
var sourcesArchive = files.sourcesArchive(root, pkg);
var sourcesArchive = files.sourcesArchive(root, customPackage);

fs.mkdirSync(specsDirectory);
fs.mkdirSync(sourcesDirectory);
generateServiceFile(root, pkg);
generateSpecFile(specsDirectory, pkg, release);
generateServiceFile(root, customPackage);
generateSpecFile(specsDirectory, customPackage, release);

archiver.compress(root, sourcesArchive, cb);
};
48 changes: 42 additions & 6 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('generate', function () {
});

it('creates the service file', function (done) {
generate('/path/to/project', pkg, null, function (err) {
generate('/path/to/project', pkg, null, null, function (err) {
assert.ifError(err);
sinon.assert.calledWith(
fs.writeFileSync,
Expand All @@ -31,15 +31,15 @@ describe('generate', function () {
});

it('creates directory for SPECS', function (done) {
generate('/path/to/project', pkg, null, function (err) {
generate('/path/to/project', pkg, null, null, function (err) {
assert.ifError(err);
sinon.assert.calledWith(fs.mkdirSync, '/path/to/project/SPECS');
done();
});
});

it('creates the spec file', function (done) {
generate('/path/to/project', pkg, null, function (err) {
generate('/path/to/project', pkg, null, null, function (err) {
assert.ifError(err);
sinon.assert.calledWith(
fs.writeFileSync,
Expand All @@ -51,15 +51,15 @@ describe('generate', function () {
});

it('creates directory for SOURCES', function (done) {
generate('/path/to/project', pkg, null, function (err) {
generate('/path/to/project', pkg, null, null, function (err) {
assert.ifError(err);
sinon.assert.calledWith(fs.mkdirSync, '/path/to/project/SOURCES');
done();
});
});

it('creates the sources archive', function (done) {
generate('/path/to/project', pkg, null, function (err) {
generate('/path/to/project', pkg, null, null, function (err) {
assert.ifError(err);
sinon.assert.calledWith(
archiver.compress,
Expand All @@ -71,7 +71,7 @@ describe('generate', function () {
});

it('creates the spec file with the correct release number', function (done) {
generate('/path/to/project', pkg, 7, function (err) {
generate('/path/to/project', pkg, 7, null, function (err) {
assert.ifError(err);
sinon.assert.calledWith(
fs.writeFileSync,
Expand All @@ -81,4 +81,40 @@ describe('generate', function () {
done();
});
});

it('creates the spec file with a custom name if specified', function (done) {
generate('/path/to/project', pkg, 1, 'penguin', function (err) {
assert.ifError(err);
sinon.assert.calledWith(
fs.writeFileSync,
'/path/to/project/SPECS/penguin.spec',
sinon.match('%define name penguin')
);
done();
});
});

it('creates the service file with a custom name if specified', function (done) {
generate('/path/to/project', pkg, 1, 'penguin', function (err) {
assert.ifError(err);
sinon.assert.calledWith(
fs.writeFileSync,
'/path/to/project/penguin.service',
sinon.match('SyslogIdentifier=penguin')
);
done();
});
});

it('creates the sources archive with a custom name if specified', function (done) {
generate('/path/to/project', pkg, null, 'penguin', function (err) {
assert.ifError(err);
sinon.assert.calledWith(
archiver.compress,
'/path/to/project',
'/path/to/project/SOURCES/penguin.tar.gz'
);
done();
});
});
});

0 comments on commit bcf5c28

Please sign in to comment.