From 0fcca3b6ac50eb8ea638162437f2ae3677151279 Mon Sep 17 00:00:00 2001 From: Valtteri Laitinen Date: Wed, 1 Nov 2023 11:37:30 +0200 Subject: [PATCH] Initialize --- .editorconfig | 11 +++ .gitattributes | 1 + .github/workflows/publish.yml | 21 +++++ .gitignore | 1 + .npmignore | 2 + CHANGELOG.md | 9 +++ LICENSE.md | 15 ++++ README.md | 57 ++++++++++++++ biome.json | 15 ++++ cli.js | 6 ++ index.d.ts | 5 ++ index.js | 27 +++++++ package-lock.json | 143 ++++++++++++++++++++++++++++++++++ package.json | 47 +++++++++++ 14 files changed, 360 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/workflows/publish.yml create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 biome.json create mode 100755 cli.js create mode 100644 index.d.ts create mode 100644 index.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..08a5601 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = tab +insert_final_newline = true +trim_trailing_whitespace = true + +[*.yml] +indent_style = space diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..3ba8cf2 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,21 @@ +name: Publish npm package +on: + release: + types: [created] +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '20.x' + registry-url: 'https://registry.npmjs.org' + - run: npm install --global npm + - run: npm pkg delete scripts devDependencies + - run: npm publish --provenance --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..2d74800 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +.* +biome.json diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1a7cc7e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Changelog + +This project uses [semantic versioning](https://semver.org/). + +## [v1.0.0] (2023-11-01) + +- Initial release + +[v1.0.0]: https://github.com/valtlai/picodel/tree/v1.0.0 diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9652b21 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,15 @@ +# ISC License + +Copyright © 2023 Valtteri Laitinen + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3ba0f06 --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# picodel + +[![NPM](https://img.shields.io/npm/v/picodel.svg)](https://www.npmjs.com/package/picodel) + +Delete files and directories inside the current working directory + +- Zero dependencies +- Only static paths, no glob patterns +- ESM and async only + +## Install + +``` +npm install --save-dev picodel +``` + +## Usage + +### CLI + +Syntax: + +``` +picodel [...paths] +``` + +Example CLI prompt: + +``` +npx picodel .cache public +``` + +Example npm script: + +```json +{ + "scripts": { + "clean": "picodel .cache public" + } +} +``` + +### Module + +Syntax: + +```ts +picodel(...string[]) +``` + +Example: + +```ts +import picodel from 'picodel'; + +await picodel('.cache', 'public'); +``` diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..125170c --- /dev/null +++ b/biome.json @@ -0,0 +1,15 @@ +{ + "javascript": { + "formatter": { + "quoteStyle": "single" + } + }, + "linter": { + "rules": { + "correctness": { + "noUndeclaredVariables": "error", + "noUnusedVariables": "error" + } + } + } +} diff --git a/cli.js b/cli.js new file mode 100755 index 0000000..ce9c606 --- /dev/null +++ b/cli.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node + +import picodel from './index.js'; + +const filePaths = process.argv.slice(2); +await picodel(...filePaths); diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..98e8f5a --- /dev/null +++ b/index.d.ts @@ -0,0 +1,5 @@ +/** + * Delete specified files and directories + * @param paths Must be inside CWD + */ +export default function picodel(...paths: string[]): Promise; diff --git a/index.js b/index.js new file mode 100644 index 0000000..8df7d07 --- /dev/null +++ b/index.js @@ -0,0 +1,27 @@ +import fsPromises from 'node:fs/promises'; +import path from 'node:path'; + +const osNormalize = (filePath) => { + return filePath.replaceAll(path.posix.sep, path.sep); +}; + +const isSubpathOf = (childPath, parentPath) => { + const parentRelative = path.relative(parentPath, osNormalize(childPath)); + return parentRelative && !parentRelative.startsWith(`..${path.sep}`); +}; + +const cwd = process.cwd(); + +export default async function picodel(...filePaths) { + for (const [index, filePath] of filePaths.entries()) { + if (!isSubpathOf(filePath, cwd)) { + throw new Error(`Path must be inside CWD: #${index + 1}: ${filePath}`); + } + } + + await Promise.all( + filePaths.map((filePath) => + fsPromises.rm(filePath, { recursive: true, force: true }), + ), + ); +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a3ea8a1 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,143 @@ +{ + "name": "picodel", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "picodel", + "version": "1.0.0", + "license": "ISC", + "bin": { + "picodel": "cli.js" + }, + "devDependencies": { + "@biomejs/biome": "1.3.3" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/@biomejs/biome": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@biomejs/biome/-/biome-1.3.3.tgz", + "integrity": "sha512-vTJn7RBzLWIabUuUIoEopO860YyBrbPEu4Pztfd28jRU5QD074hKZ9IQs24pFO6A2R296gaeYmN62f4u7pUruQ==", + "dev": true, + "hasInstallScript": true, + "bin": { + "biome": "bin/biome" + }, + "engines": { + "node": ">=14.*" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/biome" + }, + "optionalDependencies": { + "@biomejs/cli-darwin-arm64": "1.3.3", + "@biomejs/cli-darwin-x64": "1.3.3", + "@biomejs/cli-linux-arm64": "1.3.3", + "@biomejs/cli-linux-x64": "1.3.3", + "@biomejs/cli-win32-arm64": "1.3.3", + "@biomejs/cli-win32-x64": "1.3.3" + } + }, + "node_modules/@biomejs/cli-darwin-arm64": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-arm64/-/cli-darwin-arm64-1.3.3.tgz", + "integrity": "sha512-2X87ZfbmWwe4NGukrUvnoYdI//muSgjNUCAHJ2DO+kS1sB7kDy1s6PN/IYyTJuqRcJtDuOnSpaUDE7KxR1YhtA==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.*" + } + }, + "node_modules/@biomejs/cli-darwin-x64": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-x64/-/cli-darwin-x64-1.3.3.tgz", + "integrity": "sha512-t+7DWTCbSgHOBcPsGKuwS1qh1z9zbXFK8i8ktE18yW7iF/W0zI62k44fYqYeFJKlb0Q08aqUvez3L+AQJFsn+w==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.*" + } + }, + "node_modules/@biomejs/cli-linux-arm64": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-arm64/-/cli-linux-arm64-1.3.3.tgz", + "integrity": "sha512-D8CvXaB8lkXXBQ6B3n0MXSSZFiE60+aNHorBLimVTtKiMod8QvAP425oQFZFul5wMXZqPLGTKFjXbAi/rvnc1A==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.*" + } + }, + "node_modules/@biomejs/cli-linux-x64": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-x64/-/cli-linux-x64-1.3.3.tgz", + "integrity": "sha512-bqB05fwJnRZwRlcm/BS/s4qPickqiXZkiU/nOYvHApfsPeqgSHgv5HWoBYuSUjgqBbX3XZJArsC5dCcVW7vAJw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.*" + } + }, + "node_modules/@biomejs/cli-win32-arm64": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-win32-arm64/-/cli-win32-arm64-1.3.3.tgz", + "integrity": "sha512-muFOjAv1ONMfaJDlo4Ds+Qb9lkdSLM2XaxOe3AJPejSq3Vi0aRr51ZnE02BofMnL2sVsOA9cO54wibsuTcopbw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.*" + } + }, + "node_modules/@biomejs/cli-win32-x64": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-win32-x64/-/cli-win32-x64-1.3.3.tgz", + "integrity": "sha512-PMkMhS4smmmTMflxuZUx3REFSazEL9xsGscvZO1dKWI4ET23la+KxEM4TlSpjOyO66UerqSkuUlZecn0QhD63A==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.*" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b1ceaa8 --- /dev/null +++ b/package.json @@ -0,0 +1,47 @@ +{ + "name": "picodel", + "version": "1.0.0", + "description": "Delete files and directories inside the current working directory", + "license": "ISC", + "author": { + "name": "Valtteri Laitinen", + "email": "dev@valtlai.fi", + "url": "https://valtlai.fi/" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/valtlai/picodel.git" + }, + "type": "module", + "bin": "./cli.js", + "exports": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "engines": { + "node": ">=18" + }, + "scripts": { + "check": "biome check --apply ." + }, + "devDependencies": { + "@biomejs/biome": "1.3.3" + }, + "keywords": [ + "delete", + "remove", + "destroy", + "unlink", + "clean", + "files", + "folders", + "directories", + "del", + "rm", + "rmrf", + "rmdir", + "fs", + "filesystem", + "zero-dependency" + ] +}