diff --git a/package-lock.json b/package-lock.json index baa987d..a7a151e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "3.5.1", + "version": "3.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "3.5.1", + "version": "3.6.0", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0", @@ -16,7 +16,7 @@ "change-case": "^4.1.2", "collect.js": "^4.34.3", "debug": "^4.3.4", - "fastify": "^4.14.1", + "fastify": "^4.15.0", "got": "^12.6.0", "http-status-codes": "^2.2.0", "is-wsl": "^2.2.0", @@ -3947,17 +3947,17 @@ "integrity": "sha512-cIusKBIt/R/oI6z/1nyfe2FvGKVTohVRfvkOhvx0nCEW+xf5NoCXjAHcWp93uOUBchzYcsvPlrapAdX1uW+YGg==" }, "node_modules/fastify": { - "version": "4.14.1", - "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.14.1.tgz", - "integrity": "sha512-yjrDeXe77j9gRlSV2UJry8mcFWbD0NQ5JYjnPi4tkFjHZVaG3/BD5wxOmRzGnHPC0YvaBJ0XWrIfFPl2IHRa1w==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.15.0.tgz", + "integrity": "sha512-m/CaRN8nf5uyYdrDe2qqq+0z3oGyE+A++qlKQoLJTI4WI0nWK9D6R3FxXQ3MVwt/md977GMR4F43pE9oqrS2zw==", "dependencies": { "@fastify/ajv-compiler": "^3.5.0", "@fastify/error": "^3.0.0", - "@fastify/fast-json-stringify-compiler": "^4.1.0", + "@fastify/fast-json-stringify-compiler": "^4.2.0", "abstract-logging": "^2.0.1", "avvio": "^8.2.0", "fast-content-type-parse": "^1.0.0", - "find-my-way": "^7.3.0", + "find-my-way": "^7.6.0", "light-my-request": "^5.6.1", "pino": "^8.5.0", "process-warning": "^2.0.0", @@ -4061,9 +4061,9 @@ } }, "node_modules/find-my-way": { - "version": "7.3.1", - "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-7.3.1.tgz", - "integrity": "sha512-kGvM08SOkqvheLcuQ8GW9t/H901Qb9rZEbcNWbXopzy4jDRoaJpJoObPSKf4MnQLZ20ZTp7rL5MpF6rf+pqmyg==", + "version": "7.6.0", + "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-7.6.0.tgz", + "integrity": "sha512-H7berWdHJ+5CNVr4ilLWPai4ml7Y2qAsxjw3pfeBxPigZmaDTzF0wjJLj90xRCmGcWYcyt050yN+34OZDJm1eQ==", "dependencies": { "fast-deep-equal": "^3.1.3", "fast-querystring": "^1.0.0", @@ -11490,17 +11490,17 @@ "integrity": "sha512-cIusKBIt/R/oI6z/1nyfe2FvGKVTohVRfvkOhvx0nCEW+xf5NoCXjAHcWp93uOUBchzYcsvPlrapAdX1uW+YGg==" }, "fastify": { - "version": "4.14.1", - "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.14.1.tgz", - "integrity": "sha512-yjrDeXe77j9gRlSV2UJry8mcFWbD0NQ5JYjnPi4tkFjHZVaG3/BD5wxOmRzGnHPC0YvaBJ0XWrIfFPl2IHRa1w==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/fastify/-/fastify-4.15.0.tgz", + "integrity": "sha512-m/CaRN8nf5uyYdrDe2qqq+0z3oGyE+A++qlKQoLJTI4WI0nWK9D6R3FxXQ3MVwt/md977GMR4F43pE9oqrS2zw==", "requires": { "@fastify/ajv-compiler": "^3.5.0", "@fastify/error": "^3.0.0", - "@fastify/fast-json-stringify-compiler": "^4.1.0", + "@fastify/fast-json-stringify-compiler": "^4.2.0", "abstract-logging": "^2.0.1", "avvio": "^8.2.0", "fast-content-type-parse": "^1.0.0", - "find-my-way": "^7.3.0", + "find-my-way": "^7.6.0", "light-my-request": "^5.6.1", "pino": "^8.5.0", "process-warning": "^2.0.0", @@ -11581,9 +11581,9 @@ } }, "find-my-way": { - "version": "7.3.1", - "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-7.3.1.tgz", - "integrity": "sha512-kGvM08SOkqvheLcuQ8GW9t/H901Qb9rZEbcNWbXopzy4jDRoaJpJoObPSKf4MnQLZ20ZTp7rL5MpF6rf+pqmyg==", + "version": "7.6.0", + "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-7.6.0.tgz", + "integrity": "sha512-H7berWdHJ+5CNVr4ilLWPai4ml7Y2qAsxjw3pfeBxPigZmaDTzF0wjJLj90xRCmGcWYcyt050yN+34OZDJm1eQ==", "requires": { "fast-deep-equal": "^3.1.3", "fast-querystring": "^1.0.0", diff --git a/package.json b/package.json index b6d101d..53cac68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "3.5.1", + "version": "3.6.0", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", @@ -51,7 +51,7 @@ "change-case": "^4.1.2", "collect.js": "^4.34.3", "debug": "^4.3.4", - "fastify": "^4.14.1", + "fastify": "^4.15.0", "got": "^12.6.0", "http-status-codes": "^2.2.0", "is-wsl": "^2.2.0", diff --git a/src/Helpers/Path.ts b/src/Helpers/Path.ts index 1071c36..3230672 100644 --- a/src/Helpers/Path.ts +++ b/src/Helpers/Path.ts @@ -11,32 +11,52 @@ import callSite from 'callsite' import { fileURLToPath } from 'node:url' import { homedir, tmpdir } from 'node:os' -import { dirname, normalize, sep } from 'node:path' +import { PathDirs } from '#src/Types/PathDirs' +import { sep, normalize, dirname } from 'node:path' export class Path { - /** - * Set a default beforePath for all Path methods that - * use Path.pwd. - */ - public static defaultBeforePath = '' - - /** - * Resolve the environment where the application - * is running by verifying the import.meta.url. - * - * This method will auto set the IS_TS env and the - * defaultBeforePath if IS_TS is true. - * - * The beforePath is always set as '/build' by default. - */ - public static resolveEnvironment( - metaUrl: string, - beforePath = '', - ): typeof Path { - const isTs = metaUrl.endsWith('.ts') ? 'true' : 'false' - - process.env.IS_TS = process.env.IS_TS || isTs - this.defaultBeforePath = process.env.IS_TS === 'true' ? '' : beforePath + public static dirs: PathDirs = { + bin: 'bin', + src: 'src', + app: 'app', + services: 'app/Services', + exceptions: 'app/Exceptions', + repositories: 'app/Repositories', + console: 'app/Console', + commands: 'app/Console/Commands', + http: 'app/Http', + controllers: 'app/Http/Controllers', + middlewares: 'app/Http/Middlewares', + interceptors: 'app/Http/Interceptors', + terminators: 'app/Http/Terminators', + bootstrap: 'bootstrap', + config: 'config', + database: 'database', + seeders: 'database/seeders', + migrations: 'database/migrations', + lang: 'lang', + resources: 'resources', + views: 'resources/views', + locales: 'resources/locales', + nodeModules: 'node_modules', + nodeModulesBin: 'node_modules/.bin', + providers: 'providers', + facades: 'providers/facades', + public: 'public', + static: 'public/static', + assets: 'public/assets', + routes: 'routes', + storage: 'storage', + logs: 'storage/logs', + tests: 'tests', + stubs: 'tests/Stubs', + } + + /** + * Merge your custom directories with the default directories. + */ + public static mergeDirs(dirs: PathDirs): typeof Path { + this.dirs = { ...this.dirs, ...dirs } return this } @@ -58,207 +78,556 @@ export class Path { } /** - * Return the original pwd path of your project without the defaultBeforePath. + * Return the pwd path of your project. */ - public static originalPwd(subPath = sep): string { + public static pwd(subPath = sep): string { const pwd = normalize(`${process.cwd()}${sep}${normalize(subPath)}`) return this.removeSlashes(pwd) } /** - * Return the pwd path of your project. + * Return the src path of your project. */ - public static pwd(subPath = sep): string { - if (Path.defaultBeforePath) { - subPath = normalize(Path.defaultBeforePath).concat(sep, subPath) - } + public static src(subPath = sep): string { + return this.pwd(normalize(this.dirs.src) + sep + normalize(subPath)) + } - const pwd = normalize(`${process.cwd()}${sep}${normalize(subPath)}`) + /** + * Set the directory of src folder. + */ + public static setSrc(directory: string): typeof Path { + this.dirs.src = directory - return this.removeSlashes(pwd) + return this } /** - * Return the src path of your project. + * Return the bin path of your project. */ - public static src(subPath = sep): string { - return this.pwd('src' + sep + normalize(subPath)) + public static bin(subPath = sep): string { + return this.pwd(normalize(this.dirs.bin) + sep + normalize(subPath)) + } + + /** + * Set the directory of bin folder. + */ + public static setBin(directory: string): typeof Path { + this.dirs.bin = directory + + return this } /** * Return the app path of your project. */ public static app(subPath = sep): string { - return this.pwd('app' + sep + normalize(subPath)) + return this.pwd(normalize(this.dirs.app) + sep + normalize(subPath)) + } + + /** + * Set the directory of app folder. + */ + public static setApp(directory: string): typeof Path { + this.dirs.app = directory + + return this } /** * Return the bootstrap path of your project. */ public static bootstrap(subPath = sep): string { - return this.pwd('bootstrap' + sep + normalize(subPath)) + return this.pwd(normalize(this.dirs.bootstrap) + sep + normalize(subPath)) + } + + /** + * Set the directory of bootstrap folder. + */ + public static setBootstrap(directory: string): typeof Path { + this.dirs.bootstrap = directory + + return this } /** * Return the config path of your project. */ public static config(subPath = sep): string { - return this.pwd('config' + sep + normalize(subPath)) + return this.pwd(normalize(this.dirs.config) + sep + normalize(subPath)) + } + + /** + * Set the directory of config folder. + */ + public static setConfig(directory: string): typeof Path { + this.dirs.config = directory + + return this } /** * Return the database path of your project. */ public static database(subPath = sep): string { - return this.pwd('database' + sep + normalize(subPath)) + return this.pwd(this.dirs.database + sep + normalize(subPath)) + } + + /** + * Set the directory of database folder. + */ + public static setDatabase(directory: string): typeof Path { + this.dirs.database = directory + + return this } /** * Return the lang path of your project. */ public static lang(subPath = sep): string { - return this.pwd('lang' + sep + normalize(subPath)) + return this.pwd(this.dirs.lang + sep + normalize(subPath)) + } + + /** + * Set the directory of lang folder. + */ + public static setLang(directory: string): typeof Path { + this.dirs.lang = directory + + return this } /** * Return the node_modules path of your project. */ public static nodeModules(subPath = sep): string { - return this.pwd('node_modules' + sep + normalize(subPath)) + return this.pwd(this.dirs.nodeModules + sep + normalize(subPath)) + } + + /** + * Set the directory of node_modules folder. + */ + public static setNodeModules(directory: string): typeof Path { + this.dirs.nodeModules = directory + + return this } /** * Return the providers' path of your project. */ public static providers(subPath = sep): string { - return this.pwd('providers' + sep + normalize(subPath)) + return this.pwd(this.dirs.providers + sep + normalize(subPath)) + } + + /** + * Set the directory of providers folder. + */ + public static setProviders(directory: string): typeof Path { + this.dirs.providers = directory + + return this + } + + /** + * Return the facades' path of your project. + */ + public static facades(subPath = sep): string { + return this.pwd(this.dirs.facades + sep + normalize(subPath)) + } + + /** + * Set the directory of facades folder. + */ + public static setFacades(directory: string): typeof Path { + this.dirs.facades = directory + + return this } /** * Return the public path of your project. */ public static public(subPath = sep): string { - return this.pwd('public' + sep + normalize(subPath)) + return this.pwd(this.dirs.public + sep + normalize(subPath)) + } + + /** + * Set the directory of public folder. + */ + public static setPublic(directory: string): typeof Path { + this.dirs.public = directory + + return this } /** * Return the resources' path of your project. */ public static resources(subPath = sep): string { - return this.pwd('resources' + sep + normalize(subPath)) + return this.pwd(this.dirs.resources + sep + normalize(subPath)) + } + + /** + * Set the directory of resources folder. + */ + public static setResources(directory: string): typeof Path { + this.dirs.resources = directory + + return this } /** * Return the routes' path of your project. */ public static routes(subPath = sep): string { - return this.pwd('routes' + sep + normalize(subPath)) + return this.pwd(this.dirs.routes + sep + normalize(subPath)) + } + + /** + * Set the directory of routes folder. + */ + public static setRoutes(directory: string): typeof Path { + this.dirs.routes = directory + + return this } /** * Return the storage path of your project. */ public static storage(subPath = sep): string { - return this.pwd('storage' + sep + normalize(subPath)) + return this.pwd(this.dirs.storage + sep + normalize(subPath)) + } + + /** + * Set the directory of storage folder. + */ + public static setStorage(directory: string): typeof Path { + this.dirs.storage = directory + + return this } /** * Return the tests' path of your project. */ public static tests(subPath = sep): string { - return this.pwd('tests' + sep + normalize(subPath)) + return this.pwd(this.dirs.tests + sep + normalize(subPath)) + } + + /** + * Set the directory of tests folder. + */ + public static setTests(directory: string): typeof Path { + this.dirs.tests = directory + + return this } /** * Return the logs' path of your project. */ public static logs(subPath = sep): string { - return this.storage('logs' + sep + normalize(subPath)) + return this.pwd(this.dirs.logs + sep + normalize(subPath)) + } + + /** + * Set the directory of logs folder. + */ + public static setLogs(directory: string): typeof Path { + this.dirs.logs = directory + + return this } /** * Return the views' path of your project. */ public static views(subPath = sep): string { - return this.resources('views' + sep + normalize(subPath)) + return this.pwd(this.dirs.views + sep + normalize(subPath)) + } + + /** + * Set the directory of views folder. + */ + public static setViews(directory: string): typeof Path { + this.dirs.views = directory + + return this + } + + /** + * Return the static' path of your project. + */ + public static static(subPath = sep): string { + return this.pwd(this.dirs.static + sep + normalize(subPath)) + } + + /** + * Set the directory of static folder. + */ + public static setStatic(directory: string): typeof Path { + this.dirs.static = directory + + return this } /** * Return the assets' path of your project. */ public static assets(subPath = sep): string { - return this.public('assets' + sep + normalize(subPath)) + return this.pwd(this.dirs.assets + sep + normalize(subPath)) + } + + /** + * Set the directory of assets folder. + */ + public static setAssets(directory: string): typeof Path { + this.dirs.assets = directory + + return this } /** * Return the locales' path of your project. */ public static locales(subPath = sep): string { - return this.resources('locales' + sep + normalize(subPath)) + return this.pwd(this.dirs.locales + sep + normalize(subPath)) } /** - * Return the facades' path of your project. + * Set the directory of locales folder. */ - public static facades(subPath = sep): string { - return this.providers('Facades' + sep + normalize(subPath)) + public static setLocales(directory: string): typeof Path { + this.dirs.locales = directory + + return this } /** * Return the stubs' path of your project. */ public static stubs(subPath = sep): string { - return this.tests('Stubs' + sep + normalize(subPath)) + return this.pwd(this.dirs.stubs + sep + normalize(subPath)) + } + + /** + * Set the directory of stubs folder. + */ + public static setStubs(directory: string): typeof Path { + this.dirs.stubs = directory + + return this } /** * Return the http path of your project. */ public static http(subPath = sep): string { - return this.app('Http' + sep + normalize(subPath)) + return this.pwd(this.dirs.http + sep + normalize(subPath)) + } + + /** + * Set the directory of http folder. + */ + public static setHttp(directory: string): typeof Path { + this.dirs.http = directory + + return this } /** * Return the console path of your project. */ public static console(subPath = sep): string { - return this.app('Console' + sep + normalize(subPath)) + return this.pwd(this.dirs.console + sep + normalize(subPath)) + } + + /** + * Set the directory of console folder. + */ + public static setConsole(directory: string): typeof Path { + this.dirs.console = directory + + return this } /** * Return the services' path of your project. */ public static services(subPath = sep): string { - return this.app('Services' + sep + normalize(subPath)) + return this.pwd(this.dirs.services + sep + normalize(subPath)) + } + + /** + * Set the directory of services folder. + */ + public static setServices(directory: string): typeof Path { + this.dirs.services = directory + + return this } /** * Return the repositories' path of your project. */ public static repositories(subPath = sep): string { - return this.app('Repositories' + sep + normalize(subPath)) + return this.pwd(this.dirs.repositories + sep + normalize(subPath)) + } + + /** + * Set the directory of repositories folder. + */ + public static setRepositories(directory: string): typeof Path { + this.dirs.repositories = directory + + return this + } + + /** + * Return the commands' path of your project. + */ + public static commands(subPath = sep): string { + return this.pwd(this.dirs.commands + sep + normalize(subPath)) + } + + /** + * Set the directory of commands folder. + */ + public static setCommands(directory: string): typeof Path { + this.dirs.commands = directory + + return this + } + + /** + * Return the controllers' path of your project. + */ + public static controllers(subPath = sep): string { + return this.pwd(this.dirs.controllers + sep + normalize(subPath)) + } + + /** + * Set the directory of controllers folder. + */ + public static setControllers(directory: string): typeof Path { + this.dirs.controllers = directory + + return this + } + + /** + * Return the exceptions' path of your project. + */ + public static exceptions(subPath = sep): string { + return this.pwd(this.dirs.exceptions + sep + normalize(subPath)) + } + + /** + * Set the directory of exceptions folder. + */ + public static setExceptions(directory: string): typeof Path { + this.dirs.exceptions = directory + + return this + } + + /** + * Return the middlewares' path of your project. + */ + public static middlewares(subPath = sep): string { + return this.pwd(this.dirs.middlewares + sep + normalize(subPath)) + } + + /** + * Set the directory of middlewares folder. + */ + public static setMiddlewares(directory: string): typeof Path { + this.dirs.middlewares = directory + + return this + } + + /** + * Return the interceptors' path of your project. + */ + public static interceptors(subPath = sep): string { + return this.pwd(this.dirs.interceptors + sep + normalize(subPath)) + } + + /** + * Set the directory of interceptors folder. + */ + public static setInterceptors(directory: string): typeof Path { + this.dirs.interceptors = directory + + return this + } + + /** + * Return the terminators' path of your project. + */ + public static terminators(subPath = sep): string { + return this.pwd(this.dirs.terminators + sep + normalize(subPath)) + } + + /** + * Set the directory of terminators folder. + */ + public static setTerminators(directory: string): typeof Path { + this.dirs.terminators = directory + + return this } /** * Return the migrations' path of your project. */ public static migrations(subPath = sep): string { - return this.database('migrations' + sep + normalize(subPath)) + return this.pwd(this.dirs.migrations + sep + normalize(subPath)) + } + + /** + * Set the directory of migrations folder. + */ + public static setMigrations(directory: string): typeof Path { + this.dirs.migrations = directory + + return this } /** * Return the seeders' path of your project. */ public static seeders(subPath = sep): string { - return this.database('seeders' + sep + normalize(subPath)) + return this.pwd(this.dirs.seeders + sep + normalize(subPath)) + } + + /** + * Set the directory of seeders folder. + */ + public static setSeeders(directory: string): typeof Path { + this.dirs.seeders = directory + + return this } /** * Return the .bin path of your node_modules. */ - public static bin(subPath = sep): string { - return this.nodeModules('.bin' + sep + normalize(subPath)) + public static nodeModulesBin(subPath = sep): string { + return this.pwd(this.dirs.nodeModulesBin + sep + normalize(subPath)) + } + + /** + * Set the directory of .bin folder of your node_modules. + */ + public static setNodeModulesBin(directory: string): typeof Path { + this.dirs.nodeModulesBin = directory + + return this } /** @@ -299,12 +668,10 @@ export class Path { * Remove additional slashes from path. */ private static removeSlashes(path: string): string { - if (path.endsWith(sep)) { - path = path.slice(0, -1) + path = normalize(path) - if (path.endsWith(sep)) { - return this.removeSlashes(path) - } + if (path.endsWith(sep)) { + return this.removeSlashes(path.slice(0, -1)) } return path diff --git a/src/Types/PathDirs.ts b/src/Types/PathDirs.ts new file mode 100644 index 0000000..eceaf1c --- /dev/null +++ b/src/Types/PathDirs.ts @@ -0,0 +1,45 @@ +/** + * @athenna/common + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +export interface PathDirs { + bin?: string + src?: string + app?: string + services?: string + exceptions?: string + repositories?: string + console?: string + commands?: string + http?: string + controllers?: string + middlewares?: string + interceptors?: string + terminators?: string + bootstrap?: string + config?: string + database?: string + seeders?: string + migrations?: string + lang?: string + resources?: string + views?: string + locales?: string + nodeModules?: string + nodeModulesBin?: string + providers?: string + facades?: string + public?: string + static?: string + assets?: string + routes?: string + storage?: string + logs?: string + tests?: string + stubs?: string +} diff --git a/src/index.ts b/src/index.ts index f9cc951..32f281a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,7 @@ export * from './Helpers/Uuid.js' export * from './Types/Merge.js' export * from './Types/Except.js' +export * from './Types/PathDirs.js' export * from './Types/HttpClient/Body.js' export * from './Types/HttpClient/Query.js' export * from './Types/HttpClient/ReqOptions.js' diff --git a/tests/Unit/FakeApiTest.ts b/tests/Unit/FakeApiTest.ts index 7bc78aa..5a2979d 100644 --- a/tests/Unit/FakeApiTest.ts +++ b/tests/Unit/FakeApiTest.ts @@ -97,9 +97,8 @@ test.group('FakeApiTest', group => { const routes = FakeApi.listRoutes() - assert.isTrue(routes.includes('users (GET)')) - assert.isTrue(routes.includes('users (POST)')) - assert.isTrue(routes.includes(':id (DELETE)')) + assert.isTrue(routes.includes('users')) + assert.isTrue(routes.includes(':id')) assert.isTrue(routes.includes('service-unavailable')) }) }) diff --git a/tests/Unit/PathTest.ts b/tests/Unit/PathTest.ts index b6c2b43..7ed849f 100644 --- a/tests/Unit/PathTest.ts +++ b/tests/Unit/PathTest.ts @@ -9,53 +9,28 @@ import { sep } from 'node:path' import { test } from '@japa/runner' +import { Json } from '#src/index' -test.group('PathTest', () => { - test('should be able to resolve the environment where the app will run', async ({ assert }) => { - const metaUrl = import.meta.url - const metaUrlJs = metaUrl.replace('.ts', '.js') +test.group('PathTest', group => { + let defaultDirs: any = {} - Path.resolveEnvironment(metaUrl) - - assert.equal(process.env.IS_TS, 'true') - assert.equal(Path.defaultBeforePath, '') - - delete process.env.IS_TS - Path.resolveEnvironment(metaUrl, '/build') - - assert.equal(process.env.IS_TS, 'true') - assert.equal(Path.defaultBeforePath, '') - - delete process.env.IS_TS - Path.resolveEnvironment(metaUrlJs, '/build') - - assert.equal(process.env.IS_TS, 'false') - assert.equal(Path.defaultBeforePath, '/build') - - delete process.env.IS_TS - Path.resolveEnvironment(metaUrlJs) - - assert.equal(process.env.IS_TS, 'false') - assert.equal(Path.defaultBeforePath, '') + group.each.setup(() => { + defaultDirs = Json.copy(Path.dirs) + }) - process.env.IS_TS = 'true' + group.each.teardown(() => { + Path.dirs = defaultDirs }) test('should get the extension js and ts', async ({ assert }) => { - assert.equal(Path.ext(), 'ts') - process.env.IS_TS = 'false' assert.equal(Path.ext(), 'js') - process.env.IS_TS = '(false)' - assert.equal(Path.ext(), 'js') - - process.env.IS_TS = 'false' - Path.defaultBeforePath = 'build' + process.env.IS_TS = 'true' + assert.equal(Path.ext(), 'ts') - assert.isTrue(Path.pwd(`artisan.${Path.ext()}`).includes(`build${sep}artisan.js`)) + assert.isTrue(Path.pwd(`artisan.${Path.ext()}`).includes(`${sep}artisan.ts`)) - Path.defaultBeforePath = '' process.env.IS_TS = 'true' }) @@ -91,6 +66,7 @@ test.group('PathTest', () => { assert.isDefined(Path.vmHome()) assert.isTrue(Path.this().endsWith('Unit')) assert.isFalse(Path.this('../../').endsWith('tests')) + assert.isTrue(Path.this('../../package.json').endsWith('package.json')) }) test('should get the sub paths of app main path', async ({ assert }) => { @@ -99,9 +75,25 @@ test.group('PathTest', () => { assert.equal(Path.http(), mainPath.concat(sep, 'Http')) assert.equal(Path.console(), mainPath.concat(sep, 'Console')) assert.equal(Path.services(), mainPath.concat(sep, 'Services')) + assert.equal(Path.exceptions(), mainPath.concat(sep, 'Exceptions')) assert.equal(Path.repositories(), mainPath.concat(sep, 'Repositories')) }) + test('should get the sub paths of http main path', async ({ assert }) => { + const mainPath = process.cwd().concat(sep, 'app', sep, 'Http') + + assert.equal(Path.controllers(), mainPath.concat(sep, 'Controllers')) + assert.equal(Path.middlewares(), mainPath.concat(sep, 'Middlewares')) + assert.equal(Path.interceptors(), mainPath.concat(sep, 'Interceptors')) + assert.equal(Path.terminators(), mainPath.concat(sep, 'Terminators')) + }) + + test('should get the sub paths of console main path', async ({ assert }) => { + const mainPath = process.cwd().concat(sep, 'app', sep, 'Console') + + assert.equal(Path.commands(), mainPath.concat(sep, 'Commands')) + }) + test('should get the sub paths of database main path', async ({ assert }) => { const mainPath = process.cwd().concat(sep, 'database') @@ -109,15 +101,16 @@ test.group('PathTest', () => { assert.equal(Path.migrations(), mainPath.concat(sep, 'migrations')) }) - test('should get the sub paths of node_modules main path', async ({ assert }) => { + test('should get the .bin folder of node_modules main path', async ({ assert }) => { const mainPath = process.cwd().concat(sep, 'node_modules') - assert.equal(Path.bin(), mainPath.concat(sep, '.bin')) + assert.equal(Path.nodeModulesBin(), mainPath.concat(sep, '.bin')) }) test('should get the sub paths of public main path', async ({ assert }) => { const mainPath = process.cwd().concat(sep, 'public') + assert.equal(Path.static(), mainPath.concat(sep, 'static')) assert.equal(Path.assets(), mainPath.concat(sep, 'assets')) }) @@ -143,30 +136,87 @@ test.group('PathTest', () => { test('should get the sub paths of providers main path', async ({ assert }) => { const mainPath = process.cwd().concat(sep, 'providers') - assert.equal(Path.facades(), mainPath.concat(sep, 'Facades')) + assert.equal(Path.facades(), mainPath.concat(sep, 'facades')) }) - test('should be able to set a default before path in Path class', async ({ assert }) => { - const mainPath = process.cwd() - - Path.defaultBeforePath = 'build' - - assert.equal(Path.pwd('/'), `${mainPath}${sep}build`) - assert.equal(Path.app('/'), `${mainPath}${sep}build${sep}app`) - assert.equal(Path.console('/'), `${mainPath}${sep}build${sep}app${sep}Console`) - assert.isDefined(Path.vmTmp('/')) - assert.isDefined(Path.vmHome('/')) + test('should be able to setup different paths for all methods of Path class', async ({ assert }) => { + Path.setBin('build/bin') + .setSrc('build/src') + .setApp('build/app') + .setServices('build/app/Services') + .setExceptions('build/app/Exceptions') + .setRepositories('build/app/Repositories') + .setConsole('build/app/Console') + .setCommands('build/app/Console/Commands') + .setHttp('build/app/Http') + .setControllers('build/app/Http/Controllers') + .setMiddlewares('build/app/Http/Middlewares') + .setInterceptors('build/app/Http/Interceptors') + .setTerminators('build/app/Http/Terminators') + .setBootstrap('build/bootstrap') + .setConfig('build/config') + .setDatabase('build/database') + .setSeeders('build/database/seeders') + .setMigrations('build/database/migrations') + .setLang('build/lang') + .setResources('build/resources') + .setViews('build/resources/views') + .setLocales('build/resources/locales') + .setNodeModules('build/node_modules') + .setNodeModulesBin('build/node_modules/.bin') + .setProviders('build/providers') + .setFacades('build/providers/facades') + .setPublic('build/public') + .setStatic('build/public/static') + .setAssets('build/public/assets') + .setRoutes('build/routes') + .setStorage('build/storage') + .setLogs('build/storage/logs') + .setTests('build/tests') + .setStubs('build/tests/Stubs') + + assert.isTrue(Path.bin().endsWith(`build${sep}bin`)) + assert.isTrue(Path.src().endsWith(`build${sep}src`)) + assert.isTrue(Path.app().endsWith(`build${sep}app`)) + assert.isTrue(Path.services().endsWith(`build${sep}app${sep}Services`)) + assert.isTrue(Path.exceptions().endsWith(`build${sep}app${sep}Exceptions`)) + assert.isTrue(Path.repositories().endsWith(`build${sep}app${sep}Repositories`)) + assert.isTrue(Path.console().endsWith(`build${sep}app${sep}Console`)) + assert.isTrue(Path.commands().endsWith(`build${sep}app${sep}Console${sep}Commands`)) + assert.isTrue(Path.http().endsWith(`build${sep}app${sep}Http`)) + assert.isTrue(Path.controllers().endsWith(`build${sep}app${sep}Http${sep}Controllers`)) + assert.isTrue(Path.middlewares().endsWith(`build${sep}app${sep}Http${sep}Middlewares`)) + assert.isTrue(Path.interceptors().endsWith(`build${sep}app${sep}Http${sep}Interceptors`)) + assert.isTrue(Path.terminators().endsWith(`build${sep}app${sep}Http${sep}Terminators`)) + assert.isTrue(Path.bootstrap().endsWith(`build${sep}bootstrap`)) + assert.isTrue(Path.config().endsWith(`build${sep}config`)) + assert.isTrue(Path.database().endsWith(`build${sep}database`)) + assert.isTrue(Path.seeders().endsWith(`build${sep}database${sep}seeders`)) + assert.isTrue(Path.migrations().endsWith(`build${sep}database${sep}migrations`)) + assert.isTrue(Path.resources().endsWith(`build${sep}resources`)) + assert.isTrue(Path.views().endsWith(`build${sep}resources${sep}views`)) + assert.isTrue(Path.locales().endsWith(`build${sep}resources${sep}locales`)) + assert.isTrue(Path.nodeModules().endsWith(`build${sep}node_modules`)) + assert.isTrue(Path.nodeModulesBin().endsWith(`build${sep}node_modules${sep}.bin`)) + assert.isTrue(Path.providers().endsWith(`build${sep}providers`)) + assert.isTrue(Path.facades().endsWith(`build${sep}providers${sep}facades`)) + assert.isTrue(Path.public().endsWith(`build${sep}public`)) + assert.isTrue(Path.static().endsWith(`build${sep}public${sep}static`)) + assert.isTrue(Path.assets().endsWith(`build${sep}public${sep}assets`)) + assert.isTrue(Path.routes().endsWith(`build${sep}routes`)) + assert.isTrue(Path.storage().endsWith(`build${sep}storage`)) + assert.isTrue(Path.logs().endsWith(`build${sep}storage${sep}logs`)) + assert.isTrue(Path.tests().endsWith(`build${sep}tests`)) + assert.isTrue(Path.stubs().endsWith(`build${sep}tests${sep}Stubs`)) }) - test('should be able to ignore the original pwd path of the application without the default before path', async ({ - assert, - }) => { - const mainPath = process.cwd() - - Path.defaultBeforePath = 'build' + test('should be able to merge custom directories with defaults', async ({ assert }) => { + Path.mergeDirs({ + app: 'build/app', + }) - assert.equal(Path.originalPwd('/'), mainPath) - assert.equal(Path.originalPwd('/app'), `${mainPath}${sep}app`) - assert.equal(Path.originalPwd('/app/Console'), `${mainPath}${sep}app${sep}Console`) + assert.isTrue(Path.bin().endsWith('bin')) + assert.isTrue(Path.src().endsWith('src')) + assert.isTrue(Path.app().endsWith('app')) }) })