Skip to content

Commit

Permalink
support for config and --outDir argument
Browse files Browse the repository at this point in the history
  • Loading branch information
amirsaeed671 committed Jan 1, 2023
1 parent 872aa3d commit d31e0ff
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 22 deletions.
24 changes: 18 additions & 6 deletions bin/index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const matter_gen_cli_config_1 = __importDefault(require("./matter-gen-cli.config"));
const args = [
{
name: "--meta",
description: "pass values in comma separated string i.e --meta 'id,title,date'",
description: "*pass values in comma separated string i.e --meta 'id,title,date'",
},
{
name: "--outDir",
description: "provide the path where output file will be created, default is current directory",
},
{
name: "--config",
description: "path to a config file which returns a object",
},
];
function main() {
Expand All @@ -18,9 +27,8 @@ function main() {
const [id, title, date] = passedArgs[1].split(",");
generateTemplate({ title, date, id });
}
main();
function validateArguments(cliArgs) {
if (cliArgs.length > 2) {
if (cliArgs.length > 6) {
printHelp();
process.exit(1);
}
Expand All @@ -45,7 +53,7 @@ function generateTemplate({ title, id, date, }) {
const fileContent = data
.replace(/\{\{title\}\}/g, title)
.replace(/\{\{date\}\}/g, date);
const writeStream = fs_1.default.createWriteStream(`${process.cwd()}/${id}.md`, "utf8");
const writeStream = fs_1.default.createWriteStream(`${matter_gen_cli_config_1.default.outDir}/${id}.md`, "utf8");
writeStream.write(fileContent);
writeStream.on("finish", () => {
console.log(`${id}.md is generated successfully on directory: ${process.cwd()}`);
Expand All @@ -55,8 +63,12 @@ function generateTemplate({ title, id, date, }) {
}
function printHelp() {
// Print all the available arguments
console.log("List of available arguments: \n\n");
console.log("List of available arguments: \n");
console.log("-----------------------------\n");
args.forEach((ag) => {
console.log(`${ag.name} : ${ag.description}\n`);
console.log(`${ag.name} : ${ag.description}\n`);
});
console.log("-----------------------------\n");
}
// Run main program
main();
1 change: 1 addition & 0 deletions bin/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";
13 changes: 13 additions & 0 deletions bin/matter-gen-cli.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const process_1 = require("process");
const configArgIndex = process_1.argv.indexOf("--config");
const outDirIndex = process_1.argv.indexOf("--outDir");
let overrides = {};
if (configArgIndex > -1 && typeof process_1.argv[configArgIndex + 1] === "string") {
overrides = require(process_1.argv[configArgIndex + 1]);
}
if (outDirIndex > -1 && typeof process_1.argv[outDirIndex + 1] === "string") {
overrides.outDir = process_1.argv[outDirIndex + 1];
}
exports.default = Object.assign({ outDir: process.cwd() }, overrides);
5 changes: 0 additions & 5 deletions matter-gen-cli.config.js

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@amirsaeed671/matter-gen-cli",
"version": "0.0.2",
"main": "./src/index.ts",
"main": "/src/index.ts",
"description": "generate md file with meta data for gray-matter",
"scripts": {
"build": "npx tsc",
Expand All @@ -11,8 +11,8 @@
"access": "public"
},
"bin": {
"@amirsaeed671/matter-gen-cli": "./bin/index.js",
"matter-gen-cli": "./bin/index.js"
"@amirsaeed671/matter-gen-cli": "./bin/src/index.js",
"matter-gen-cli": "./bin/src/index.js"
},
"repository": "https://github.com/amirsaeed671/matter-gen-cli.git",
"author": "amirsaeed671 <amir.saeed.ali92@gmail.com>",
Expand All @@ -26,6 +26,7 @@
"matter-gen-cli"
],
"files": [
"matter-gen-cli.config.ts",
"bin/",
"src/",
"templates/"
Expand Down
26 changes: 19 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

import fs from "fs";
import path from "path";
import config from "./matter-gen-cli.config";

const args = [
{
name: "--meta",
description:
"pass values in comma separated string i.e --meta 'id,title,date'",
"*pass values in comma separated string i.e --meta 'id,title,date'",
},
{
name: "--outDir",
description: "provide the path where output file will be created, default is current directory",
},
{
name: "--config",
description: "path to a config file which returns a object",
},
];

Expand All @@ -21,10 +30,8 @@ function main() {
generateTemplate({ title, date, id });
}

main();

function validateArguments(cliArgs: string[]) {
if (cliArgs.length > 2) {
if (cliArgs.length > 6) {
printHelp();
process.exit(1);
}
Expand Down Expand Up @@ -65,7 +72,7 @@ function generateTemplate({
.replace(/\{\{title\}\}/g, title)
.replace(/\{\{date\}\}/g, date);
const writeStream = fs.createWriteStream(
`${process.cwd()}/${id}.md`,
`${config.outDir}/${id}.md`,
"utf8"
);

Expand All @@ -82,8 +89,13 @@ function generateTemplate({

function printHelp() {
// Print all the available arguments
console.log("List of available arguments: \n\n");
console.log("List of available arguments: \n");
console.log("-----------------------------\n");
args.forEach((ag) => {
console.log(`${ag.name} : ${ag.description}\n`);
console.log(`${ag.name} : ${ag.description}\n`);
});
console.log("-----------------------------\n");
}

// Run main program
main();
Empty file added src/init.ts
Empty file.
23 changes: 23 additions & 0 deletions src/matter-gen-cli.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { argv } from "process";

export interface cliConfig {
outDir?: string;
}

const configArgIndex = argv.indexOf("--config");
const outDirIndex = argv.indexOf("--outDir");

let overrides: cliConfig = {};

if (configArgIndex > -1 && typeof argv[configArgIndex + 1] === "string") {
overrides = require(argv[configArgIndex + 1]);
}

if (outDirIndex > -1 && typeof argv[outDirIndex + 1] === "string") {
overrides.outDir = argv[outDirIndex + 1];
}

export default {
outDir: process.cwd(),
...overrides,
} as cliConfig;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"baseUrl": "./",
"baseUrl": ".",
"paths": {
"include": ["./src/*.ts"]
},
"allowJs": false,
"outDir": "./bin",
"rootDir": "./src",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down

0 comments on commit d31e0ff

Please sign in to comment.