-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
180 lines (153 loc) · 6.07 KB
/
index.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
import { input, select } from '@inquirer/prompts';
import * as path from 'path';
import { createRequire } from 'module';
import fetch from 'node-fetch'
import * as fs from 'fs';
const require = createRequire(import.meta.url)
const userName = require('git-user-name');
const Spinner = require('cli-spinner').Spinner;
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
let loading = new Spinner('%s generating license')
loading.setSpinnerString('⣾⣽⣻⢿⡿⣟⣯⣷')
loading.setSpinnerDelay(100)
process.stdout.write('\x1Bc')
class LicenseClass {
licenseSelection: string = '';
licenseYear: string = '';
licenseAuthorName: string = '';
licenseProjectName: string = '';
licenseProjectDescription: string = '';
licenseUrl: string = '';
licenseDestination: string = '';
licenseSelectionRegex: string = '';
}
const selectedLicense: string = await select({
message: 'Select your license: (Use arrow keys)',
choices: ['MIT', 'Apache 2.0', 'MPL 2.0', 'LGPL 3.0', 'GPL 3.0', 'AGPL 3.0', 'Unlicense']
});
const licenseSelectionRegex = selectedLicense.toLowerCase().replace(/ /g, '-');
const projectDescription = licenseSelectionRegex.match(/(^gpl|agpl)/)
? await input({
message: 'Give the project\'s name and a brief idea of what it does (one line):\n',
default: (function () {
return 'mycli. A CLI tool that generates awesome stuff.'
})(),
})
: '';
const projectName = licenseSelectionRegex.match(/(^gpl\-3\.0)/)
? await input({
message: 'Enter the project\'s name:\n',
default: (function () {
return 'My Project'
})(),
})
: '';
let licenseData: LicenseClass
licenseData = {
licenseSelection: selectedLicense,
licenseYear: await input(
{
message: 'Enter the project\'s year:',
default: (function () {
const currentDate = new Date();
return currentDate.getFullYear().toString();
})(),
}
),
licenseAuthorName: await input(
{
message: 'Enter the project\'s author:',
default: (function () {
return userName();
})(),
}
),
licenseProjectName: projectName.trim() !== '' ? projectName : '',
licenseProjectDescription: projectDescription.trim() !== '' ? projectDescription : '',
licenseUrl: 'http://choosealicense.com/licenses/' + licenseSelectionRegex,
licenseDestination: path.resolve('.'),
licenseSelectionRegex: licenseSelectionRegex,
};
async function fetchLicenseText(url: string): Promise<string | null> {
loading.start()
try {
// Fetch the HTML content from the URL
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Get the text content of the response
const htmlText = await response.text();
// Use JSDOM to parse the HTML content
const dom = new JSDOM(htmlText);
const document = dom.window.document;
// Extract the content within the element with the specified ID
const element = document.getElementById('license-text');
return element ? element.textContent : null;
} catch (error) {
console.error('Error fetching or parsing the document:', error);
return null;
}
}
function customizeLicenseText(licenseText: string, data: LicenseClass) {
let updatedLicense: string = ''
let licYear: string = data.licenseYear
let licAuthor: string = data.licenseAuthorName
let licProjectName: string = data.licenseProjectName
let licDescription: string = data.licenseProjectDescription
let licenseId: string = data.licenseSelectionRegex
switch (licenseId) {
case 'mit':
updatedLicense = licenseText.replace('[year]', licYear).replace('[fullname]', licAuthor)
break;
case 'apache-2.0':
updatedLicense = licenseText.replace('[yyyy]', licYear).replace('[name of copyright owner]', licAuthor)
break;
case 'gpl-3.0':
updatedLicense = licenseText.replace('<year>', licYear).replace('<name of author>', licAuthor).replace('<program>', licProjectName)
.replace('<one line to give the program\'s name and a brief idea of what it does.>', licDescription)
break;
case 'agpl-3.0':
updatedLicense = licenseText.replace('<year>', licYear).replace('<name of author>', licAuthor)
.replace('<one line to give the program\'s name and a brief idea of what it does.>', licDescription)
break;
default:
updatedLicense = licenseText
}
return updatedLicense
}
async function populateLicense() {
try {
const licenseText = await fetchLicenseText(licenseData.licenseUrl);
if (licenseText !== null) {
let populatedLicense = customizeLicenseText(licenseText, licenseData);
return populatedLicense
} else {
console.log('Element not found or error occurred.');
}
} catch (error) {
console.error('An error occurred while fetching the license text:', error);
}
}
async function writeLicenseToFile() {
let fileName = 'LICENSE'
try {
let finalLicense = await populateLicense();
if (finalLicense) {
fs.writeFile(fileName, finalLicense, 'utf-8', function (err) {
if (err) {
return console.log('Error writing license to disk.', err);
}
loading.stop(true);
console.log('Complete: License has been created: ' + '"' + licenseData.licenseDestination + '\\' + fileName + '"');
});
} else {
console.log('No license text was generated.');
}
} catch (error) {
console.error('An error occurred:', error);
}
}
writeLicenseToFile();