gotta-go-fast
Version:
Cross-platform shell shortcuts
93 lines (73 loc) • 1.7 kB
JavaScript
;
const path = require("path");
const fs = require("fs");
const query = require("../util/query");
const fsExt = require("../util/fs");
const help = require("../util/help");
help(`
Usage:
ggf license [license [author]]
Outputs a new license file to ./LICENSE.md.
`);
let data = {
year: new Date().getFullYear(),
license: process.argv[3],
author: process.argv[4]
};
function getLicensePath(name) {
let file = name.toLowerCase() + ".md";
return path.resolve(__dirname, "../data/licenses", file);
}
function isLicenseValid(name) {
let file = getLicensePath(name);
try {
let stat = fs.statSync(file);
return stat.isFile();
} catch (e) {
return false;
}
}
function promptLicense() {
if (data.license != null) {
if (isLicenseValid(data.license)) {
return Promise.resolve(data.license);
} else {
console.log(`"${data.license}" is not a recognized license.`);
data.license = null;
}
}
return query.prompt("license", "MIT")
.then(r => {
data.license = r;
return promptLicense();
});
};
function isNameValid(name) {
return name.length > 0;
}
function promptName() {
if (data.author != null) {
if (isNameValid(data.author)) {
return Promise.resolve(data.author);
} else {
console.log("Name must include at least one character");
data.author = null;
}
}
return query.prompt("author")
.then(r => {
data.author = r;
return promptName();
});
}
promptLicense()
.then(promptName)
.then(r => {
let inPath = getLicensePath(data.license);
let outPath = path.resolve(process.cwd(), "LICENSE.md");
return fsExt.dataCopyFile(inPath, outPath, data);
})
.catch(e => {
console.error(e);
});