Skip to content

Commit

Permalink
Workaround for --name option in Commander.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tamsingreen committed Sep 15, 2016
1 parent 26f8a68 commit 7a1ff0c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ 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 `--custom-name` flag:
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 --custom-name=my-cool-api
speculate --name=my-cool-api
```

This is useful if you are using private NPM packages which start with an `@`.
13 changes: 7 additions & 6 deletions bin/speculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ var projectPkg = require(path.resolve(cwd, './package.json'));
program
.version(commandPkg.version)
.option('-r --release <release>', 'Specify release number of package')
.option('-c --custom-name <custom-name>', 'Specify custom name for package')
.option('-n --name <name>', 'Specify custom name for package')
.parse(process.argv);

var name = program.customName || projectPkg.name;
// 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, program.customName, 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);
});
27 changes: 24 additions & 3 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var _ = require('lodash');
var fs = require('fs');
var path = require('path');

var archiver = require('./archiver');
var createServiceFile = require('./service');
Expand All @@ -11,13 +12,17 @@ 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) {
Expand All @@ -28,6 +33,12 @@ function addCustomFieldsToPackage(pkg, customName) {
return pkg;
}

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

module.exports = function (root, pkg, release, customName, cb) {
var customPackage = addCustomFieldsToPackage(pkg, customName);
var specsDirectory = files.specsDirectory(root);
Expand All @@ -36,8 +47,18 @@ module.exports = function (root, pkg, release, customName, cb) {

fs.mkdirSync(specsDirectory);
fs.mkdirSync(sourcesDirectory);
generateServiceFile(root, customPackage);
generateSpecFile(specsDirectory, customPackage, 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
]));
});
};
9 changes: 9 additions & 0 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,13 @@ describe('generate', function () {
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 7a1ff0c

Please sign in to comment.