Skip to content

Commit

Permalink
Merge pull request #14 from bbc/allow-custom-name
Browse files Browse the repository at this point in the history
Allow custom name
  • Loading branch information
Mousius authored Sep 15, 2016
2 parents 1bb116b + 7a1ff0c commit ddc90ae
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 17 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 `@`.
13 changes: 8 additions & 5 deletions bin/speculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@ if (!isValid) {
}

var projectPkg = require(path.resolve(cwd, './package.json'));
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);

// Commander has a magic property called name when not overriden by a parameter
var name = program.name instanceof Function ? undefined : program.name;

clean(cwd, projectPkg);
generate(cwd, projectPkg, program.release, function (err) {
generate(cwd, projectPkg, program.release, name, function (err, generated) {
if (err) {
console.error('Error:', err.message);
process.exit(1);
}

console.log('Created ./SPECS/%s.spec', name);
console.log('Created ./SOURCES/%s.tar.gz', name);
console.log('Created ./%s.service', name);
generated.forEach(function (file) {
console.log('Created ./%s', file);
});
process.exit(0);
});
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
41 changes: 36 additions & 5 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var _ = require('lodash');
var fs = require('fs');
var path = require('path');

var archiver = require('./archiver');
var createServiceFile = require('./service');
Expand All @@ -10,24 +12,53 @@ function generateServiceFile(root, pkg) {
var serviceFilePath = files.serviceFile(root, pkg);

fs.writeFileSync(serviceFilePath, serviceFileContents);

return serviceFilePath;
}

function generateSpecFile(root, pkg, release) {
var specFileContents = createSpecFile(pkg, release);
var specFilePath = files.specFile(root, pkg);

fs.writeFileSync(specFilePath, specFileContents);

return specFilePath;
}

function addCustomFieldsToPackage(pkg, customName) {
if (customName) {
return _.extend({}, pkg, { name: customName });
}

return pkg;
}

function relativeToRoot(root, files) {
return files.map(function (file) {
return path.relative(root, file);
});
}

module.exports = function (root, pkg, release, cb) {
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);
var serviceFile = generateServiceFile(root, customPackage);
var specFile = generateSpecFile(specsDirectory, customPackage, release);

archiver.compress(root, sourcesArchive, function (err) {
if (err) {
return cb(err);
}

archiver.compress(root, sourcesArchive, cb);
cb(null, relativeToRoot(root, [
specFile,
sourcesArchive,
serviceFile
]));
});
};
57 changes: 51 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,49 @@ 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();
});
});

it('returns an array of files created', function (done) {
var filesExpected = ['SPECS/my-cool-api.spec', 'SOURCES/my-cool-api.tar.gz', 'my-cool-api.service'];
generate('/path/to/project', pkg, null, null, function (err, files) {
assert.ifError(err);
assert.deepEqual(files, filesExpected);
done();
});
});
});

0 comments on commit ddc90ae

Please sign in to comment.