UNPKG

3.56 kBJavaScriptView Raw
1'use strict';
2var _ = require('lodash');
3var generators = require('yeoman-generator');
4var gitConfig = require('git-config');
5
6var licenses = [
7 { name: 'Apache 2.0', value: 'Apache-2.0' },
8 { name: 'MIT', value: 'MIT' },
9 { name: 'Unlicense', value: 'unlicense' },
10 { name: 'FreeBSD', value: 'BSD-2-Clause-FreeBSD' },
11 { name: 'NewBSD', value: 'BSD-3-Clause' },
12 { name: 'Internet Systems Consortium (ISC)', value: 'ISC' },
13 { name: 'No License (Copyrighted)', value: 'nolicense' }
14];
15
16module.exports = generators.Base.extend({
17 constructor: function () {
18 generators.Base.apply(this, arguments);
19
20 this.option('name', {
21 desc: 'Name of the license owner',
22 required: false
23 });
24
25 this.option('email', {
26 desc: 'Email of the license owner',
27 required: false
28 });
29
30 this.option('website', {
31 desc: 'Website of the license owner',
32 required: false
33 });
34
35 this.option('year', {
36 desc: 'Year(s) to include on the license',
37 required: false,
38 defaults: (new Date()).getFullYear()
39 });
40 },
41
42 initializing: function () {
43 this.gitc = gitConfig.sync();
44 this.gitc.user = this.gitc.user || {};
45 },
46
47 prompting: function () {
48 var done = this.async();
49
50 var choices = licenses;
51
52 var prompts = [
53 {
54 name: 'name',
55 message: 'What\'s your name:',
56 default: this.options.name || this.gitc.user.name,
57 when: !this.options.name
58 },
59 {
60 name: 'email',
61 message: 'Your email (optional):',
62 default: this.options.email || this.gitc.user.email,
63 when: !this.options.email
64 },
65 {
66 name: 'website',
67 message: 'Your website (optional):',
68 default: this.options.website,
69 when: !this.options.website
70 },
71 {
72 type: 'list',
73 name: 'license',
74 message: 'Which license do you want to use?',
75 choices: choices
76 }
77 ];
78
79 this.prompt(prompts, function (props) {
80 this.props = _.extend({
81 name: this.options.name,
82 email: this.options.email,
83 website: this.options.website
84 }, props);
85 done();
86 }.bind(this));
87 },
88
89 writing: {
90 license: function () {
91 var filename = this.props.license + '.txt';
92 var author = this.props.name.trim();
93 if (this.props.email) {
94 author += ' <' + this.props.email.trim() + '>';
95 }
96 if (this.props.website) {
97 author += ' (' + this.props.website.trim() + ')';
98 }
99
100 this.fs.copyTpl(
101 this.templatePath(filename),
102 this.destinationPath('LICENSE'),
103 {
104 year: this.options.year,
105 author: author
106 }
107 );
108 },
109
110 pkg: function () {
111 var done = this.async();
112 var fs = require('fs');
113
114 fs.exists(this.destinationPath('package.json'), function (exists) {
115 if (!exists) {
116 return done();
117 }
118
119 var pkg = this.fs.readJSON(this.destinationPath('package.json'), {});
120 pkg.license = this.props.license;
121
122 // We don't want users to publish their module to NPM if they copyrighted
123 // their content.
124 if (this.props.license === 'nolicense') {
125 delete pkg.license;
126 pkg.private = true;
127 }
128
129 this.fs.writeJSON(this.destinationPath('package.json'), pkg);
130
131 done();
132 }.bind(this));
133 }
134 }
135}, {
136 licenses: licenses
137});