@profitsniper/telegram
Version:
While developing with Typescript and Node.js is awesome, **setting up a new project is painful**. This minimal and modern starter repo is here to help you get started with Node.js and Typecript without the pain.
179 lines • 6.43 kB
JavaScript
;
// @ts-check
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.lint = exports.format = exports.clean = exports.watchTs = exports.watchNode = exports.start = exports.bundle = exports.test = exports.build = void 0;
/**
* Specifying all tasks (build, test, bundle, etc..) inside code to be able to
* more easily guarantuee a similar interface across operating systems (linux,
* macos, windows).
*/
var node_path_1 = __importDefault(require("node:path"));
var nodemon_1 = __importDefault(require("nodemon"));
var shelljs_1 = __importDefault(require("shelljs"));
/**
* Builds project. Typescript -> Javascript
* @param {Array<string>} [additionalArguments]
*/
var build = function (additionalArguments) {
if (additionalArguments === void 0) { additionalArguments = []; }
(0, exports.clean)();
var pathToTsc = ["node_modules", "typescript", "bin", "tsc"].join(node_path_1.default.sep);
var code = shelljs_1.default.exec("node ".concat(pathToTsc, " ").concat(additionalArguments.join(" "))).code;
handleNonZeroReturnCode(code);
};
exports.build = build;
/**
* Tests the project.
*/
var test = function () {
(0, exports.build)();
// runs tests based on the test runner execution model
// src: https://nodejs.org/api/test.html#test-runner-execution-model"
var code = shelljs_1.default.exec("node --test --test-reporter spec").code;
handleNonZeroReturnCode(code);
};
exports.test = test;
/**
* Bundles the code into a single file.
*/
var bundle = function () {
// Builds without emitting to ensure that the code is still valid typescript
// before bundling.
(0, exports.build)(["--noEmit"]);
var pathToEsbuild = ["node_modules", "esbuild", "bin", "esbuild"].join(node_path_1.default.sep);
var code = shelljs_1.default.exec("node ".concat(pathToEsbuild, " src/index.ts --outdir=bundle --bundle --platform=node --target=node18.16.0")).code;
handleNonZeroReturnCode(code);
};
exports.bundle = bundle;
/**
* Runs your code. Only works if it was built before.
*/
var start = function () {
var pathToCode = ["dist", "src", "index.js"].join(node_path_1.default.sep);
var code = shelljs_1.default.exec("node ".concat(pathToCode)).code;
handleNonZeroReturnCode(code);
};
exports.start = start;
/**
* Runs your javscript code every time it changes. Use this while developing to
* reduce the time taken to get feedback.
* Typically invoked by running "yarn dev".
*/
var watchNode = function () {
var pathToCode = ["dist", "src", "index.js"].join(node_path_1.default.sep);
(0, nodemon_1.default)({
script: pathToCode,
ext: "js json",
});
nodemon_1.default
.on("start", function () {
console.log("App has started");
})
.on("quit", function () {
console.log("App has quit");
process.exit();
})
.on("restart", function (files) {
console.log("App restarted due to: ", files);
});
};
exports.watchNode = watchNode;
/**
* Compiles your typescript code to javascript every time it changes. Use this
* while developing to reduce the time taken to get feedback.
* Typically invoked by running "yarn dev".
*/
var watchTs = function () {
var pathToTsc = ["node_modules", "typescript", "bin", "tsc"].join(node_path_1.default.sep);
var code = shelljs_1.default.exec("node ".concat(pathToTsc, " -w")).code;
handleNonZeroReturnCode(code);
};
exports.watchTs = watchTs;
/**
* Cleans up all built/bundled files by removing the bundle and dist
* directories.
*/
var clean = function () {
shelljs_1.default.rm("-rf", ["dist", "build"]);
};
exports.clean = clean;
/**
* Formats the whole codebase (ignores files specified in .prettierignore).
* @param {Array<string>} [additionalArguments]
*/
var format = function (additionalArguments) {
var pathToPrettier = [
"node_modules",
"prettier",
"bin",
"prettier.cjs",
].join(node_path_1.default.sep);
var code = shelljs_1.default.exec("node ".concat(pathToPrettier, " . ").concat(additionalArguments)).code;
if (code !== 0) {
console.error("task failed with code ".concat(code));
console.error("Run `yarn format` to format your codebase. Or set up the pre-commit hook to avoid these kind of issues.");
process.exit(code);
}
};
exports.format = format;
/**
* Lints the whole codebase (ignores files specified in .eslintignore).
* @param {Array<string>} [additionalArguments]
*/
var lint = function (additionalArguments) {
var pathToEslint = ["node_modules", "eslint", "bin", "eslint.js"].join(node_path_1.default.sep);
var code = shelljs_1.default.exec("node ".concat(pathToEslint, " . ").concat(additionalArguments)).code;
if (code !== 0) {
console.error("task failed with code ".concat(code));
console.error("Run `yarn lint` to lint your codebase. It fixes automatically fixable problems for you." +
"The rest of the problems have to be figured out and cleared by yourself.");
process.exit(code);
}
};
exports.lint = lint;
/**
* @param {number} code
**/
var handleNonZeroReturnCode = function (code) {
if (code !== 0) {
console.error("task failed with code ".concat(code));
process.exit(code);
}
};
var main = function () {
var _a = process.argv, _ = _a[0], __ = _a[1], taskName = _a[2], additionalArguments = _a.slice(3);
if (taskName === "build") {
return (0, exports.build)();
}
if (taskName === "bundle") {
return (0, exports.bundle)();
}
if (taskName === "clean") {
return (0, exports.clean)();
}
if (taskName === "format") {
return (0, exports.format)(additionalArguments);
}
if (taskName === "lint") {
return (0, exports.lint)(additionalArguments);
}
if (taskName === "start") {
return (0, exports.start)();
}
if (taskName === "test") {
return (0, exports.test)();
}
if (taskName === "watch-node") {
return (0, exports.watchNode)();
}
if (taskName === "watch-ts") {
return (0, exports.watchTs)();
}
console.error("Unknown task: ".concat(taskName));
process.exit(-1);
};
main();
//# sourceMappingURL=tasks.mjs.map