neamar / auto-load Goto Github PK
View Code? Open in Web Editor NEWrequire() all files in subfolders.
License: MIT License
require() all files in subfolders.
License: MIT License
I find myself using auto-load
more and more, and there's a pattern I keep repeating and some options that would make it much more easier as per keeping my code DRY. So I think it'd be nice if auto-load
would implement options by default. From my past needs, these might be:
Option | Default | Meaning |
---|---|---|
deep |
true |
Perform a deep recursive search in subfolders as now |
js |
true |
Include javascript files (for fine-tunning) |
json |
true |
Include json files (for fine-tunning) |
As you can see, they are also backwards compatible as per the defaults and the current functionality. I think that accepting them as the second parameter would be nice:
var controllers = require('auto-load')('controllers', { deep: false, json: false });
What do you think of this? Might it be possible within the scope of auto-load
? I can submit a pull request with the functionality described. Any other options that anyone needs?
An alternative would be accepting any random extension like extensions: ['js', 'json']
, but since this uses require()
internally and only js
and json
can be accepted I thought of only allowing them. However, a case can be made for loading files as strings (for example a .jade
or a .txt
as string), so it's worth noting it here. It might be worth another issue as it's extending the current implementation rather than adding options as suggested previously.
can we get an extension resolver function so that we can dynamically evaluate which extensions we would like to load?
Following the discussion here, it might be interesting to load more file types with auto-load
. I open this issue to discuss whether this is within the scope of auto-loader or would be better suited for a package that uses auto-loader. For a folder containing the following files:
config.json
README.md
script.js
data.txt
LICENSE
hello.txt
Would it make sense to load more than just config.json
and script.js
? The following is proposed:
var files = require('auto-load')('files', { extensions: ['txt'] });
This would load the files with the extensions .txt
and load them as strings. Only .js
and .json
are parsed, while other files are loaded as strings.
Hi,
Why do you do a lowerCase on all file names?
I want the function names/exports to be added as they are. I can understand removing of dashes and dots but why convert the case as well?
Acc to your logic:
MyFunction.js => myfunction
myFunction.js => myfunction
But I want them to stay as they are i.e
MyFunction.js => MyFunction
myFunction.js => myFunction
I don't think there should be a blanket lowercase conversion.
Are you able to put in a fix and release?
Is it possible to specify load order ?
Is there async support for files that should complete a promise first ?
Is it possible to have relative paths, or absolute to the root, for auto-load? So it becomes easier to just include a full directory. Example:
// Relative to the root
var models = require('auto-load')('models');
// Or relative to the current file
var models = require('auto-load')('../models');
Currently the only way to do this is to use a more cumbersome notation:
var models = require('auto-load')(__dirname + '/../models');
This question in StackOverflow illustrates the issue perfectly, as it's also about readdirSync(), which is used in auto-load.
Note: I'm just proposing it since I had a really similar personal package, until I decided to find one pre-made and found this one. This is basically the only difference so I'm proposing it for improvement, but of course there's no problem typing 15-16 characters more.
If i do
import * as autoload from 'auto-load';
i've an error "It's not a module".
I've modified your index.d.ts to :
declare module autoload {
}
declare type ModuleExports = any;
declare type AutoloadTree = {
[key: string]: AutoloadTree | ModuleExports
};
declare interface AutoloadOptions {
deep?: boolean,
js?: boolean,
json?: boolean
}
declare function autoload(baseDirectory: string, options?: AutoloadOptions): AutoloadTree;
export = autoload;
and it's works (i've moved to "}" from end to start to do "module autoload {}" and rename "auto-load" to "autoload").
I can't fork/commit/push because github isn't open from my company :(
Regards
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.