UNPKG

4.54 kBMarkdownView Raw
1locator
2=======
3
4The locator gives semantic meaning to files on the filesystem.
5It does this with a set of "rules" that describes how each file path should be interpreted.
6In addition it groups files into "bundles".
7(Each bundle is usually an NPM module, but not always.)
8
9The locator doesn't _interpret_ the semantic meaning of the files.
10It is up to the user to understand and use the semantic meanings associated with the files.
11
12[![Build Status](https://travis-ci.org/yahoo/locator.png?branch=master)](https://travis-ci.org/yahoo/locator)
13
14
15## Goals & Design
16* provide an abstraction over filesystem paths
17 * set of "rules" (regexps basically) that determine the semantic meaning of each file
18 * files that match a rule (and thus have semantic meaning) are called "resources"
19 * built-in handling of "selectors", for resources that have multiple versions
20* organize files in bundles
21 * bundles are usually NPM modules
22 * ...but can be something else, if an NPM module delivers multiple child bundles
23 * bundles are recursively walked, since they are often organized in a tree structure on disk
24 * bundles can have different types
25 * for example, a mojito application bundle is walked differently (uses a different ruleset) than a mojito mojit bundle
26 * each bundle can declare its type in its `package.json` (for those bundles that are NPM modules)
27 * each bundle can also describe the type of child bundles found at certain paths (for e.g. mojito application that has mojit bundles at a certain place)
28* configurable
29 * the behavior of the locator should be configurable, which should include
30 * ...defining new rulesets, for new bundle types
31 * ...general runtime behavior configuration of returned values
32 * ...etc
33
34
35## Installation
36Install using npm:
37
38```shell
39$ npm install locator
40```
41
42
43
44## Example: Mojito Application
45In your app's `package.json`:
46```javascript
47{
48 "dependencies": {
49 ...
50 "mojito": "*",
51 ...
52 },
53 "locator": {
54 "rulesets": "mojito/locator-rulesets"
55 }
56}
57```
58
59
60## Example: Defining Your Own Semantics
61In your app's `package.json`:
62```javascript
63{
64 "locator": {
65 "rulesets": "locator-rulesets"
66 }
67}
68```
69
70A new `locator-rulesets.js` file which defines how to add semantic meaning to filesystem paths:
71```javascript
72module.exports = {
73 // nameKey defaults to 1
74 // selectorKey has no default. selector is only used if selectorKey is given
75 main: {
76 // we can use this to skip files
77 _skip: [
78 /^tests?\b/i
79 ],
80
81 // where to find configuration files
82 configs: {
83 regex: /^configs\/([a-z_\-\/]+)\.(json|js)$/i
84 },
85
86 // where to find templates
87 templates: {
88 regex: /^templates\/([a-z_\-\/]+)(\.([\w_\-]+))?\.[^\.\/]+\.html$/i,
89 // We use "selectors" because we might use different templates based
90 // on different circumstances.
91 selectorKey: 3
92 },
93
94 // where to find CSS files
95 css: {
96 regex: /^public\/css\/([a-z_\-\/]+)\.css$/i
97 }
98 }
99};
100```
101
102
103Then, in your `app.js` (or wherever makes sense to you) you can do something like:
104```javascript
105var Locator = require('locator');
106
107locator = new Locator();
108var resources = locator.parseBundle(__dirname);
109
110// access your "configs/foo.json" configuration file
111... resources.configs.foo ...
112
113// access all your templates
114Object.keys(resources.templates).forEach(function (templateName) {
115 var templateResource = resources.templates[templateName];
116 ...
117});
118```
119
120## Example: Defining Your Own Bundle Name
121In your app's `package.json`:
122```javascript
123{
124 "name": "usually-a-long-name-for-npm"
125 "locator": {
126 "name": "foo"
127 }
128}
129```
130
131By default, locator will select the name of the bundle from the `package.json->name` entry, but you should be able to specify a custom name by adding a `name` entry under the `locator` entry in package.json. This will help to decouple the name of the package from the urls that you will use to fetch those scripts from the client side.
132
133
134## License
135This software is free to use under the Yahoo! Inc. BSD license.
136See the [LICENSE file][] for license text and copyright information.
137
138[LICENSE file]: https://github.com/yahoo/locator/blob/master/LICENSE.txt
139
140
141## Contribute
142See the [CONTRIBUTING.md file][] for information on contributing back to Locator.
143
144[CONTRIBUTING.md file]: https://github.com/yahoo/locator/blob/master/CONTRIBUTING.md