Skip to content

Commit

Permalink
Start with piqure to provide and inject
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuk committed Jul 31, 2023
0 parents commit 24bb283
Show file tree
Hide file tree
Showing 13 changed files with 5,015 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
/coverage
65 changes: 65 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module.exports = {
root: true,
env: {
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript',
],
plugins: ['prettier', '@typescript-eslint'],
rules: {
quotes: ['error', 'single', { avoidEscape: true }],
'comma-dangle': [
'error',
{
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'never',
},
],
'import/order': [
'error',
{
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
'newlines-between': 'always',
},
],
'prettier/prettier': [
'error',
{
singleQuote: true,
trailingComma: 'es5',
printWidth: 140,
},
],
},
settings: {
'import/resolver': {
typescript: {
alwaysTryTypes: true,
},
},
'import/core-modules': ['sinon'],
},
parserOptions: {
parser: '@typescript-eslint/parser',
},
overrides: [
{
files: ['**/*.ts', '**/*.tsx'],
rules: {
'no-unused-vars': ['off'],
'no-undef': ['off'],
},
},
],
};
48 changes: 48 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Node CI

on: [push]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-2022, ubuntu-latest]
node-version: [18.x]
steps:
- run: git config --global core.autocrlf false
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: node check
run: |
npm ci
npm run eslint:ci
npm run build
npm run test:ci
deploy:
if: startsWith(github.event.ref, 'refs/tags/')
needs: build
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: 'https://registry.npmjs.org'
- name: deploy
run: |
PACKAGE_VERSION=$(cat package.json|grep version|head -1|awk -F: '{ print $2 }'|sed 's/[", ]//g')
echo "Package: $PACKAGE_VERSION"
echo "Tag: $GITHUB_REF_NAME"
if [ "$PACKAGE_VERSION" != "$GITHUB_REF_NAME" ]; then echo "$PACKAGE_VERSION and $GITHUB_REF_NAME are not the same, skipping"; exit 1; fi
echo "Publishing $PACKAGE_VERSION …"
npm ci
npm run build
npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules/
/.idea/
/coverage/
/dist
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.editorconfig
/.eslintignore
/.eslintrc.js
/.github
/.idea
/coverage
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023 Anthony Rey

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Piqure

Just a dependency injection system in JavaScript.

## Prerequisites

* [Node](https://nodejs.org/) LTS

## Install

```shell
npm i piqure
```

## Usage

Classic usage:

```typescript
import { key, piqure } from './Piqure';

const { provide, inject } = piqure();

// Create an injection key with a type and a description
const KEY_TO_INJECT = key<string>('Key to inject');

// Register a text using provide
provide(KEY_TO_INJECT, 'Injected text');

// Use your text
const injected = inject(KEY_TO_INJECT);

console.log(injected); // Injected text
```

Attach piqure to the map you want:

```typescript
import { piqure } from './Piqure';

const memory = new Map();

const { provide, inject } = piqure(memory); // Now, the injected values will be inside "memory"

// […]
```

Attach piqure to a wrapper like `window`:

```typescript
import { piqureWrapper } from './Piqure';

const { provide, inject } = piqureWrapper(window, 'piqure'); // This will reuse or create a "piqure" field

// […]
```

To expose `provide` and `inject` globally with the `window` (if you're in a browser environment), just create a file in your project with:

```typescript
import { piqureWrapper } from './Piqure';

const { provide, inject } = piqureWrapper(window, 'piqure');

export { provide, inject };
```

## Contribute

Install with:

```shell
npm i
```

Launch tests with:

```shell
npm test
```

Then start coding and don't hesitate to send PR.
Loading

0 comments on commit 24bb283

Please sign in to comment.