Skip to content

Commit ddaa42d

Browse files
authored
Merge pull request #1 from luistak/refactor/pkg-usage
refactor: changed package name to pkg-usage and implemented tests
2 parents 2d27ae9 + 54eeac7 commit ddaa42d

File tree

17 files changed

+360
-89
lines changed

17 files changed

+360
-89
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist/
22
example/
33
coverage/*
4-
node_modules/
4+
node_modules/
5+
__mocks__/

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"parser": "@typescript-eslint/parser",
44
"plugins": ["@typescript-eslint/eslint-plugin"],
55
"parserOptions": {
6+
"sourceType": "module",
67
"project": "./tsconfig.json"
78
},
89
"env": {
9-
"node": true
10+
"node": true,
11+
"jest": true
1012
}
1113
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ build/Release
4141
node_modules/
4242
jspm_packages/
4343

44+
# Mocks directories
45+
__mocks__/
46+
4447
# TypeScript v1 declaration files
4548
typings/
4649

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
We love pull requests. And following this guidelines will make your pull request easier to merge.
44

5-
If you want to contribute but don’t know what to do, take a look at these two labels: [help wanted](https://github.com/luistak/npm-package-usage/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and [good first issue](https://github.com/luistak/npm-package-usage/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22).
5+
If you want to contribute but don’t know what to do, take a look at these two labels: [help wanted](https://github.com/luistak/pkg-usage/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and [good first issue](https://github.com/luistak/pkg-usage/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22).
66

77
_[Use GitHub interface](https://blog.sapegin.me/all/open-source-for-everyone/) for simple documentation changes, otherwise follow the steps below._
88

README.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,57 @@
1-
# WIP: 🏗️ npm-pkg-usage 🚧
1+
# 📦 pkg-usage
22

3-
[![npm](https://img.shields.io/npm/v/npm-pkg-usage.svg)](https://www.npmjs.com/package/npm-pkg-usage)
3+
[![npm](https://img.shields.io/npm/v/pkg-usage.svg)](https://www.npmjs.com/package/pkg-usage)
44

5-
> ⚠️ This library is under development so feel free to check it out but it's not ready for production.
5+
## Installation
6+
7+
```sh
8+
npm install windowed-observable
9+
10+
# or
11+
12+
yarn add windowed-observable
13+
```
614

715
## Introduction
816

917
Have you ever wondered how your library is being used in other repositories? It's your lucky day because this package aims to solve it.
1018

11-
Given a `npm-pkg-usage.yml` file you could configure and gets details about how your library is being imported
19+
Given a few options you can gets details about how your library is being imported along with its version
20+
21+
## Usage
22+
23+
```js
24+
import { getPackagesUsages } from 'pkg-usage';
25+
26+
const usages = getPackagesUsages({
27+
packages: ['react'],
28+
fileGlobs: `**/**.ts`,
29+
});
30+
31+
console.log(usages);
32+
```
33+
34+
Output
35+
36+
```json
37+
[
38+
{
39+
count: 1,
40+
files: [
41+
{
42+
defaultImport: undefined,
43+
name: `${fileName}.ts`,
44+
namedImports: imports,
45+
},
46+
],
47+
name: pkg,
48+
version,
49+
}
50+
]
51+
```
1252

13-
### Current features:
53+
## Current features:
1454

55+
- The package version
1556
- How many files are importing your `package`
1657
- What are the default and named imports by file

__tests__/pkg-usage.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
MOCKS_DIR,
3+
MOCKS_DIR_CWD,
4+
mockPackageUsageFile,
5+
cleanupMockFiles,
6+
} from './utils';
7+
import { getPackagesUsages } from '../src';
8+
9+
afterEach(() => {
10+
cleanupMockFiles();
11+
});
12+
13+
beforeEach(() => {
14+
cleanupMockFiles();
15+
});
16+
17+
describe('getPackagesUsages()', () => {
18+
describe('given a wrong file glob', () => {
19+
it('should THROW an Error', () => {
20+
const { pkg } = mockPackageUsageFile();
21+
22+
expect(() =>
23+
getPackagesUsages({
24+
packages: [pkg],
25+
fileGlobs: `WRONG_FILE_GLOB/**.xyz`,
26+
})
27+
).toThrow();
28+
});
29+
});
30+
31+
describe('given a file without import declaration', () => {
32+
it('should return a package usage without a single count', () => {
33+
const { pkg, version } = mockPackageUsageFile(
34+
` file data without import declaration `
35+
);
36+
37+
expect(
38+
getPackagesUsages({
39+
packages: [pkg],
40+
fileGlobs: `${MOCKS_DIR}/**.ts`,
41+
packageJsonCWD: MOCKS_DIR_CWD,
42+
})
43+
).toStrictEqual([{ count: 0, files: [], name: pkg, version }]);
44+
});
45+
});
46+
47+
describe('given any package name and named imports', () => {
48+
it('should return the right package usage', () => {
49+
const { fileName, imports, pkg, version } = mockPackageUsageFile();
50+
51+
expect(
52+
getPackagesUsages({
53+
packages: [pkg],
54+
fileGlobs: `${MOCKS_DIR}/**.ts`,
55+
packageJsonCWD: MOCKS_DIR_CWD,
56+
})
57+
).toStrictEqual([
58+
{
59+
count: 1,
60+
files: [
61+
{
62+
defaultImport: undefined,
63+
name: `${fileName}.ts`,
64+
namedImports: imports,
65+
},
66+
],
67+
name: pkg,
68+
version,
69+
},
70+
]);
71+
});
72+
});
73+
});

__tests__/utils.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { join, dirname } from 'path';
2+
import { cwd } from 'process';
3+
import { writeFileSync, existsSync, mkdirSync, rmdirSync } from 'fs';
4+
import f from 'faker';
5+
6+
export const MOCKS_DIR = '__mocks__';
7+
export const MOCKS_DIR_CWD = join(join(cwd(), MOCKS_DIR));
8+
9+
function writeFile(fileName: string, data: string) {
10+
const path = `${MOCKS_DIR}/${fileName}`;
11+
12+
if (!existsSync(dirname(path))) {
13+
mkdirSync(dirname(path));
14+
}
15+
16+
writeFileSync(join(cwd(), path), data);
17+
}
18+
19+
export function cleanupMockFiles() {
20+
rmdirSync(MOCKS_DIR, { recursive: true });
21+
}
22+
23+
function mockUniqueList<T, U>(
24+
// eslint-disable-next-line no-unused-vars
25+
mapFn: (value: T, index: number, array: T[]) => U
26+
) {
27+
return new Array(f.datatype.number({ min: 2, max: 10 }))
28+
.fill('')
29+
.map(mapFn)
30+
.filter((value, index, self) => self.indexOf(value) === index);
31+
}
32+
33+
export function mockPackageUsageFile(customData?: string) {
34+
const imports = mockUniqueList(() => f.hacker.noun().replace(/ |-/g, ''));
35+
const fileName = f.datatype.uuid();
36+
const pkg = f.hacker.noun();
37+
const version = f.system.semver();
38+
39+
// Mocked with random data to generate a resilient test
40+
const data = customData ?? `import { ${imports.join(', ')} } from '${pkg}';`;
41+
writeFile(`${fileName}.ts`, data);
42+
43+
writeFile(
44+
'package.json',
45+
JSON.stringify({
46+
name: 'example',
47+
version: '1.0.0',
48+
main: 'index.js',
49+
author: 'Luis Takahashi',
50+
license: 'MIT',
51+
dependencies: {
52+
[pkg]: version,
53+
},
54+
})
55+
);
56+
57+
return {
58+
imports,
59+
fileName,
60+
pkg,
61+
version,
62+
};
63+
}

example/npm-pkg-usage.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

example/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"author": "Luis Takahashi",
6+
"license": "MIT",
7+
"dependencies": {
8+
"bla": "^0.0.1"
9+
},
10+
"peerDependencies": {
11+
"@scoped/package": "^4.3.5"
12+
},
13+
"devDependencies": {
14+
"@scoped/package1": "^1.0.0"
15+
}
16+
}

package.json

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "npm-pkg-usage",
2+
"name": "pkg-usage",
33
"version": "0.1.0",
44
"description": "Npm package usage metrics",
55
"main": "dist/index.js",
@@ -8,16 +8,20 @@
88
"dist",
99
"example"
1010
],
11-
"repository": "https://github.com/luistak/npm-package-usage.git",
11+
"bin": {
12+
"pkg-usage": "./scripts/pkg-usage.js"
13+
},
14+
"repository": "https://github.com/luistak/pkg-usage.git",
1215
"bugs": {
13-
"url": "https://github.com/luistak/npm-package-usage/issues"
16+
"url": "https://github.com/luistak/pkg-usage/issues"
1417
},
15-
"homepage": "https://github.com/luistak/npm-package-usage",
18+
"homepage": "https://github.com/luistak/pkg-usage",
1619
"author": "Luis Takahashi <takahashihideki408@gmail.com>",
1720
"license": "MIT",
1821
"private": false,
1922
"devDependencies": {
2023
"@tsconfig/node14": "^1.0.1",
24+
"@types/faker": "^5.5.8",
2125
"@types/jest": "^26.0.24",
2226
"@types/js-yaml": "^4.0.2",
2327
"@types/node": "^16.4.2",
@@ -26,8 +30,8 @@
2630
"commitizen": "^4.2.4",
2731
"cz-conventional-changelog": "3.3.0",
2832
"eslint": "^7.31.0",
33+
"faker": "^5.5.3",
2934
"jest": "^27.0.6",
30-
"js-yaml": "^4.1.0",
3135
"prettier": ">=2",
3236
"ts-jest": "^27.0.4",
3337
"ts-node": "^10.1.0",
@@ -36,10 +40,10 @@
3640
"scripts": {
3741
"commit": "cz",
3842
"preexample": "yarn build",
39-
"example": "cd ./example && node run-in-ci.js",
43+
"example": "cd ./example && node ../scripts/pkg-usage.js",
4044
"lint": "eslint . --cache --fix --ext .ts,.tsx",
4145
"format": "prettier --loglevel warn --write \"**/*.{ts,tsx,css,md}\"",
42-
"test:ci": "yarn test --passWithNoTests",
46+
"test:ci": "CI=true yarn test:coverage",
4347
"test:jest": "jest",
4448
"test:watch": "jest --watch",
4549
"test:coverage": "jest --coverage",
@@ -50,21 +54,20 @@
5054
"jest": {
5155
"testPathIgnorePatterns": [
5256
"/node_modules/",
53-
"<rootDir>/lib/"
57+
"/__tests__/utils.ts"
58+
],
59+
"coveragePathIgnorePatterns": [
60+
"/__tests__/utils.ts"
5461
],
5562
"transform": {
56-
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
63+
"^.+\\.tsx?$": "ts-jest"
5764
},
58-
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(ts?|tsx?)$",
59-
"moduleFileExtensions": [
60-
"ts",
61-
"tsx",
62-
"js",
63-
"jsx",
64-
"json"
65+
"modulePathIgnorePatterns": [
66+
"__mocks__"
6567
]
6668
},
6769
"dependencies": {
70+
"pkg-up": "^3.1.0",
6871
"ts-morph": "^11.0.3"
6972
},
7073
"release": {
@@ -78,4 +81,4 @@
7881
"path": "cz-conventional-changelog"
7982
}
8083
}
81-
}
84+
}

0 commit comments

Comments
 (0)