Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit aa20b07

Browse files
committed
about to delete old dev
1 parent 6b08c6c commit aa20b07

File tree

6 files changed

+190
-147
lines changed

6 files changed

+190
-147
lines changed

drafts/sample_draft_post.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ In this tutorial we will learn how to build an Instagram app, where users can vi
99

1010
For this tutorial you'll need a GraphQL project with the following schema:
1111

12-
```graphql
12+
```json
1313
type Post {
14-
id: ID!
15-
description: String!
14+
id: ID!,
15+
description: String!,
1616
imageUrl: String!
1717
}
1818
```

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dev": "npm-run-all --parallel clean:dev build:dev webpack:dev",
1010
"upload": "node scripts/upload.js",
1111
"push": "node scripts/push.js",
12-
"build:dev": "node scripts/dev.js",
12+
"build:dev": "node scripts/deu.js",
1313
"build:dist": "node scripts/generate.js",
1414
"webpack:dev": "webpack --env=dev --progress --colors --watch",
1515
"webpack:dist": "webpack --env=prod --progress --colors",
@@ -27,6 +27,7 @@
2727
},
2828
"homepage": "https://github.com/geekodour/gitpushblog#readme",
2929
"dependencies": {
30+
"async": "2.5.0",
3031
"bulma": "0.4.1",
3132
"chokidar": "1.7.0",
3233
"cp": "0.2.0",

scripts/deu.js

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
'use strict';
2+
3+
process.env.NODE_ENV = 'development';
4+
5+
const chokidar = require('chokidar');
6+
const ora = require('ora');
7+
const server = require('pushstate-server');
8+
const chalk = require('chalk');
9+
const mkdirp = require('mkdirp');
10+
const fs = require('fs');
11+
const path = require('path');
12+
const slugify = require('slugify');
13+
14+
// import configuration files
15+
const init = require('./init');
16+
const bc = require('../blog_config.json');
17+
const utils = require('./utils.js');
18+
19+
// init nunjucks and blog and env variables
20+
const {blog} = init.init();
21+
22+
// initilize some constants
23+
const ROOT_DIR = process.env.ROOT_DIR;
24+
const THEME_DIR = path.join(ROOT_DIR,'themes',bc.meta.blog_theme);
25+
const PORT = 3000;
26+
let posts = [];
27+
let labels = [];
28+
let listOfFiles = [];
29+
const spinner = ora({text:'Fetching posts',spinner:'line'});
30+
31+
32+
// template generation
33+
function generateTemplates(){
34+
let flatPosts = posts.reduce((posts_prev,posts_next)=>posts_prev.concat(posts_next));
35+
let pagination = {next:null,prev:null};
36+
37+
// index pages and pagination
38+
posts.forEach((post_arr,cur_page)=>{
39+
pagination = Object.assign(pagination,
40+
{
41+
next:(posts.length === cur_page+1)?0:cur_page+2,
42+
prev:cur_page>0
43+
?cur_page==1?'index':cur_page
44+
:0
45+
}
46+
);
47+
48+
if(cur_page==0){
49+
utils.generateIndexTemplate(post_arr,labels,pagination,'dev','index.html');
50+
} else {
51+
utils.generateIndexTemplate(post_arr,labels,pagination,'dev',cur_page+1+'.html');
52+
}
53+
});
54+
55+
// post pages
56+
flatPosts.forEach(post=>{
57+
utils.generatePostTemplate(post,labels,'dev');
58+
});
59+
60+
// other pages
61+
utils.generatePageTemplate('dev');
62+
63+
// category pages
64+
utils.generateCategoryTemplates2(labels,'dev')
65+
66+
console.log(chalk.bold.green(`Templates generated. \nAvailable on:\n http://localhost:${PORT}`));
67+
}
68+
69+
function startDevMode(){
70+
71+
mkdirp.sync(path.join(ROOT_DIR,'dev','category'));
72+
mkdirp.sync(path.join(ROOT_DIR,'dev','posts'));
73+
generateTemplates();
74+
75+
// watch for changes in the theme directory and regenerate on change
76+
chokidar.watch(THEME_DIR, {ignored: /(^|[\/\\])\../}).on('all', (event, path) => {
77+
if(event === "change"){
78+
generateTemplates();
79+
}
80+
});
81+
}
82+
83+
function getAllPosts(){
84+
let posts = [];
85+
return new Promise((resolve,reject)=>{
86+
(function callFetchBlogPosts(){
87+
blog.fetchBlogPosts()
88+
.then(_posts=>{
89+
if(blog.settings.posts.last_reached){
90+
resolve(posts);
91+
}
92+
else{
93+
callFetchBlogPosts();
94+
}
95+
posts.push(_posts);
96+
})
97+
.catch(function(err){
98+
console.log(chalk.bold.red('Could not fetch github data for some reason\nUsing offline data.'));
99+
resolve(posts);
100+
});
101+
})()
102+
});
103+
}
104+
105+
function getAllLabels(){
106+
return new Promise((resolve,reject)=>{
107+
blog.fetchAllLabels()
108+
.then(_labels=>{
109+
resolve(_labels);
110+
})
111+
.catch(err=>{
112+
console.log(chalk.bold.red('Could not fetch github label data due to network issue'));
113+
resolve(labels);
114+
});
115+
});
116+
}
117+
118+
119+
function fetchAndStoreData(){
120+
Promise.all([getAllPosts(), getAllLabels(),utils.getOfflineFileContents()])
121+
.then(vals=>{
122+
spinner.stop();
123+
posts = vals[0];
124+
labels = vals[1];
125+
listOfFiles = vals[2];
126+
posts[0].unshift(...listOfFiles)
127+
startDevMode();
128+
})
129+
.catch(err=>{
130+
spinner.stop();
131+
utils.getOfflineFileContents()
132+
.then(files=>{posts.push(files)})
133+
startDevMode();
134+
})
135+
}
136+
137+
spinner.start();
138+
fetchAndStoreData();
139+
140+
// start the dev server silently
141+
server.start({
142+
port: PORT,
143+
directory: path.join(ROOT_DIR,'dev')
144+
});

