Skip to content

Commit

Permalink
Scaffold: Normalize repository
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jan 4, 2019
1 parent 29ae9cc commit f33eec6
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 214 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .eslintrc
@@ -0,0 +1,3 @@
{
"extends": "gulp"
}
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text eol=lf
31 changes: 30 additions & 1 deletion .gitignore
@@ -1,2 +1,31 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules
egcache

# Users Environment Variables
.lock-wscript

# Garbage files
.DS_Store
1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
14 changes: 8 additions & 6 deletions .travis.yml
@@ -1,9 +1,11 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "6"
- "8"
- "10"
- '10'
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
after_script:
- npm run coveralls
38 changes: 0 additions & 38 deletions CHANGELOG

This file was deleted.

4 changes: 3 additions & 1 deletion LICENSE
@@ -1,4 +1,6 @@
Copyright (c) 2015 Tyler Kellen
The MIT License (MIT)

Copyright (c) 2014-2019 Tyler Kellen <tyler@sleekcode.net>, Blaine Bublitz <blaine.bublitz@gmail.com>, and Eric Schoffstall <yo@contra.io>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
76 changes: 54 additions & 22 deletions README.md
@@ -1,31 +1,23 @@
# rechoir [![Build Status](https://secure.travis-ci.org/js-cli/js-rechoir.png)](http://travis-ci.org/js-cli/js-rechoir)
> Require any supported file as a node module.
<p align="center">
<a href="http://gulpjs.com">
<img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
</a>
</p>

