vite.js
Version:
The simplest way to bootstrap a nodejs server and dive straight into coding.
264 lines (261 loc) • 8.6 kB
JavaScript
const { log, error } = require("../utils/index");
const prompts = require("prompts");
const chalk = require("chalk");
const fs = require("file-system");
const path = require("path");
var custom = {
location: "vitejs",
name: "vitejs-template",
port: 8080,
logging: true,
mongo: "",
views: "views",
public: "public"
};
exports.run = function(args) {
console.log(
chalk.hex("#29B120")(` %8
!
!
^
/ \
/___\
|= =|
| |
| |
| |
| |
| |
| |
| |
| |
| |
/|##!##|\
/ |##!##| \
/ |##!##| \
| / ^ | ^ \ |
| / ( | ) \ |
|/ ( | ) \|
(( ))
(( : ))
(( : ))
(( ))
(( ))
( )
. `)
);
log(
chalk.bgHex("#29B120")(" Init started ") +
"\n\nAnswer the questions below to generate a template of vite.js :)\n",
"white"
);
if (!process.env.TEST) {
const ask = prompts(
[
{
type: "text",
initial: custom.name,
name: "name",
message: "Name:"
},
{
type: "text",
initial: custom.location,
name: "location",
message: "Directory:"
},
{
type: "number",
initial: custom.port,
name: "port",
message: "Port:",
validate: value =>
parseInt(value) <= 0 || parseInt(value) > 65536
? "Port must be greater than 0 and less than 65536"
: true
},
{
type: "select",
name: "logging",
message: "Logging:",
choices: [
{ title: "Yes", value: true },
{ title: "No", value: false }
]
},
{
type: "text",
name: "mongo",
initial: "",
message: "Mongo URL:"
},
{
type: "text",
name: "views",
initial: custom.views,
message: "Views directory:"
},
{
type: "text",
name: "public",
initial: custom.public,
message: "Public directory:"
}
],
{
onCancel() {
error({ message: "Vite.js init canceled :(" });
process.exit(0);
}
}
).then(function(res) {
prompts({
type: "select",
name: "continue",
message: "Are you sure you would like to continue?",
choices: [{ title: "Yes", value: true }, { title: "No", value: false }]
}).then(function(next) {
if (next.continue === 1) {
return error({ message: "Vite.js init canceled :(" });
}
const location = path.resolve(process.cwd(), res.location);
fs.stat(location, function(err, stats) {
if (!err || stats) {
return error({
message:
"For safety reasons we do not overwrite existing directories, either delete " +
res.location +
" or choose a new directory name"
});
}
fs.mkdir(location, function(err) {
if (err) return error(err);
fs.mkdir(path.resolve(location, res.views), function(err) {
if (err) return error(err);
fs.mkdir(path.resolve(location, res.public), function(err) {
if (err) return error(err);
fs.writeFile(
path.resolve(location, "main.js"),
`
const ViteJS = require("vite.js");
const { app } = new ViteJS({
name: "${res.name}",
port: ${res.port},
log: ${res.logging},
mongo: "${res.mongo}",
viewsDir: "${path.resolve(location, res.views)}",
publicDir: "${path.resolve(location, res.public)}"
});
`,
function(err) {
if (err) {
return error(err);
}
fs.writeFile(
path.resolve(location, "package.json"),
`{
"name": "${res.name}",
"version": "1.0.0",
"main": "./main.js",
"scripts": {
"start": "node ./main.js"
},
"author": "AlQaholic007 <mail@sohamp.dev>",
"repository": "git://github.com/AlQaholic007/vite.js.git",
"bugs": {
"url": "https://github.com/AlQaholic007/tracker/issues"
},
"dependencies": {
"vite.js": "^${
require("../../../package.json").version
}"
},
"license": "MIT"
}`,
function(err) {
if (err) return error(err);
require("child_process").exec(
"cd " +
location +
"; npm install --save vite.js",
function() {
log("Init was finished!");
}
);
}
);
}
);
});
});
});
});
});
});
} else {
const res = custom;
const location = path.resolve(process.cwd(), "src/test", res.location);
fs.stat(location, function(err, stats) {
if (!err || stats) {
return error({
message:
"For safety reasons we do not overwrite existing directories, either delete " +
res.location +
" or choose a new directory name"
});
}
fs.mkdir(location, function(err) {
if (err) return error(err);
fs.mkdir(path.resolve(location, res.views), function(err) {
if (err) return error(err);
fs.mkdir(path.resolve(location, res.public), function(err) {
if (err) return error(err);
fs.writeFile(
path.resolve(location, "main.js"),
`const ViteJS = require("vite.js");
const { app } = new ViteJS({
name: "${res.name}",
port: ${res.port},
log: ${res.logging},
mongo: "${res.mongo}",
viewsDir: "${path.resolve(location, res.views)}",
publicDir: "${path.resolve(location, res.public)}"
});`,
function(err) {
if (err) return error(err);
fs.writeFile(
path.resolve(location, "package.json"),
`{
"name": "${res.name}",
"version": "1.0.0",
"main": "./main.js",
"scripts": {
"start": "node ./main.js"
},
"author": "AlQaholic007 <mail@sohamp.dev>",
"repository": "git://github.com/AlQaholic007/vite.js.git",
"bugs": {
"url": "https://github.com/AlQaholic007/tracker/issues"
},
"dependencies": {
"vite.js": "^${require("../../../package.json").version}"
},
"license": "MIT"
}`,
function(err) {
if (err) return error(err);
log("Init was finished!");
}
);
}
);
});
});
});
});
}
};
module.exports.info = {
name: "init",
alias: ["start", "run", "generate"]
};