|
| 1 | +/* |
| 2 | + * @secjs/utils |
| 3 | + * |
| 4 | + * (c) João Lenon <lenon@secjs.com.br> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +export class Path { |
| 11 | + private static _tempBuild = null |
| 12 | + private static _forceBuild = false |
| 13 | + private static _defaultBuild = 'dist' |
| 14 | + private static _verifyNodeEnv = true |
| 15 | + |
| 16 | + static forBuild(name: string) { |
| 17 | + this._tempBuild = name |
| 18 | + |
| 19 | + return this |
| 20 | + } |
| 21 | + |
| 22 | + static switchEnvVerify() { |
| 23 | + this._verifyNodeEnv = !this._verifyNodeEnv |
| 24 | + |
| 25 | + return this |
| 26 | + } |
| 27 | + |
| 28 | + static changeBuild(name: string) { |
| 29 | + this._defaultBuild = name |
| 30 | + |
| 31 | + return this |
| 32 | + } |
| 33 | + |
| 34 | + static forceBuild() { |
| 35 | + this._forceBuild = true |
| 36 | + |
| 37 | + return this |
| 38 | + } |
| 39 | + |
| 40 | + static nodeCwdPath() { |
| 41 | + let cwdNodePath = process.cwd() |
| 42 | + |
| 43 | + if (this._tempBuild) { |
| 44 | + cwdNodePath += this.adjustSlashes(this._tempBuild) |
| 45 | + |
| 46 | + this._tempBuild = null |
| 47 | + |
| 48 | + return cwdNodePath |
| 49 | + } |
| 50 | + |
| 51 | + if (this._forceBuild) { |
| 52 | + cwdNodePath += this.adjustSlashes(this._defaultBuild) |
| 53 | + |
| 54 | + this._forceBuild = false |
| 55 | + } |
| 56 | + |
| 57 | + if (!this._forceBuild && this._verifyNodeEnv) { |
| 58 | + if (['ci', 'testing', 'ts-development'].includes(process.env.NODE_ENV)) { |
| 59 | + cwdNodePath += this.adjustSlashes(this._defaultBuild) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return this.adjustSlashes(cwdNodePath) |
| 64 | + } |
| 65 | + |
| 66 | + static pwd(subPath = '/') { |
| 67 | + return `${this.nodeCwdPath()}${this.adjustSlashes(subPath)}` |
| 68 | + } |
| 69 | + |
| 70 | + static app(subPath = '/') { |
| 71 | + return this.pwd('app' + this.adjustSlashes(subPath)) |
| 72 | + } |
| 73 | + |
| 74 | + static logs(subPath = '/') { |
| 75 | + return this.storage('logs' + this.adjustSlashes(subPath)) |
| 76 | + } |
| 77 | + |
| 78 | + static start(subPath = '/') { |
| 79 | + return this.pwd('start' + this.adjustSlashes(subPath)) |
| 80 | + } |
| 81 | + |
| 82 | + static views(subPath = '/') { |
| 83 | + return this.resources('views' + this.adjustSlashes(subPath)) |
| 84 | + } |
| 85 | + |
| 86 | + static config(subPath = '/') { |
| 87 | + return this.pwd('config' + this.adjustSlashes(subPath)) |
| 88 | + } |
| 89 | + |
| 90 | + static tests(subPath = '/') { |
| 91 | + return this.pwd('tests' + this.adjustSlashes(subPath)) |
| 92 | + } |
| 93 | + |
| 94 | + static public(subPath = '/') { |
| 95 | + return this.pwd('public' + this.adjustSlashes(subPath)) |
| 96 | + } |
| 97 | + |
| 98 | + static assets(subPath = '/') { |
| 99 | + return this.public('assets' + this.adjustSlashes(subPath)) |
| 100 | + } |
| 101 | + |
| 102 | + static storage(subPath = '/') { |
| 103 | + return this.pwd('storage' + this.adjustSlashes(subPath)) |
| 104 | + } |
| 105 | + |
| 106 | + static database(subPath = '/') { |
| 107 | + return this.pwd('database' + this.adjustSlashes(subPath)) |
| 108 | + } |
| 109 | + |
| 110 | + static locales(subPath = '/') { |
| 111 | + return this.resources('locales' + this.adjustSlashes(subPath)) |
| 112 | + } |
| 113 | + |
| 114 | + static resources(subPath = '/') { |
| 115 | + return this.pwd('resources' + this.adjustSlashes(subPath)) |
| 116 | + } |
| 117 | + |
| 118 | + static providers(subPath = '/') { |
| 119 | + return this.pwd('providers' + this.adjustSlashes(subPath)) |
| 120 | + } |
| 121 | + |
| 122 | + private static adjustSlashes(path: string) { |
| 123 | + let subPathArray = path.split('/') |
| 124 | + |
| 125 | + subPathArray = subPathArray.filter(p => p !== '') |
| 126 | + subPathArray.unshift('') |
| 127 | + |
| 128 | + return subPathArray.join('/') |
| 129 | + } |
| 130 | +} |
0 commit comments