-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
mklicense.js
148 lines (144 loc) · 4.33 KB
/
mklicense.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node
'use strict'
// validar ES6
// condicional pra versao do node
// console antes de baixar isso ser show
const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer')
const jsdom = require('jsdom')
const username = require('git-user-name')
const Spinner = require('cli-spinner').Spinner
let loading = new Spinner('%s generating license')
loading.setSpinnerString('⣾⣽⣻⢿⡿⣟⣯⣷')
loading.setSpinnerDelay(100)
inquirer.prompt([
{
type: 'list',
name: 'licenseList',
message: 'Select your license:',
choices: [
'Unlicense', 'MIT', 'Apache 2.0', 'MPL 2.0', 'LGPL 3.0', 'GPL 3.0', 'AGPL 3.0'
],
filter: function (val) {
return val.toLowerCase().replace(/ /g, '-')
}
},
{
type: 'input',
name: 'license_year',
message: 'The project\'s license begins in:',
default: function () {
let getTheDate = new Date()
let currentYear = getTheDate.getFullYear()
return currentYear
},
when: function (answers) {
let licenseOption = answers.licenseList
if (licenseOption.match(/(mit|apache|^gpl|agpl)/)) {
return licenseOption
}
}
},
{
type: 'input',
name: 'author_name',
message: 'The project\'s author full name:',
default: function () {
return username()
},
when: function (answers) {
let licenseOption = answers.licenseList
if (licenseOption.match(/(mit|apache|^gpl|agpl)/)) {
return licenseOption
}
}
},
{
type: 'input',
name: 'project_description',
message: 'Give the project\'s name and a brief idea of what it does (one line):\n',
default: function () {
return 'mkdocs. A CLI tool that generates your next project\'s License. Available on NPM.'
},
when: function (answers) {
let licenseOption = answers.licenseList
if (licenseOption.match(/(^gpl|agpl)/)) {
return licenseOption
}
}
}
]).then(function (answers) {
const thisLicense = answers.licenseList
const licenseUrl = 'http://choosealicense.com/licenses/' + thisLicense
const whereIsMyLicense = path.resolve('.')
loading.start()
jsdom.env(
licenseUrl,
function (err, window) {
const license = window.document.getElementById('license-text').textContent
if (err) {
console.log('Getting the license was not possible. Error: ', err)
}
let customizer = function (thisLicense) {
let rewrite = {
'mit': function () {
return license.replace(
'[year]', answers.license_year
)
.replace(
'[fullname]', answers.author_name
)
},
'apache-2.0': function () {
return license.replace(
'{yyyy}', answers.license_year
)
.replace(
'{name of copyright owner}', answers.author_name
)
},
'gpl-3.0': function () {
return license.replace(
'{year}', answers.license_year
)
.replace(
'{name of author}', answers.author_name
)
.replace(
'{fullname}', answers.author_name
)
.replace(
'{project}', answers.project_name
)
.replace(
'{one line to give the program\'s name and a brief idea of what it does.}', answers.project_description
)
},
'agpl-3.0': function () {
return license.replace(
'<year>', answers.license_year
)
.replace(
'<name of author>', answers.author_name
)
.replace(
'<one line to give the program\'s name and a brief idea of what it does.>', answers.project_description
)
},
'default': function () {
return license
}
}
return (rewrite[thisLicense] || rewrite['default'])()
}
fs.writeFile('LICENSE', customizer(thisLicense), 'UTF-8', function (err) {
if (err) {
return console.log('error creating license', err)
}
loading.stop(true)
console.log('Done. license created!\nYour license is under: ' + whereIsMyLicense + '\nLawyers are now happy.')
})
}
)
})