-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
116 lines (104 loc) · 3.09 KB
/
Copy pathextension.js
File metadata and controls
116 lines (104 loc) · 3.09 KB
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
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log("Activating extension 'License file generator'...");
const createLicenseFile = (licensePath, license) => {
fs.writeFile(licensePath, license, (err) => {
if (err) {
vscode.window.showErrorMessage(
`An error occurred while creating the LICENSE file: ${err.message}`
);
return;
}
vscode.window.showInformationMessage("LICENSE file created.");
vscode.workspace.openTextDocument(licensePath).then((doc) => {
vscode.window.showTextDocument(doc);
});
});
};
const buildCommandForLicense = (licenseTemplateFileName) => {
return () => {
let licenseText = fs.readFileSync(
path.join(__dirname, "resources/licenses", licenseTemplateFileName),
"utf8"
);
licenseText = licenseText.replace("{year}", new Date().getFullYear());
let authorName = "";
try {
authorName = require("child_process")
.execSync("git config user.name")
.toString()
.trim();
} catch (e) {
if (e.status === 1) {
vscode.window.showErrorMessage(
"Your git user.name is not set. Please set it in to use this extension."
);
} else {
vscode.window.showErrorMessage(
`An error occurred while trying to read your git user.name: ${e.message}`
);
}
return;
}
licenseText = licenseText.replace("{author}", authorName);
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
vscode.window.showErrorMessage(
"No workspace folder is open. Open a workspace folder first."
);
return;
}
const rootPath = workspaceFolders[0].uri.fsPath;
const licensePath = path.join(rootPath, "LICENSE");
if (!fs.existsSync(licensePath)) {
createLicenseFile(licensePath, licenseText);
return;
}
vscode.window
.showInformationMessage(
"LICENSE file already exists. Do you want to overwrite it?",
"Yes",
"No"
)
.then((res) => {
if (res === "Yes") {
createLicenseFile(licensePath, licenseText);
}
});
};
};
[
"apacheLicense",
"gnuGeneralPublicLicense3",
"mitLicense",
"bsdSimplifiedLicense",
"bsdNewRevisedLicense",
"boostSoftwareLicense",
"creativeCommonsZero",
"eclipsePublicLicense",
"gnuAfferoGeneralPublicLicense",
"gnuGeneralPublicLicense2",
"gnuLesserGeneralPublicLicense",
"mozillaPublicLicense",
"unlicense",
].forEach((license) => {
context.subscriptions.push(
vscode.commands.registerCommand(
`license-file-generator.${license}`,
buildCommandForLicense(license + ".txt")
)
);
});
}
function deactivate() {
console.log("Deactivating extension 'license-file-generator'...");
}
module.exports = {
activate,
deactivate,
};