From 502b37d25fe369086021a2062293605c6a518cf5 Mon Sep 17 00:00:00 2001 From: Anik Ghosh <52236930+anikghosh256@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:41:56 +0600 Subject: [PATCH] feat: add output file name (#11) --- cmds/create.js | 139 +++++++++++++++++++--------------- package-lock.json | 18 ++--- package.json | 4 +- templates/README.md | 1 + templates/example/config.json | 1 + 5 files changed, 91 insertions(+), 72 deletions(-) diff --git a/cmds/create.js b/cmds/create.js index b00464a..57cd029 100644 --- a/cmds/create.js +++ b/cmds/create.js @@ -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)); + } +}; diff --git a/package-lock.json b/package-lock.json index ca5bae0..990ef38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@anikghosh256/kitty-cli", - "version": "0.3.2", + "version": "0.4.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@anikghosh256/kitty-cli", - "version": "0.3.2", + "version": "0.4.0", "license": "MIT", "dependencies": { - "@anikghosh256/compile-template": "^1.2.0", + "@anikghosh256/compile-template": "^1.4.0", "chalk": "^4.1.2", "yargs": "^17.7.1" }, @@ -22,9 +22,9 @@ } }, "node_modules/@anikghosh256/compile-template": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/@anikghosh256/compile-template/-/compile-template-1.2.0.tgz", - "integrity": "sha512-IyXgjuYH4iz0ERhTen3piR+G5+8Qtd57RIggroZTEVU+MWrpjYbUDi9ICdYJOvW+kGAoOJ1oB1nLFJ1caZz11g==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/@anikghosh256/compile-template/-/compile-template-1.4.0.tgz", + "integrity": "sha512-EMVR9I59bvy2eV4jY31Vv3TyWYbkIAE8cX6vYNda8rLG4WNz5w9JA3ahdUgsr0/FXPe0EncQ65u2iWAprHS0dQ==", "dependencies": { "pluralize": "^8.0.0" } @@ -250,9 +250,9 @@ }, "dependencies": { "@anikghosh256/compile-template": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/@anikghosh256/compile-template/-/compile-template-1.2.0.tgz", - "integrity": "sha512-IyXgjuYH4iz0ERhTen3piR+G5+8Qtd57RIggroZTEVU+MWrpjYbUDi9ICdYJOvW+kGAoOJ1oB1nLFJ1caZz11g==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/@anikghosh256/compile-template/-/compile-template-1.4.0.tgz", + "integrity": "sha512-EMVR9I59bvy2eV4jY31Vv3TyWYbkIAE8cX6vYNda8rLG4WNz5w9JA3ahdUgsr0/FXPe0EncQ65u2iWAprHS0dQ==", "requires": { "pluralize": "^8.0.0" } diff --git a/package.json b/package.json index 48822a5..e62122d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/templates/README.md b/templates/README.md index a286e36..1eb04ad 100644 --- a/templates/README.md +++ b/templates/README.md @@ -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": { diff --git a/templates/example/config.json b/templates/example/config.json index 53b61ef..7df9774 100644 --- a/templates/example/config.json +++ b/templates/example/config.json @@ -1,5 +1,6 @@ { "outputExtension": "js", + "outputFile": "example.js", "sourceFile": "./example.kitty", "dir": "./", "inputs": {