Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
Merge 70208e0 into 9b2e189
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Gaunt committed Oct 10, 2018
2 parents 9b2e189 + 70208e0 commit a464442
Show file tree
Hide file tree
Showing 23 changed files with 388 additions and 135 deletions.
6 changes: 4 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ gulp.task('build',
'clean',
() => {
// TODO: Move to web build tool
return fs.copy(path.join(__dirname, 'src', 'assets'), path.join(__dirname, 'build', 'assets'));
return fs.copy(path.join(__dirname, 'src', 'themes'), path.join(__dirname, 'build', 'themes'));
},
tsNode.gulpBuild()
tsNode.gulpBuild({
flags: ['--skipLibCheck'],
})
)
);
44 changes: 23 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
},
"dependencies": {
"@hopin/logger": "1.0.2",
"@hopin/markdown": "0.0.3",
"@hopin/render": "^3.0.2",
"@hopin/markdown": "0.1.0",
"@hopin/render": "^3.1.0",
"chalk": "^2.4.1",
"fs-extra": "^7.0.0",
"glob": "^7.1.2",
"gray-matter": "^4.0.1",
"json5": "^2.0.1",
"meow": "^5.0.0"
},
Expand Down
164 changes: 104 additions & 60 deletions src/controllers/file-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,141 @@ import * as fs from 'fs-extra';
import * as path from 'path';
import { logger } from '@hopin/logger';

import { Message } from './worker-pool';
import { Message, RUN_WITH_DETAILS_MSG } from './worker-pool';
import { Config } from '../models/config';
import {NavNode} from '../models/nav-tree';

