You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
16 KiB

4 years ago
  1. <p align="center">
  2. <a href="https://github.com/node-base/base">
  3. <img height="250" width="250" src="https://raw.githubusercontent.com/node-base/base/master/docs/logo.png">
  4. </a>
  5. </p>
  6. # base [![NPM version](https://img.shields.io/npm/v/base.svg?style=flat)](https://www.npmjs.com/package/base) [![NPM monthly downloads](https://img.shields.io/npm/dm/base.svg?style=flat)](https://npmjs.org/package/base) [![NPM total downloads](https://img.shields.io/npm/dt/base.svg?style=flat)](https://npmjs.org/package/base) [![Linux Build Status](https://img.shields.io/travis/node-base/base.svg?style=flat&label=Travis)](https://travis-ci.org/node-base/base)
  7. > base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.
  8. ## Install
  9. Install with [npm](https://www.npmjs.com/):
  10. ```sh
  11. $ npm install --save base
  12. ```
  13. ## What is Base?
  14. Base is a framework for rapidly creating high quality node.js applications, using plugins like building blocks.
  15. ### Guiding principles
  16. The core team follows these principles to help guide API decisions:
  17. * **Compact API surface**: The smaller the API surface, the easier the library will be to learn and use.
  18. * **Easy to extend**: Implementors can use any npm package, and write plugins in pure JavaScript. If you're building complex apps, Base simplifies inheritance.
  19. * **Easy to test**: No special setup should be required to unit test `Base` or base plugins
  20. ### Minimal API surface
  21. [The API](#api) was designed to provide only the minimum necessary functionality for creating a useful application, with or without [plugins](#plugins).
  22. **Base core**
  23. Base itself ships with only a handful of [useful methods](#api), such as:
  24. * `.set`: for setting values on the instance
  25. * `.get`: for getting values from the instance
  26. * `.has`: to check if a property exists on the instance
  27. * `.define`: for setting non-enumerable values on the instance
  28. * `.use`: for adding plugins
  29. **Be generic**
  30. When deciding on method to add or remove, we try to answer these questions:
  31. 1. Will all or most Base applications need this method?
  32. 2. Will this method encourage practices or enforce conventions that are beneficial to implementors?
  33. 3. Can or should this be done in a plugin instead?
  34. ### Composability
  35. **Plugin system**
  36. It couldn't be easier to extend Base with any features or custom functionality you can think of.
  37. Base plugins are just functions that take an instance of `Base`:
  38. ```js
  39. var base = new Base();
  40. function plugin(base) {
  41. // do plugin stuff, in pure JavaScript
  42. }
  43. // use the plugin
  44. base.use(plugin);
  45. ```
  46. **Inheritance**
  47. Easily inherit Base using `.extend`:
  48. ```js
  49. var Base = require('base');
  50. function MyApp() {
  51. Base.call(this);
  52. }
  53. Base.extend(MyApp);
  54. var app = new MyApp();
  55. app.set('a', 'b');
  56. app.get('a');
  57. //=> 'b';
  58. ```
  59. **Inherit or instantiate with a namespace**
  60. By default, the `.get`, `.set` and `.has` methods set and get values from the root of the `base` instance. You can customize this using the `.namespace` method exposed on the exported function. For example:
  61. ```js
  62. var Base = require('base');
  63. // get and set values on the `base.cache` object
  64. var base = Base.namespace('cache');
  65. var app = base();
  66. app.set('foo', 'bar');
  67. console.log(app.cache.foo);
  68. //=> 'bar'
  69. ```
  70. ## API
  71. **Usage**
  72. ```js
  73. var Base = require('base');
  74. var app = new Base();
  75. app.set('foo', 'bar');
  76. console.log(app.foo);
  77. //=> 'bar'
  78. ```
  79. ### [Base](index.js#L44)
  80. Create an instance of `Base` with the given `config` and `options`.
  81. **Params**
  82. * `config` **{Object}**: If supplied, this object is passed to [cache-base](https://github.com/jonschlinkert/cache-base) to merge onto the the instance upon instantiation.
  83. * `options` **{Object}**: If supplied, this object is used to initialize the `base.options` object.
  84. **Example**
  85. ```js
  86. // initialize with `config` and `options`
  87. var app = new Base({isApp: true}, {abc: true});
  88. app.set('foo', 'bar');
  89. // values defined with the given `config` object will be on the root of the instance
  90. console.log(app.baz); //=> undefined
  91. console.log(app.foo); //=> 'bar'
  92. // or use `.get`
  93. console.log(app.get('isApp')); //=> true
  94. console.log(app.get('foo')); //=> 'bar'
  95. // values defined with the given `options` object will be on `app.options
  96. console.log(app.options.abc); //=> true
  97. ```
  98. ### [.is](index.js#L107)
  99. Set the given `name` on `app._name` and `app.is*` properties. Used for doing lookups in plugins.
  100. **Params**
  101. * `name` **{String}**
  102. * `returns` **{Boolean}**
  103. **Example**
  104. ```js
  105. app.is('foo');
  106. console.log(app._name);
  107. //=> 'foo'
  108. console.log(app.isFoo);
  109. //=> true
  110. app.is('bar');
  111. console.log(app.isFoo);
  112. //=> true
  113. console.log(app.isBar);
  114. //=> true
  115. console.log(app._name);
  116. //=> 'bar'
  117. ```
  118. ### [.isRegistered](index.js#L145)
  119. Returns true if a plugin has already been registered on an instance.
  120. Plugin implementors are encouraged to use this first thing in a plugin
  121. to prevent the plugin from being called more than once on the same
  122. instance.
  123. **Params**
  124. * `name` **{String}**: The plugin name.
  125. * `register` **{Boolean}**: If the plugin if not already registered, to record it as being registered pass `true` as the second argument.
  126. * `returns` **{Boolean}**: Returns true if a plugin is already registered.
  127. **Events**
  128. * `emits`: `plugin` Emits the name of the plugin being registered. Useful for unit tests, to ensure plugins are only registered once.
  129. **Example**
  130. ```js
  131. var base = new Base();
  132. base.use(function(app) {
  133. if (app.isRegistered('myPlugin')) return;
  134. // do stuff to `app`
  135. });
  136. // to also record the plugin as being registered
  137. base.use(function(app) {
  138. if (app.isRegistered('myPlugin', true)) return;
  139. // do stuff to `app`
  140. });
  141. ```
  142. ### [.use](index.js#L175)
  143. Define a plugin function to be called immediately upon init. Plugins are chainable and expose the following arguments to the plugin function:
  144. * `app`: the current instance of `Base`
  145. * `base`: the [first ancestor instance](#base) of `Base`
  146. **Params**
  147. * `fn` **{Function}**: plugin function to call
  148. * `returns` **{Object}**: Returns the item instance for chaining.
  149. **Example**
  150. ```js
  151. var app = new Base()
  152. .use(foo)
  153. .use(bar)
  154. .use(baz)
  155. ```
  156. ### [.define](index.js#L197)
  157. The `.define` method is used for adding non-enumerable property on the instance. Dot-notation is **not supported** with `define`.
  158. **Params**
  159. * `key` **{String}**: The name of the property to define.
  160. * `value` **{any}**
  161. * `returns` **{Object}**: Returns the instance for chaining.
  162. **Example**
  163. ```js
  164. // arbitrary `render` function using lodash `template`
  165. app.define('render', function(str, locals) {
  166. return _.template(str)(locals);
  167. });
  168. ```
  169. ### [.mixin](index.js#L222)
  170. Mix property `key` onto the Base prototype. If base is inherited using `Base.extend` this method will be overridden by a new `mixin` method that will only add properties to the prototype of the inheriting application.
  171. **Params**
  172. * `key` **{String}**
  173. * `val` **{Object|Array}**
  174. * `returns` **{Object}**: Returns the `base` instance for chaining.
  175. **Example**
  176. ```js
  177. app.mixin('foo', function() {
  178. // do stuff
  179. });
  180. ```
  181. ### [.base](index.js#L268)
  182. Getter/setter used when creating nested instances of `Base`, for storing a reference to the first ancestor instance. This works by setting an instance of `Base` on the `parent` property of a "child" instance. The `base` property defaults to the current instance if no `parent` property is defined.
  183. **Example**
  184. ```js
  185. // create an instance of `Base`, this is our first ("base") instance
  186. var first = new Base();
  187. first.foo = 'bar'; // arbitrary property, to make it easier to see what's happening later
  188. // create another instance
  189. var second = new Base();
  190. // create a reference to the first instance (`first`)
  191. second.parent = first;
  192. // create another instance
  193. var third = new Base();
  194. // create a reference to the previous instance (`second`)
  195. // repeat this pattern every time a "child" instance is created
  196. third.parent = second;
  197. // we can always access the first instance using the `base` property
  198. console.log(first.base.foo);
  199. //=> 'bar'
  200. console.log(second.base.foo);
  201. //=> 'bar'
  202. console.log(third.base.foo);
  203. //=> 'bar'
  204. // and now you know how to get to third base ;)
  205. ```
  206. ### [#use](index.js#L293)
  207. Static method for adding global plugin functions that will be added to an instance when created.
  208. **Params**
  209. * `fn` **{Function}**: Plugin function to use on each instance.
  210. * `returns` **{Object}**: Returns the `Base` constructor for chaining
  211. **Example**
  212. ```js
  213. Base.use(function(app) {
  214. app.foo = 'bar';
  215. });
  216. var app = new Base();
  217. console.log(app.foo);
  218. //=> 'bar'
  219. ```
  220. ### [#extend](index.js#L337)
  221. Static method for inheriting the prototype and static methods of the `Base` class. This method greatly simplifies the process of creating inheritance-based applications. See [static-extend](https://github.com/jonschlinkert/static-extend) for more details.
  222. **Params**
  223. * `Ctor` **{Function}**: constructor to extend
  224. * `methods` **{Object}**: Optional prototype properties to mix in.
  225. * `returns` **{Object}**: Returns the `Base` constructor for chaining
  226. **Example**
  227. ```js
  228. var extend = cu.extend(Parent);
  229. Parent.extend(Child);
  230. // optional methods
  231. Parent.extend(Child, {
  232. foo: function() {},
  233. bar: function() {}
  234. });
  235. ```
  236. ### [#mixin](index.js#L379)
  237. Used for adding methods to the `Base` prototype, and/or to the prototype of child instances. When a mixin function returns a function, the returned function is pushed onto the `.mixins` array, making it available to be used on inheriting classes whenever `Base.mixins()` is called (e.g. `Base.mixins(Child)`).
  238. **Params**
  239. * `fn` **{Function}**: Function to call
  240. * `returns` **{Object}**: Returns the `Base` constructor for chaining
  241. **Example**
  242. ```js
  243. Base.mixin(function(proto) {
  244. proto.foo = function(msg) {
  245. return 'foo ' + msg;
  246. };
  247. });
  248. ```
  249. ### [#mixins](index.js#L401)
  250. Static method for running global mixin functions against a child constructor. Mixins must be registered before calling this method.
  251. **Params**
  252. * `Child` **{Function}**: Constructor function of a child class
  253. * `returns` **{Object}**: Returns the `Base` constructor for chaining
  254. **Example**
  255. ```js
  256. Base.extend(Child);
  257. Base.mixins(Child);
  258. ```
  259. ### [#inherit](index.js#L420)
  260. Similar to `util.inherit`, but copies all static properties, prototype properties, and getters/setters from `Provider` to `Receiver`. See [class-utils](https://github.com/jonschlinkert/class-utils#inherit) for more details.
  261. **Params**
  262. * `Receiver` **{Function}**: Receiving (child) constructor
  263. * `Provider` **{Function}**: Providing (parent) constructor
  264. * `returns` **{Object}**: Returns the `Base` constructor for chaining
  265. **Example**
  266. ```js
  267. Base.inherit(Foo, Bar);
  268. ```
  269. ## In the wild
  270. The following node.js applications were built with `Base`:
  271. * [assemble](https://github.com/assemble/assemble)
  272. * [verb](https://github.com/verbose/verb)
  273. * [generate](https://github.com/generate/generate)
  274. * [scaffold](https://github.com/jonschlinkert/scaffold)
  275. * [boilerplate](https://github.com/jonschlinkert/boilerplate)
  276. ## Test coverage
  277. ```
  278. Statements : 98.91% ( 91/92 )
  279. Branches : 92.86% ( 26/28 )
  280. Functions : 100% ( 17/17 )
  281. Lines : 98.9% ( 90/91 )
  282. ```
  283. ## History
  284. ### v0.11.2
  285. * fixes https://github.com/micromatch/micromatch/issues/99
  286. ### v0.11.0
  287. **Breaking changes**
  288. * Static `.use` and `.run` methods are now non-enumerable
  289. ### v0.9.0
  290. **Breaking changes**
  291. * `.is` no longer takes a function, a string must be passed
  292. * all remaining `.debug` code has been removed
  293. * `app._namespace` was removed (related to `debug`)
  294. * `.plugin`, `.use`, and `.define` no longer emit events
  295. * `.assertPlugin` was removed
  296. * `.lazy` was removed
  297. ## About
  298. ### Related projects
  299. * [base-cwd](https://www.npmjs.com/package/base-cwd): Base plugin that adds a getter/setter for the current working directory. | [homepage](https://github.com/node-base/base-cwd "Base plugin that adds a getter/setter for the current working directory.")
  300. * [base-data](https://www.npmjs.com/package/base-data): adds a `data` method to base-methods. | [homepage](https://github.com/node-base/base-data "adds a `data` method to base-methods.")
  301. * [base-fs](https://www.npmjs.com/package/base-fs): base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file… [more](https://github.com/node-base/base-fs) | [homepage](https://github.com/node-base/base-fs "base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file system, like src, dest, copy and symlink.")
  302. * [base-generators](https://www.npmjs.com/package/base-generators): Adds project-generator support to your `base` application. | [homepage](https://github.com/node-base/base-generators "Adds project-generator support to your `base` application.")
  303. * [base-option](https://www.npmjs.com/package/base-option): Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme… [more](https://github.com/node-base/base-option) | [homepage](https://github.com/node-base/base-option "Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme for the full API.")
  304. * [base-pipeline](https://www.npmjs.com/package/base-pipeline): base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines. | [homepage](https://github.com/node-base/base-pipeline "base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines.")
  305. * [base-pkg](https://www.npmjs.com/package/base-pkg): Plugin for adding a `pkg` method that exposes pkg-store to your base application. | [homepage](https://github.com/node-base/base-pkg "Plugin for adding a `pkg` method that exposes pkg-store to your base application.")
  306. * [base-plugins](https://www.npmjs.com/package/base-plugins): Adds 'smart plugin' support to your base application. | [homepage](https://github.com/node-base/base-plugins "Adds 'smart plugin' support to your base application.")
  307. * [base-questions](https://www.npmjs.com/package/base-questions): Plugin for base-methods that adds methods for prompting the user and storing the answers on… [more](https://github.com/node-base/base-questions) | [homepage](https://github.com/node-base/base-questions "Plugin for base-methods that adds methods for prompting the user and storing the answers on a project-by-project basis.")
  308. * [base-store](https://www.npmjs.com/package/base-store): Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object… [more](https://github.com/node-base/base-store) | [homepage](https://github.com/node-base/base-store "Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object that exposes all of the methods from the data-store library. Also now supports sub-stores!")
  309. * [base-task](https://www.npmjs.com/package/base-task): base plugin that provides a very thin wrapper around [https://github.com/doowb/composer](https://github.com/doowb/composer) for adding task methods to… [more](https://github.com/node-base/base-task) | [homepage](https://github.com/node-base/base-task "base plugin that provides a very thin wrapper around <https://github.com/doowb/composer> for adding task methods to your application.")
  310. ### Contributing
  311. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  312. ### Contributors
  313. | **Commits** | **Contributor** |
  314. | --- | --- |
  315. | 141 | [jonschlinkert](https://github.com/jonschlinkert) |
  316. | 30 | [doowb](https://github.com/doowb) |
  317. | 3 | [charlike](https://github.com/charlike) |
  318. | 1 | [criticalmash](https://github.com/criticalmash) |
  319. | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
  320. ### Building docs
  321. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  322. To generate the readme, run the following command:
  323. ```sh
  324. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  325. ```
  326. ### Running tests
  327. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  328. ```sh
  329. $ npm install && npm test
  330. ```
  331. ### Author
  332. **Jon Schlinkert**
  333. * [github/jonschlinkert](https://github.com/jonschlinkert)
  334. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  335. ### License
  336. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  337. Released under the [MIT License](LICENSE).
  338. ***
  339. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 07, 2017._