Skip to content

Commit

Permalink
Merge pull request #6 from aminya/bundle [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Aug 21, 2022
2 parents 1920841 + e6c1d10 commit 0e9c5d3
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 71 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [`normalizeTrim` (function)](#normalizetrim-function)
- [`removeExt` (function)](#removeext-function)
- [`replaceExt` (function)](#replaceext-function)
- [`isPathInside` (function)](#ispathinside-function)
- [🤝 Contributing](#contributing)

<!-- /code_chunk_output -->
Expand Down Expand Up @@ -234,6 +235,37 @@ import { replaceExt } from "patha"
replaceExt("path/to/file.md", ".html") // gives "path/to/file.html"
```

### `isPathInside` (function)

Check if a path is inside another path.

Note that relative paths are resolved against `process.cwd()` to make them absolute.

This function does not check if the paths exist and it only works with strings.

**Parameters:**

- childPath (`string`)
- parentPath (`string`)

**returns:** boolean

```js
import { isPathInside } from "patha"

isPathInside("a/b/c", "a/b")
//=> true

isPathInside("a/b/c", "x/y")
//=> false

isPathInside("a/b/c", "a/b/c")
//=> false

isPathInside("/Users/some/dev/aa", "/Users/some")
//=> true
```

<!-- INSERT GENERATED DOCS END -->

## 🤝 Contributing
Expand Down
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ words:
- ghes
- Graphviz
- isci
- ispathinside
- isroot
- kcov
- LDFLAGS
Expand Down
12 changes: 8 additions & 4 deletions dev/tsc-mjs.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { readdirSync } from "fs"
import { rename } from "fs/promises"
import { copyFile, rename } from "fs/promises"
import { join } from "path"

async function main() {
const dir = "./dist/node"
const dir = "./dist"
await Promise.all(
readdirSync(dir).map((file) => {
return rename(join(dir, file), join(dir, file.replace(".js", ".mjs")))
readdirSync(dir).map(async (file) => {
if (file.endsWith(".js")) {
await rename(join(dir, file), join(dir, file.replace(".js", ".mjs")))
} else if (file.endsWith(".d.ts")) {
await copyFile(join(dir, file), join(dir, file.replace(".d.ts", ".d.mts")))
}
})
)
}
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
testMatch: ["**/*.test.ts"],
testPathIgnorePatterns: [],
// coverage
collectCoverageFrom: ["src/**/*.{ts,tsx}"],
coveragePathIgnorePatterns: ["assets", ".css.d.ts"],
collectCoverageFrom: ["src/**/*.{ts,mjs,cjs}"],
coveragePathIgnorePatterns: ["assets"],
verbose: true,
}
40 changes: 23 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,44 @@
"license": "Apache-2.0",
"author": "Amin Yahyaabadi",
"exports": {
"import": "./dist/node/index.mjs",
"require": "./dist/node/index.js"
"import": "./dist/index.node.mjs",
"require": "./dist/index.node.cjs"
},
"main": "./dist/node/index.js",
"module": "./dist/node/index.mjs",
"main.browser": "./dist/browser/index.js",
"module.browser": "./dist/browser/index.mjs",
"main.deno": "./dist/deno/index.js",
"module.deno": "./dist/deno/index.mjs",
"main": "./dist/index.node.cjs",
"module": "./dist/index.node.mjs",
"main.browser": "./dist/index.browser.legacy.js",
"module.browser": "./dist/index.browser.mjs",
"module.deno": "./dist/index.deno.mjs",
"source": "./src/index.ts",
"browser": {
"path": "./node_modules/path-browserify",
"process": "./node_modules/process"
},
"targets": {
"main": {
"source": "./src/index-node.ts",
"context": "node",
"engines": {
"node": ">=12.x"
},
"optimize": true,
"includeNodeModules": true,
"outputFormat": "commonjs",
"isLibrary": false
"isLibrary": true
},
"module": {
"source": "./src/index.ts",
"context": "node",
"engines": {
"node": ">=16.x"
},
"optimize": true,
"includeNodeModules": true,
"outputFormat": "esmodule",
"isLibrary": false
"isLibrary": true
},
"main.browser": {
"source": "./src/index-browser.ts",
"context": "browser",
"engines": {
"browsers": "> 0.5%"
Expand All @@ -53,6 +55,7 @@
"isLibrary": true
},
"module.browser": {
"source": "./src/index-browser.ts",
"context": "browser",
"engines": {
"browsers": "last 2 versions"
Expand All @@ -71,12 +74,13 @@
"LICENSE_dependencies.txt"
],
"scripts": {
"build": "run-s clean build.tsc.esm build.parcel",
"build.parcel": "shx rm -rf dist/node/index.js* && cross-env NODE_ENV=production parcel build --target main --target module --target main.browser --target module.browser && cp -r ./dist/browser/*.mjs* ./dist/deno",
"build.tsc.cjs": "tsc -p ./tsconfig.cjs.json",
"build.tsc.esm": "tsc -p ./tsconfig.json && node ./dev/tsc-mjs.mjs",
"build": "run-s clean build.tsc build.parcel build.types build.deno",
"build.parcel": "shx rm -rf dist/node/index.js* && cross-env NODE_ENV=production parcel build --target main --target module --target main.browser --target module.browser",
"build.types": "shx cp ./dist/index.d.ts ./dist/index.node.d.ts && shx cp ./dist/index.d.ts ./dist/index.node.d.cts && shx cp ./dist/index.d.ts ./dist/browser.d.ts && shx cp ./dist/index.d.ts ./dist/index.deno.d.ts",
"build.deno": "shx cp ./dist/index.browser.mjs ./dist/index.deno.mjs && shx cp ./dist/index.browser.mjs.map ./dist/index.deno.mjs.map",
"build.tsc": "tsc -p ./tsconfig.json && node ./dev/tsc-mjs.mjs",
"bump": "ncu -u -x execa,numerous && pnpm update",
"clean": "shx rm -rf dist && shx mkdir ./dist && shx mkdir ./dist/deno",
"clean": "shx rm -rf dist && shx mkdir ./dist",
"dev": "cross-env NODE_ENV=development parcel watch",
"docs": "shx rm -rf ./README.md && pnpm exec readme --path ./dev/docs/readme.md -y && pnpm exec ts-readme --header-depth 3 && pnpm run format",
"format": "run-s lint.prettier",
Expand All @@ -85,8 +89,10 @@
"lint.eslint": "eslint **/*.{ts,tsx,js,jsx,cjs,mjs,json,yaml} --no-error-on-unmatched-pattern --cache --cache-location ./.cache/eslint/ --fix",
"lint.prettier": "prettier --write .",
"prepare": "run-s build",
"test": "run-p --continue-on-error test.lint test.unit test.integration",
"test.integration": "run-s build && node ./test/node-integration.mjs && node ./test/node-integration.js && deno run ./test/deno-integration.ts",
"test": "run-s build && run-p --continue-on-error --aggregate-output test.lint test.unit test.integration.node.js test.integration.node.mjs test.integration.deno",
"test.integration.node.js": "node ./test/node-integration.js",
"test.integration.node.mjs": "node ./test/node-integration.mjs",
"test.integration.deno": "deno run ./test/deno-integration.ts",
"test.lint": "run-p --aggregate-output --continue-on-error lint.cspell test.lint.eslint test.lint.prettier",
"test.lint.eslint": "eslint **/*.{ts,tsx,js,jsx,cjs,mjs,json,yaml} --no-error-on-unmatched-pattern --cache --cache-location ./.cache/eslint/",
"test.lint.prettier": "prettier . --check",
Expand Down
38 changes: 38 additions & 0 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as path from "path"
const {
basename,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
normalize,
parse,
posix,
relative,
resolve,
sep,
toNamespacedPath,
win32,
} = path

export {
basename,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
normalize,
parse,
posix,
relative,
resolve,
sep,
toNamespacedPath,
win32,
}

export * from "./lib"
38 changes: 38 additions & 0 deletions src/index-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as path from "path"
const {
basename,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
normalize,
parse,
posix,
relative,
resolve,
sep,
toNamespacedPath,
win32,
} = path

export {
basename,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
normalize,
parse,
posix,
relative,
resolve,
sep,
toNamespacedPath,
win32,
}

export * from "./lib"
32 changes: 18 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
export * from "./path"
export {
basename,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
normalize,
parse,
posix,
relative,
resolve,
sep,
toNamespacedPath,
win32,
} from "path"

export * from "./name"

export * from "./normalize-trim"

export * from "./add-exe-ext"
export * from "./add-name-prefix"
export * from "./add-name-suffix"
export * from "./add-sh-ext"
export * from "./add-sh-relative-prefix"
export * from "./remove-ext"
export * from "./replace-ext"

export * from "./is-path-inside"
export * from "./lib"
13 changes: 13 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export * from "./name"

export * from "./normalize-trim"

export * from "./add-exe-ext"
export * from "./add-name-prefix"
export * from "./add-name-suffix"
export * from "./add-sh-ext"
export * from "./add-sh-relative-prefix"
export * from "./remove-ext"
export * from "./replace-ext"

export * from "./is-path-inside"
4 changes: 0 additions & 4 deletions src/path.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/path.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/path.ts

This file was deleted.

3 changes: 2 additions & 1 deletion test/deno-integration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable import/no-unresolved */

import { name, normalizeTrim } from "../dist/deno/index.mjs"
import { name, normalizeTrim, join } from "../dist/index.deno.mjs"

name("path/to/file.md")
normalizeTrim("/foo/../")
join("a", "b")
5 changes: 3 additions & 2 deletions test/node-integration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const filePaths = require("../dist/node/index.js")
const { name, normalizeTrim } = filePaths
const filePaths = require("../dist/index.node.cjs")
const { name, normalizeTrim, join } = filePaths

name("path/to/file.md")
normalizeTrim("/foo/../")
join("a", "b")
5 changes: 3 additions & 2 deletions test/node-integration.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable import/no-unresolved */

import { name } from "../dist/node/index.mjs"
import { normalizeTrim } from "../dist/node/normalize-trim.mjs"
import { name, join } from "../dist/index.node.mjs"
import { normalizeTrim } from "../dist/normalize-trim.mjs"

name("path/to/file.md")
normalizeTrim("/foo/../")
join("a", "b")
6 changes: 0 additions & 6 deletions tsconfig.cjs.json

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"module": "ESNext",
"moduleResolution": "node",
"importHelpers": false,
"outDir": "./dist/node"
"outDir": "./dist"
},
"compileOnSave": false,
"include": ["./src"]
Expand Down

0 comments on commit 0e9c5d3

Please sign in to comment.