Skip to content

Commit a14aafa

Browse files
author
Ridha Hassoun
committed
Merge remote-tracking branch 'upstream/master'
2 parents c06f187 + 431a596 commit a14aafa

12 files changed

+243
-37
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,3 @@ typings/
5656

5757
# dotenv environment variables file
5858
.env
59-
60-
# Ignore licenses directory
61-
licenses/

CODE_OF_CONDUCT.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, gender identity and expression, level of experience,
9+
nationality, personal appearance, race, religion, or sexual identity and
10+
orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [email protected]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at [http://contributor-covenant.org/version/1/4][version]
72+
73+
[homepage]: http://contributor-covenant.org
74+
[version]: http://contributor-covenant.org/version/1/4/

README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# legit
22

33
legit is a command line application that allows you to automagically generate a
4-
LICENSE file for the current working directory that you are in.
4+
LICENSE file for the current working directory that you are in or a license header
5+
for a file where applicable.
56

67
### Installation
78

@@ -17,16 +18,27 @@ npm install --global @captainsafia/legit
1718
1819
Commands:
1920
20-
list|l List all available licenses
21-
put|p <license> [user] [year] Put a license in this directory
21+
list|l List all available licenses
22+
put|p [options] <license> Put a license in this directory
2223
2324
Options:
2425
2526
-h, --help output usage information
2627
-V, --version output the version number
28+
29+
Usage: put|p [options] <license>
30+
31+
Put a license in this directory
32+
33+
Options:
34+
35+
-h, --help output usage information
36+
-f --file <file> The file to add a header to
37+
-u --user [user] The user/organization who holds the license
38+
-y --year [year] The year the license is in effect
2739
```
2840

29-
![Legit Demo](legit-demo.gif)
41+
![Legit Demo](https://cloud.githubusercontent.com/assets/1857993/23821404/bea5dfc2-05f6-11e7-8525-7f5bd88a7829.gif)
3042

3143
### Currently Supported Licenses
3244
- BSD 2-clause Simplified License (bsd-2-clause)

legit-demo.gif

-1.72 MB
Binary file not shown.

licenses/headers/agpl-3.0

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright (C) <year> <name of author>
2+
3+
This program is free software: you can redistribute it and/or modify
4+
it under the terms of the GNU Affero General Public License as published
5+
by the Free Software Foundation, either version 3 of the License, or
6+
(at your option) any later version.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU Affero General Public License for more details.
12+
13+
You should have received a copy of the GNU Affero General Public License
14+
along with this program. If not, see <http://www.gnu.org/licenses/>.

licenses/headers/apache-2.0

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright {yyyy} {name of copyright owner}
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

licenses/headers/gpl-2.0

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Copyright (C) {year} {fullname}
2+
3+
This program is free software; you can redistribute it and/or
4+
modify it under the terms of the GNU General Public License
5+
as published by the Free Software Foundation; either version 2
6+
of the License, or (at your option) any later version.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

licenses/headers/gpl-3.0

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright (C) {year} {fullname}
2+
3+
This program is free software: you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation, either version 3 of the License, or
6+
(at your option) any later version.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program. If not, see <http://www.gnu.org/licenses/>.

licenses/headers/lgpl-2.0

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Copyright (C) {year} {fullname}
2+
3+
This library is free software; you can redistribute it and/or
4+
modify it under the terms of the GNU Lesser General Public
5+
License as published by the Free Software Foundation; either
6+
version 2.1 of the License, or (at your option) any later version.
7+
8+
This library is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
Lesser General Public License for more details.
12+
13+
You should have received a copy of the GNU Lesser General Public
14+
License along with this library; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "@captainsafia/legit",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "Automatically add a license to a GitHub project",
55
"main": "index.js",
66
"bin": {
77
"legit": "src/index.js"
88
},
99
"scripts": {
10-
"postinstall": "mkdir -p licenses && node src/load-licenses.js"
10+
"prepublish": "find licenses -type f -maxdepth 1 -delete",
11+
"postinstall": "node src/load-licenses.js"
1112
},
1213
"keywords": [
1314
"license",
@@ -18,6 +19,7 @@
1819
"author": "Safia Abdalla <[email protected]>",
1920
"license": "MIT",
2021
"dependencies": {
22+
"@captainsafia/commentator": "^1.1.0",
2123
"commander": "^2.9.0",
2224
"node-fetch": "^1.6.3",
2325
"username": "^2.3.0",

src/index.js

+77-27
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#! /usr/bin/env node
22

33
const fs = require('fs');
4+
const path = require('path');
45
const program = require('commander');
56
const username = require('username');
67
const placeholders = require('./placeholders');
8+
const commentator = require('@captainsafia/commentator');
79
const firstCommitDate = require('first-commit-date');
810

9-
const licensesPath = __dirname + '/../licenses/';
11+
const licensesPath = path.join(__dirname, '/../licenses/');
1012

1113
function validateLicense(license) {
1214
license = license.toLowerCase();
@@ -15,7 +17,7 @@ function validateLicense(license) {
1517
}
1618

1719
program
18-
.version('2.0.0')
20+
.version('2.0.0');
1921

2022
program
2123
.command('list').alias('l')
@@ -28,38 +30,86 @@ program
2830
});
2931

3032
program
31-
.command('put <license> [user] [year]').alias('p')
33+
.command('put <license>').alias('p')
34+
.option('-f --file [file]', 'The file to add a header to')
35+
.option('-u --user [user]', 'The user/organization who holds the license')
36+
.option('-y --year [year]', 'The year the license is in effect')
3237
.description('Put a license in this directory')
33-
.action(function(licenseArg, userArg, yearArg) {
38+
.action(function(licenseArg) {
39+
const cwd = process.cwd();
40+
const fileArg = this.file;
41+
var yearArg;
42+
const userArg = this.user || username.sync();
43+
44+
if (this.year === "range") {
45+
var firstCommitYear = firstCommitDate.sync(cwd + '/.git').getFullYear();
46+
var currentYear = new Date().getFullYear();
47+
if (currentYear === firstCommitYear) {
48+
yearArg = currentYear;
49+
} else {
50+
yearArg = firstCommitYear + "-" + currentYear;
51+
}
52+
} else {
53+
yearArg = this.year || new Date().getFullYear();
54+
}
55+
56+
const user = placeholders[licenseArg]['user'];
57+
const year = placeholders[licenseArg]['year'];
58+
3459
if (!validateLicense(licenseArg)) {
3560
return console.log('Please choose one of the licenses under `legit list`!');
3661
}
3762

38-
const cwd = process.cwd();
39-
const licenseFile = licensesPath + licenseArg;
40-
fs.readFile(licenseFile, 'utf8', function (error, data) {
41-
if (error) console.log(error);
42-
if (yearArg === "auto") {
43-
var firstCommitYear = firstCommitDate.sync(cwd + '/.git').getFullYear();
44-
var currentYear = new Date().getFullYear();
45-
if (currentYear === firstCommitYear) {
46-
yearArg = currentYear;
47-
} else {
48-
yearArg = firstCommitYear + "-" + currentYear;
49-
}
50-
} else {
51-
yearArg = yearArg || new Date().getFullYear();
63+
if (fileArg) {
64+
const headerFile = path.join(__dirname, '/../licenses/headers/', licenseArg);
65+
const fileExtension = fileArg.split('.').pop();
66+
if (!fs.existsSync(headerFile)) {
67+
return console.log('Header not available for', licenseArg, 'license!');
5268
}
53-
userArg = userArg || username.sync();
54-
if (placeholders[licenseArg]) {
55-
const user = placeholders[licenseArg]['user'];
56-
const year = placeholders[licenseArg]['year'];
57-
var result = data.replace(user, userArg).replace(year, yearArg);
58-
}
59-
fs.writeFile(cwd + '/LICENSE', result || data, 'utf8', function (error) {
69+
70+
fs.readFile(headerFile, 'utf8', function(error, data) {
71+
if (error) return console.log(error);
72+
var result;
73+
74+
try {
75+
result = commentator.makeBlockComment(
76+
data.replace(user, userArg).replace(year, yearArg), fileExtension);
77+
} catch (error) {
78+
if (error.message.includes('Block comment')) {
79+
try {
80+
result = commentator.makeInlineComment(
81+
data.replace(user, userArg).replace(year, yearArg), fileExtension);
82+
} catch(error) {
83+
if (error.message.includes('Inline comment')) {
84+
return console.log('@captainsafia/commentator doesn\'t support block comments',
85+
'for files with this extension, please open an issue at https://github.com/captainsafia/commentator/issues',
86+
'to add support for this programming language.');
87+
}
88+
}
89+
}
90+
}
91+
92+
const filePath = path.join(cwd, '/', fileArg);
93+
94+
fs.readFile(filePath, 'utf8', function(error, data) {
95+
const newData = result + '\n' + data;
96+
fs.writeFile(filePath, newData, 'utf8', function(error) {
97+
if (error) return console.log(error);
98+
});
99+
});
100+
});
101+
} else {
102+
const licenseFile = licensesPath + licenseArg;
103+
fs.readFile(licenseFile, 'utf8', function (error, data) {
60104
if (error) return console.log(error);
105+
if (placeholders[licenseArg]) {
106+
var result = data.replace(user, userArg).replace(year, yearArg);
107+
}
108+
fs.writeFile(path.join(cwd, '/LICENSE'), result || data, 'utf8', function (error) {
109+
if (error) return console.log(error);
110+
});
61111
});
62-
});
112+
}
63113
});
64114

65-
program.parse(process.argv);
115+
program.parse(process.argv);

src/placeholders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const placeholders = {
22
'agpl-3.0': {
3-
user: '<name of author',
3+
user: '<name of author>',
44
year: '<year>'
55
},
66
'apache-2.0': {

0 commit comments

Comments
 (0)