Skip to content

Commit

Permalink
feat(dedupe): new package (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd authored Jan 3, 2024
2 parents bc7c7ee + e2e5c6c commit 0425ca4
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 6 deletions.
17 changes: 17 additions & 0 deletions packages/dedupe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Dedupe

A package manager helper tool for debug list of defined (imported) packages in your ecosystem and prevent to duplicate import (install) multiple versions of the same package in your project (deduplicate packages).

## Example usage

```ts
import {definePackage} from '@alwatr/dedupe';

definePackage('@alwatr/logger', '2.0.0');
```

You can use __package_version__ to automatically obtain the version of the package if you are using @alwatr/nano-build to build your package.

```ts
definePackage('@alwatr/logger', __package_version__);
```
14 changes: 14 additions & 0 deletions packages/dedupe/demo/dedupe.mjs
Original file line number Diff line number Diff line change
@@ -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');
77 changes: 77 additions & 0 deletions packages/dedupe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"name": "@alwatr/dedupe",
"version": "2.4.1",
"description": "A package manager helper tool for debug list of defined (imported) packages in your ecosystem and prevent to duplicate import (install) multiple versions of the same package in your project (deduplicate packages).",
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
"keywords": [
"dedupe",
"define-package",
"deduplicate",
"package",
"package-manager",
"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"
}
}
37 changes: 37 additions & 0 deletions packages/dedupe/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type {Dictionary} from '@alwatr/type-helper'

declare global {
// eslint-disable-next-line no-var
var __dedupe__: true;

// eslint-disable-next-line no-var
var __package_version__: string;
}

if (typeof __dedupe__ !== 'undefined') {
throw new Error('duplicate_dedupe');
}

export const definedPackageList: Dictionary<string> = {};

/**
* 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', __package_version__);
* ```
*/
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__);
11 changes: 11 additions & 0 deletions packages/dedupe/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@alwatr/tsconfig-base/tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"emitDeclarationOnly": true,
"composite": true,
},
"include": ["src/**/*.ts"],
"references": []
}
2 changes: 1 addition & 1 deletion packages/logger/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Alwatr Logger - `@alwatr/logger`
# Logger

Fancy colorful console debugger with custom scope written in tiny TypeScript, ES module.

Expand Down
7 changes: 4 additions & 3 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
"clean": "rm -rfv dist *.tsbuildinfo"
},
"dependencies": {
"@alwatr/dedupe": "workspace:^",
"@alwatr/platform-info": "workspace:^"
},
"devDependencies": {
"@alwatr/nano-build": "workspace:^",
"@alwatr/prettier-config": "workspace:^",
"@alwatr/tsconfig-base": "workspace:^",
"@types/node": "^20.10.6",
"typescript": "^5.3.3"
},
"dependencies": {
"@alwatr/platform-info": "workspace:^"
}
}
3 changes: 3 additions & 0 deletions packages/logger/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {definePackage} from '@alwatr/dedupe';
import {platformInfo} from '@alwatr/platform-info';

import type {AlwatrLogger} from './type.js';

definePackage('@alwatr/logger', __package_version__);

export const debugMode = platformInfo.development || platformInfo.isCli
? process.env.DEBUG !== undefined && process.env.DEBUG !== ''
: typeof localStorage !== 'undefined' && localStorage.getItem('debug') === '1';
Expand Down
2 changes: 1 addition & 1 deletion packages/nano-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Add the following scripts to your `package.json`:
js: "/* @package_name v@package_version */"
},
define: {
__package_version: `'@package_version'`,
__package_version__: `'@package_version'`,
},
}
```
Expand Down
5 changes: 5 additions & 0 deletions packages/nano-build/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {};

declare global {
var __package_version__: string;
}
2 changes: 1 addition & 1 deletion packages/nano-build/nano-build.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const defaultOptions = {
js: '/* ' + packageJson.name + ' v' + packageJson.version + ' */',
},
define: {
__package_version: `'${packageJson.version}'`,
__package_version__: `'${packageJson.version}'`,
},
};

Expand Down
2 changes: 2 additions & 0 deletions packages/nano-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
],
"type": "commonjs",
"bin": "./nano-build.cjs",
"main": "",
"types": "./global.d.ts",
"license": "MIT",
"files": [
"**/*.{js,mjs,cjs,map,d.ts,html,md}",
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:^"
Expand Down

0 comments on commit 0425ca4

Please sign in to comment.