Skip to content

Commit

Permalink
moon-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jul 17, 2018
1 parent 7928036 commit c6afd10
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
Empty file added packages/moon-cli/README.md
Empty file.
84 changes: 84 additions & 0 deletions packages/moon-cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");
const https = require("https");
const exec = require("child_process").exec;

const name = process.argv[2];
const repo = process.argv[3] || "kbrsh/moon-template";
const archive = {
method: "GET",
host: "api.github.com",
path: `/repos/${repo}/tarball/master`,
headers: {
"User-Agent": "Node.js"
}
};

const log = (type, message) => {
console.log(`\x1b[34m${type}\x1b[0m ${message}`);
};

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

res.on("data", (chunk) => {
stream.write(chunk);
});

res.on("end", () => {
stream.end();
log("download", "template");
install(archivePath);
});
};

const install = (archivePath) => {
const targetPath = path.join(process.cwd(), name);
exec(`mkdir ${targetPath}; tar -xzf ${archivePath} -C ${targetPath} --strip=1`, (err) => {
if (err) throw err;

log("install", "template");

clean(archivePath, targetPath);
});
};

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

log("clean", "template");

create(targetPath, targetPath);
log("success", `Generated project "${name}"`)
console.log(`To start, run:
npm install
npm run dev`);
});
};

const create = (currentPath, targetPath) => {
const files = fs.readdirSync(currentPath);
for (let i = 0; i < files.length; i++) {
const file = files[i];
const nextPath = path.join(currentPath, file);
if (fs.statSync(nextPath).isDirectory()) {
create(nextPath, targetPath);
} else {
log("create", path.relative(targetPath, nextPath));
}
}
};

https.get(archive, (res) => {
if (res.statusCode > 300 && res.statusCode < 400 && res.headers.location !== undefined) {
https.get(res.headers.location, (redirectRes) => {
download(redirectRes);
});
} else {
download(res);
}
});
27 changes: 27 additions & 0 deletions packages/moon-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "moon-cli",
"version": "1.0.0-beta.1",
"description": "Moon project generator",
"main": "index.js",
"bin": {
"moon": "index.js"
},
"scripts": {
"build": "",
"test": ""
},
"repository": {
"type": "git",
"url": "git+https://github.com/kbrsh/moon.git"
},
"keywords": [
"moon",
"cli"
],
"author": "Kabir Shah",
"license": "MIT",
"bugs": {
"url": "https://github.com/kbrsh/moon/issues"
},
"homepage": "https://github.com/kbrsh/moon#readme"
}

0 comments on commit c6afd10

Please sign in to comment.