Skip to content

Commit

Permalink
Update Pagic class
Browse files Browse the repository at this point in the history
  • Loading branch information
xcatliu committed Mar 7, 2017
1 parent a52422b commit bb1fc91
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 135 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
6 changes: 3 additions & 3 deletions bin/pagic.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#! /usr/bin/env node

const pkg = require('../package.json');
const program = require('commander');
const fs = require('fs');
const Pagic = require('..').Pagic;
const program = require('commander');
const pkg = require('../package.json');
const Pagic = require('../src/Pagic');

program
.version(pkg.version)
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>
<body>
<h1 id="pagic">Pagic</h1>
<p>The easiest way to generate static html page from</p>
<p>The easiest way to generate static html page from markdown</p>
<h2 id="pages">Pages</h2>
<ul>
<li><a href="css/site.css">css/site.css</a></li>
Expand Down
119 changes: 119 additions & 0 deletions src/Pagic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* eslint no-console:0 */

const path = require('path');
const fse = require('fs-extra');
const glob = require('glob');
const findParentDir = require('find-parent-dir');

const processors = [
require('./processor/parseFrontMatter'),
require('./processor/parseMarkdown'),
require('./processor/injectRelativeToRoot'),
];

const LAYOUT_FILENAME = '_layout.js';
const DEFAULT_OPTIONS = {
srcDir: 'src',
distDir: 'public',
};

class Pagic {
constructor(options = {}) {
this.options = Object.assign({}, DEFAULT_OPTIONS, options);

if (typeof this.options.srcDir === 'undefined' || this.options.srcDir === null) {
this.options.srcDir = DEFAULT_OPTIONS.srcDir;
}
if (typeof this.options.distDir === 'undefined' || this.options.distDir === null) {
this.options.distDir = DEFAULT_OPTIONS.distDir;
}
}

build() {
this.clearDistDir();
this.buildMD();
this.copyStaticFiles();
}

clearDistDir() {
console.log(`Clear ${this.options.distDir}`);
fse.emptyDirSync(this.options.distDir);
}

buildMD() {
const mdFiles = glob.sync('**/*.md', {
cwd: this.options.srcDir,
});

if (mdFiles.length === 0) {
console.log('No markdown files found');
return;
}

mdFiles.forEach(filePath => {
const resolvedFilePath = path.resolve(this.options.srcDir, filePath);
const resolvedDistPath = path.resolve(this.options.distDir, filePath)
.replace(/\.md$/, '.html');

const layout = this.getLayout(resolvedFilePath);

if (!layout) {
console.error(`CANNOT find a layout for ${resolvedFilePath}, will skip this file`);
return;
}

const originalContent = fse.readFileSync(resolvedFilePath, 'utf-8');

const context = processors.reduce((prevContext, processor) => processor(prevContext), {
path: filePath,
content: originalContent,
options: this.options,
});

const html = layout(context);

fse.outputFileSync(resolvedDistPath, html);

console.log(`Generated ${resolvedDistPath}`);
});
}

copyStaticFiles() {
const staticFiles = glob.sync('**/*', {
ignore: [
'**/*.md',
'**/_*',
],
nodir: true,
cwd: this.options.srcDir,
});

if (staticFiles.length === 0) {
return;
}

staticFiles.forEach(filePath => {
const resolvedFilePath = path.resolve(this.options.srcDir, filePath);
const resolvedDistPath = path.resolve(this.options.distDir, filePath);

fse.copySync(resolvedFilePath, resolvedDistPath);

console.log(`Copied ${resolvedDistPath}`);
});
}

getLayout(currentPath) {
const layoutDir = findParentDir.sync(currentPath, LAYOUT_FILENAME);

if (!layoutDir) {
return null;
}

/* eslint global-require:0 */
const layout = require(path.resolve(layoutDir, LAYOUT_FILENAME));

return layout;
}
}

module.exports = Pagic;
117 changes: 1 addition & 116 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,4 @@
/* eslint no-console:0 */

const path = require('path');
const fse = require('fs-extra');
const glob = require('glob');
const findParentDir = require('find-parent-dir');

const processors = [
require('./processor/parseFrontMatter'),
require('./processor/parseMarkdown'),
require('./processor/injectRelativeToRoot'),
];

const LAYOUT_FILENAME = '_layout.js';
const DEFAULT_OPTIONS = {
srcDir: 'src',
distDir: 'public',
};

