|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | + |
| 4 | +const html = (__dirname + '/temp.html'); |
| 5 | + |
| 6 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes |
| 7 | +class BasicNodeModule { |
| 8 | + |
| 9 | + constructor(options = {}) { |
| 10 | + |
| 11 | + this.defaults = { |
| 12 | + red: 'red', |
| 13 | + green: 'green' |
| 14 | + }; |
| 15 | + |
| 16 | + if ( ! BasicNodeModule.instance) { |
| 17 | + |
| 18 | + BasicNodeModule.instance = this; |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + this.defaults = { |
| 23 | + ...this.defaults, |
| 24 | + ...options |
| 25 | + }; |
| 26 | + |
| 27 | + return BasicNodeModule.instance; |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + static of(options) { |
| 32 | + |
| 33 | + const basicNodeModule = new BasicNodeModule(); |
| 34 | + |
| 35 | + basicNodeModule.defaults = { |
| 36 | + ...basicNodeModule.defaults, |
| 37 | + ...options |
| 38 | + }; |
| 39 | + |
| 40 | + console.log(basicNodeModule.defaults) |
| 41 | + |
| 42 | + return basicNodeModule; |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + async run() { |
| 47 | + |
| 48 | + //console.log('ddd', this.options); |
| 49 | + |
| 50 | + // console.log(options); |
| 51 | + // |
| 52 | + // if (options.foo && options.bar) { |
| 53 | + // |
| 54 | + // await fs.writeFile(html, 'Hello world!', 'utf8'); |
| 55 | + // |
| 56 | + // let result = await fs.readFile(html, 'utf8'); |
| 57 | + // |
| 58 | + // await fs.unlink(html); |
| 59 | + // |
| 60 | + // return result; |
| 61 | + // |
| 62 | + // } else { |
| 63 | + // |
| 64 | + // throw new Error('missing foo AND bar'); |
| 65 | + // |
| 66 | + // } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +// These options come from `require()({ … options … })` syntax: |
| 73 | +module.exports = (options = {}) => { |
| 74 | + |
| 75 | + if (Object.entries(options).length) { |
| 76 | + |
| 77 | + new BasicNodeModule(options); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + return BasicNodeModule.of |
| 82 | + |
| 83 | +}; |
0 commit comments