{"id":19806639,"url":"https://github.com/timnew/approot","last_synced_at":"2025-07-26T21:11:22.208Z","repository":{"id":18589796,"uuid":"21794023","full_name":"timnew/approot","owner":"timnew","description":"A helper class to build file path. Useful in application, to provide path reference to whole app","archived":false,"fork":false,"pushed_at":"2016-07-08T09:56:04.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-11T06:24:11.611Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/timnew/approot","language":"CoffeeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timnew.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-13T15:52:47.000Z","updated_at":"2016-07-08T09:56:05.000Z","dependencies_parsed_at":"2022-09-11T19:11:33.836Z","dependency_job_id":null,"html_url":"https://github.com/timnew/approot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fapproot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fapproot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fapproot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fapproot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timnew","download_url":"https://codeload.github.com/timnew/approot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241150393,"owners_count":19918350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T09:08:04.295Z","updated_at":"2025-02-28T12:24:21.606Z","avatar_url":"https://github.com/timnew.png","language":"CoffeeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"approot [![NPM version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Dependency Status][depstat-image]][depstat-url]\n================\n\n\u003e An utility to build path based on a base path. Useful in node.js application, such as web app server.\n\u003e Recommend to instantiate an `approot` in global namespace, to provide an specific reference path across all files in the project\n\n## Install\n\nInstall using [npm][npm-url].\n\n    $ npm install approot\n\n## Usage\n\nSuppose the code is located in `/var/application`\n```javascript\n// build approot based on __dirname, and __dirname is /var/application\nvar approot = require('approot')(__dirname);\n\n```\n\n**HINT:** It is recommended to expose `approot` in global, which will be helpful to share the path across the whole javascript files.\n```javascript\nglobal.approot = require('approot')(__dirname);\n```\n\n### Basic Usage\n\n```javascript\napproot()                         // return /var/application\napproot('models')                 // return /var/application/models\napproot('models', 'user.js')      // return /var/application/models/user.js\n\n```\n### Consolidate\n\n`consolidate` can inject sub-directories automatically, which is still a `approot` function.\n\n```javascript\napproot.consolidate();            // Scan then inject all subdirectories automatically\napproot.models()                  // return /var/application/models, exists after consolidate is called\napproot.models('user.js')         // return /var/application/models/user.js\n\n```\n\n**HINT:** since `consolidate` method return the `approot` itself, so you can use in this way:\n\n```javascript\nvar approot = require('approot')(__dirname).consolidate();\n```\n\n### Consolidate with arguments\n`consolidate` with arguments will extend generate `approot` instance as argument **regardeless** the file system.\nDue to this nature, it can be used in browser environment.\n\n```javascript\nvar rootPath = '/'\nvar approot;\n\napproot = require('approot')(rootPath);\napproot()                         // return '/'\n\napproot = require('approot')(rootPath).consolidate('models');\napproot.models()                  // return '/models'\n\napproot = require('approot')(rootPath).consolidate(['models', 'routes']);\napproot.models()                  // return '/models'\napproot.routes()                  // return '/routes'\n\napproot = require('approot')(rootPath).consolidate({models: true, routes: 'admin', assets: ['js', 'css'], config: { credential: 'secret' }});\napproot.models()                  // return '/models'\napproot.routes()                  // return '/routes'\napproot.routes.admin()            // return '/routes/admin'\napproot.assets()                  // return '/routes/assets'\napproot.assets.js()               // return '/routes/assets/js'\napproot.assets.css()              // return '/routes/assets/css'\napproot.config()                  // return '/routes/config'\napproot.config.credential()       // return '/routes/config/credential'\napproot.config.credential.secret()// return '/routes/config/credential/secret'\n```\n\n### List Children\n\n```javascript\napproot.listChildren()                  // list all files and folders in current folder\napproot.listChildren('sub', 'grandsub') // list all files and folders in './sub/grandsub'\n\n```\n\n### Relative Require\n\n`require` method is a syntactic sugar that make `require` a little bit easier\n\n```javascript\n// suppose approot is create somewhere before\nvar User = approot.models.require('user'); // Equivalent to \"require(approot.models('user'))\"\n```\n\n### Browser Support\n\n`approot` can be used in bowser with the help of [Browserify] or [Webpack]. But there are some limitations due to lack of `fs` API support.\n\nWhen used in browser, `approot` cannot scan file system automatically, so the API behavior changes in following circumstances:\n\n* Use `consolidate` **without arguments** does not scan the file system automatically, it just return itself. Use `consolidate` with **arguments** instead.\n* `listChildren` **always** returns a empty array\n\n## Used with [lazily-require]\n\nWhen used in conjunction of [lazily-require] to initialize the application environment.\n\n```javascript\n// initEnv.js\nvar lazy = require('lazily-require');\n\n// suppose __dirname is /var/application\nglobal.appRoot = require('approot')(__dirname).consolidate(); // expose the approot to global and consolidate first-layer directories\n\nglobal.configuration = require(appRoot.config('configuration'));\n\nglobal.Services = lazy appRoot.services();\nglobal.Routes = lazy appRoot.routes();\nglobal.Records = lazy appRoot.records();\nglobal.Models = lazy appRoot.models();\nglobal.Entities = lazy appRoot.entities();\n\n// a differnt javascript file\nvar User = Models.User; // var User = require('/var/application/models/User');\n```\n\n## License\nMIT\n\n[![NPM downloads][npm-downloads]][npm-url]\n\n[homepage]: https://github.com/timnew/approot\n\n[npm-url]: https://npmjs.org/package/approot\n[npm-image]: http://img.shields.io/npm/v/approot.svg?style=flat\n[npm-downloads]: http://img.shields.io/npm/dm/approot.svg?style=flat\n\n[ci-url]: https://drone.io/github.com/timnew/approot/latest\n[ci-image]: https://drone.io/github.com/timnew/approot/status.png\n\n[depstat-url]: https://gemnasium.com/timnew/approot\n[depstat-image]: http://img.shields.io/gemnasium/timnew/approot.svg?style=flat\n\n[lazily-require]: https://github.com/timnew/lazily-require\n[Browserify]: http://browserify.org/\n[Webpack]: http://webpack.github.io/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimnew%2Fapproot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimnew%2Fapproot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimnew%2Fapproot/lists"}