diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml new file mode 100644 index 0000000..b65c919 --- /dev/null +++ b/.github/workflows/deno.yml @@ -0,0 +1,39 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +# This workflow will install Deno and run tests across stable and nightly builds on Windows, Ubuntu and macOS. +# For more information see: https://github.com/denolib/setup-deno + +name: Deno + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + test: + runs-on: ${{ matrix.os }} # runs a test on Ubuntu, Windows and macOS + + strategy: + matrix: + deno: ["v1.x", "nightly"] + os: [macOS-latest, windows-latest, ubuntu-latest] + + steps: + - name: Setup repo + uses: actions/checkout@v2 + + - name: Setup Deno + uses: denolib/setup-deno@v2 + with: + deno-version: ${{ matrix.deno }} # tests across multiple Deno versions + + - name: Cache Dependencies + run: deno cache deps.ts + + - name: Run Tests + run: deno test -A --unstable diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b943dbc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "deno.enable": true +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..21d1d2f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Santiago Aguilar Hernández + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9cb4400 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# cjs ![Deno](https://github.com/sant123/cjs-vars/workflows/Deno/badge.svg) + +Create variables like `__dirname` and `__filename` in Deno. Their behavior is the same as Node.js variables. + +## Usage + +```ts +// example.ts +import { getDirname, getFilename } from 'https://deno.land/x/cjs/mod.ts'; + +const __dirname = getDirname(import.meta.url); +const __filename = getFilename(import.meta.url); + +// Your code here... +``` + +## API + +### getDirname(importMetaUrl: string): string + +The directory name of the current module. + +### getFilename(importMetaUrl: string): string + +The file name of the current module. This is the current module file's absolute path. + +## Node.js support + +This should work with the [stable](https://nodejs.org/dist/latest-v15.x/docs/api/esm.html#esm_modules_ecmascript_modules) Ecmascript modules implementation of Node.js. See this [link](https://nodejs.org/dist/latest-v15.x/docs/api/esm.html#esm_no_filename_or_dirname) for more information. + +## Testing + +This library is highly tested to provide the same variables behavior of Node.js. If you see something is missing or ¿did you find a bug? pull requests are really welcome. diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..e99f501 --- /dev/null +++ b/mod.ts @@ -0,0 +1,2 @@ +export { getDirname } from './src/get_dirname.ts'; +export { getFilename } from './src/get_filename.ts'; diff --git a/src/get_dirname.ts b/src/get_dirname.ts new file mode 100644 index 0000000..22ad054 --- /dev/null +++ b/src/get_dirname.ts @@ -0,0 +1,5 @@ +const removeProtocolAndFilenameRegex = /\w+:\/\/(.*)\/[^/]+$/; + +export function getDirname(importMetaUrl: string): string { + return importMetaUrl.replace(removeProtocolAndFilenameRegex, '$1'); +} diff --git a/src/get_filename.ts b/src/get_filename.ts new file mode 100644 index 0000000..dc4ef60 --- /dev/null +++ b/src/get_filename.ts @@ -0,0 +1,5 @@ +const removeProtocolRegex = /\w+:\/\//; + +export function getFilename(importMetaUrl: string): string { + return importMetaUrl.replace(removeProtocolRegex, ''); +} diff --git a/tests/get_dirname.test.ts b/tests/get_dirname.test.ts new file mode 100644 index 0000000..ab631c9 --- /dev/null +++ b/tests/get_dirname.test.ts @@ -0,0 +1,49 @@ +import { assertEquals } from 'https://deno.land/std@0.82.0/testing/asserts.ts'; +import { getDirname } from '../mod.ts'; + +Deno.test('Get dirname from a simple directory', () => { + const __dirname = getDirname( + 'file:///Users/santiago.aguilar/Desktop/index.ts', + ); + + assertEquals(__dirname, '/Users/santiago.aguilar/Desktop'); +}); + +Deno.test('Get dirname with a directory with spaces', () => { + const __dirname = getDirname( + 'file:///Users/santiago.aguilar/Desktop/dir%20with%20spaces/index.ts', + ); + + assertEquals( + __dirname, + '/Users/santiago.aguilar/Desktop/dir%20with%20spaces', + ); +}); + +Deno.test('Get dirname with a directory with dots', () => { + const __dirname = getDirname( + 'file:///Users/santiago.aguilar/Desktop/dir.with.dots/index.ts', + ); + + assertEquals(__dirname, '/Users/santiago.aguilar/Desktop/dir.with.dots'); +}); + +Deno.test('Get dirname with a directory with dashes', () => { + const __dirname = getDirname( + 'file:///Users/santiago.aguilar/Desktop/dir-with-dashes/index.ts', + ); + + assertEquals(__dirname, '/Users/santiago.aguilar/Desktop/dir-with-dashes'); +}); + +Deno.test('Get dirname with a file without extension', () => { + const __dirname = getDirname('file:///Users/santiago.aguilar/Desktop/index'); + + assertEquals(__dirname, '/Users/santiago.aguilar/Desktop'); +}); + +Deno.test('Get dirname with a hidden file', () => { + const __dirname = getDirname('file:///Users/santiago.aguilar/Desktop/.index'); + + assertEquals(__dirname, '/Users/santiago.aguilar/Desktop'); +}); diff --git a/tests/get_filename.test.ts b/tests/get_filename.test.ts new file mode 100644 index 0000000..60f0092 --- /dev/null +++ b/tests/get_filename.test.ts @@ -0,0 +1,59 @@ +import { assertEquals } from 'https://deno.land/std@0.82.0/testing/asserts.ts'; +import { getFilename } from '../mod.ts'; + +Deno.test('Get filename from a simple directory', () => { + const __filename = getFilename( + 'file:///Users/santiago.aguilar/Desktop/index.ts', + ); + + assertEquals(__filename, '/Users/santiago.aguilar/Desktop/index.ts'); +}); + +Deno.test('Get filename with a directory with spaces', () => { + const __filename = getFilename( + 'file:///Users/santiago.aguilar/Desktop/dir%20with%20spaces/index.ts', + ); + + assertEquals( + __filename, + '/Users/santiago.aguilar/Desktop/dir%20with%20spaces/index.ts', + ); +}); + +Deno.test('Get filename with a directory with dots', () => { + const __filename = getFilename( + 'file:///Users/santiago.aguilar/Desktop/dir.with.dots/index.ts', + ); + + assertEquals( + __filename, + '/Users/santiago.aguilar/Desktop/dir.with.dots/index.ts', + ); +}); + +Deno.test('Get filename with a directory with dashes', () => { + const __filename = getFilename( + 'file:///Users/santiago.aguilar/Desktop/dir-with-dashes/index.ts', + ); + + assertEquals( + __filename, + '/Users/santiago.aguilar/Desktop/dir-with-dashes/index.ts', + ); +}); + +Deno.test('Get filename without extension', () => { + const __filename = getFilename( + 'file:///Users/santiago.aguilar/Desktop/index', + ); + + assertEquals(__filename, '/Users/santiago.aguilar/Desktop/index'); +}); + +Deno.test('Get filename in a hidden file', () => { + const __filename = getFilename( + 'file:///Users/santiago.aguilar/Desktop/.index', + ); + + assertEquals(__filename, '/Users/santiago.aguilar/Desktop/.index'); +});