GithubHelp home page GithubHelp logo

imclab / locator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yahooarchive/locator

0.0 2.0 0.0 744 KB

node module which gives semantic meaning to filesystem paths

License: BSD 3-Clause "New" or "Revised" License

locator's Introduction

locator

The locator gives semantic meaning to files on the filesystem. It does this with a set of "rules" that describes how each file path should be interpreted. In addition it groups files into "bundles". (Each bundle is usually an NPM module, but not always.)

The locator doesn't interpret the semantic meaning of the files. It is up to the user to understand and use the semantic meanings associated with the files.

Build Status

Goals & Design

  • provide an abstraction over filesystem paths
    • set of "rules" (regexps basically) that determine the semantic meaning of each file
    • files that match a rule (and thus have semantic meaning) are called "resources"
    • built-in handling of "selectors", for resources that have multiple versions
  • organize files in bundles
    • bundles are usually NPM modules
    • ...but can be something else, if an NPM module delivers multiple child bundles
    • bundles are recursively walked, since they are often organized in a tree structure on disk
    • bundles can have different types
      • for example, a mojito application bundle is walked differently (uses a different ruleset) than a mojito mojit bundle
      • each bundle can declare its type in its package.json (for those bundles that are NPM modules)
      • 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)
  • configurable
    • the behavior of the locator should be configurable, which should include
    • ...defining new rulesets, for new bundle types
    • ...general runtime behavior configuration of returned values
    • ...etc
  • extensible (plugins)
    • the locator also allows "plugins" to be informed about files/resources/bundles
    • one type of plugin is a "compiler" which takes a file in an unknown format and "compiles" it into a known format
      • for example, a "sass" compiler which generates "css" files
    • other types of plugins can do general processing
      • for example, a "config helper" plugin could load config files and cache their contents in memory
    • other types of plugins can process a bundle as a whole
      • for example, a "yui helper" plugin which can generate YUI loader metadata for the whole bundle

Installation

Install using npm:

$ npm install locator

Example: Mojito Application

In your app's package.json:

{
    "dependencies": {
        ...
        "mojito": "*",
        ...
    },
    "locator": {
        "rulesets": "mojito/locator-rulesets"
    }
}

Example: Defining Your Own Semantics

In your app's package.json:

{
    "locator": {
        "rulesets": "locator-rulesets"
    }
}

A new locator-rulesets.js file which defines how to add semantic meaning to filesystem paths:

module.exports = {
    // nameKey defaults to 1
    // selectorKey has no default. selector is only used if selectorKey is given
    main: {
        // we can use this to skip files
        _skip: [
            /^tests?\b/i
        ],

        // where to find configuration files
        configs: {
            regex: /^configs\/([a-z_\-\/]+)\.(json|js)$/i
        },

        // where to find templates
        templates: {
            regex: /^templates\/([a-z_\-\/]+)(\.([\w_\-]+))?\.[^\.\/]+\.html$/i,
            // We use "selectors" because we might use different templates based
            // on different circumstances.
            selectorKey: 3
        },

        // where to find CSS files
        css: {
            regex: /^public\/css\/([a-z_\-\/]+)\.css$/i
        }
    }
};

Then, in your app.js (or wherever makes sense to you) you can do something like:

var Locator = require('locator');

locator = new Locator();
locator.parseBundle(__dirname).then(function() {
    var resources = locator.getRootBundle().getResources();

    // access your "configs/foo.json" configuration file
    ... resources.configs.foo ...

    // access all your templates
    Object.keys(resources.templates).forEach(function (templateName) {
        var templateResource = resources.templates[templateName];
        ...
    });

    // File watching is optional, and probably only desired when running in
    // a development environment.
    locator.watch(__dirname).then(function () {
        // File watching has started.
        // Actual changes will be reflected into the results of `getBundle()`,
        // `getRootBundle()`, `listAllResources()`, and `listBundleNames()`.
        // Changes are also reported (as they happen) to the locator plugins.
    });
});

License

This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.

Contribute

See the CONTRIBUTING.md file for information on contributing back to Locator.

locator's People

Contributors

caridy avatar drewfish avatar ericf avatar imalberto avatar juandopazo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.