Skip to content

Commit

Permalink
support for other platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
amirsaeed671 committed Dec 31, 2022
1 parent 4ed2894 commit 91364ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
24 changes: 16 additions & 8 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const args = [
Expand Down Expand Up @@ -37,13 +36,22 @@ function validateArguments(cliArgs) {
}
}
function generateTemplate({ title, id, date, }) {
const fileContent = fs_1.default
.readFileSync(path_1.default.join(__dirname, "../", "templates", "blog-post.md"), "utf8")
.replace(/\{\{title\}\}/g, title)
.replace(/\{\{date\}\}/g, date);
(0, child_process_1.exec)(`echo '${fileContent}' > ${process.cwd()}/${id}.md`);
console.log(`${id}.md is generated successfully on directory: ${process.cwd()}`);
process.exit(0);
const readStream = fs_1.default.createReadStream(path_1.default.join(__dirname, "../", "templates", "blog-post.md"));
let data = "";
readStream.on("data", (chunk) => {
data += chunk;
});
readStream.on("end", () => {
const fileContent = data
.replace(/\{\{title\}\}/g, title)
.replace(/\{\{date\}\}/g, date);
const writeStream = fs_1.default.createWriteStream(`${process.cwd()}/${id}.md`, "utf8");
writeStream.write(fileContent);
writeStream.on("finish", () => {
console.log(`${id}.md is generated successfully on directory: ${process.cwd()}`);
process.exit(0);
});
});
}
function printHelp() {
// Print all the available arguments
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@amirsaeed671/matter-gen-cli",
"version": "0.0.1",
"version": "0.0.2",
"main": "./src/index.ts",
"description": "generate md file with meta data for gray-matter",
"scripts": {
Expand Down
36 changes: 24 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node

import { exec } from "child_process";
import fs from "fs";
import path from "path";

Expand Down Expand Up @@ -52,20 +51,33 @@ function generateTemplate({
date: string;
id: string;
}): void {
const fileContent = fs
.readFileSync(
path.join(__dirname, "../", "templates", "blog-post.md"),
const readStream = fs.createReadStream(
path.join(__dirname, "../", "templates", "blog-post.md")
);
let data = "";

readStream.on("data", (chunk) => {
data += chunk;
});

readStream.on("end", () => {
const fileContent = data
.replace(/\{\{title\}\}/g, title)
.replace(/\{\{date\}\}/g, date);
const writeStream = fs.createWriteStream(
`${process.cwd()}/${id}.md`,
"utf8"
)
.replace(/\{\{title\}\}/g, title)
.replace(/\{\{date\}\}/g, date);
);

exec(`echo '${fileContent}' > ${process.cwd()}/${id}.md`);
writeStream.write(fileContent);

console.log(
`${id}.md is generated successfully on directory: ${process.cwd()}`
);
process.exit(0);
writeStream.on("finish", () => {
console.log(
`${id}.md is generated successfully on directory: ${process.cwd()}`
);
process.exit(0);
});
});
}

function printHelp() {
Expand Down

0 comments on commit 91364ec

Please sign in to comment.