Add support for static pages
This commit is contained in:
21
scripts/build.js
Normal file
21
scripts/build.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import {
|
||||
createRadar,
|
||||
outputRadar,
|
||||
} from './radar';
|
||||
import {
|
||||
createStatic,
|
||||
} from './static';
|
||||
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const radar = await createRadar();
|
||||
outputRadar(radar);
|
||||
createStatic(radar);
|
||||
|
||||
console.log('Radar build!');
|
||||
console.log(JSON.stringify(radar, null, 2));
|
||||
} catch(e) {
|
||||
console.error('error:', e);
|
||||
}
|
||||
})()
|
||||
@@ -1,51 +0,0 @@
|
||||
import {
|
||||
readFile,
|
||||
outputFile,
|
||||
} from 'fs-extra';
|
||||
import frontmatter from 'front-matter';
|
||||
import marked from 'marked';
|
||||
import waterfall from 'async/waterfall';
|
||||
import {
|
||||
srcPath,
|
||||
distPath,
|
||||
} from './file';
|
||||
import {
|
||||
createRadar,
|
||||
outputRadar,
|
||||
} from './radar';
|
||||
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const radar = await createRadar();
|
||||
outputRadar(radar);
|
||||
// console.log(JSON.stringify(radar, null, 2));
|
||||
} catch(e) {
|
||||
console.error('error:', e);
|
||||
}
|
||||
})()
|
||||
|
||||
//
|
||||
// const fileName = srcPath('v1/tools/grunt.md');
|
||||
//
|
||||
// console.log('<<< start creating files');
|
||||
//
|
||||
// waterfall([
|
||||
// (callback) => {
|
||||
// readFile(fileName, 'utf8', callback);
|
||||
// },
|
||||
// (data, callback) => {
|
||||
// const item = frontmatter(data);
|
||||
// const html = marked(item.body);
|
||||
//
|
||||
// outputFile(distPath('test3.html'), html, callback);
|
||||
// }
|
||||
// ],
|
||||
// (err, results) => {
|
||||
// if (!err) {
|
||||
// console.log('done creating files >>>');
|
||||
// } else {
|
||||
// console.error(err);
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
@@ -1,13 +1,56 @@
|
||||
import path from 'path';
|
||||
import { walk } from 'walk';
|
||||
|
||||
export const relativePath = (...relativePath) => (
|
||||
path.resolve(__dirname, '..', ...relativePath)
|
||||
);
|
||||
|
||||
export const srcPath = (...pathInSrc) => (
|
||||
export const radarPath = (...pathInSrc) => (
|
||||
relativePath('radar', ...pathInSrc)
|
||||
);
|
||||
|
||||
export const staticPath = (...pathInSrc) => (
|
||||
relativePath('static-pages', ...pathInSrc)
|
||||
);
|
||||
|
||||
export const distPath = (...pathInDist) => (
|
||||
relativePath('dist', ...pathInDist)
|
||||
);
|
||||
|
||||
export const getAllMarkdownFiles = (folder) => (
|
||||
getAllFiles(folder, isMarkdownFile)
|
||||
);
|
||||
|
||||
export const getAllPugFiles = (folder) => (
|
||||
getAllFiles(folder, isPugFile)
|
||||
);
|
||||
|
||||
const getAllFiles = (folder, predicate) => (
|
||||
new Promise((resolve, reject) => {
|
||||
const walker = walk(folder, { followLinks: false });
|
||||
const files = [];
|
||||
|
||||
walker.on("file", (root, fileStat, next) => {
|
||||
if (predicate(fileStat.name)) {
|
||||
files.push(path.resolve(root, fileStat.name));
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
walker.on("errors", (root, nodeStatsArray, next) => {
|
||||
nodeStatsArray.forEach(function (n) {
|
||||
console.error("[ERROR] " + n.name)
|
||||
console.error(n.error.message || (n.error.code + ": " + n.error.path));
|
||||
});
|
||||
next();
|
||||
});
|
||||
|
||||
walker.on("end", () => {
|
||||
resolve(files.sort());
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const isMarkdownFile = (name) => name.match(/\.md$/);
|
||||
|
||||
const isPugFile = (name) => name.match(/\.pug$/);
|
||||
|
||||
@@ -1,46 +1,21 @@
|
||||
import { walk } from 'walk';
|
||||
import fs, { readFile, outputFile } from 'fs-extra';
|
||||
import path from 'path';
|
||||
import frontmatter from 'front-matter';
|
||||
import marked from 'marked';
|
||||
import { srcPath, distPath } from './file';
|
||||
import {
|
||||
radarPath,
|
||||
distPath,
|
||||
getAllMarkdownFiles,
|
||||
} from './file';
|
||||
import { item as itemTemplate } from './template';
|
||||
|
||||
export const createRadar = async (tree) => {
|
||||
const fileNames = await getAllMarkdownFiles();
|
||||
const fileNames = (await getAllMarkdownFiles(radarPath())).reverse();
|
||||
const revisions = await createRevisionsFromFiles(fileNames);
|
||||
const quadrants = createQuadrants(revisions);
|
||||
return quadrants;
|
||||
};
|
||||
|
||||
const getAllMarkdownFiles = () => (
|
||||
new Promise((resolve, reject) => {
|
||||
const walker = walk(srcPath(), { followLinks: false });
|
||||
const files = [];
|
||||
|
||||
walker.on("file", (root, fileStat, next) => {
|
||||
if (isMarkdownFile(fileStat.name)) {
|
||||
files.push(path.resolve(root, fileStat.name));
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
walker.on("errors", (root, nodeStatsArray, next) => {
|
||||
nodeStatsArray.forEach(function (n) {
|
||||
console.error("[ERROR] " + n.name)
|
||||
console.error(n.error.message || (n.error.code + ": " + n.error.path));
|
||||
});
|
||||
next();
|
||||
});
|
||||
|
||||
walker.on("end", () => {
|
||||
resolve(files.sort().reverse());
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const isMarkdownFile = (name) => name.match(/\.md$/);
|
||||
|
||||
const createRevisionsFromFiles = (fileNames) => (
|
||||
Promise.all(fileNames.map((fileName) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -99,7 +74,6 @@ const addRevisionToItem = (item = {
|
||||
...rest,
|
||||
} = revision;
|
||||
return {
|
||||
quadrant,
|
||||
attributes: {
|
||||
...item.attributes,
|
||||
...revision.attributes,
|
||||
@@ -114,8 +88,10 @@ export const outputRadar = (radar) => {
|
||||
Object.entries(radar).map(([quadrantName, quadrant]) => (
|
||||
Object.entries(quadrant).map(([itemName, item]) => (
|
||||
new Promise((resolve, reject) => {
|
||||
console.log(JSON.stringify(item, null, 2));
|
||||
outputFile(distPath(quadrantName, `${itemName}.html`), itemTemplate(item), (err, data) => {
|
||||
outputFile(distPath(quadrantName, `${itemName}.html`), itemTemplate({
|
||||
quadrantName,
|
||||
item,
|
||||
}), (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
|
||||
39
scripts/static.js
Normal file
39
scripts/static.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { outputFile } from 'fs-extra';
|
||||
import pug from 'pug';
|
||||
import frontmatter from 'front-matter';
|
||||
import marked from 'marked';
|
||||
import {
|
||||
staticPath,
|
||||
distPath,
|
||||
getAllPugFiles,
|
||||
} from './file';
|
||||
|
||||
export const createStatic = async (radar) => {
|
||||
const paths = await getAllPugFiles(staticPath());
|
||||
const fileNames = getPlainFileNames(paths);
|
||||
return renderStaticPages(radar, fileNames);
|
||||
return fileNames;
|
||||
};
|
||||
|
||||
const getPlainFileNames = (paths) => (
|
||||
paths.map((fileName) => {
|
||||
const [ nameWithSuffix ] = fileName.split('/').slice(-1);
|
||||
return nameWithSuffix.substr(0, nameWithSuffix.length - 4);
|
||||
})
|
||||
)
|
||||
|
||||
const renderStaticPages = (radar, fileNames) => (
|
||||
Promise.all(fileNames.map((name) => (
|
||||
new Promise((resolve, reject) => (
|
||||
outputFile(distPath(`${name}.html`), pug.renderFile(staticPath(`${name}.pug`), {
|
||||
radar,
|
||||
}), (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
))
|
||||
)))
|
||||
);
|
||||
Reference in New Issue
Block a user