Skip to content

Commit

Permalink
Use current working directory relative to the input
Browse files Browse the repository at this point in the history
Closes GH-5.
  • Loading branch information
ben-eb authored and wooorm committed Apr 25, 2017
1 parent 93f789c commit f8fbab6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 65 deletions.
118 changes: 59 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,78 @@ var https = 'https://';

/* Add a license section. */
function license(options) {
var settings = {};
var pack = {};
var entries = [];
var cwd = global.process && global.process.cwd();
var url;
var length;
var index;

if (!options) {
options = {};
}
return transformer;

try {
pack = require(path.resolve(cwd, 'package.json'));
} catch (err) {}

if (typeof pack.author === 'string') {
url = EXPRESSION.exec(pack.author);
settings.name = url[1];
settings.url = url[3];
} else if (pack.author && pack.author.name) {
settings.name = pack.author.name;
settings.url = pack.author.url;
}
function transformer(tree, file) {
var settings = {};
var pack = {};
var entries = [];
var cwd = file.cwd;
var url;
var length;
var index;

if (!options) {
options = {};
}

if (options.file) {
settings.file = options.file;
} else {
try {
entries = fs.readdirSync(cwd);
} catch (err) { /* Empty */ }
pack = require(path.resolve(cwd, 'package.json'));
} catch (err) {}

if (typeof pack.author === 'string') {
url = EXPRESSION.exec(pack.author);
settings.name = url[1];
settings.url = url[3];
} else if (pack.author && pack.author.name) {
settings.name = pack.author.name;
settings.url = pack.author.url;
}

length = entries.length;
index = -1;
if (options.file) {
settings.file = options.file;
} else {
try {
entries = fs.readdirSync(cwd);
} catch (err) { /* Empty */ }

while (++index < length) {
if (LICENSE.test(entries[index])) {
settings.file = entries[index];
break;
length = entries.length;
index = -1;

while (++index < length) {
if (LICENSE.test(entries[index])) {
settings.file = entries[index];
break;
}
}
}
}

if (options.url) {
settings.url = options.url;
}

if (options.name) {
settings.name = options.name;
}
if (options.url) {
settings.url = options.url;
}

settings.license = options.license || pack.license;
if (options.name) {
settings.name = options.name;
}

if (!settings.license) {
throw new Error(
'Missing required `license` in settings.\n' +
'Either add a `license` to a `package.json` file\n' +
'or pass it into `remark-license`'
);
}
settings.license = options.license || pack.license;

if (!settings.name) {
throw new Error(
'Missing required `name` in settings.\n' +
'Either add an `author` to a `package.json` file\n' +
'or pass it into `remark-license`'
);
}
if (!settings.license) {
throw new Error(
'Missing required `license` in settings.\n' +
'Either add a `license` to a `package.json` file\n' +
'or pass it into `remark-license`'
);
}

return transformer;
if (!settings.name) {
throw new Error(
'Missing required `name` in settings.\n' +
'Either add an `author` to a `package.json` file\n' +
'or pass it into `remark-license`'
);
}

function transformer(tree) {
heading(tree, /^licen[cs]e$/i, function (start, nodes, end) {
var children = [];
var node = {type: 'paragraph', children: children};
Expand Down
11 changes: 5 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ test('license()', function (t) {
t.end();
});

test.onFinish(function () {
process.chdir(path.join(__dirname, '..'));
test('current working directory', function (t) {
var result = remark().use(license).processSync('# License').toString();
t.equal(result, '# License\n\n[MIT](LICENSE) © [Titus Wormer](http://wooorm.com)\n');
t.end();
});

test('Fixtures', function (t) {
Expand All @@ -38,17 +40,14 @@ test('Fixtures', function (t) {
var result;
var fail;

process.chdir(filepath);

config = exists(config) ? require(config) : {};
output = exists(output) ? read(output, 'utf-8') : '';
input = read(path.join(filepath, 'readme.md'), 'utf-8');

fail = fixture.indexOf('fail-') === 0 ? fixture.slice(5) : '';

try {
result = remark().use(license, config).processSync(input).toString();

result = remark().use(license, config).processSync({contents: input, cwd: filepath}).toString();
t.equal(result, output, 'should work on `' + fixture + '`');
} catch (err) {
if (!fail) {
Expand Down

0 comments on commit f8fbab6

Please sign in to comment.