Skip to content

Commit

Permalink
feat: cmds add for op and nonop templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Mar 21, 2024
1 parent ecc693d commit 53b450f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"husky": "^8.0.3",
"prettier": "^2.8.4",
"release-it": "^16.1.5",
"rimraf": "^5.0.5",
"shx": "^0.3.4",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.5",
Expand Down
74 changes: 53 additions & 21 deletions src/commands/project.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@ import { spawn } from "child_process";
import { promises as fs } from "fs";
import path from "path";
import { Argv, CommandModule } from "yargs";
// TODO: To validate on non opinionated template
import Compiler from "../utils/compiler";

/**
* Load the configuration from the compiler
* @param compiler The compiler to load the configuration from
* @returns The configuration
*/

const opinionatedConfig: Array<string> = [
"--transpile-only",
"-r",
"dotenv/config",
"-r",
"tsconfig-paths/register",
"./src/main.ts",
];

const nonOpinionatedConfig: Array<string> = [
"--transpile-only",
"-r",
"dotenv/config",
"./src/main.ts",
];

/**
* Helper function to execute a command
* @param command The command to execute
Expand Down Expand Up @@ -44,11 +67,17 @@ const compileTypescript = async () => {

// Helper to copy files
const copyFiles = async () => {
const filesToCopy = [
"./register-path.js",
"tsconfig.build.json",
"package.json",
];
const { opinionated } = await Compiler.loadConfig();
let filesToCopy: Array<string> = [];
if (opinionated) {
filesToCopy = [
"./register-path.js",
"tsconfig.build.json",
"package.json",
];
} else {
filesToCopy = ["tsconfig.json", "package.json"];
}
filesToCopy.forEach((file) => {
fs.copyFile(file, path.join("./dist", path.basename(file)));
});
Expand All @@ -74,33 +103,36 @@ export const runCommandModule: CommandModule<{}, { command: string }> = {
};

const runCommand = async ({ command }: { command: string }): Promise<void> => {
const { opinionated } = await Compiler.loadConfig();
try {
switch (command) {
case "dev":
// Use execSync or spawn to run ts-node-dev programmatically
execCmd("tsnd", [
"--transpile-only",
"-r",
"dotenv/config",
"-r",
"tsconfig-paths/register",
"./src/main.ts",
]);
execCmd(
"tsnd",
opinionated ? opinionatedConfig : nonOpinionatedConfig,
);
break;
case "build":
await cleanDist();
await compileTypescript();
await copyFiles();
break;
case "prod":
let config: Array<string> = [];
if (opinionated) {
config = [
"-r",
"dotenv/config",
"-r",
"./dist/register-path.js",
"./dist/src/main.js",
];
} else {
config = ["-r", "dotenv/config", "./dist/main.js"];
}
// Ensure environment variables are set
execCmd("node", [
"-r",
"dotenv/config",
"-r",
"./dist/register-path.js",
"./dist/src/main.js",
]);
execCmd("node", config);
break;
default:
console.log(`Unknown command: ${command}`);
Expand Down

0 comments on commit 53b450f

Please sign in to comment.