diff --git a/README.md b/README.md index 586571d..cf54b9f 100644 --- a/README.md +++ b/README.md @@ -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 `@`. diff --git a/bin/speculate.js b/bin/speculate.js index 940e341..985645c 100755 --- a/bin/speculate.js +++ b/bin/speculate.js @@ -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 ', 'Specify release number of package') + .option('-n --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); }); diff --git a/lib/files.js b/lib/files.js index ced8c55..5fe5fa4 100644 --- a/lib/files.js +++ b/lib/files.js @@ -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'); } diff --git a/lib/generate.js b/lib/generate.js index a9c6f7a..f6566d9 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -1,4 +1,6 @@ +var _ = require('lodash'); var fs = require('fs'); +var path = require('path'); var archiver = require('./archiver'); var createServiceFile = require('./service'); @@ -10,6 +12,8 @@ function generateServiceFile(root, pkg) { var serviceFilePath = files.serviceFile(root, pkg); fs.writeFileSync(serviceFilePath, serviceFileContents); + + return serviceFilePath; } function generateSpecFile(root, pkg, release) { @@ -17,17 +21,44 @@ function generateSpecFile(root, 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 + ])); + }); }; diff --git a/test/generate.js b/test/generate.js index 8fa8ade..454ea48 100644 --- a/test/generate.js +++ b/test/generate.js @@ -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, @@ -31,7 +31,7 @@ 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(); @@ -39,7 +39,7 @@ describe('generate', function () { }); 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, @@ -51,7 +51,7 @@ 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(); @@ -59,7 +59,7 @@ describe('generate', function () { }); 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, @@ -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, @@ -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(); + }); + }); });