scripts/generate.js

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ process.env.NODE_ENV = 'production';
55
const ora = require('ora');
66
const chalk = require('chalk');
77
const path = require('path');
8-
//const gitblog = require('github-blog-api');
98
const mkdirp = require('mkdirp');
109

1110
// import configuration files
@@ -22,8 +21,6 @@ const ROOT_DIR = process.env.ROOT_DIR;
2221
const spinner = ora({text:'Fetching posts',spinner:'line'});
2322
let posts = [];
2423
let labels = [];
25-
// const pagination = {next:0,prev:0};
26-
2724

2825
// template generation
2926
function fetchAndStoreData(){

scripts/utils.js

+41-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const mkdirp = require('mkdirp');
44
const marked = require('marked');
55
const fs = require('fs');
6+
const map = require('async/map');
67
const slugify = require('slugify');
78
const path = require('path');
89
const _nunjucks = require('./nunjucks_config.js');
@@ -17,15 +18,11 @@ const {nunjucks} = init.init();
1718
const ROOT_DIR = process.env.ROOT_DIR;
1819
const THEME_DIR = path.join(ROOT_DIR,'themes',bc.meta.blog_theme);
1920
const DIR_NAME = process.env.NODE_ENV === 'production'?'dist':'dev'; // output directory
20-
//const nunjucks = _nunjucks.init();
2121

2222
module.exports = {
2323

2424
generatePostTemplate: function(post,labels,dirName=DIR_NAME){
2525
let fileName = post.slug+'.html';
26-
// marked is required for offline support, the `body.html` already exists
27-
// when using the api. Maybe put a check for that?
28-
post.html = marked(post.body);
2926
let renderContent = nunjucks.render('post_page.html',
3027
{
3128
meta: bc.meta,
@@ -35,8 +32,8 @@ module.exports = {
3532
labels: labels
3633
}
3734
);
38-
fs.writeFile(path.join(ROOT_DIR,dirName,'posts',fileName), renderContent, function(err) {
39-
if(err) { return console.log(err); }
35+
fs.writeFile(path.join(ROOT_DIR,dirName,'posts',fileName), renderContent, (err) => {
36+
if(err) { console.log("disk error"); }
4037
});
4138
},
4239

@@ -52,11 +49,10 @@ module.exports = {
5249
}
5350
);
5451
// should we make this writeFile sync? or make all writeFile async?
55-
fs.writeFile(path.join(ROOT_DIR,dirName,fileName), renderContent, function(err) {
52+
fs.writeFile(path.join(ROOT_DIR,dirName,fileName), renderContent, (err) => {
5653
if(err) { return console.log(err); }
5754
});
5855
},
59-
6056
generateCategoryTemplates: function(labels,dirName=DIR_NAME){
6157
// takes array of labels
6258
// creates files with name label.slug.html
@@ -70,12 +66,31 @@ module.exports = {
7066
labels:labels
7167
}
7268
);
73-
fs.writeFileSync(path.join(ROOT_DIR,dirName,'category',label.slug+'.html'),renderContent);
74-
resolve();
69+
//fs.writeFileSync(path.join(ROOT_DIR,dirName,'category',label.slug+'.html'),renderContent);
70+
fs.writeFile(path.join(ROOT_DIR,dirName,'category',label.slug+'.html'),renderContent, (err) => {
71+
if(err) { return console.log(err); }
72+
resolve();
73+
});
7574
});
7675
});
7776
},
77+
generateCategoryTemplates2: function(labels,dirName=DIR_NAME){
78+
//const categoryNames = labels.map(label=>`${label.slug}.html`);
7879

80+
labels.forEach((label)=>{
81+
const renderContent = nunjucks.render('category_page.html',
82+
{
83+
meta: bc.meta,
84+
bc: bc,
85+
label:label,
86+
labels:labels
87+
}
88+
);
89+
fs.writeFile(path.join(ROOT_DIR,dirName,'category',`${label.slug}.html`),renderContent, (err) => {
90+
if(err) { return console.log(err); }
91+
});
92+
});
93+
},
7994
generatePageTemplate: function(dirName=DIR_NAME){
8095
var pageTemplatesFiles = fs.readdirSync(path.join(THEME_DIR,'pages'));
8196
pageTemplatesFiles.forEach(function(fileName){
@@ -90,17 +105,22 @@ module.exports = {
90105
},
91106

92107
getOfflineFileContents: function(){
93-
let fileNames = fs.readdirSync(path.join(ROOT_DIR,'drafts'));
94-
return fileNames.map(fileName=>{
95-
return new Promise((resolve)=>{
96-
let content = fs.readFileSync(path.join(ROOT_DIR,'drafts',fileName),{encoding:"utf8"});
97-
let post = {};
98-
// this part really need a good fix
99-
post.title = content.split("\n")[0].split(" ").slice(1).join(' ').trim();
100-
post.body = content.split("\n").slice(1).join("\n");
101-
post.slug = slugify(post.title);
102-
resolve(post);
103-
});
108+
const genPost = (fileName,cb) =>{
109+
let content = fs.readFileSync(path.join(ROOT_DIR,'drafts',fileName),{encoding:"utf8"});
110+
let post = {};
111+
// this part really need a good fix
112+
post.title = content.split("\n")[0].split(" ").slice(1).join(' ').trim();
113+
post.body = content.split("\n").slice(1).join("\n");
114+
post.slug = slugify(post.title);
115+
post.html = marked(post.body);
116+
cb(null,post);
117+
};
118+
119+
return new Promise((resolve,reject)=>{
120+
const fileNames = fs.readdirSync(path.join(ROOT_DIR,'drafts'));
121+
map(fileNames, genPost, function(err, posts) {
122+
resolve(posts);
123+
});
104124
});
105125
}
106126
};

0 commit comments

Comments
 (0)