Skip to content

Commit

Permalink
Merge pull request #50 from arclix/48-create-separate-command-to-gene…
Browse files Browse the repository at this point in the history
…rate-arclixconfigjson-file-to-existing-react-project

feat(config): create a command to generate config file (#48)
  • Loading branch information
jitiendran authored Mar 29, 2023
2 parents fe00964 + 1458282 commit 0b12aaa
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": "https://github.com/arclix/core.git"
},
"version": "0.1.0",
"description": "An Open Source CLI for React.JS",
"description": "An Open Source CLI with Creation and Component Generation for React.",
"main": "dist/index.js",
"type": "module",
"files": [
Expand Down
33 changes: 29 additions & 4 deletions src/functions/GenerateConfigFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import chalk from "chalk";
import fs from "fs";
import path from "path";
import getRootPath from "../helpers/getRootPath.js";
import { emptyLine, log, spinner } from "../utilities/utility.js";
import { ArclixConfig, GenerateConfig } from "../types/interface.js";

/**
Expand Down Expand Up @@ -33,12 +36,34 @@ export default class GenerateConfigFile {
return GenerateConfigFile.instance;
}

public generateConfigFile = (projectName: string) => {
/**
* Generate config file while creating the project if projectName exists.
* Otherwise generates config file to existing project.
*
* @param projectName to be created with arclix
*/
public generateConfigFile = (projectName?: string) => {
const currentDir = process.cwd();
const content = `${JSON.stringify(this.config, null, 2)}\n`;

process.chdir(path.join(currentDir, `./${projectName}`));
fs.writeFileSync("arclix.config.json", content);
process.chdir(currentDir);
if (projectName) {
process.chdir(path.join(currentDir, `./${projectName}`));
fs.writeFileSync("arclix.config.json", content);
process.chdir(currentDir);
} else {
const rootPath = getRootPath(currentDir);
const configPath = path.join(rootPath, "./arclix.config.json");

if (fs.existsSync(configPath)) {
log("\nArclix config file already exists.\n");
return;
}

fs.writeFileSync(configPath, content);
emptyLine();
spinner.success({
text: `Generated ${chalk.green("arclix.config.json")} file.\n`,
});
}
};
}
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import chalk from "chalk";
import CreateProject from "./functions/CreateProject.js";
import GenerateComponent from "./functions/GenerateComponent.js";
import GenerateConfigFile from "./functions/GenerateConfigFile.js";
import { OptionValues, program } from "commander";
import { Command, AliasCommand } from "./types/enum.js";
import { log, emptyLine, primaryChalk, spinner } from "./utilities/utility.js";

const version = "ARCLIX v0.1.0";
const createProjectInstance = CreateProject.getInstance();
const generateComponentInstance = GenerateComponent.getInstance();
const generateConfigFileInstance = GenerateConfigFile.getInstance();

const checkProjectName = (projectName: string): boolean => {
if (projectName !== projectName.toLowerCase()) {
Expand All @@ -18,7 +21,7 @@ const checkProjectName = (projectName: string): boolean => {
};

program.version(
"ARCLIX v0.1.0",
version,
"-v --version",
"Displays the version of Arclix in use",
);
Expand All @@ -27,7 +30,7 @@ program
.command(Command.CREATE)
.description("Creates React project in the current directory")
.action(async (_str, options) => {
log("\n" + primaryChalk.italic.bold("ARCLIX v0.1.0"));
log("\n" + primaryChalk.italic.bold(version));
emptyLine();

const projectName = options.args[0];
Expand Down Expand Up @@ -71,9 +74,17 @@ generate
.option("-f, --flat", "Generates components without parent folder.")
.option("-p, --path <string>", "Generates components based on the path.")
.action((componentName: string, options: OptionValues) => {
log("\n" + primaryChalk.italic.bold("ARCLIX v0.1.0"));
log("\n" + primaryChalk.italic.bold(version));
emptyLine();
generateComponentInstance.generateProject(componentName, options);
});

program
.command(Command.INIT)
.description("Generated config file to the existing react project.")
.action(() => {
log("\n" + primaryChalk.italic.bold(version));
generateConfigFileInstance.generateConfigFile();
});

program.parse(process.argv);
1 change: 1 addition & 0 deletions src/types/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ enum Command {
CREATE = "create",
GENERATE = "generate",
COMPONENT = "component",
INIT = "init",
}

enum AliasCommand {
Expand Down

0 comments on commit 0b12aaa

Please sign in to comment.