Skip to content

Commit

Permalink
Added utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oss6 committed Jun 13, 2016
1 parent f22903e commit 9278be5
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 204 deletions.
251 changes: 128 additions & 123 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,10 @@ var jsonfile = require('jsonfile');
var x = module.exports;
var contentsRepo = git('contents');

x.getModifiedFiles = function (repo, cb) {
repo.add('.', () => {
repo.status((err, status) => {
var pageFiles = [];
var articleFiles = [];
var deleted = [];

if (!err) {
// Get modified + created
var all = status.modified.concat(status.created);
deleted = status.deleted;

all.forEach(file => {
var parts = file.split(path.sep);
var type = parts[0];
var fileName = parts[1];

if (type === 'pages') {
pageFiles.push(fileName);
} else if (type === 'articles') {
articleFiles.push(fileName);
}
});
}

cb(err, pageFiles, articleFiles, deleted);
});
});
};

x.createIndex = function (g, cb) { // articles
x.createIndex = function (g, cb) {
// Read all articles
reader.read('articles', 'all', (articles, errors) => {
// Get only the metadata and url
articles = articles.map(article => {
var date = utils.extractISODate(article.metadata.date);
var urlParts = [
Expand Down Expand Up @@ -83,34 +55,106 @@ x.createIndex = function (g, cb) { // articles
});
};

x.transferStatic = function (action, cb) {
var staticPath = 'static';
x.generate = function (action, smart, cb) {
// Process parameters
// --------------------
if (typeof action === 'boolean') {
smart = action;
action = {
log: function () {}
};
}

if (typeof smart === 'function') {
cb = smart;
}
// --------------------

var errors = [];

fs.readdir(staticPath, (err, files) => {
if (err) {
errors.push(err);
return;
// Configure template engine
var env = nj.configure('templates', { autoescape: true });
env.addFilter('human_date', utils.humanDate);

x.getModifiedFiles(contentsRepo, (err, pageFiles, articleFiles, deletedFiles) => {
// Get sakin file contents
var sakinFile = jsonfile.readFileSync('.sakin');

// Delete output files if necessary
if (deletedFiles.length !== 0) {
deletedFiles.forEach(contentFile => {
var key = path.join('contents', contentFile);
var outputFile = sakinFile.contents[key];

if (outputFile !== undefined) {
// Remove file and update contents mapping
fs.removeSync(outputFile);
delete sakinFile.contents[key];

// Update category mappings
var urlParts = outputFile.split('/');
urlParts.shift();
var url = urlParts.join('/');
sakinFile.categories = x.updateCategoriesMapping('delete', sakinFile.categories, url);
}
});
}

var toGo = files.length;
// Read and process pages and articles
reader.read('pages', smart ? pageFiles : 'all', (pages, pErrors) => {
if (pErrors.length !== 0) {
errors = errors.concat(pErrors);
}

files
.map(file => path.join(staticPath, file))
.forEach(file => {
var p = path.join('output', path.basename(file));
reader.read('articles', smart ? articleFiles : 'all', (articles, aErrors) => {
if (aErrors.length !== 0) {
errors = errors.concat(aErrors);
}

fs.copy(file, p, copyErr => {
if (err) {
action.log('error', file + ' -> ' + p);
errors.push(copyErr);
} else if (--toGo === 0) {
cb(errors);
} else {
action.log('success', file + ' -> ' + p);
}
x.processFiles(action, pages, articles, (contentsMapping, categoriesMapping, processErrors) => {
errors = errors.concat(processErrors);

x.updateSakinFile(contentsMapping, categoriesMapping, sakinFile, usErr => {
if (usErr) {
errors.push(usErr);
}
if (cb !== undefined && typeof cb === 'function') {
cb(errors);
}
});
});
});
});
});
};

x.getModifiedFiles = function (repo, cb) {
repo.add('.', () => {
repo.status((err, status) => {
var pageFiles = [];
var articleFiles = [];
var deleted = [];

if (!err) {
// Get modified + created
var all = status.modified.concat(status.created);
deleted = status.deleted;

all.forEach(file => {
var parts = file.split(path.sep);
var type = parts[0];
var fileName = parts[1];

if (type === 'pages') {
pageFiles.push(fileName);
} else if (type === 'articles') {
articleFiles.push(fileName);
}
});
}

cb(err, pageFiles, articleFiles, deleted);
});
});
};

Expand Down Expand Up @@ -229,86 +273,34 @@ x.processFiles = function (action, pages, articles, cb) {
});
};

x.updateSakinFile = function (contentsMapping, categoriesMapping, sakinFile, cb) {
var currentContentsMapping = sakinFile.contents;
var newContentsMapping = utils.extend(currentContentsMapping, contentsMapping);

var currentCategoriesMapping = sakinFile.categories;
var newCategoriesMapping = utils.extend(currentCategoriesMapping, categoriesMapping);

sakinFile.contents = newContentsMapping;
sakinFile.categories = newCategoriesMapping;

jsonfile.writeFile('.sakin', sakinFile, cb);
};

x.generate = function (action, smart, cb) {
if (typeof action === 'boolean') {
smart = action;
action = {
log: function () {}
};
}

if (typeof smart === 'function') {
cb = smart;
}

x.transferStatic = function (action, cb) {
var staticPath = 'static';
var errors = [];

// Configure template engine
var env = nj.configure('templates', { autoescape: true });
env.addFilter('human_date', utils.humanDate);

x.getModifiedFiles(contentsRepo, (err, pageFiles, articleFiles, deletedFiles) => {
// Get sakin file contents
var sakinFile = jsonfile.readFileSync('.sakin');

// Delete output files if necessary
if (deletedFiles.length !== 0) {
deletedFiles.forEach(contentFile => {
var key = path.join('contents', contentFile);
var outputFile = sakinFile.contents[key];

if (outputFile !== undefined) {
// Remove file and update contents mapping
fs.removeSync(outputFile);
delete sakinFile.contents[key];

// Update category mappings
var urlParts = outputFile.split('/');
urlParts.shift();
var url = urlParts.join('/');
sakinFile.categories = x.updateCategoriesMapping('delete', sakinFile.categories, url);
}
});
fs.readdir(staticPath, (err, files) => {
if (err) {
errors.push(err);
return;
}

// Read and process pages and articles
reader.read('pages', smart ? pageFiles : 'all', (pages, pErrors) => {
if (pErrors.length !== 0) {
errors = errors.concat(pErrors);
}

reader.read('articles', smart ? articleFiles : 'all', (articles, aErrors) => {
if (aErrors.length !== 0) {
errors = errors.concat(aErrors);
}
var toGo = files.length;

x.processFiles(action, pages, articles, (contentsMapping, categoriesMapping, processErrors) => {
errors = errors.concat(processErrors);
files
.map(file => path.join(staticPath, file))
.forEach(file => {
var p = path.join('output', path.basename(file));

x.updateSakinFile(contentsMapping, categoriesMapping, sakinFile, usErr => {
if (usErr) {
errors.push(usErr);
}
if (cb !== undefined && typeof cb === 'function') {
cb(errors);
}
});
fs.copy(file, p, copyErr => {
if (err) {
action.log('error', file + ' -> ' + p);
errors.push(copyErr);
} else if (--toGo === 0) {
cb(errors);
} else {
action.log('success', file + ' -> ' + p);
}
});
});
});
});
};

Expand Down Expand Up @@ -349,6 +341,19 @@ x.updateCategoriesMapping = function (mode, mapping, articleUrl, metadata) {
return mapping;
};

x.updateSakinFile = function (contentsMapping, categoriesMapping, sakinFile, cb) {
var currentContentsMapping = sakinFile.contents;
var newContentsMapping = utils.extend(currentContentsMapping, contentsMapping);

var currentCategoriesMapping = sakinFile.categories;
var newCategoriesMapping = utils.extend(currentCategoriesMapping, categoriesMapping);

sakinFile.contents = newContentsMapping;
sakinFile.categories = newCategoriesMapping;

jsonfile.writeFile('.sakin', sakinFile, cb);
};

x.watchChanges = function (action, directory) {
var watcher = chokidar.watch(directory, {
ignoreInitial: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ x.repeat = function (s, n) {
};

x.stringListToArray = function (str) {
return str.split(',').map(word => word.trim());
return str.split(',').map(word => word.trim()).filter(word => word !== '');
};

x.titleCase = function (str) {
Expand Down

0 comments on commit 9278be5

Please sign in to comment.