diff --git a/README.md b/README.md index cbe9de5..89d7f57 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,24 @@ - Read file excel to json - Custom mapping with custom columns and configs +- Custom props + +# Install +```shell +npm i excel2json-xlsx +``` +OR +```shell +yarn i excel2json-xlsx +``` +OR Global Install +```shell +yarn add global excel2json-xlsx +``` +Usage +```shell +excel2json-xlsx -i people.xlsx -o exported.json +``` # Build diff --git a/bin/cli.js b/bin/cli.js new file mode 100644 index 0000000..55af658 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,44 @@ +#!/usr/bin/env node + +const yargs = require("yargs"); +const excel2json = require("./../excel2json"); + +const options = yargs + .usage("Usage: -i -o -m ") + .option("i", { + alias: "input", + describe: "Input File Excel (Required .xlsx)", + type: "string", + demandOption: true, + }) + .option("o", { + alias: "output", + describe: "Output File to JSON", + type: "string", + demandOption: false, + }) + .option("m", { + alias: "mapper", + describe: "Mapper File in JSON", + type: "string", + demandOption: false, + }) + .option("s", { + alias: "sheet", + describe: "Sheet Name in workbook", + type: "string", + demandOption: false, + }) + .argv; + +const inputFile = options.input; +const outputFile = options.output; +const mapperFile = options.mapper; +const sheetName = options.sheet; + +const exported = excel2json({ + inputFile: inputFile, + mapperFile: mapperFile, + outputFile: outputFile, + sheetName: sheetName, +}); diff --git a/excel2json.d.ts b/excel2json.d.ts index c1408ca..78d5980 100644 --- a/excel2json.d.ts +++ b/excel2json.d.ts @@ -2,6 +2,7 @@ interface Excel2JsonProps { mappings?: Array; saveToOutput?: boolean; outputPath?: string; + outputFile?: string; outputName?: string; sheetName?: string; inputFile?: string; diff --git a/excel2json.js b/excel2json.js index 2797060..7d5f5ef 100644 --- a/excel2json.js +++ b/excel2json.js @@ -1,10 +1,11 @@ const XLSX = require("xlsx"); const fs = require("fs"); +const { splitFilepath } = require("./util"); // do export for excel to json output or data json object function _internalExport(props = {}) { const NAME = process.env.APP_NAME || "excel2json"; - console.log("APP NAME =>", NAME, "\n"); + console.log("Name =>", NAME, "\n"); // load from env const INPUT_FILE = @@ -36,7 +37,6 @@ function _internalExport(props = {}) { // convert mapper from string to json object const mapperJson = mapperString ? JSON.parse(mapperString) : {}; const configs = { ...mapperJson.configs, ...props }; - console.log(configs); const columsData = props.mappings || mapperJson.data || undefined; // read workbook from excel file @@ -61,31 +61,47 @@ function _internalExport(props = {}) { }); // able to save to output or not (default is true) - if (configs.saveToOutputput) { - // parse a new path - const outputPath = configs.outputPath || OUTPUT_PATH; + if (configs.saveToOutputput || configs.outputFile) { + var outputPath = undefined; + const filePath = splitFilepath(configs.outputFile); - // check directory and create it if not exist - if (!fs.existsSync(outputPath)) { - fs.mkdirSync(outputPath, { recursive: true }); + // validate path and filename + if (filePath && filePath.path) { + outputPath = filePath.path; + } + + if (filePath.path) { + // parse a new path + outputPath = outputPath || configs.outputPath || OUTPUT_PATH; + + // check directory and create it if not exist + if (!fs.existsSync(outputPath)) { + fs.mkdirSync(outputPath, { recursive: true }); + } } // json data output const jsonStringData = JSON.stringify(data); try { - // write to file - fs.writeFileSync( - `${outputPath}/${ + var outFile = undefined; + if (configs.outputFile) { + outFile = configs.outputFile; + } else { + outFile = `${outputPath}/${ configs.outputName || "exported" - }_${new Date().getTime()}.json`, - jsonStringData, - (err) => { - if (err) throw err; - console.log("Write succeed!"); - } - ); - } catch (err) {} + }_${new Date().getTime()}.json`; + } + + // write to file + fs.writeFileSync(outFile, jsonStringData, (err) => { + if (err) throw err; + }); + + console.log("Exported excel to json to output =>", outFile); + } catch (err) { + console.error(err); + } } return data; diff --git a/package.json b/package.json index 89134c9..f636b4f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,17 @@ { "name": "excel2json-xlsx", - "version": "1.0.0", + "version": "1.0.1", + "keywords": [ + "excel", + "json", + "excel2json", + "parser", + "xlsx" + ], + "publishConfig": { + "access": "public" + }, + "bin": "bin/cli.js", "description": "Excel to JSON, able to export JSON from Excel (xlsx)", "repository": { "url": "https://github.com/CUBETIQ/excel2json.git" @@ -13,6 +24,7 @@ "author": "Sambo Chea ", "license": "MIT", "dependencies": { - "xlsx": "^0.16.9" + "xlsx": "^0.16.9", + "yargs": "^16.2.0" } } diff --git a/util.js b/util.js new file mode 100644 index 0000000..86e4416 --- /dev/null +++ b/util.js @@ -0,0 +1,15 @@ +/** + * Split Filepath to Path and Filename + * + * @param {String} filePath + * @returns Object of JSON + */ +module.exports.splitFilepath = function splitFilepath(filePath) { + const filename = filePath.replace(/^.*[\\\/]/, ""); + const path = filePath.replace("/" + filename, ""); + + return { + path: path == filename ? undefined : path, + filename, + }; +}