[![NPM](https://nodei.co/npm/rechoir.png)](https://nodei.co/npm/rechoir/)
# rechoir

## What is it?
This module, in conjunction with [interpret]-like objects can register any file type the npm ecosystem has a module loader for. This library is a dependency of [Liftoff].

## API

### prepare(config, filepath, requireFrom)
Look for a module loader associated with the provided file and attempt require it. If necessary, run any setup required to inject it into [require.extensions](http://nodejs.org/api/globals.html#globals_require_extensions).
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Travis Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url]

`config` An [interpret]-like configuration object.
Prepare a node environment to require files with different extensions.

`filepath` A file whose type you'd like to register a module loader for.
## What is it?

`requireFrom` An optional path to start searching for the module required to load the requested file. Defaults to the directory of `filepath`.
This module, in conjunction with [interpret]-like objects, can register any filetype the npm ecosystem has a module loader for. This library is a dependency of [Liftoff].

If calling this method is successful (aka: it doesn't throw), you can now require files of the type you requested natively.
**Note:** While `rechoir` will automatically load and register transpilers like `coffee-script`, you must provide a local installation. The transpilers are **not** bundled with this module.

An error with a `failures` property will be thrown if the module loader(s) configured for a given extension cannot be registered.
## Usage

If a loader is already registered, this will simply return `true`.

**Note:** While rechoir will automatically load and register transpilers like `coffee-script`, you must provide a local installation. The transpilers are **not** bundled with this module.

#### Usage
```js
const config = require('interpret').extensions;
const rechoir = require('rechoir');
Expand All @@ -38,5 +30,45 @@ console.log(require('./test/fixtures/test.csv'));
console.log(require('./test/fixtures/test.toml'));
```

[interpret]: http://github.com/tkellen/js-interpret
[Liftoff]: http://github.com/tkellen/js-liftoff
## API

### `prepare(config, filepath, [cwd], [noThrow])`

Look for a module loader associated with the provided file and attempt require it. If necessary, run any setup required to inject it into [require.extensions](http://nodejs.org/api/globals.html#globals_require_extensions).

`config` An [interpret]-like configuration object.

`filepath` A file whose type you'd like to register a module loader for.

`cwd` An optional path to start searching for the module required to load the requested file. Defaults to the directory of `filepath`.

`noThrow` An optional boolean indicating if the method should avoid throwing.

If calling this method is successful (e.g. it doesn't throw), you can now require files of the type you requested natively.

An error with a `failures` property will be thrown if the module loader(s) configured for a given extension cannot be registered.

If a loader is already registered, this will simply return `true`.

## License

MIT

[interpret]: http://github.com/gulpjs/interpret
[Liftoff]: http://github.com/gulpjs/liftoff

[downloads-image]: http://img.shields.io/npm/dm/rechoir.svg
[npm-url]: https://www.npmjs.com/package/rechoir
[npm-image]: http://img.shields.io/npm/v/rechoir.svg

[travis-url]: https://travis-ci.org/gulpjs/rechoir
[travis-image]: http://img.shields.io/travis/gulpjs/rechoir.svg?label=travis-ci

[appveyor-url]: https://ci.appveyor.com/project/gulpjs/rechoir
[appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/rechoir.svg?label=appveyor

[coveralls-url]: https://coveralls.io/r/gulpjs/rechoir
[coveralls-image]: http://img.shields.io/coveralls/gulpjs/rechoir/master.svg

[gitter-url]: https://gitter.im/gulpjs/gulp
[gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg
26 changes: 26 additions & 0 deletions appveyor.yml
@@ -0,0 +1,26 @@
# http://www.appveyor.com/docs/appveyor-yml
# http://www.appveyor.com/docs/lang/nodejs-iojs

environment:
matrix:
# node.js
- nodejs_version: "0.10"
- nodejs_version: "0.12"
- nodejs_version: "4"
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "10"

install:
- ps: Install-Product node $env:nodejs_version
- npm install

test_script:
- node --version
- npm --version
- cmd: npm test

build: off

# build version format
version: "{build}"
30 changes: 14 additions & 16 deletions index.js
@@ -1,18 +1,15 @@
const path = require('path');
var path = require('path');

const extension = require('./lib/extension');
const normalize = require('./lib/normalize');
const register = require('./lib/register');
var extension = require('./lib/extension');
var normalize = require('./lib/normalize');
var register = require('./lib/register');

exports.prepare = function (extensions, filepath, cwd, nothrow) {
var option, attempt;
exports.prepare = function(extensions, filepath, cwd, nothrow) {
var config, usedExtension, err, option, attempt, error;
var attempts = [];
var err;
var onlyErrors = true;
var exts = extension(filepath);

var config;
var usedExtension;
if (exts) {
exts.some(function(ext) {
usedExtension = ext;
Expand All @@ -21,16 +18,16 @@ exports.prepare = function (extensions, filepath, cwd, nothrow) {
});
}

if(Object.keys(require.extensions).indexOf(usedExtension) !== -1) {
if (Object.keys(require.extensions).indexOf(usedExtension) !== -1) {
return true;
}

if (!config) {
if (nothrow) {
return;
} else {
throw new Error('No module loader found for "'+usedExtension+'".');
}

throw new Error('No module loader found for "' + usedExtension + '".');
}

if (!cwd) {
Expand All @@ -39,6 +36,7 @@ exports.prepare = function (extensions, filepath, cwd, nothrow) {
if (!Array.isArray(config)) {
config = [config];
}

for (var i in config) {
option = config[i];
attempt = register(cwd, option.module, option.register);
Expand All @@ -49,21 +47,21 @@ exports.prepare = function (extensions, filepath, cwd, nothrow) {
attempts.push({
moduleName: option.module,
module: attempt,
error: error
error: error,
});
if (!error) {
onlyErrors = false;
break;
}
}
if (onlyErrors) {
err = new Error('Unable to use specified module loaders for "'+usedExtension+'".');
err = new Error('Unable to use specified module loaders for "' + usedExtension + '".');
err.failures = attempts;
if (nothrow) {
return err;
} else {
throw err;
}

throw err;
}
return attempts;
};
10 changes: 5 additions & 5 deletions lib/extension.js
@@ -1,9 +1,9 @@
const path = require('path');
var path = require('path');

const EXTRE = /\.[^.]*/g;
const LONGEXTRE = /^[.]?[^.]+([.].+[^.])$/;
var EXTRE = /\.[^.]*/g;
var LONGEXTRE = /^[.]?[^.]+([.].+[^.])$/;

module.exports = function (input) {
module.exports = function(input) {
var basename = path.basename(input);
var longExtension = LONGEXTRE.exec(basename);
if (!longExtension) {
Expand All @@ -13,7 +13,7 @@ module.exports = function (input) {
if (!possibleExtensions) {
return;
}
return possibleExtensions.map(function (_, idx, exts) {
return possibleExtensions.map(function(_, idx, exts) {
return exts.slice(idx).join('');
});
};
10 changes: 4 additions & 6 deletions lib/normalize.js
@@ -1,13 +1,11 @@
function normalizer (config) {
function normalizer(config) {
if (typeof config === 'string') {
return {
module: config
}
return { module: config };
}
return config;
};
}

module.exports = function (config) {
module.exports = function(config) {
if (Array.isArray(config)) {
return config.map(normalizer);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/register.js
@@ -1,10 +1,10 @@
const path = require('path');
const resolve = require('resolve');
var resolve = require('resolve');

module.exports = function (cwd, moduleName, register) {
module.exports = function(cwd, moduleName, register) {
var result;
try {
var modulePath = resolve.sync(moduleName, {basedir: cwd});
var result = require(modulePath);
var modulePath = resolve.sync(moduleName, { basedir: cwd });
result = require(modulePath);
if (typeof register === 'function') {
register(result);
}
Expand Down

0 comments on commit f33eec6

Please sign in to comment.