Add chalk factory method
This commit is contained in:
parent
8cb6c683cb
commit
ed509bcf0b
1 changed files with 18 additions and 14 deletions
32
index.js
32
index.js
|
|
@ -26,27 +26,31 @@ function applyOptions(object, options = {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ChalkClass {
|
class ChalkClass {
|
||||||
constructor (options) {
|
constructor(options) {
|
||||||
return Chalk(options)
|
return chalkFactory(options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function chalkFactory(options) {
|
||||||
|
const chalk = {};
|
||||||
|
applyOptions(chalk, options);
|
||||||
|
|
||||||
|
chalk.template = (...args) => chalkTag(chalk.template, ...args);
|
||||||
|
|
||||||
|
Object.setPrototypeOf(chalk, Chalk.prototype);
|
||||||
|
Object.setPrototypeOf(chalk.template, chalk);
|
||||||
|
|
||||||
|
chalk.template.constructor = Chalk;
|
||||||
|
chalk.template.instance = ChalkClass;
|
||||||
|
|
||||||
|
return chalk.template;
|
||||||
|
}
|
||||||
|
|
||||||
function Chalk(options) {
|
function Chalk(options) {
|
||||||
// We check for this.template here since calling `chalk.constructor()`
|
// We check for this.template here since calling `chalk.constructor()`
|
||||||
// by itself will have a `this` of a previously constructed chalk object
|
// by itself will have a `this` of a previously constructed chalk object
|
||||||
if (!this || !(this instanceof Chalk) || this.template) {
|
if (!this || !(this instanceof Chalk) || this.template) {
|
||||||
const chalk = {};
|
return chalkFactory(options);
|
||||||
applyOptions(chalk, options);
|
|
||||||
|
|
||||||
chalk.template = (...args) => chalkTag(chalk.template, ...args);
|
|
||||||
|
|
||||||
Object.setPrototypeOf(chalk, Chalk.prototype);
|
|
||||||
Object.setPrototypeOf(chalk.template, chalk);
|
|
||||||
|
|
||||||
chalk.template.constructor = Chalk;
|
|
||||||
chalk.template.instance = ChalkClass;
|
|
||||||
|
|
||||||
return chalk.template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
applyOptions(this, options);
|
applyOptions(this, options);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue