ocat-lang
Version:
A programming language for the web design and development
125 lines (124 loc) • 5.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const yargs_1 = __importDefault(require("yargs"));
const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process");
const lang_1 = require("../lang");
const utils_1 = require("../runner/utils");
const constants_1 = require("../runner/constants");
// Functions
function init(name) {
console.log(`Initializing ${name}`);
console.time("Create Project");
if (fs_1.default.existsSync(name)) {
fs_1.default.rmSync(name, { recursive: true });
}
console.time("Folders");
console.log();
fs_1.default.mkdirSync(name);
fs_1.default.mkdirSync(`${name}/src`);
fs_1.default.mkdirSync(`${name}/lib`);
fs_1.default.mkdirSync(`${name}/out`);
fs_1.default.mkdirSync(`${name}/.ocat`);
fs_1.default.mkdirSync(`${name}/css`);
fs_1.default.mkdirSync(`${name}/assets`);
fs_1.default.mkdirSync(`${name}/templates`);
fs_1.default.mkdirSync(`${name}/views`);
console.timeEnd("Folders");
console.log();
console.time("Files");
fs_1.default.writeFileSync(`${name}/.ocat/config.json`, `{
"name": "${name}",
"views": false,
"port": 8080,
"properties": {
"defined": {},
"provided": []
},
"path": {
"@/": "./src/"
}
}`);
fs_1.default.writeFileSync(`${name}/src/main.ocat`, 'print ( "Hello World" )');
fs_1.default.writeFileSync(`${name}/css/style.css`, constants_1.preStyles);
fs_1.default.writeFileSync(`${name}/css/globals.css`, "/* Set here your styles */");
fs_1.default.writeFileSync(`${name}/assets/ocat.js`, constants_1.preJs);
fs_1.default.writeFileSync(`${name}/templates/404.html`, constants_1.code404);
fs_1.default.writeFileSync(`${name}/templates/routeTemplate.html`, constants_1.codeRouteTemplate);
console.timeEnd("Files");
console.log();
console.timeEnd("Create Project");
console.log();
console.log();
console.log(`Project ${name} created`);
(0, child_process_1.exec)(`cd ${name}`);
}
function run() {
(0, lang_1.init)(false, false, "./src/main.ocat");
}
function newComponent(type, name) {
const path = `./src/${type}s/${name}/${name}`;
if (fs_1.default.existsSync(path)) {
console.log(`${type} ${name} already exists`);
return;
}
let content = "";
if (type === "component") {
content = `<title>${name}</title>\n<div>Component works!</div>`;
}
else if (type === "layout") {
content = `<div></div>{*children*}`;
}
if (!fs_1.default.existsSync(`./src/${type}s`)) {
fs_1.default.mkdirSync(`./src/${type}s`);
}
fs_1.default.mkdirSync(`./src/${type}s/${name}`);
fs_1.default.writeFileSync(path + ".component.html", content);
fs_1.default.writeFileSync(path + ".component.css", "/* Put your styles here */");
console.log(`${type} ${name} created`);
}
function AMPath(path, to) {
var _a;
const pathJson = JSON.parse((_a = (0, utils_1.readFile)("./.ocat/ocat.json")) !== null && _a !== void 0 ? _a : "{}");
const hasPath = pathJson["path"].hasOwnProperty(path);
pathJson["path"][path] = to;
fs_1.default.writeFileSync("./.ocat/ocat.json", JSON.stringify(pathJson, null, 4));
console.log(`Path ${path} ${hasPath ? "changed" : "added"} to ${to}`);
}
function delComponent(name) {
const path = `./src/components/${name}`;
if (fs_1.default.existsSync(path)) {
fs_1.default.rmSync(path, { recursive: true });
console.log(`${name} deleted`);
}
else {
console.log(`${name} not found`);
}
}
function modifyPort(port) {
var _a;
const config = JSON.parse((_a = (0, utils_1.readFile)("./.ocat/config.json")) !== null && _a !== void 0 ? _a : "{}");
config.port = port;
fs_1.default.writeFileSync("./.ocat/config.json", JSON.stringify(config, null, 4));
console.log(`Port modified to ${port}`);
}
yargs_1.default
.command("init <name>", "Initialize a new project", (args) => args.positional("name", { describe: "Project name" }), (argv) => init(argv.name))
.command("run", "Run the project", () => { }, () => run())
.command("new <type> <name>", "Create a new component or layout", (args) => args
.positional("type", { describe: "Component or layout" })
.positional("name", {
describe: "Name of the component or layout",
}), (argv) => newComponent(argv.type, argv.name))
.command("del <name>", "Delete a component", (args) => args.positional("name", { describe: "Name of the component" }), (argv) => delComponent(argv.name))
.command("path <path> <to>", "Add or change a RelPath", (args) => args
.positional("path", { describe: "Path to change" })
.positional("to", { describe: "New path" }), (argv) => AMPath(argv.path, argv.to))
.command("port <port>", "Modify the port", (args) => args.positional("port", { describe: "New port" }), (argv) => modifyPort(argv.port))
.help()
.alias("version", "v")
.alias("help", "h").argv;