Skip to content

Commit

Permalink
feat(dedupe): refactor & new api
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadhonarvar authored and alimd committed Sep 28, 2024
1 parent d4be224 commit 393d873
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
4 changes: 3 additions & 1 deletion packages/dedupe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@
"clean": "rm -rfv dist *.tsbuildinfo"
},
"devDependencies": {
"@alwatr/global-scope": "workspace:^",
"@alwatr/nano-build": "workspace:^",
"@alwatr/prettier-config": "workspace:^",
"@alwatr/tsconfig-base": "workspace:^",
"@alwatr/type-helper": "workspace:^",
"@types/node": "^22.7.4",
"@alwatr/polyfill-has-own": "workspace:^",
"@types/node": "^22.5.5",
"typescript": "^5.6.2"
}
}
65 changes: 36 additions & 29 deletions packages/dedupe/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,59 @@
import {globalScope} from '@alwatr/global-scope';

import type {} from '@alwatr/nano-build';
import type {Dictionary} from '@alwatr/type-helper';

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

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

const globalScope: typeof globalThis =
(typeof globalThis === 'object' && globalThis) ||
(typeof window === 'object' && window) ||
(typeof global === 'object' && global) ||
self;

if (typeof globalScope.__alwatr_dedupe__ === 'undefined') {
globalScope.__alwatr_dedupe__ = true;
globalScope.__alwatr_dedupe__ = __package_version__;
}
else {
console.error('duplicate_alwatr_dedupe', {version: __package_version__});
if (globalScope.__alwatr_dedupe__ === true) {
globalScope.__alwatr_dedupe__ = '1.0.x';
}

console.error(new Error('duplication_detected', {
cause: {
name: '@alwatr/dedupe',
oldVersion: globalScope.__alwatr_dedupe__,
newVersion: __package_version__
},
}));
}

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

/**
* Global define package for managing package versions to prevent version conflicts.
* @param packageName package name including scope. e.g. `@scope/package-name`
* Prevent duplication in any entities like loading node packages.
* @param name package name including scope. e.g. `@scope/package-name`
* @param version package version (optional)
*
* @example
* ```typescript
* definePackage('@scope/package-name', __package_version__);
* deduplicate({name: __package_name__, strict: true});
* ```
*/
export function definePackage(packageName: string, version = 'v?'): void {
if (Object.prototype.hasOwnProperty.call(definedPackageList, packageName)) {
console.error(
new Error('duplicate_package', {
cause: {
packageName,
newVersion: version,
oldVersion: definedPackageList[packageName],
},
}),
);
export function deduplicate(args: {name: string, strict?: true}): void {
if (Object.hasOwn(list, args.name)) {
const error = new Error('duplication_detected', {
cause: {
name: args.name,
},
});

if (args.strict) {
throw error;
}
else {
console.error(error);
}
}

definedPackageList[packageName] = version;
list[args.name] = true;
}

definePackage('@alwatr/dedupe', __package_version__);
deduplicate({name: __package_name__});
2 changes: 1 addition & 1 deletion packages/dedupe/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"composite": true,
},
"include": ["src/**/*.ts"],
"references": [{"path": "../type-helper"}],
"references": [{"path": "../type-helper"}, { "path": "../global-scope" }, {"path": "../polyfill-has-own"}],
}

0 comments on commit 393d873

Please sign in to comment.