Skip to content

Commit

Permalink
refactor moon cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Dec 21, 2019
1 parent 7b391f8 commit b842b29
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 73 deletions.
71 changes: 35 additions & 36 deletions packages/moon-cli/dist/moon-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,18 @@
}
}
};
var repo, name;

function log(type, message) {
console.log("\x1B[34m" + type + "\x1B[0m " + message);
}

function logHelp(message) {
console.log(highlight(message));
}

function logError(message) {
console.log("\x1B[31merror\x1B[0m " + message);
}

function highlight(string) {
return string.replace(parameterRE, "\x1b[33m$&\x1b[0m").replace(optionRE, "\x1b[36m$&\x1b[0m");
Expand All @@ -57,18 +68,6 @@
}).join("\n");
}

function log(type, message) {
console.log("\x1B[34m" + type + "\x1B[0m " + message);
}

function logHelp(message) {
console.log(highlight(message));
}

function error(message) {
console.log("\x1B[31merror\x1B[0m " + message);
}

function replace(content, sub, subNewString) {
var index = content.indexOf(sub);

Expand All @@ -82,7 +81,7 @@
}
}

function download(res) {
function download(name, repo, res) {
var archivePath = path.join(__dirname, "moon-template.tar.gz");
var stream = fs.createWriteStream(archivePath);
res.on("data", function (chunk) {
Expand All @@ -91,44 +90,44 @@
res.on("end", function () {
stream.end();
log("download", repo);
install(archivePath);
install(name, archivePath);
});
}

function install(archivePath) {
function install(name, archivePath) {
var targetPath = path.join(process.cwd(), name);
exec("mkdir " + targetPath, function (err) {
if (err) throw err;
if (err) logError(err);
exec("tar -xzf " + archivePath + " -C " + targetPath + " --strip=1", function (err) {
if (err) throw err;
if (err) logError(err);
log("install", targetPath);
clean(archivePath, targetPath);
clean(name, archivePath, targetPath);
});
});
}

function clean(archivePath, targetPath) {
function clean(name, archivePath, targetPath) {
fs.unlink(archivePath, function (err) {
if (err) throw err;
if (err) logError(err);
log("clean", archivePath);
create(targetPath, targetPath);
create(name, targetPath, targetPath);
log("success", "Generated application \x1B[36m" + name + "\x1B[0m");
console.log("To start, run:\n\tcd " + name + "\n\tnpm install\n\tnpm run dev");
});
}

function create(currentPath, targetPath) {
var files = fs.readdirSync(currentPath);
function create(name, directoryPath, targetPath) {
var files = fs.readdirSync(directoryPath);

for (var i = 0; i < files.length; i++) {
var file = files[i];
var nextPath = path.join(currentPath, file);
var filePath = path.join(directoryPath, file);

if (fs.statSync(nextPath).isDirectory()) {
create(nextPath, targetPath);
if (fs.statSync(filePath).isDirectory()) {
create(name, filePath, targetPath);
} else {
fs.writeFileSync(nextPath, replace(fs.readFileSync(nextPath), "{# MoonName #}", name));
log("create", path.relative(targetPath, nextPath));
fs.writeFileSync(filePath, replace(fs.readFileSync(filePath), "{# MoonName #}", name));
log("create", path.relative(targetPath, filePath));
}
}
}
Expand Down Expand Up @@ -194,15 +193,15 @@

case "create":
{
name = commandArguments[0];
repo = commandOptions["-t"] || commandOptions["--template"] || "kbrsh/moon-template";
var name = commandArguments[0];
var repo = commandOptions["-t"] || commandOptions["--template"] || "kbrsh/moon-template";

if (name === undefined || name.length === 0) {
error("Invalid or unknown name.\n\nAttempted to create an application.\n\nReceived an invalid or unknown name.\n\nExpected a valid name. Run \x1B[35mmoon help create\x1B[0m to see usage information.");
logError("Invalid or unknown name.\n\nAttempted to create an application.\n\nReceived an invalid or unknown name.\n\nExpected a valid name. Run \x1B[35mmoon help create\x1B[0m to see usage information.");
}

if (repo === true) {
error("Invalid or unknown template.\n\nAttempted to create an application.\n\nReceived an invalid or unknown template.\n\nExpected a valid template. Run \x1B[35mmoon help create\x1B[0m to see usage information.");
logError("Invalid or unknown template.\n\nAttempted to create an application.\n\nReceived an invalid or unknown template.\n\nExpected a valid template. Run \x1B[35mmoon help create\x1B[0m to see usage information.");
}

var archive = {
Expand All @@ -217,18 +216,18 @@
https.get(archive, function (res) {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location !== undefined) {
https.get(res.headers.location, function (redirectRes) {
download(redirectRes);
download(name, repo, redirectRes);
});
} else {
download(res);
download(name, repo, res);
}
});
break;
}

default:
{
error("Unrecognized command.\n\nAttempted to execute a command.\n\nReceived a command that does not exist:\n\t" + commandName + "\n\nExpected a valid command. Run \x1B[35mmoon help\x1B[0m to see valid commands.");
logError("Unrecognized command.\n\nAttempted to execute a command.\n\nReceived a command that does not exist:\n\t" + commandName + "\n\nExpected a valid command. Run \x1B[35mmoon help\x1B[0m to see valid commands.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/moon-cli/dist/moon-cli.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 34 additions & 36 deletions packages/moon-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ const help = {
}
};

let repo, name;

function highlight(string) {
return string.replace(parameterRE, "\x1b[33m$&\x1b[0m").replace(optionRE, "\x1b[36m$&\x1b[0m");
}

function table(object) {
const keys = Object.keys(object);
const max = Math.max.apply(null, keys.map(key => key.length));
return keys.map(key => "\t" + key + " ".repeat(max - key.length + 3) + object[key]).join("\n");
}

function log(type, message) {
console.log(`\x1b[34m${type}\x1b[0m ${message}`);
}
Expand All @@ -50,10 +38,20 @@ function logHelp(message) {
console.log(highlight(message));
}

function error(message) {
function logError(message) {
console.log(`\x1b[31merror\x1b[0m ${message}`);
}

function highlight(string) {
return string.replace(parameterRE, "\x1b[33m$&\x1b[0m").replace(optionRE, "\x1b[36m$&\x1b[0m");
}

function table(object) {
const keys = Object.keys(object);
const max = Math.max.apply(null, keys.map(key => key.length));
return keys.map(key => "\t" + key + " ".repeat(max - key.length + 3) + object[key]).join("\n");
}

function replace(content, sub, subNewString) {
const index = content.indexOf(sub);

Expand All @@ -68,7 +66,7 @@ function replace(content, sub, subNewString) {
}
}

function download(res) {
function download(name, repo, res) {
const archivePath = path.join(__dirname, "moon-template.tar.gz");
const stream = fs.createWriteStream(archivePath);

Expand All @@ -80,31 +78,31 @@ function download(res) {
stream.end();

log("download", repo);
install(archivePath);
install(name, archivePath);
});
}

function install(archivePath) {
function install(name, archivePath) {
const targetPath = path.join(process.cwd(), name);

exec(`mkdir ${targetPath}`, err => {
if (err) throw err;
if (err) logError(err);

exec(`tar -xzf ${archivePath} -C ${targetPath} --strip=1`, err => {
if (err) throw err;
if (err) logError(err);

log("install", targetPath);
clean(archivePath, targetPath);
clean(name, archivePath, targetPath);
});
});
}

function clean(archivePath, targetPath) {
function clean(name, archivePath, targetPath) {
fs.unlink(archivePath, err => {
if (err) throw err;
if (err) logError(err);

log("clean", archivePath);
create(targetPath, targetPath);
create(name, targetPath, targetPath);
log("success", `Generated application \x1b[36m${name}\x1b[0m`);
console.log(`To start, run:
cd ${name}
Expand All @@ -113,18 +111,18 @@ function clean(archivePath, targetPath) {
});
}

function create(currentPath, targetPath) {
const files = fs.readdirSync(currentPath);
function create(name, directoryPath, targetPath) {
const files = fs.readdirSync(directoryPath);

for (let i = 0; i < files.length; i++) {
const file = files[i];
const nextPath = path.join(currentPath, file);
const filePath = path.join(directoryPath, file);

if (fs.statSync(nextPath).isDirectory()) {
create(nextPath, targetPath);
if (fs.statSync(filePath).isDirectory()) {
create(name, filePath, targetPath);
} else {
fs.writeFileSync(nextPath, replace(fs.readFileSync(nextPath), "{# MoonName #}", name));
log("create", path.relative(targetPath, nextPath));
fs.writeFileSync(filePath, replace(fs.readFileSync(filePath), "{# MoonName #}", name));
log("create", path.relative(targetPath, filePath));
}
}
}
Expand Down Expand Up @@ -195,11 +193,11 @@ ${table(tableUsageDescription)}`);
}

case "create": {
name = commandArguments[0];
repo = commandOptions["-t"] || commandOptions["--template"] || "kbrsh/moon-template";
const name = commandArguments[0];
const repo = commandOptions["-t"] || commandOptions["--template"] || "kbrsh/moon-template";

if (name === undefined || name.length === 0) {
error(`Invalid or unknown name.
logError(`Invalid or unknown name.
Attempted to create an application.
Expand All @@ -209,7 +207,7 @@ Expected a valid name. Run \x1b[35mmoon help create\x1b[0m to see usage informat
}

if (repo === true) {
error(`Invalid or unknown template.
logError(`Invalid or unknown template.
Attempted to create an application.
Expand All @@ -232,17 +230,17 @@ Expected a valid template. Run \x1b[35mmoon help create\x1b[0m to see usage info
https.get(archive, res => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location !== undefined) {
https.get(res.headers.location, redirectRes => {
download(redirectRes);
download(name, repo, redirectRes);
});
} else {
download(res);
download(name, repo, res);
}
});
break;
}

default: {
error(`Unrecognized command.
logError(`Unrecognized command.
Attempted to execute a command.
Expand Down

0 comments on commit b842b29

Please sign in to comment.