npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pitijs/core

v0.0.1-beta.2

Published

Piti is a small cli framework developed with typescript.

Downloads

7

Readme

Piti

npm version

Piti is a small cli framework developed with Typescript. You can develop reactive applications by Redux and RxJS in Piti.

Install

$ yarn add @pitijs/core

or

$ npm i @pitijs/core --save

Quick Start

hello.ts

import { Command } from 'piti';

@Command({
  name: 'hello',
  description: 'The hello world command',
})
class HelloCommand {
  handler() {
    console.log('Hello World!');
  }
}

export default HelloCommand;

index.ts

import Piti from 'piti';
import HelloCommand from './hello';

Piti.run({
  scriptName: 'console-app',
  commands: [HelloCommand],
});

Test

$ npx ts-node index.ts hello

Command arguments

Piti uses yargs for command arguments. Created command builder will be inject to command class constructor. So you can be detail your command arguments.

Example:

import { Command } from 'piti';
import { Argv, Arguments } from 'yargs';

@Command({
  name: 'login',
  description: 'Login to platform',
})
class LoginCommand {
  builder(yargs: Argv) {
    builder
      .positional('username', {
        type: 'string',
        describe: 'The user name',
      })
      .positional('password', {
        type: 'string',
        describe: 'The user password',
      });
  }

  handler(args: Arguments) {
    console.log('username:', args.username, 'password:', args.password);
  }
}

export default LoginCommand;

Test

$ npx ts-node index.ts login --username [email protected] --password 1234

For more advanced usage, please visit: http://yargs.js.org

Dependency Injection

With the @Command decorator you can inject parameters into the command class constructor.

@Command({
  name: '...',
  description: '...',
  commands: [],
  inject: [auth, user],
})
class LoginCommand {
  constructor(auth, user) {
    // ...
  }
}

Use with Redux

You can manage state of objects using pure ReduxJS library. For this first of all, you should be configure the redux then pass the store to Piti.

Install Redux:

$ yarn add redux

Create store:

import Piti from 'piti';
import { createStore } from 'redux';

const store = createStore(reducers);

Piti.run({
  commands: [],
  scriptName: 'console-app',
  store,
});

That's all.

Actions and Subscribers

Action creator

const fetchUser = (email: string) => async (dispatch) => {
  try {
    dispatch({ type: 'FETCH_USER_PENDING' });
    const result = await fetch(/**api request**/);
    dispatch({ type: 'FETCH_USER_FULFILLED', data: result });
  } catch (e) {
    dispatch({ type: 'FETCH_USER_REJECTED', error: e });
  }
};

Reducer

const initialState = {
  pending: false,
  error: null,
  user: null,
};

const userReducer = (state = initialState, action) => {
  switch (action.type) {
    case 'FETCH_USER_PENDING': {
      return {
        ...state,
        pending: true,
      };
    },
    case 'FETCH_USER_FULFILLED': {
      return {
        ...state,
        pending: false,
        user: action.data
      }
    },
    case 'FETCH_USER_REJECTED': {
      return {
        error: action.error,
        pending: false,
        user: null,
      }
    }
    default:
      return state;
  }
};

Command

import { Command, Subscribe, dispatch, getState } from 'piti';

@Command({
  name: 'create-user [email]',
  description: 'Create a new user',
})
class CreateUserCommand {
  args = {};

  @Subscribe('FETCH_USER_FULFILLED')
  userAlreadyExists() {
    console.log('The user already created!');
  }

  @Subscribe('FETCH_USER_REJECTED')
  fetchUserRejected() {
    const { user } = getState();
    if (user.error.message === 'user not found') {
      this.createNewUser();
    }
  }

  @Subscribe('CREATE_USER_FULFILLED')
  createUserFulfilled() {
    console.log('User created!');
  }

  createNewUser() {
    const { email } = this.args;
    dispatch(createUser(email));
  }

  handler(args: Arguments) {
    this.args = args;
    dispatch(fetchUser(args.email));
  }
}

Test

$ npx ts-node index.ts create-user [email protected]

RxJS Operators

import { Subject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';

@Command({
  name: 'fetch-users',
  description: 'Fetch users and filter vip ones',
})
class CreateUserCommand {
  @Subscribe({
    action: 'FETCH_USERS_FULFILLED',
    observer(subject: Subject<any>): Observable<any> {
      return subject.pipe(filter((user) => user.isVip));
    },
  })
  fetchUsersFulfilled(result) {
    console.log(result);
    // [{name: 'Thor', isVip: true}]
  }

  handle() {
    dispatch(fetchUsers());
  }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT