Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Adds subdir listing/linking macro #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var _es6TemplateStrings = require('es6-template-strings');

var _es6TemplateStrings2 = _interopRequireDefault(_es6TemplateStrings);

var _macros = require('./macros');
var _macros = require("./macros");

var macros = _interopRequireWildcard(_macros);

Expand Down Expand Up @@ -76,7 +76,7 @@ function compile(userPath, fallbackPath, cb) {
* or the default node-readme `src/.README.md` template
*/
function createReadme() {
var overwrite = arguments[0] === undefined ? true : arguments[0];
var overwrite = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];

compile('.README.md', _path2['default'].join(__dirname, './.README.md'), function (markdown) {
var destination = _path2['default'].join(process.cwd(), './README.md');
Expand Down
2 changes: 1 addition & 1 deletion dist/macros/badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Object.defineProperty(exports, '__esModule', {
exports['default'] = badge;

function image(src) {
var title = arguments[1] === undefined ? '' : arguments[1];
var title = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];

return '![' + title + '](' + src + ')';
}
Expand Down
4 changes: 2 additions & 2 deletions dist/macros/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ exports['default'] = dependencies;
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }

function singleDep(name, version) {
var dev = arguments[2] === undefined ? false : arguments[2];
var dev = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2];

var check = dev ? '✔' : '✖';
return '[' + name + '](https://www.npmjs.com/package/' + name + ') | ' + version + ' | ' + check;
}

function dependenciesList(obj) {
var dev = arguments[1] === undefined ? false : arguments[1];
var dev = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];

if (!obj) {
obj = {};
Expand Down
8 changes: 7 additions & 1 deletion dist/macros/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ var _dependencies2 = require('./dependencies');

var _dependencies3 = _interopRequireDefault(_dependencies2);

exports.dependencies = _dependencies3['default'];
exports.dependencies = _dependencies3['default'];

var _subdirs2 = require('./subdirs');

var _subdirs3 = _interopRequireDefault(_subdirs2);

exports.subdirs = _subdirs3['default'];
2 changes: 1 addition & 1 deletion dist/macros/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function license(options, scope) {
} else if (scope.pkg.licenses) {
return scope.pkg.licenses.map(function (license) {
return singleLicense(license.type);
}).join(';');
}).join('\;');
}
}

Expand Down
48 changes: 48 additions & 0 deletions dist/macros/subdirs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});
exports['default'] = subdirs;
var path = require('path');
var fs = require('fs');

var walk = function walk(dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function (file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) results = results.concat(walk(file));else results.push(file);
});
return results;
};

function subdirs(options, scope) {
var subdir = options || '';
var basePath = path.resolve('.', subdir);
var readmes = walk(basePath);
var struct = readmes.reduce(function (acc, filepath) {
var relativePath = filepath.replace(basePath + '/', '');
var componentName = relativePath.split('/').slice(0, 1)[0];
var firstLetter = componentName[0];
if (/\./i.test(componentName)) {
return acc;
}
if (!acc.hasOwnProperty(componentName)) {
acc[componentName] = true;
}
if (/readme\.md/i.test(relativePath)) {
acc[componentName] = true;
}
return acc;
}, {});
return Object.keys(struct).map(function (key) {
if (struct[key]) {
return '- [' + key + '](' + subdir + '/' + key + ')';
}
return '- ' + key;
}).join('\n');
}

module.exports = exports['default'];
1 change: 1 addition & 0 deletions src/macros/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export badge from './badge';
export license from './license';
export author from './author';
export dependencies from './dependencies';
export subdirs from './subdirs';
42 changes: 42 additions & 0 deletions src/macros/subdirs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var path = require('path');
var fs = require('fs');


var walk = function(dir) {
var results = []
var list = fs.readdirSync(dir)
list.forEach(function(file) {
file = dir + '/' + file
var stat = fs.statSync(file)
if (stat && stat.isDirectory()) results = results.concat(walk(file))
else results.push(file)
})
return results
}

export default function subdirs(options, scope) {
const subdir = options || ''
var basePath = path.resolve('.', subdir)
var readmes = walk(basePath)
const struct = readmes.reduce((acc, filepath) => {
const relativePath = filepath.replace(basePath + '/', '')
const componentName = relativePath.split('/').slice(0, 1)[0]
const firstLetter = componentName[0]
if (/\./i.test(componentName)) {
return acc
}
if (!acc.hasOwnProperty(componentName)) {
acc[componentName] = true
}
if (/readme\.md/i.test(relativePath)) {
acc[componentName] = true
}
return acc
}, {})
return Object.keys(struct).map(key => {
if (struct[key]) {
return `- [${key}](${subdir}/${key})`
}
return `- ${key}`
}).join('\n')
}