Cleanup and file reorganization
This commit is contained in:
@@ -2,7 +2,7 @@ import { copy } from 'fs-extra';
|
||||
import {
|
||||
assetsPath,
|
||||
distPath,
|
||||
} from './file';
|
||||
} from '../common/file';
|
||||
|
||||
copy(assetsPath(), distPath('assets'), (err) => {
|
||||
if (err) {
|
||||
|
||||
@@ -2,16 +2,26 @@ import {
|
||||
createRadar,
|
||||
groupByQuadrants,
|
||||
outputRadar,
|
||||
} from './radar';
|
||||
import {
|
||||
renderApp,
|
||||
} from './static';
|
||||
} from '../common/radar';
|
||||
import { save } from '../common/file';
|
||||
import { getPageNames } from '../common/config';
|
||||
import { renderPage } from '../js/server';
|
||||
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const radar = await createRadar();
|
||||
renderApp(radar);
|
||||
|
||||
getPageNames(radar).map((pageName) => {
|
||||
const pageHtml = renderPage(radar, pageName);
|
||||
console.log(pageHtml);
|
||||
save(pageHtml, `${pageName}.html`);
|
||||
});
|
||||
|
||||
// pages.map((pageHtml) => {
|
||||
// save(pageHtml, `${pageName}.html`)
|
||||
// });
|
||||
// console.log(pages);
|
||||
// outputRadar(radar);
|
||||
|
||||
// const radarByQuadrants = groupByQuadrants(radar);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { emptyDir } from 'fs-extra';
|
||||
import { distPath } from './file';
|
||||
import { distPath } from '../common/file';
|
||||
|
||||
var distDir = distPath();
|
||||
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
import path from 'path';
|
||||
import { walk } from 'walk';
|
||||
|
||||
export const relativePath = (...relativePath) => (
|
||||
path.resolve(__dirname, '..', ...relativePath)
|
||||
);
|
||||
|
||||
export const radarPath = (...pathInSrc) => (
|
||||
relativePath('radar', ...pathInSrc)
|
||||
);
|
||||
|
||||
export const staticPath = (...pathInSrc) => (
|
||||
relativePath('static-pages', ...pathInSrc)
|
||||
);
|
||||
|
||||
export const templatesPath = (...pathInSrc) => (
|
||||
relativePath('templates', ...pathInSrc)
|
||||
);
|
||||
|
||||
export const stylesPath = (...pathInSrc) => (
|
||||
relativePath('styles', ...pathInSrc)
|
||||
);
|
||||
|
||||
export const assetsPath = (...pathInSrc) => (
|
||||
relativePath('assets', ...pathInSrc)
|
||||
);
|
||||
|
||||
export const jsPath = (...pathInSrc) => (
|
||||
relativePath('js', ...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$/);
|
||||
243
tasks/radar.js
243
tasks/radar.js
@@ -1,243 +0,0 @@
|
||||
import fs, { readFile, outputFile } from 'fs-extra';
|
||||
import path from 'path';
|
||||
import frontmatter from 'front-matter';
|
||||
import marked from 'marked';
|
||||
import {
|
||||
radarPath,
|
||||
distPath,
|
||||
getAllMarkdownFiles,
|
||||
} from './file';
|
||||
import {
|
||||
item as itemTemplate,
|
||||
quadrant as quadrantTemplate,
|
||||
vars,
|
||||
} from './template';
|
||||
|
||||
export const createRadar = async (tree) => {
|
||||
const fileNames = (await getAllMarkdownFiles(radarPath()));
|
||||
const revisions = await createRevisionsFromFiles(fileNames);
|
||||
const allReleases = getAllReleases(revisions);
|
||||
const items = createItems(revisions);
|
||||
const flaggedItems = flagWithIsNew(items, allReleases);
|
||||
|
||||
return {
|
||||
items: flaggedItems,
|
||||
releases: allReleases,
|
||||
};
|
||||
};
|
||||
|
||||
export const groupByQuadrants = (items) => (
|
||||
items.reduce((quadrants, item) => ({
|
||||
...quadrants,
|
||||
[item.quadrant]: addItemToQuadrant(quadrants[item.quadrant], item),
|
||||
}), {})
|
||||
);
|
||||
|
||||
const addItemToQuadrant = (quadrant = {}, item) => ({
|
||||
...quadrant,
|
||||
[item.ring]: addItemToRing(quadrant[item.ring], item),
|
||||
});
|
||||
|
||||
export const groupByFirstLetter = (items) => (
|
||||
items.reduce((letterIndex, item) => ({
|
||||
...letterIndex,
|
||||
[getFirstLetter(item)]: addItemToList(letterIndex[getFirstLetter(item)], item),
|
||||
}), {})
|
||||
);
|
||||
|
||||
export const groupByRing = (items) => (
|
||||
items.reduce((rings, item) => ({
|
||||
...rings,
|
||||
[item.ring]: addItemToList(rings[item.ring], item),
|
||||
}), {})
|
||||
);
|
||||
|
||||
const addItemToList = (list = [], item) => ([
|
||||
...list,
|
||||
item,
|
||||
]);
|
||||
|
||||
export const getFirstLetter = (item) => item.title.substr(0,1).toUpperCase();
|
||||
|
||||
|
||||
const checkAttributes = (fileName, attributes) => {
|
||||
const rings = ['trial', 'assess', 'adopt', 'hold'];
|
||||
if (attributes.ring && !rings.includes(attributes.ring)) {
|
||||
throw new Error(`Error: ${fileName} has an illegal value for 'ring' - must be one of ${rings}`);
|
||||
}
|
||||
|
||||
const quadrants = ['languages-and-frameworks', 'methods-and-patterns', 'platforms-and-aoe-services', 'tools'];
|
||||
if (attributes.quadrant && !quadrants.includes(attributes.quadrant)) {
|
||||
throw new Error(`Error: ${fileName} has an illegal value for 'quadrant' - must be one of ${quadrants}`);
|
||||
}
|
||||
}
|
||||
|
||||
const createRevisionsFromFiles = (fileNames) => (
|
||||
Promise.all(fileNames.map((fileName) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
readFile(fileName, 'utf8', (err, data) => {
|
||||
if(err) {
|
||||
reject(err);
|
||||
} else {
|
||||
const fm = frontmatter(data);
|
||||
checkAttributes(fileName, fm.attributes);
|
||||
resolve({
|
||||
...itemInfoFromFilename(fileName),
|
||||
...fm.attributes,
|
||||
fileName,
|
||||
body: marked(fm.body),
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
}))
|
||||
)
|
||||
|
||||
const itemInfoFromFilename = (fileName) => {
|
||||
const [
|
||||
release,
|
||||
nameWithSuffix,
|
||||
] = fileName.split('/').slice(-2);
|
||||
return {
|
||||
name: nameWithSuffix.substr(0, nameWithSuffix.length - 3),
|
||||
release,
|
||||
}
|
||||
};
|
||||
|
||||
const getAllReleases = (revisions) => (
|
||||
revisions.reduce((allReleases, { release }) => {
|
||||
if(!allReleases.includes(release)) {
|
||||
return [...allReleases, release];
|
||||
}
|
||||
return allReleases;
|
||||
}, []).sort()
|
||||
)
|
||||
|
||||
// const createQuadrants = (revisions) => (
|
||||
// revisions.reduce((quadrants, revision) => {
|
||||
// return {
|
||||
// ...quadrants,
|
||||
// [revision.quadrant]: addRevisionToQuadrant(quadrants[revision.quadrant], revision),
|
||||
// };
|
||||
// }, {})
|
||||
// );
|
||||
|
||||
// const addRevisionToQuadrant = (quadrant = {}, revision) => ({
|
||||
// ...quadrant,
|
||||
// [revision.name]: addRevisionToItem(quadrant[revision.name], revision),
|
||||
// });
|
||||
|
||||
const addRevisionToQuadrant = (quadrant = {}, revision) => ({
|
||||
...quadrant,
|
||||
[revision.ring]: addRevisionToRing(quadrant[revision.ring], revision),
|
||||
});
|
||||
|
||||
const createItems = (revisions) => {
|
||||
const itemMap = revisions.reduce((items, revision) => {
|
||||
return {
|
||||
...items,
|
||||
[revision.name]: addRevisionToItem(items[revision.name], revision),
|
||||
};
|
||||
}, {});
|
||||
|
||||
return Object
|
||||
.values(itemMap)
|
||||
.sort((x, y) => (x.name > y.name ? 1 : -1));
|
||||
}
|
||||
|
||||
const addRevisionToItem = (item = {
|
||||
attributes: {
|
||||
isFeatured: true,
|
||||
},
|
||||
revisions: [],
|
||||
}, revision) => {
|
||||
const {
|
||||
fileName,
|
||||
...rest,
|
||||
} = revision;
|
||||
let newItem = {
|
||||
...item,
|
||||
...rest,
|
||||
attributes: {
|
||||
...item.attributes,
|
||||
...revision.attributes,
|
||||
},
|
||||
};
|
||||
|
||||
if (revisionCreatesNewHistoryEntry(revision)) {
|
||||
newItem = {
|
||||
...newItem,
|
||||
revisions: [
|
||||
rest,
|
||||
...newItem.revisions,
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
return newItem;
|
||||
};
|
||||
|
||||
const revisionCreatesNewHistoryEntry = (revision) => {
|
||||
return revision.body.trim() !== '' ||
|
||||
typeof revision.ring !== 'undefined';
|
||||
};
|
||||
|
||||
export const outputRadar = ({ items }) => {
|
||||
const quadrants = groupByQuadrants(items);
|
||||
|
||||
Object.entries(quadrants).map(([quadrantName, quadrant]) => (
|
||||
outputQuadrantPage(quadrantName, quadrant)
|
||||
));
|
||||
|
||||
return Promise.all(
|
||||
items.map(async (item) => {
|
||||
|
||||
// Object.entries(quadrant).map(([itemName, item]) => (
|
||||
new Promise((resolve, reject) => {
|
||||
outputFile(distPath(item.quadrant, `${item.name}.html`), itemTemplate(vars({
|
||||
itemsInRing: quadrants[item.quadrant][item.ring],
|
||||
item,
|
||||
})), (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
})
|
||||
// ))
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
const outputQuadrantPage = (quadrantName, quadrant) => (
|
||||
new Promise((resolve, reject) => {
|
||||
outputFile(distPath(`${quadrantName}.html`), quadrantTemplate(vars({
|
||||
quadrantName,
|
||||
quadrant,
|
||||
})), (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
const flagWithIsNew = (items, allReleases) => (
|
||||
items.map((item) => ({
|
||||
...item,
|
||||
isNew: isNewItem(item, allReleases),
|
||||
}), [])
|
||||
);
|
||||
|
||||
const isNewItem = (item, allReleases) => {
|
||||
return item.revisions.length > 1 && item.revisions[0].release === allReleases[allReleases.length-1]
|
||||
}
|
||||
|
||||
const addItemToRing = (ring = [], item) => ([
|
||||
...ring,
|
||||
item,
|
||||
]);
|
||||
@@ -1,92 +0,0 @@
|
||||
import { outputFile } from 'fs-extra';
|
||||
import React from 'react';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import { createStore } from 'redux';
|
||||
import { Provider } from 'react-redux';
|
||||
|
||||
import { distPath } from './file';
|
||||
import App from '../components/App';
|
||||
|
||||
const appReducer = (state = {}, action) => {
|
||||
return state;
|
||||
}
|
||||
|
||||
const getPageNames = (radar) => {
|
||||
return [
|
||||
'index',
|
||||
'overview',
|
||||
'help',
|
||||
'foo/bar',
|
||||
]
|
||||
}
|
||||
|
||||
export const renderApp = (radar) => {
|
||||
const pageNames = getPageNames(radar);
|
||||
pageNames.map(pageName => renderPage(radar, pageName))
|
||||
}
|
||||
|
||||
export const renderPage = (radar, pageName) => {
|
||||
// Create a new Redux store instance
|
||||
const store = createStore(appReducer, {
|
||||
...radar,
|
||||
pageName
|
||||
});
|
||||
|
||||
// Render the component to a string
|
||||
const html = renderToString(
|
||||
<Provider store={store}>
|
||||
<App />
|
||||
</Provider>
|
||||
)
|
||||
|
||||
// Grab the initial state from our Redux store
|
||||
const preloadedState = store.getState()
|
||||
|
||||
// Send the rendered page back to the client
|
||||
const fullHtml = renderFullPage(html, preloadedState);
|
||||
|
||||
// Save file
|
||||
save(fullHtml, pageName);
|
||||
}
|
||||
|
||||
const renderFullPage = (html, preloadedState) => {
|
||||
return `
|
||||
<html>
|
||||
<head>
|
||||
<title>AOE Technology Radar - AOE Tech Radar</title>
|
||||
<link rel="stylesheet" href="/styles.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root">${html}</div>
|
||||
<script>
|
||||
window.__PRELOADED_STATE__ = ${JSON.stringify(preloadedState)}
|
||||
</script>
|
||||
<script src="/bundle.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Redux Universal Example</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script src="/static/bundle.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
}
|
||||
|
||||
const save = (html, pageName) => (
|
||||
new Promise((resolve, reject) => (
|
||||
outputFile(distPath(`${pageName}.html`), html, (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
))
|
||||
);
|
||||
@@ -1,42 +0,0 @@
|
||||
import { outputFile } from 'fs-extra';
|
||||
import pug from 'pug';
|
||||
import frontmatter from 'front-matter';
|
||||
import marked from 'marked';
|
||||
import {
|
||||
staticPath,
|
||||
distPath,
|
||||
getAllPugFiles,
|
||||
} from './file';
|
||||
import {
|
||||
vars,
|
||||
} from './template';
|
||||
|
||||
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`), vars({
|
||||
...radar,
|
||||
})), (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
))
|
||||
)))
|
||||
);
|
||||
@@ -1,6 +1,6 @@
|
||||
import pug from 'pug';
|
||||
import moment from 'moment';
|
||||
import { relativePath } from './file';
|
||||
import { relativePath } from '../common/file';
|
||||
import {
|
||||
groupByQuadrants,
|
||||
groupByFirstLetter,
|
||||
|
||||
Reference in New Issue
Block a user