class Pagic {
constructor(options = {}) {
this.options = Object.assign({}, DEFAULT_OPTIONS, options);

if (typeof this.options.srcDir === 'undefined' || this.options.srcDir === null) {
this.options.srcDir = DEFAULT_OPTIONS.srcDir;
}
if (typeof this.options.distDir === 'undefined' || this.options.distDir === null) {
this.options.distDir = DEFAULT_OPTIONS.distDir;
}
}

build() {
this.clearDistDir();
this.buildMD();
this.copyStaticFiles();
}

clearDistDir() {
fse.emptyDirSync(this.options.distDir);
}

buildMD() {
const mdFiles = glob.sync('**/*.md', {
cwd: this.options.srcDir,
});

if (mdFiles.length === 0) {
console.log('No markdown files found');
return;
}

mdFiles.forEach(filePath => {
const resolvedFilePath = path.resolve(this.options.srcDir, filePath);
const resolvedDistPath = path.resolve(this.options.distDir, filePath)
.replace(/\.md$/, '.html');

const layout = this.getLayout(resolvedFilePath);

if (!layout) {
console.error(`CANNOT find a layout for ${resolvedFilePath}, will skip this file`);
return;
}

const originalContent = fse.readFileSync(resolvedFilePath, 'utf-8');

const context = processors.reduce((prevContext, processor) => processor(prevContext), {
path: filePath,
content: originalContent,
options: this.options,
});

const html = layout(context);

fse.outputFileSync(resolvedDistPath, html);

console.log(`Generated ${resolvedDistPath}`);
});
}

copyStaticFiles() {
const staticFiles = glob.sync('**/*', {
ignore: [
'**/*.md',
'**/_*',
],
nodir: true,
cwd: this.options.srcDir,
});

if (staticFiles.length === 0) {
return;
}

staticFiles.forEach(filePath => {
const resolvedFilePath = path.resolve(this.options.srcDir, filePath);
const resolvedDistPath = path.resolve(this.options.distDir, filePath);

fse.copySync(resolvedFilePath, resolvedDistPath);

console.log(`Copied ${resolvedDistPath}`);
});
}

getLayout(currentPath) {
const layoutDir = findParentDir.sync(currentPath, LAYOUT_FILENAME);

if (!layoutDir) {
return null;
}

/* eslint global-require:0 */
const layout = require(path.resolve(layoutDir, LAYOUT_FILENAME));

return layout;
}
}
const Pagic = require('./Pagic');

module.exports = (...args) => {
const pagic = new Pagic(...args);
Expand Down
31 changes: 16 additions & 15 deletions test/unit/index.test.js → test/unit/Pagic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const stub = {
'./processor/injectRelativeToRoot': () => ({}),
};

const proxiedPagic = proxyquire('../..', stub);
const Pagic = proxiedPagic.Pagic;
const Pagic = proxyquire('../../src/Pagic', stub);

const DEFAULT_OPTIONS = {
srcDir: 'src',
Expand All @@ -42,42 +41,42 @@ describe('Pagic Class', () => {
});

describe('constructor()', () => {
it('should have default srcDir and publicDir when pass 0 arguments', () => {
it('should have default srcDir and distDir when pass 0 arguments', () => {
const pagic = new Pagic();
verifySrcDirAndDistDir(pagic);
});
it('should have default srcDir and publicDir when pass empty object', () => {
it('should have default srcDir and distDir when pass empty object', () => {
const pagic = new Pagic({});
verifySrcDirAndDistDir(pagic);
});

it('should have custom srcDir and default publicDir when only pass srcDir', () => {
it('should have custom srcDir and default distDir when only pass srcDir', () => {
const pagic = new Pagic({ srcDir: 'site' });
verifySrcDirAndDistDir(pagic, { srcDir: 'site' });
});
it('should have default srcDir and custom publicDir when only pass publicDir', () => {
const pagic = new Pagic({ publicDir: 'docs' });
verifySrcDirAndDistDir(pagic, { publicDir: 'docs' });
it('should have default srcDir and custom distDir when only pass distDir', () => {
const pagic = new Pagic({ distDir: 'docs' });
verifySrcDirAndDistDir(pagic, { distDir: 'docs' });
});
it('should have custom srcDir and custom publicDir', () => {
const pagic = new Pagic({ srcDir: 'site', publicDir: 'docs' });
verifySrcDirAndDistDir(pagic, { srcDir: 'site', publicDir: 'docs' });
it('should have custom srcDir and custom distDir', () => {
const pagic = new Pagic({ srcDir: 'site', distDir: 'docs' });
verifySrcDirAndDistDir(pagic, { srcDir: 'site', distDir: 'docs' });
});

it('should have default srcDir when pass undefined srcDir', () => {
const pagic = new Pagic({ srcDir: undefined });
verifySrcDirAndDistDir(pagic);
});
it('should have default publicDir when pass undefined publicDir', () => {
const pagic = new Pagic({ publicDir: undefined });
it('should have default distDir when pass undefined distDir', () => {
const pagic = new Pagic({ distDir: undefined });
verifySrcDirAndDistDir(pagic);
});
it('should have default srcDir when pass null srcDir', () => {
const pagic = new Pagic({ srcDir: null });
verifySrcDirAndDistDir(pagic);
});
it('should have default publicDir when pass null publicDir', () => {
const pagic = new Pagic({ publicDir: null });
it('should have default distDir when pass null distDir', () => {
const pagic = new Pagic({ distDir: null });
verifySrcDirAndDistDir(pagic);
});
});
Expand All @@ -99,12 +98,14 @@ describe('Pagic Class', () => {

describe('clearDistDir()', () => {
it('should call emptyDirSync', function () {
this.sinon.spy(console, 'log');
this.sinon.stub(stub['fs-extra'], 'emptyDirSync');

const pagic = new Pagic();

pagic.clearDistDir();

sinon.assert.calledWith(console.log, 'Clear public');
sinon.assert.calledOnce(stub['fs-extra'].emptyDirSync);
});
});
Expand Down

0 comments on commit bb1fc91

Please sign in to comment.