Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
feat(CLI) Add new CLI
Browse files Browse the repository at this point in the history
Adding a new CLI functionality to basically use and inject back2roots as an
NPM dep into any other project and use the binary CLI to generate the static
files.
  • Loading branch information
andi1984 committed May 18, 2020
1 parent d6b0f69 commit 5fc097d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 68 deletions.
110 changes: 56 additions & 54 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,67 @@ const { getSiteObject } = require("./utils/md");
const { sitesSorting } = require("./config/hooks");
var RSS = require("rss");

// Step 1: Clear output directory
if (args.clean) {
syncRmRf(DISTRIBUTION_FOLDER);
}
module.exports = () => {
// Step 1: Clear output directory
if (args.clean) {
syncRmRf(DISTRIBUTION_FOLDER);
}

// Step 2: Create files
(async () => {
let sites: Site[] = [];
// Step 2: Create files
(async () => {
let sites: Site[] = [];

/* lets create an rss feed */
var feed = new RSS({
title: process.env.URL,
description: `${process.env.URL} - RSS`,
feed_url: `${process.env.URL}/rss.xml`,
site_url: process.env.URL,
image_url: `${process.env.URL}/icon.png`,
// managingEditor: "Dylan Greene",
// webMaster: "Dylan Greene",
// copyright: "2013 Dylan Greene",
language: "en",
// categories: ["Category 1", "Category 2", "Category 3"],
pubDate: new Date().toString(),
ttl: "60",
});
/* lets create an rss feed */
var feed = new RSS({
title: process.env.URL,
description: `${process.env.URL} - RSS`,
feed_url: `${process.env.URL}/rss.xml`,
site_url: process.env.URL,
image_url: `${process.env.URL}/icon.png`,
// managingEditor: "Dylan Greene",
// webMaster: "Dylan Greene",
// copyright: "2013 Dylan Greene",
language: "en",
// categories: ["Category 1", "Category 2", "Category 3"],
pubDate: new Date().toString(),
ttl: "60",
});

// cache the xml to send to clients
for await (const f of filter(
getFiles(MARKDOWN_FOLDER),
(filePath: string) => {
const matchesMDFiletype = filePath.match(/^.*\.md$/g);
return matchesMDFiletype && matchesMDFiletype.length > 0;
// cache the xml to send to clients
for await (const f of filter(
getFiles(MARKDOWN_FOLDER),
(filePath: string) => {
const matchesMDFiletype = filePath.match(/^.*\.md$/g);
return matchesMDFiletype && matchesMDFiletype.length > 0;
}
)) {
sites.push(getSiteObject(f));
}
)) {
sites.push(getSiteObject(f));
}

// Apply "sites" hooks
sites.sort(sitesSorting);
// Apply "sites" hooks
sites.sort(sitesSorting);

for (const site of sites) {
renderMarkdownFile(site, sites);
/* loop over data and add to feed */
feed.item({
title: site.title,
description: site.html,
url: site.url.href, // link to the item
// author: "Guest Authors", // optional - defaults to feed author property
// date: "May 27, 2012" // any format that js Date can parse.
});
}
for (const site of sites) {
renderMarkdownFile(site, sites);
/* loop over data and add to feed */
feed.item({
title: site.title,
description: site.html,
url: site.url.href, // link to the item
// author: "Guest Authors", // optional - defaults to feed author property
// date: "May 27, 2012" // any format that js Date can parse.
});
}

var xml = feed.xml();
fs.writeFile(
path.join(DISTRIBUTION_FOLDER, "rss.xml"),
xml,
(writeErr: Error) => {
if (writeErr) {
throw writeErr;
var xml = feed.xml();
fs.writeFile(
path.join(DISTRIBUTION_FOLDER, "rss.xml"),
xml,
(writeErr: Error) => {
if (writeErr) {
throw writeErr;
}
}
}
);
})();
);
})();
};
5 changes: 5 additions & 0 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

const [, , ...args] = process.argv;
const build = require('./build');
build();
26 changes: 12 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@
"name": "back2roots",
"version": "0.2.0",
"description": "",
"main": "server.js",
"main": "build/build.js",
"files": [
"build/**/*.js"
],
"engines": {
"node": "12.x"
},
"watch": {
"start": {
"patterns": [
"templates",
"md"
],
"extensions": "twig,md"
}
"keywords": [
"cli"
],
"bin": {
"back2roots": "build/cli.js"
},
"scripts": {
"test": "jest",
"tsc": "tsc",
"dev": "npx concurrently \"npx http-server dist\" \"npm run watch-content\" \"npm run watch\"",
"watch-content": "npm-watch",
"watch": "ts-node-dev --respawn --transpileOnly ./build.ts",
"build": "tsc && node ./build/build.js",
"assets": "echo \"Run your asset generation script here putting assets into the dist folder\"",
"start": "npm run build && npm run assets",
"build": "tsc",
"start": "./build/cli.js",
"prepare": "npm run build",
"release": "release-it"
},
"author": "Andreas Sander <mail@andi1984.de>",
Expand Down

0 comments on commit 5fc097d

Please sign in to comment.