From 77fcdf6fc8701910a7d503b7fba89fd284f36833 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Wed, 3 Jan 2024 21:24:24 +0330 Subject: [PATCH] feat(dedupe): new package --- packages/dedupe/README.md | 11 +++++ packages/dedupe/demo/dedupe.mjs | 14 ++++++ packages/dedupe/package.json | 76 +++++++++++++++++++++++++++++++++ packages/dedupe/src/main.ts | 36 ++++++++++++++++ packages/dedupe/tsconfig.json | 11 +++++ yarn.lock | 14 ++++++ 6 files changed, 162 insertions(+) create mode 100644 packages/dedupe/README.md create mode 100644 packages/dedupe/demo/dedupe.mjs create mode 100644 packages/dedupe/package.json create mode 100644 packages/dedupe/src/main.ts create mode 100644 packages/dedupe/tsconfig.json diff --git a/packages/dedupe/README.md b/packages/dedupe/README.md new file mode 100644 index 00000000..0af28969 --- /dev/null +++ b/packages/dedupe/README.md @@ -0,0 +1,11 @@ +# Dedupe + +A global package manager for defining and managing package versions across different parts of applications to prevent version conflicts. + +## Example usage + +```ts +import {definePackage} from '@alwatr/dedupe'; + +definePackage('@alwatr/logger', '2.0.0'); +``` diff --git a/packages/dedupe/demo/dedupe.mjs b/packages/dedupe/demo/dedupe.mjs new file mode 100644 index 00000000..06250964 --- /dev/null +++ b/packages/dedupe/demo/dedupe.mjs @@ -0,0 +1,14 @@ +import {definePackage, definedPackageList} from '@alwatr/dedupe'; + +// Must throw an error + +console.log('define test1 package') +definePackage('@scope/test1'); + +console.log('define test2 package') +definePackage('@scope/test2', 'v1.0.0'); + +console.log('definedPackageList:', definedPackageList) + +console.log('redefine test2 package') +definePackage('@scope/test2'); diff --git a/packages/dedupe/package.json b/packages/dedupe/package.json new file mode 100644 index 00000000..047d955d --- /dev/null +++ b/packages/dedupe/package.json @@ -0,0 +1,76 @@ +{ + "name": "@alwatr/dedupe", + "version": "2.4.1", + "description": "A global package manager for defining and managing package versions across different parts of applications to prevent version conflicts.", + "author": "S. Ali Mihandoost ", + "keywords": [ + "dedupe", + "define-package", + "deduplicate", + "package", + "cross-platform", + "ECMAScript", + "typescript", + "javascript", + "node", + "nodejs", + "esm", + "module", + "utility", + "util", + "utils", + "nanolib", + "alwatr" + ], + "type": "module", + "main": "./dist/main.cjs", + "module": "./dist/main.mjs", + "types": "./dist/main.d.ts", + "exports": { + ".": { + "import": "./dist/main.mjs", + "require": "./dist/main.cjs", + "types": "./dist/main.d.ts" + } + }, + "license": "MIT", + "files": [ + "**/*.{js,mjs,cjs,map,d.ts,html,md}", + "!demo/**/*" + ], + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "https://github.com/Alwatr/nanolib", + "directory": "packages/dedupe" + }, + "homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/dedupe#readme", + "bugs": { + "url": "https://github.com/Alwatr/nanolib/issues" + }, + "prettier": "@alwatr/prettier-config", + "scripts": { + "b": "yarn run build", + "w": "yarn run watch", + "c": "yarn run clean", + "cb": "yarn run clean && yarn run build", + "d": "yarn run build:es && DEBUG=1 yarn node", + "build": "yarn run build:ts & yarn run build:es", + "build:es": "nano-build --preset=module", + "build:ts": "tsc --build", + "watch": "yarn run watch:ts & yarn run watch:es", + "watch:es": "yarn run build:es --watch", + "watch:ts": "yarn run build:ts --watch --preserveWatchOutput", + "clean": "rm -rfv dist *.tsbuildinfo" + }, + "devDependencies": { + "@alwatr/nano-build": "workspace:^", + "@alwatr/prettier-config": "workspace:^", + "@alwatr/tsconfig-base": "workspace:^", + "@alwatr/type-helper": "workspace:^", + "@types/node": "^20.10.6", + "typescript": "^5.3.3" + } +} diff --git a/packages/dedupe/src/main.ts b/packages/dedupe/src/main.ts new file mode 100644 index 00000000..f0710eca --- /dev/null +++ b/packages/dedupe/src/main.ts @@ -0,0 +1,36 @@ +/// + +import type {Dictionary} from '@alwatr/type-helper' + +declare global { + // eslint-disable-next-line no-var + var __dedupe__: true; +} + +if (typeof __dedupe__ !== 'undefined') { + throw new Error('duplicate_dedupe'); +} + +export const definedPackageList: Dictionary = {}; + +/** + * Global define package for managing package versions to prevent version conflicts. + * @param packageName package name including scope. e.g. `@scope/package-name` + * @param version package version (optional) + * + * @example + * ```typescript + * definePackage('@scope/package-name', '2.0.1'); + * ``` + */ +export function definePackage (packageName: string, version = 'v?'): void { + if (packageName in definedPackageList) { + throw new Error('duplicate_package', { + cause: packageName, + }); + } + + definedPackageList[packageName] = version; +} + +definePackage('@alwatr/dedupe', __package_version__); diff --git a/packages/dedupe/tsconfig.json b/packages/dedupe/tsconfig.json new file mode 100644 index 00000000..879b4c5c --- /dev/null +++ b/packages/dedupe/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "@alwatr/tsconfig-base/tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "emitDeclarationOnly": true, + "composite": true, + }, + "include": ["src/**/*.ts"], + "references": [] +} diff --git a/yarn.lock b/yarn.lock index 98d528d9..5eafa5b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -26,6 +26,19 @@ __metadata: languageName: unknown linkType: soft +"@alwatr/dedupe@workspace:^, @alwatr/dedupe@workspace:packages/dedupe": + version: 0.0.0-use.local + resolution: "@alwatr/dedupe@workspace:packages/dedupe" + dependencies: + "@alwatr/nano-build": "workspace:^" + "@alwatr/prettier-config": "workspace:^" + "@alwatr/tsconfig-base": "workspace:^" + "@alwatr/type-helper": "workspace:^" + "@types/node": "npm:^20.10.6" + typescript: "npm:^5.3.3" + languageName: unknown + linkType: soft + "@alwatr/deep-clone@workspace:packages/deep-clone": version: 0.0.0-use.local resolution: "@alwatr/deep-clone@workspace:packages/deep-clone" @@ -110,6 +123,7 @@ __metadata: version: 0.0.0-use.local resolution: "@alwatr/logger@workspace:packages/logger" dependencies: + "@alwatr/dedupe": "workspace:^" "@alwatr/nano-build": "workspace:^" "@alwatr/platform-info": "workspace:^" "@alwatr/prettier-config": "workspace:^"