roblox-ts
Version:
<div align="center"><img width=25% src="https://i.imgur.com/yCjHmng.png"></div> <h1 align="center"><a href="https://roblox-ts.github.io/">roblox-ts</a></h1> <div align="center">A TypeScript-to-Lua Compiler for Roblox</div> <br> <div align="center"> <a hr
211 lines • 7.11 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chokidar_1 = __importDefault(require("chokidar"));
const cross_spawn_1 = __importDefault(require("cross-spawn"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const yargs_1 = __importDefault(require("yargs"));
const CompilerError_1 = require("./errors/CompilerError");
const ProjectError_1 = require("./errors/ProjectError");
const Project_1 = require("./Project");
const utility_1 = require("./utility");
const CHOKIDAR_OPTIONS = {
awaitWriteFinish: {
pollInterval: 10,
stabilityThreshold: 50,
},
ignoreInitial: true,
ignorePermissionErrors: true,
interval: 100,
usePolling: true,
};
// cli interface
const argv = yargs_1.default
.usage("Usage: rbxtsc [options]")
// version
.alias("v", "version")
.version(require("../package.json").version)
.describe("version", "show version information")
// help
.alias("h", "help")
.help("help")
.describe("help", "show help information")
.showHelpOnFail(false, "specify --help for available options")
// watch
.option("w", {
alias: "watch",
boolean: true,
describe: "enable watch mode",
})
// project
.option("p", {
alias: "project",
default: ".",
describe: "project path",
})
// noInclude
.option("noInclude", {
boolean: true,
default: false,
describe: "do not copy runtime files",
})
// includePath
.option("i", {
alias: "includePath",
default: "include",
describe: "folder to copy runtime files to",
})
// modulesPath
.option("m", {
alias: "modulesPath",
default: "modules",
describe: "folder to copy modules to",
})
// minify
.option("minify", {
alias: "min",
boolean: true,
default: false,
describe: "minify emitted Lua code",
})
// onSuccess
.option("onSuccess", {
default: "",
describe: "Command to run on watch success",
})
// rojo
.option("rojo", {
alias: "r",
default: "",
describe: "Manually select Rojo configuration file",
})
// parse
.parse();
const project = new Project_1.Project(argv);
if (argv.watch === true) {
const rootDir = project.getRootDirOrThrow();
let isCompiling = false;
const onSuccessCommand = argv.onSuccess;
function onSuccess() {
return __awaiter(this, void 0, void 0, function* () {
if (onSuccessCommand.length > 0) {
const parts = onSuccessCommand.split(/\s+/);
yield cross_spawn_1.default(parts.shift(), parts, { stdio: "inherit" });
}
});
}
const time = (callback) => __awaiter(this, void 0, void 0, function* () {
const start = Date.now();
try {
yield callback();
}
catch (e) {
if (e instanceof ProjectError_1.ProjectError || e instanceof CompilerError_1.CompilerError) {
process.exitCode = 0;
}
else {
throw e;
}
}
console.log(`Done, took ${Date.now() - start} ms!`);
});
function update(filePath) {
return __awaiter(this, void 0, void 0, function* () {
console.log("Change detected, compiling..");
yield project.refreshFile(filePath);
utility_1.clearContextCache();
yield time(() => __awaiter(this, void 0, void 0, function* () {
try {
yield project.compileFileByPath(filePath);
}
catch (e) {
console.log(e);
process.exit();
}
}));
if (process.exitCode === 0) {
yield onSuccess();
}
});
}
function updateAll() {
return __awaiter(this, void 0, void 0, function* () {
yield time(() => __awaiter(this, void 0, void 0, function* () {
try {
yield project.compileAll();
}
catch (e) {
console.log(e);
process.exit();
}
}));
if (process.exitCode === 0) {
yield onSuccess();
}
});
}
chokidar_1.default
.watch(rootDir, CHOKIDAR_OPTIONS)
.on("change", (filePath) => __awaiter(this, void 0, void 0, function* () {
if (!isCompiling) {
isCompiling = true;
yield update(filePath);
isCompiling = false;
}
}))
.on("add", (filePath) => __awaiter(this, void 0, void 0, function* () {
if (!isCompiling) {
isCompiling = true;
project.addFile(filePath);
yield update(filePath);
isCompiling = false;
}
}))
.on("unlink", (filePath) => __awaiter(this, void 0, void 0, function* () {
if (!isCompiling) {
isCompiling = true;
yield project.removeFile(filePath);
isCompiling = false;
}
}));
if (project.configFilePath) {
chokidar_1.default.watch(project.configFilePath, CHOKIDAR_OPTIONS).on("change", () => __awaiter(this, void 0, void 0, function* () {
console.log("tsconfig.json changed! Recompiling project..");
project.reloadProject();
yield updateAll();
}));
}
if (project.rojoFilePath) {
chokidar_1.default.watch(project.rojoFilePath, CHOKIDAR_OPTIONS).on("change", () => __awaiter(this, void 0, void 0, function* () {
console.log("Rojo configuration changed! Recompiling project..");
project.reloadRojo();
yield updateAll();
}));
}
const pkgLockJsonPath = path_1.default.resolve("package-lock.json");
if (fs_1.default.existsSync(pkgLockJsonPath)) {
chokidar_1.default.watch(pkgLockJsonPath).on("change", (filePath) => __awaiter(this, void 0, void 0, function* () {
console.log("Modules updated, copying..");
yield project.copyModuleFiles();
}));
}
console.log("Running in watch mode..");
console.log("Starting initial compile..");
updateAll();
}
else {
project.compileAll();
}
//# sourceMappingURL=cli.js.map