Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit ebabfc7

Browse files
authored
feat(packages): 🏗 Split snippets related to package & dependencies (#77)
1 parent 2630f51 commit ebabfc7

File tree

18 files changed

+515
-167
lines changed

18 files changed

+515
-167
lines changed

.changeset/orange-rats-ring.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@terminal-nerds/snippets-package": minor
3+
"@terminal-nerds/snippets-runtime": minor
4+
---
5+
6+
✨ Split snippets related to package and dependencies into a separate package - `package`

packages/config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
],
4343
"dependencies": {
4444
"@terminal-nerds/snippets-error": "workspace:*",
45-
"@terminal-nerds/snippets-object": "workspace:^",
45+
"@terminal-nerds/snippets-object": "workspace:*",
4646
"@terminal-nerds/snippets-runtime": "workspace:*",
4747
"pkg-dir": "7.0.0",
4848
"type-fest": "3.7.2"

packages/package/.depcheckrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignores": ["@terminal-nerds/*-config", "tsup", "vitest"]
3+
}

packages/package/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @terminal-nerds/snippets-package<!-- markdownlint-disable line-length list-marker-space no-duplicate-header ul-style ul-indent no-bare-urls -->

packages/package/LICENSE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/home/xeho91/Nextcloud/Projects/terminal-nerds/snippets/LICENSE.md

packages/package/README.md

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# @terminal-nerds/snippets-package
2+
3+
![package version badge]
4+
[![dependencies badge]][dependencies url]\
5+
[![install size badge]][install size url]
6+
7+
➡️ **This package wraps all available modules with snippets related to [package]**
8+
— part of the [terminal-nerds/snippets] project.
9+
10+
[package]: https://docs.npmjs.com/cli/v9/configuring-npm/package-json
11+
[terminal-nerds/snippets]: https://github.com/terminal-nerds/snippets
12+
[package version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-package/latest?style=for-the-badge&logo=npm
13+
[dependencies badge]: https://img.shields.io/librariesio/release/npm/@terminal-nerds/snippets-package?style=for-the-badge
14+
[dependencies url]: https://libraries.io/npm/@terminal-nerds%2snippets-package
15+
[install size badge]: https://packagephobia.com/badge?p=@terminal-nerds/snippets-package
16+
[install size url]: https://packagephobia.com/result?p=@terminal-nerds/snippets-package
17+
18+
## Modules included
19+
20+
[![documentation badge]][documentation url]
21+
22+
[documentation badge]: https://img.shields.io/static/v1?color=informational&style=for-the-badge&label=documentation&message=jsdocs.io
23+
[documentation url]: https://jsdocs.io/package/@terminal-nerds/snippets-package
24+
25+
<!-- prettier-sort-markdown-table -->
26+
27+
| Name | Size |
28+
| ------------------------------------------------- | ----------------------------------------------------------------- |
29+
| [`@terminal-nerds/snippets-package/dependencies`] | ![dependencies size gzip badge] ![dependencies size brotli badge] |
30+
| [`@terminal-nerds/snippets-package/json`] | ![json size gzip badge] ![json size brotli badge] |
31+
| [`@terminal-nerds/snippets-package/schema`] | ![schema size gzip badge] ![schema size brotli badge] |
32+
33+
<!-- prettier-ignore-start -->
34+
<!-- MODULES LINKS -->
35+
[`@terminal-nerds/snippets-package/dependencies`]: https://github.com/terminal-nerds/snippets/blob/main/packages/package/source/dependencies/dependencies.ts
36+
[dependencies size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-package/dist/dependencies/dependencies.js?label=gzip
37+
[dependencies size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-package/dist/dependencies/dependencies.js?label=brotli
38+
39+
[`@terminal-nerds/snippets-package/json`]: https://github.com/terminal-nerds/snippets/blob/main/packages/package/source/json/json.ts
40+
[json size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-package/dist/json/json.js?label=gzip
41+
[json size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-package/dist/json/json.js?label=brotli
42+
43+
[`@terminal-nerds/snippets-package/schema`]: https://github.com/terminal-nerds/snippets/blob/main/packages/package/source/schema/schema.ts
44+
[schema size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-package/dist/schema/schema.js?label=gzip
45+
[schema size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-package/dist/schema/schema.js?label=brotli
46+
<!-- prettier-ignore-end -->
47+
48+
---
49+
50+
## Prerequisites & usage
51+
52+
### Optional
53+
54+
[![supported typescript version badge]][typescript]
55+
56+
[typescript]: https://typescriptlang.org/
57+
[typescript icon]: https://api.iconify.design/logos/typescript-icon.svg
58+
[supported typescript version badge]: https://img.shields.io/github/package-json/dependency-version/terminal-nerds/snippets/peer/typescript?filename=packages%2Ftypescript%2Fpackage.json&logo=typescript&style=for-the-badge&label=typescript
59+
60+
If you are using ![typescript icon] [TypeScript],
61+
the latest version, which supports new features _(such as `satisfies`)_, is supported.
62+
63+
### Runtime environments
64+
65+
This package can be used in several runtime environments.
66+
We aim for cross-runtime compatibility and ensure proper error messages
67+
if a particular snippet cannot be run in the currently running environment.
68+
69+
#### Browsers
70+
71+
We use [browserslist] to define the minimum browsers versions supported.\
72+
Take a look at our [shared browserslist configuration] for more details.
73+
74+
[browserslist]: https://github.com/browserslist/browserslist
75+
[shared browserslist configuration]: https://github.com/terminal-nerds/configs/blob/main/packages/browserslist/source/browsers.ts
76+
77+
**Usage**:
78+
79+
```html
80+
<script type="module">
81+
import { snippet } from "https://cdn.jsdelivr.net/npm/@terminal-nerds/snippets-package";
82+
</script>
83+
```
84+
85+
---
86+
87+
#### Bun
88+
89+
We aim to support the latest version of ![bun icon] [bun].
90+
91+
**Usage**:
92+
93+
Firstly, install it:
94+
95+
```sh
96+
bun add @terminal-nerds/snippets-package
97+
```
98+
99+
And then in a particular file:
100+
101+
```js
102+
import { snippet } from "@terminal-nerds/snippets-package";
103+
```
104+
105+
[bun]: https://bun.sh/
106+
[bun icon]: https://api.iconify.design/logos/bun.svg
107+
108+
---
109+
110+
#### Deno
111+
112+
We aim to support the latest version of ![deno icon] [Deno].
113+
114+
**Usage**:
115+
116+
```ts
117+
import { snippet } from "npm:@terminal-nerds/snippets-package";
118+
```
119+
120+
[deno]: https://deno.land/
121+
[deno icon]: https://api.iconify.design/logos/deno.svg
122+
123+
---
124+
125+
#### Node.js
126+
127+
[![node.js version support badge]][node.js]
128+
129+
The latest ![node.js icon] [Node.js] LTS _(Long-Term Support)_ version is the minimum one supported.
130+
131+
> **Warning**\
132+
> **This package is written in [ES Module] _(ESM)_ type.**\
133+
> So, if you wish to use it in a project with CommonJS (CJS) type, you need to bundle this package or a particular module(s).
134+
135+
**Usage**:
136+
137+
Install it first with the Node.js package manager of your choice. In our example, we use [pnpm].
138+
139+
```sh
140+
pnpm add @terminal-nerds/snippets-package
141+
```
142+
143+
And then in a particular file:
144+
145+
```js
146+
import { snippet } from "@terminal-nerds/snippets-package";
147+
```
148+
149+
[ES Module]: https://www.freecodecamp.org/news/javascript-es-modules-and-module-bundlers
150+
[pnpm]: https://pnpm.io
151+
[node.js]: https://nodejs.org/en/
152+
[node.js icon]: https://api.iconify.design/logos/nodejs-icon.svg
153+
[node.js version support badge]: https://img.shields.io/node/v-lts/@terminal-nerds/snippets?style=for-the-badge&logo=nodedotjs
154+
155+
---
156+
157+
## Security
158+
159+
[![workflow security badge]][security policy]
160+
161+
🔐 For more information, please refer to the [Security section] at the root of
162+
the [terminal-nerds/snippets] monorepo.
163+
164+
[workflow security badge]: https://img.shields.io/github/actions/workflow/status/terminal-nerds/snippets/maintenance.yml?label=Security&logo=github&style=for-the-badge&branch=main
165+
[security section]: https://github.com/terminal-nerds/snippets#security
166+
[security policy]: https://github.com/terminal-nerds/snippets/security/policy
167+
168+
---
169+
170+
## License
171+
172+
[![license badge]][license]
173+
174+
⚖️ For more information, please refer to the [License section] at the root of the [terminal-nerds/snippets] monorepo.
175+
176+
[license]: https://github.com/terminal-nerds/snippets/blob/main/LICENSE.md
177+
[license badge]: https://img.shields.io/github/license/terminal-nerds/snippets?style=for-the-badge
178+
[license section]: https://github.com/terminal-nerds/snippets#License
179+
180+
### Contributing
181+
182+
[![contributors badge]][contributors url]
183+
184+
🤝 **Contributions of any kind are welcome!**
185+
186+
Please refer to the monorepo _([terminal-nerds/snippets])_ project's [CONTRIBUTING file] for more information
187+
if you wish to get involved.
188+
189+
[contributing file]: https://github.com/terminal-nerds/snippets/blob/main/.github/CONTRIBUTING.md
190+
[contributors badge]: https://img.shields.io/github/contributors/terminal-nerds/snippets?style=for-the-badge
191+
[contributors url]: https://github.com/terminal-nerds/snippets#contributors
192+
193+
### Author
194+
195+
🎉 The idea of this project was initiated by [xeho91]. However, it's the [contributors] who matter the most.
196+
197+
[contributors]: https://github.com/terminal-nerds/snippets/blob/main/README.md#project-contributors
198+
[xeho91]: https://github.com/xeho91

packages/package/package.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"$schema": "https://json.schemastore.org/package",
3+
"type": "module",
4+
"name": "@terminal-nerds/snippets-package",
5+
"version": "0.0.0",
6+
"description": "@terminal-nerds reusable snippets for packages.",
7+
"keywords": [
8+
"snippets",
9+
"typescript",
10+
"package"
11+
],
12+
"author": {
13+
"name": "Mateusz Kadlubowski",
14+
"email": "[email protected]",
15+
"url": "https://xeho91.com"
16+
},
17+
"license": "MIT",
18+
"homepage": "https://github.com/terminal-nerds/snippets",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/terminal-nerds/snippets.git",
22+
"directory": "packages/package"
23+
},
24+
"bugs": "https://github.com/terminal-nerds/snippets/issues",
25+
"engines": {
26+
"node": ">=18"
27+
},
28+
"exports": {
29+
".": {
30+
"types": "./dist/main.d.ts",
31+
"import": "./dist/main.js",
32+
"require": "./dist/main.cjs"
33+
},
34+
"./*": {
35+
"types": "./dist/*/*.d.ts",
36+
"import": "./dist/*/*.js",
37+
"require": "./dist/*/*.cjs"
38+
}
39+
},
40+
"files": [
41+
"dist/"
42+
],
43+
"dependencies": {
44+
"@terminal-nerds/snippets-error": "workspace:*",
45+
"@terminal-nerds/snippets-runtime": "workspace:*",
46+
"read-pkg-up": "9.1.0",
47+
"type-fest": "3.7.2"
48+
},
49+
"peerDependencies": {
50+
"typescript": "5.0.2"
51+
},
52+
"peerDependenciesMeta": {
53+
"typescript": {
54+
"optional": true
55+
}
56+
},
57+
"devDependencies": {
58+
"@terminal-nerds/snippets-test": "workspace:*"
59+
},
60+
"scripts": {
61+
"build": "tsup",
62+
"clean": "concurrently \"pnpm:clean:*\" --group --timings",
63+
"clean:build": "del \"./dist\"",
64+
"clean:cache": "del \"./node_modules/.cache\"",
65+
"clean:test": "del \"./coverage\"",
66+
"dev:pkg": "tsup --watch",
67+
"dev:test": "vitest watch --ui",
68+
"fix": "concurrently \"pnpm:fix:*\" --group --timings",
69+
"fix:format": "pretty-quick --branch main --pattern \"packages/package/**/*\" --verbose",
70+
"fix:js": "pnpm lint:js --fix",
71+
"fix:md": "pnpm lint:md --fix",
72+
"fix:pkg": "syncpack format ; syncpack fix-mismatches",
73+
"lint": "concurrently \"pnpm:lint:*\" --group --timings",
74+
"lint:deps": "depcheck",
75+
"lint:format": "pnpm fix:format --check",
76+
"lint:js": "DEBUG=\"eslint:cli-engine\" eslint . --ext .cjs,.js,.ts,.json,.yml --cache --cache-location \"./node_modules/.cache/eslint\"",
77+
"lint:md": "markdownlint . --config \"../../.markdownlint.json\" --dot --ignore \"./LICENSE.md\" --ignore-path \"../../.gitignore\"",
78+
"lint:pkg": "syncpack list-mismatches",
79+
"lint:types": "tsc --noEmit",
80+
"test:unit": "vitest run --dir \"./source\""
81+
}
82+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { returns } from "@terminal-nerds/snippets-test/unit";
2+
import { describe, expect, it } from "vitest";
3+
4+
import { getDependenciesMap, getPackagesNames } from "./dependencies.ts";
5+
6+
describe(`getDependenciesMap()`, () => {
7+
const map = getDependenciesMap();
8+
9+
it(returns(map).on(`the current package directory`), () => {
10+
expect(map).toBeInstanceOf(Map);
11+
});
12+
13+
const expectedKeys = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"] as const;
14+
15+
it(`has keys: ${JSON.stringify(expectedKeys, undefined, 1)}`, () => {
16+
expect([...map.keys()]).toStrictEqual(expectedKeys);
17+
});
18+
19+
/**
20+
* NOTE: This is apparently an expected behaviour...?
21+
*
22+
* @see {@link https://github.com/npm/cli/issues/1886}
23+
*/
24+
it.fails(`'dependencies' at the root is '${undefined}'`, () => {
25+
expect(map.get("dependencies")).toBeUndefined();
26+
});
27+
28+
// eslint-disable-next-line unicorn/prevent-abbreviations
29+
const expectedDevDependencyName = "eslint";
30+
31+
it(`'devDeependencies' at the root has '${expectedDevDependencyName}'`, () => {
32+
expect(map.get("devDependencies")).toHaveProperty(expectedDevDependencyName);
33+
});
34+
35+
it(`'peerDeependencies' at the root has '${undefined}'`, () => {
36+
expect(map.get("peerDependencies")).toBeUndefined();
37+
});
38+
39+
const expectedOptionalDependencyName = "husky";
40+
41+
it(`'optionalDeependencies' at the root has '${expectedOptionalDependencyName}'`, () => {
42+
expect(map.get("optionalDependencies")).toHaveProperty(expectedOptionalDependencyName);
43+
});
44+
});
45+
46+
describe(`getAllPackagesNames()`, () => {
47+
const packages = getPackagesNames();
48+
49+
it(returns(packages).on(`the current repository with all package names`), () => {
50+
expect(packages).toBeInstanceOf(Set);
51+
expect(packages).toContain("typescript");
52+
});
53+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { readPackageJSON } from "../json/json.ts";
2+
import type { PackageName, PackageVersion } from "../schema/schema.ts";
3+
4+
export type DepedencyType = "dependencies" | "devDependencies" | "optionalDependencies" | "peerDependencies";
5+
export type Depedencies = Partial<Record<PackageName, PackageVersion>>;
6+
7+
export function getDependenciesMap(): Map<DepedencyType, Depedencies | undefined> {
8+
const { dependencies, devDependencies, optionalDependencies, peerDependencies } = readPackageJSON();
9+
10+
return new Map(
11+
Object.entries({
12+
dependencies,
13+
devDependencies,
14+
optionalDependencies,
15+
peerDependencies,
16+
}) as Array<[DepedencyType, Depedencies]>,
17+
);
18+
}
19+
20+
/** TODO: Add targetting, for better debugging (required, dev, optional or peer) */
21+
export function getDepedencies(): Depedencies {
22+
const { dependencies, devDependencies, optionalDependencies, peerDependencies } = readPackageJSON();
23+
24+
return { ...dependencies, ...devDependencies, ...optionalDependencies, ...peerDependencies };
25+
}
26+
27+
export function getPackagesNames(): Set<PackageName> {
28+
return new Set(Object.keys(getDepedencies()));
29+
}
30+
31+
/**
32+
* TODO: Add targetting, for better debugging (required, dev, optional or peer)
33+
*/
34+
export function hasPackage(name: string): boolean {
35+
return getPackagesNames().has(name);
36+
}

0 commit comments

Comments
 (0)