-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |