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

Commit 2aa8755

Browse files
authored
feat(packages): ✨ Add snippets related to project (#75)
1 parent 0e39b7a commit 2aa8755

File tree

16 files changed

+443
-0
lines changed

16 files changed

+443
-0
lines changed

.changeset/healthy-bobcats-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@terminal-nerds/snippets-project": minor
3+
---
4+
5+
✨ Added a new module `monorepo` for grouped snippets

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Reusable pieces of code, which can be **reused** across the projects.
100100
| ------------------------------- | ------------------------------------------------ |
101101
| [![snippets badge]][snippets] | [![snippets version badge]][snippets npm page] |
102102
| <!-- PACKAGES --> | <!-- PACKAGES --> |
103+
| [![project badge]][project] | [![project version badge]][project npm page] |
103104
| [![object badge]][object] | [![object version badge]][object npm page] |
104105
| [![url badge]][url] | [![url version badge]][url npm page] |
105106
| [![array badge]][array] | [![array version badge]][array npm page] |
@@ -120,6 +121,11 @@ Reusable pieces of code, which can be **reused** across the projects.
120121

121122
<!-- prettier-ignore-start -->
122123
<!-- PACKAGES LINKS -->
124+
[project]: ./packages/project/README.md
125+
[project badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-project&style=flat-square&color=informational
126+
[project version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-project/latest?style=flat-square&logo=npm
127+
[project npm page]: https://www.npmjs.com/package/@terminal-nerds/snippets-project
128+
123129
[object]: ./packages/object/README.md
124130
[object badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-object&style=flat-square&color=informational
125131
[object version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-object/latest?style=flat-square&logo=npm

packages/project/.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/project/CHANGELOG.md

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

packages/project/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/project/README.md

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

packages/project/package.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"$schema": "https://json.schemastore.org/package",
3+
"type": "module",
4+
"name": "@terminal-nerds/snippets-project",
5+
"version": "0.0.0",
6+
"description": "@terminal-nerds reusable snippets for projects.",
7+
"keywords": [
8+
"snippets",
9+
"typescript",
10+
"project"
11+
],
12+
"license": "MIT",
13+
"author": {
14+
"name": "Mateusz Kadlubowski",
15+
"email": "[email protected]",
16+
"url": "https://xeho91.com"
17+
},
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/project"
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+
"scripts": {
44+
"build": "tsup",
45+
"clean": "concurrently \"pnpm:clean:*\" --group --timings",
46+
"clean:build": "del \"./dist\"",
47+
"clean:cache": "del \"./node_modules/.cache\"",
48+
"clean:test": "del \"./coverage\"",
49+
"dev:test": "vitest watch --ui",
50+
"dev:pkg": "tsup --watch",
51+
"fix": "concurrently \"pnpm:fix:*\" --group --timings",
52+
"fix:format": "pretty-quick --branch main --pattern \"packages/project/**/*\" --verbose",
53+
"fix:js": "pnpm lint:js --fix",
54+
"fix:md": "pnpm lint:md --fix",
55+
"fix:pkg": "syncpack format ; syncpack fix-mismatches",
56+
"lint": "concurrently \"pnpm:lint:*\" --group --timings",
57+
"lint:deps": "depcheck",
58+
"lint:format": "pnpm fix:format --check",
59+
"lint:js": "DEBUG=\"eslint:cli-engine\" eslint . --ext .cjs,.js,.ts,.json,.yml --cache --cache-location \"./node_modules/.cache/eslint\"",
60+
"lint:md": "markdownlint . --config \"../../.markdownlint.json\" --dot --ignore \"./LICENSE.md\" --ignore-path \"../../.gitignore\"",
61+
"lint:pkg": "syncpack list-mismatches",
62+
"lint:types": "tsc --noEmit",
63+
"test:unit": "vitest run --dir \"./source\""
64+
},
65+
"peerDependencies": {
66+
"typescript": "5.0.2"
67+
},
68+
"devDependencies": {
69+
"@terminal-nerds/snippets-test": "workspace:*"
70+
},
71+
"peerDependenciesMeta": {
72+
"typescript": {
73+
"optional": true
74+
}
75+
},
76+
"dependencies": {
77+
"@terminal-nerds/snippets-error": "workspace:*",
78+
"@terminal-nerds/snippets-runtime": "workspace:*",
79+
"monorepo-root": "2.3.0"
80+
}
81+
}

packages/project/source/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* MODULES */
2+
export * from "./monorepo/monorepo.ts";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { join } from "node:path";
2+
3+
import { RuntimeError } from "@terminal-nerds/snippets-error/custom";
4+
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
5+
import { describe, it } from "vitest";
6+
7+
import { getProjectMonorepoRootPath, isProjectMonorepo } from "./monorepo.ts";
8+
9+
describe(`getProjectMonorepoRoothPath(cwd?)`, () => {
10+
const sampleCwd = join(process.env["HOME"] as string);
11+
12+
it(throws(RuntimeError).on(`random cwd`).sample(sampleCwd), ({ expect }) => {
13+
expect(() => getProjectMonorepoRootPath(sampleCwd)).toThrowError(RuntimeError);
14+
});
15+
16+
it(returns({ what: "String", value: "<project monorepo path>" }).on(`monorepo project`), ({ expect }) => {
17+
expect(getProjectMonorepoRootPath()).toBeTypeOf("string");
18+
});
19+
});
20+
21+
describe(`isProjectMonorepo()`, () => {
22+
// TODO: Find a way to test this case.
23+
describe.todo("returns false on random cwd - don't know how to test it yet.");
24+
25+
it(returns(true).on(`monorepo project`), ({ expect }) => {
26+
expect(isProjectMonorepo()).toBe(true);
27+
});
28+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { RuntimeError } from "@terminal-nerds/snippets-error/custom";
2+
import { IN_BROWSER } from "@terminal-nerds/snippets-runtime/environment";
3+
import { monorepoRootSync } from "monorepo-root";
4+
5+
type Path = `/${string}`;
6+
7+
export function getProjectMonorepoRootPath(cwd?: string): Path {
8+
if (IN_BROWSER) {
9+
throw new RuntimeError(`You cannot determine if the current process is monorepo inside the browser.`);
10+
} else {
11+
const monorepoPath = monorepoRootSync(cwd);
12+
13+
if (monorepoPath) {
14+
return monorepoPath as Path;
15+
} else {
16+
throw new RuntimeError(
17+
`The current project is either not a monorepo, or there was an issue determining it.`,
18+
);
19+
}
20+
}
21+
}
22+
23+
export function isProjectMonorepo(): boolean {
24+
try {
25+
getProjectMonorepoRootPath();
26+
27+
return true;
28+
} catch {
29+
return false;
30+
}
31+
}

0 commit comments

Comments
 (0)