async function run(inputPath: string, config: Config): Promise<Message> {
// TODO: Check files exist first
try {
const template = await createTemplateFromFile(inputPath);
// TODO: Make this configurable in markdown file itself
const wrappingTemplate = await createTemplateFromFile(config.defaultHTMLTmpl);

// Wrapping template will be the template that displays the styles etc
// so copy template styles and scripts over
wrappingTemplate.styles.add(template.styles);
wrappingTemplate.scripts.add(template.scripts);

// Render the original page and run it through the markdown parser
const plainPage = await template.render();
const markdownRender = await renderMarkdown(plainPage);

// TODO Add the markdown token styles to the wrapping template
for (const t of markdownRender.tokens) {
const tokenAssets = config.tokenAssets[t];
if (!tokenAssets) {
continue;
}
async function run(inputPath: string, config: Config, navigation: {[id: string]: Array<NavNode>}): Promise<Message> {
// TODO: Check files exist first
try {
const template = await createTemplateFromFile(inputPath);

// Render the original page and run it through the markdown parser
const plainPage = await template.render({
topLevel: {
navigation,
}
});
const markdownRender = await renderMarkdown(plainPage, {
staticDir: config.staticPath,
});

const topLevelTemplate = (template.yaml as any)['template'] ? (template.yaml as any)['template'] : 'default.tmpl'
const themeFile = path.join(config.themePath, topLevelTemplate);
const wrappingTemplate = await createTemplateFromFile(themeFile);

if (tokenAssets.styles) {
const styles = tokenAssets.styles;
if (styles.inline) {
wrappingTemplate.styles.inline.add(styles.inline, styles.inline);
// Wrapping template will be the template that displays the styles etc
// so copy template styles and scripts over
wrappingTemplate.styles.prepend(template.styles);
wrappingTemplate.scripts.add(template.scripts);

for (const t of markdownRender.tokens) {
const tokenAssets = config.tokenAssets[t];
if (!tokenAssets) {
continue;
}

if (tokenAssets.styles) {
const styles = tokenAssets.styles;
if (styles.inline) {
for (const filePath of styles.inline) {
const buffer = await fs.readFile(filePath);
wrappingTemplate.styles.inline.prepend(filePath, buffer.toString());
}
}
if (styles.sync) {
for (const filePath of styles.sync) {
wrappingTemplate.styles.sync.prepend(filePath, filePath);
}
if (styles.sync) {
wrappingTemplate.styles.sync.add(styles.sync, styles.sync);
}
if (styles.async) {
for (const filePath of styles.async) {
wrappingTemplate.styles.async.prepend(filePath, filePath);
}
}
}

if (tokenAssets.scripts) {
const scripts = tokenAssets.scripts;
if (scripts.inline) {
for (const filePath of scripts.inline) {
const buffer = await fs.readFile(filePath);
wrappingTemplate.scripts.inline.prepend(filePath, {
src: buffer.toString(),
type: path.extname(filePath) === '.mjs' ? 'module' : 'nomodule',
});
}
if (styles.async) {
wrappingTemplate.styles.async.add(styles.async, styles.async);
}
if (scripts.sync) {
for (const filePath of scripts.sync) {
wrappingTemplate.scripts.sync.prepend(filePath, filePath);
}
}
if (scripts.async) {
for (const filePath of scripts.async) {
wrappingTemplate.scripts.async.prepend(filePath, filePath);
}
}
}
}

// Finally render the content in the wrapping template
const wrappedHTML = await wrappingTemplate.render({
// Finally render the content in the wrapping template
const wrappedHTML = await wrappingTemplate.render({
topLevel: {
content: markdownRender.html,
});
navigation,
page: template.yaml
},
});

const relativePath = path.relative(config.contentPath, inputPath);

// replace .md with .html
const relPathPieces = path.parse(relativePath);
delete relPathPieces.base;
relPathPieces.ext = '.html';
const relativePath = path.relative(config.contentPath, inputPath);

const outputPath = path.join(config.outputPath, path.format(relPathPieces));
await fs.mkdirp(path.dirname(outputPath));
await fs.writeFile(outputPath, wrappedHTML);
// replace .md with .html
const relPathPieces = path.parse(relativePath);
delete relPathPieces.base;
relPathPieces.ext = '.html';

return {
result: {
inputPath,
outputPath,
}
};
} catch (err) {
return {
error: `Unable to read and parse: ${err.message}`,
};
}
const outputPath = path.join(config.outputPath, path.format(relPathPieces));
await fs.mkdirp(path.dirname(outputPath));
await fs.writeFile(outputPath, wrappedHTML);

return {
result: {
inputPath,
outputPath,
}
};
} catch (err) {
logger.error('File Processor run failed.');
logger.error(err);
return {
error: `Unable to read and parse: ${err.message}`,
};
}
}

export async function start(args: Array<string>, config: Config): Promise<Message> {
export async function start(args: Array<string>, config: Config, navigation: {[id: string]: Array<NavNode>} = {}): Promise<Message> {
if (args.length != 3) {
logger.warn('Unexpected number of process args passed to file-processor: ', process.argv);
return {
error: `Unexpected number of process args: ${JSON.stringify(process.argv)}`,
};
}

return run(args[2], config);
return run(args[2], config, navigation);
}

let isRunning = false;
process.on('message', (msg) => {
process.on('message', (msg: any) => {
switch(msg.name) {
case 'run-with-config': {
case RUN_WITH_DETAILS_MSG: {
if (isRunning) {
console.log('File processor already running, ignoring msg: ', msg);
logger.warn('File processor already running, ignoring msg: ', msg);
return;
}

isRunning = true;
start(process.argv, msg.config).then((msg) => {
start(process.argv, msg.config, msg.navigation).then((msg) => {
process.send(msg);
process.exit(0);
})
Expand Down
27 changes: 27 additions & 0 deletions src/controllers/site-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import { logger } from '@hopin/logger';

import {getConfig} from '../models/config';
Expand Down Expand Up @@ -39,5 +40,31 @@ export class SiteGenerator {
}
throw new Error(`${errors} errors occured.`);
}

// Copy over static/ files from theme
const themeStatic = path.join(config.themePath, 'static');
let staticExists = false;
try {
await fs.access(themeStatic);
staticExists = true;
} catch (err) {
logger.debug('No <theme>/static/ directory found in theme.', err);
}

if (staticExists) {
await fs.copy(themeStatic, config.outputPath);
}

staticExists = false;
try {
await fs.access(config.staticPath);
staticExists = true;
} catch (err) {
logger.debug('No static/ directory found in theme.', err);
}

if (staticExists) {
await fs.copy(config.staticPath, config.outputPath);
}
}
}

0 comments on commit a464442

Please sign in to comment.