Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 59 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,29 @@

<!-- [![browser support][test-png]][test] -->

Utility to deal with Uber OSS licences
**Utility to deal with Uber OSS licences**

## Example

`uber-licence`

Running the `uber-licence` binary adds licencing information
```shell
> uber-licence
```
Running the `uber-licence` binary adds licencing information
to every javascript file in your project.

You can run `uber-licence --dry` where it does not
mutate any files and instead outputs -1.
You can run `uber-licence --dry` where it does not mutate
any files and instead outputs -1.

You can use `--file` and `--dir` to specify your own file
and directory filters to select source files to consider.
You can use `--file` and `--dir` flags to specify your own
file and directory filters to select source files to consider.

## Recommended usage
Use the `-h` or `--help` flags to see all the available options.

```js
## Recommended Config

We recommend that you add the following two scripts to your `package.json` and run `check-licence` in a git pre-commit.

```json
// package.json
{
"scripts": {
Expand All @@ -45,22 +50,61 @@ You can use `--file` and `--dir` to specify your own file
}
```

We recommend you add two scripts to your package and run
`check-licence` in a git pre commit.

## Installation

`npm install uber-licence`
```shell
> npm install uber-licence --save-dev
```

## CLI Usage

```shell
> uber-licence [options]
```

## NodeJS API

```js
var uberLicence = require('uber-licence');
uberLicence(options);
```

## Options:

| API | CLI | Type | Default | Description |
| --- | --- | --- | --- | --- |
|`dry` |`-d`, `--dry` |`Boolean`|`false` | does not write to files |
|`file` |`-F`, `--file` |`String` |`*.js` | pattern of files to modify |
| | |`Array`[*](#n0)| | |
|`dir` |`-D`, `--dir` |`String` |`.*`<sup>[1](#n1)</sup>| list of directory patterns containing files |
| | |`Array`[*](#n0)| | |
|`license`|`-L`, `--license`|`String` |`undefined`| intended license template (file)<sup>[2](#n2)</sup> |
| | |`Array`[*](#n0)| | |
|`legacy` |`-O`, `--legacy` |`String` |`undefined`| list of licenses to replace |
| | |`Array`[*](#n0)| | |
|`verbose`|`-v`, `--verbose`|`Boolean`|`false` | log skipped and empty files |
|`silent` |`-s`, `--silent `|`Boolean`|`false` | do not log fixed files |

<a name="n0">*</a> *On CLI use comma to separate multiple values*
eg: `--flag=a,b,c` or `--flag a,b,c`

<a name="n1"><sup>1</sup></a> *The following directories are excluded by default:*
`.git, node_modules, coverage, env, .tox, vendor, Godeps`

<a name="n2"><sup>2</sup></a> *by default uses Uber's templates*

## Tests

`npm test`
```shell
> npm test
```

## Contributors

- [Raynos](https://github.com/raynos)
- [Kriskowal](https://github.com/kriskowal)
- [Dawsonbotsford](https://github.com/dawsonbotsford)
- [CxRes](https://github.com/cxres)

## MIT Licenced

Expand Down
177 changes: 46 additions & 131 deletions bin/licence.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
// Copyright (c) 2017 Uber Technologies, Inc.
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,145 +21,60 @@

'use strict';

var readdirp = require('readdirp');
var minimist = require('minimist');
var fs = require('fs');
var process = require('process');
var console = require('console');

// automatically prompt user to update if on older version
var updateNotifier = require('update-notifier');
var pkg = require('../package.json');
updateNotifier({pkg: pkg}).notify();

var LicenseFixer = require('../license-fixer');

var VALID_LICENSES = require('../valid-licences.js');

var argv = minimist(process.argv.slice(2));
var cwd = process.cwd();
var uberLicense = require('../index.js');

var argv = minimist(process.argv.slice(2), {
boolean: [
'dry',
'verbose',
'silent',
'help'
],
string: [
'file',
'dir',
'license',
'legacy'
],
alias: {
license: ['licence', 'L'],
file: 'F',
dir: 'D',
legacy: 'O',
dry: 'd',
verbose: 'v',
silent: 's',
help: 'h'
}
});

/*eslint no-process-exit: 0, no-console: 0*/
/*eslint no-console: 0*/
// jscs:disable maximumLineLength
if (argv.help || argv.h) {
if (argv.help) {
console.log('uber-licence');
console.log(' ');
console.log(' This binary will add a license to the top');
console.log(' of all your files');
console.log('');
console.log(' Options:');
console.log(' --dry does not write to files');
console.log(' --file pattern of files to modify');
console.log(' --dir pattern for directories containing files');
console.log(' --license intended license (file)');
console.log(' --legacy licenses to replace');
console.log(' --verbose log skipped and empty files');
console.log(' --silent do not log fixed files');
process.exit(0);
}

var fileFilter = ['*.js'];
if (typeof argv.file === 'string') {
fileFilter = [argv.file];
} else if (Array.isArray(argv.file)) {
fileFilter = argv.file;
}

var directoryFilter = ['!.git', '!node_modules', '!coverage', '!env', '!.tox', '!vendor', '!Godeps'];
if (typeof argv.dir === 'string') {
directoryFilter = [argv.dir];
} else if (Array.isArray(argv.dir)) {
directoryFilter = argv.dir;
}

var licenses = null;

if (typeof argv.license === 'string') {
licenses = licenses || [];
licenses.push(argv.license);
} else if (Array.isArray(argv.license)) {
licenses = licenses || [];
Array.prototype.push.apply(licenses, argv.license);
}

// licence non-American spelling
if (typeof argv.licence === 'string') {
licenses = licenses || [];
licenses.push(argv.licence);
} else if (Array.isArray(argv.licence)) {
licenses = licenses || [];
Array.prototype.push.apply(licenses, argv.licence);
}

if (typeof argv.legacy === 'string') {
licenses = licenses || [];
licenses.push(argv.legacy);
} else if (Array.isArray(argv.legacy)) {
licenses = licenses || [];
Array.prototype.push.apply(licenses, argv.legacy);
}

if (licenses) {
for (var i = 0; i < licenses.length; i++) {
// Replace file names with content of files
licenses[i] = fs.readFileSync(licenses[i], 'utf8');
}
} else {
// In the absense of any command line arguments,
// use the Uber defaults.
licenses = VALID_LICENSES;
console.log(' -d, --dry does not write to files');
console.log(' -F, --file pattern of files to modify');
console.log(' -D, --dir pattern for directories containing files');
console.log(' -L, --license intended license template (file)');
console.log(' -O, --legacy licenses to replace');
console.log(' -v, --verbose log skipped and empty files');
console.log(' -s, --silent do not log fixed files');
}

var licenseFixer = new LicenseFixer({
dry: argv.dry,
silent: argv.silent,
verbose: argv.verbose
});

// Set the intended license text
licenseFixer.setLicense(licenses[0]);
// Add a license to match and replace.
// There can be multiple recognized licenses, for migration purposes.
for (var i = 0; i < licenses.length; i++) {
licenseFixer.addLicense(licenses[i]);
}

readTree({
root: cwd,
fileFilter: fileFilter,
directoryFilter: directoryFilter
}, processFiles);

function readTree(options, callback) {
var stream = readdirp(options);
var files = [];
stream.on('data', onData);
stream.on('end', onEnd);
stream.on('error', onEnd);
function onData(event) {
files.push(event.path);
}
function onEnd(err) {
callback(err, files);
}
}

function processFiles(err, files) {
if (err) {
console.error(err.message);
process.exit(1);
return;
}

var fixed = 0;
for (var filesIndex = 0; filesIndex < files.length; filesIndex++) {
var file = files[filesIndex];
fixed = fixed + licenseFixer.fixFile(file);
}

if (argv.dry) {
process.exit(fixed);
} else {
process.exit(0);
}
else {
uberLicense({
dry: argv.dry,
silent: argv.silent,
verbose: argv.verbose,
fileFilter: argv.file && argv.file.split(','),
directoryFilter: argv.dir && argv.dir.split(','),
license: argv.license && argv.license.split(','),
legacy: argv.legacy && argv.legacy.split(',')
});
}
35 changes: 35 additions & 0 deletions defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

module.exports = {
directoryFilter: [
'!.git',
'!node_modules',
'!coverage',
'!env',
'!.tox',
'!vendor',
'!Godeps'
],

fileFilter: [
'*.js'
]
};
Loading