Skip to content

Commit

Permalink
feat: add output file name (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikghosh256 authored Apr 2, 2024
1 parent 5df3c76 commit 502b37d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 72 deletions.
139 changes: 78 additions & 61 deletions cmds/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,95 @@ const chalk = require('chalk');
const fs = require('fs');
const compile = require('@anikghosh256/compile-template');

exports.command = 'create [moduleName]'
exports.desc = 'Create a kitty component.'
exports.command = 'create [moduleName]';
exports.desc = 'Create a kitty component.';

exports.handler = function (argv) {
try {
if(argv.moduleName === undefined) {
throw new Error('Please provide a name for the component.')
}
const projectPath = process.cwd();
const configFilePath = path.resolve(projectPath, `./kitty/${argv.moduleName}/config.json`);
try {
if (argv.moduleName === undefined) {
throw new Error('Please provide a name for the component.');
}
const projectPath = process.cwd();
const configFilePath = path.resolve(
projectPath,
`./kitty/${argv.moduleName}/config.json`
);

// check if config file exists
if(!fs.existsSync(configFilePath)) {
throw new Error(`Component ${argv.moduleName} does not exist.`)
}
// check if config file exists
if (!fs.existsSync(configFilePath)) {
throw new Error(`Component ${argv.moduleName} does not exist.`);
}

const config = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));
const config = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));

// check source file
if(config.sourceFile === undefined) {
throw new Error(`Source file not defined in config file.`)
}
// check source file
if (config.sourceFile === undefined) {
throw new Error(`Source file not defined in config file.`);
}

// check source file exists
const sourceFile = path.resolve(projectPath, `./kitty/${argv.moduleName}/${config.sourceFile}`);
if(!fs.existsSync(sourceFile)) {
throw new Error(`Source file ${config.sourceFile} does not exist.`)
}
// check source file exists
const sourceFile = path.resolve(
projectPath,
`./kitty/${argv.moduleName}/${config.sourceFile}`
);
if (!fs.existsSync(sourceFile)) {
throw new Error(`Source file ${config.sourceFile} does not exist.`);
}

// check required fields and make object
const inputKeys = Object.keys(config.inputs);
const inputObject = {};

for(let i = 0; i < inputKeys.length; i++) {
if(argv[inputKeys[i]] === undefined) {
if(config.inputs[inputKeys[i]].default === undefined) {
throw new Error(`Please provide a value for ${inputKeys[i]}.`)
}
argv[inputKeys[i]] = config.inputs[inputKeys[i]].default;
}
inputObject[inputKeys[i]] = argv[inputKeys[i]];
}
// check required fields and make object
const inputKeys = Object.keys(config.inputs);
const inputObject = {};

// check name field
if(argv.name === undefined) {
throw new Error(`Please provide a name for the component. It will be used as the output file name.`)
}
for (let i = 0; i < inputKeys.length; i++) {
if (argv[inputKeys[i]] === undefined) {
if (config.inputs[inputKeys[i]].default === undefined) {
throw new Error(
`Please provide a value for ${inputKeys[i]}.`
);
}
argv[inputKeys[i]] = config.inputs[inputKeys[i]].default;
}
inputObject[inputKeys[i]] = argv[inputKeys[i]];
}

// check output folder or create if not exist
const outputFolder = path.resolve(projectPath, config.dir);
if(!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
}
// check name field
if (argv.name === undefined) {
throw new Error(
`Please provide a name for the component. It will be used as the output file name.`
);
}

// compile template
const compiled = compile(sourceFile, inputObject);
// check output folder or create if not exist
const outputFolder = path.resolve(projectPath, config.dir);
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
}

// write to output file
const outputFile = path.resolve(projectPath, `${config.dir}/${argv.name}.${config.outputExtension}`);
fs.writeFileSync(outputFile, compiled);

console.log();
console.log(chalk.green('Component created successfully!'));
// compile template
const compiled = compile(sourceFile, inputObject);

// check if config file has successMessage
if(config.successMessage !== undefined) {
console.log();
console.log(chalk.green(config.successMessage));
}
// write to output file
let outputFile = path.resolve(
projectPath,
`${config.dir}/${argv.name}.${config.outputExtension}`
);
if (config.outputFile) {
outputFile = path.resolve(
projectPath,
`${config.dir}/${compile(null, inputObject, config.outputFile)}`
);
}
fs.writeFileSync(outputFile, compiled);

} catch (error) {
console.log(chalk.red(error.message));
}
}
console.log();
console.log(chalk.green('Component created successfully!'));

// check if config file has successMessage
if (config.successMessage !== undefined) {
console.log();
console.log(chalk.green(config.successMessage));
}
} catch (error) {
console.log(chalk.red(error.message));
}
};
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@anikghosh256/kitty-cli",
"description": "CLI tool for creating kitty component.",
"version": "0.3.2",
"version": "0.4.0",
"license": "MIT",
"bin": {
"kitty-cli": "kitty.js",
Expand Down Expand Up @@ -36,7 +36,7 @@
"format": "prettier --write \"./**/*.{js,json}\""
},
"dependencies": {
"@anikghosh256/compile-template": "^1.2.0",
"@anikghosh256/compile-template": "^1.4.0",
"chalk": "^4.1.2",
"yargs": "^17.7.1"
},
Expand Down
1 change: 1 addition & 0 deletions templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ To create a module template you have to create new folder in kitty folder with m
```json
{
"outputExtension": "js",
"outputFile": "example.js",
"sourceFile": "./example.kitty",
"dir": "./",
"inputs": {
Expand Down
1 change: 1 addition & 0 deletions templates/example/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"outputExtension": "js",
"outputFile": "example.js",
"sourceFile": "./example.kitty",
"dir": "./",
"inputs": {
Expand Down

0 comments on commit 502b37d

Please sign in to comment.