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

@powerkraut/data-utils-js

v1.6.0

Published

JS Data Utils for PowerKraut Data Projects

Downloads

67

Readme

NPM Package for PowerKraut Data Utils.

Utility library to be used by PowerKraut data projects.

Installation

npm install @powerkraut/data-utils-js --save

Available Modules

  • GoogleCloud\Logging\Logger This allows logging to stackdriver logging API, with support for labels and severity levels.
  • GoogleCloud\Functions\sendMessage A helper binary to allow sending test to your local cloud-functions framework.

The documentation of the individual modules can be found in the class document blocks.

Config installation / loader

This packages contains several classes to support with config installation / loading. To allow the installation of a config, create a js script to handle the installation, for example: cli/installConfig.js. The default install script in this package can be used to create a basic config installation.

import {InstallConfig} from "@powerkraut/data-utils-js";

const installer = new InstallConfig({
    configDefaultPath: `path/to/config/output`,
});

// Execute the script
installer.handle();

The install script can be extended to adopt custom installation logic if necessary. The ConfigLoader class can then be used to read the configuration and load them into environment variables. To create an instance of this class use the following code:

import {ConfigLoader} from "@powerkraut/data-utils-js";

const loader = new ConfigLoader({
    configDefaultFile: '[config-file-name].js',
    configDefaultPath: 'path/to/config/[config-file-name].js'
});

This class is also used by the AbstractCli class, used to create CLI commands.

CLI commands

Creating CLI commands you should extend the AbstractCli class from this packages. For now this class only allows access to the config loader. Be sure to call the super constructor with the correct config info.

import {AbstractCli} from "@powerkraut/data-utils.js";

class SayHello extends AbstractCli {
    constructor() {
        super({
            configDefaultFile: '[config-file-name].js',
            configDefaultPath: 'path/to/config/[config-file-name].js'
        });
    }
    
    handle() {
        console.log('Hello from: ' + super.configLoader.getCwdDir());
    }
}

GCloud / Deployment

This package exposes several classes to help with deployments. To start with, the GCloudCli class allows for the execution of commands to google cloud. The class contains several pre-coded commands and has a method to execute any command. The gcloud prefix should be omitted.

import {GCloudCli} from "@powerkraut/data-utils.js";

const cli = new GCloudCli();

cli.getCurrentProject();
cli.setProject('[project]');
cli.getIdentityToken();

// Or execute any other command:
cli.exec(['config', 'get', 'project']);

The GCloudCli is used by this package to handle deployments. The GoogleDeploymentManager class extends the GCloudCli class and adds CRUD methods to manage deployments. Combining the CLI and deployment classes, the DeployCloudFunction class adds helper methods to assist with the creation of a deployment script.

Like the installation of the config file, a js file should be created to allow cli execution, for example: cli/deployCloudFunction.js. A most basic implementation could look like this:

DeploymentScript.js

import {DeployCloudFunction} from "@powerkraut/data-utils-js";

export default class DeploymentScript extends DeployCloudFunction {
    constructor() {
        super(
            {
                npmRcDistPath:      './../../../.npmrc.dist',
                cloudFunctionsPath: `path/to/cloud-function`
            },
            {
                configDefaultFile: '[config-file-name].js',
                configDefaultPath: 'path/to/config/[config-file-name].js'
            }
        );
    }

    /**
     * @return {Promise<void>}
     */
    async handle() {
        await super.configLoader.loadActiveConfigIntoProcessEnv();
        await this.deployCloudFunction();
    }

    /**
     * @return {Promise<void>}
     */
    async deployCloudFunction() {
        await super.createDeploymentFolder();
        await super.writeEnvFile();
        await super.writeGoogleApplicationCredentialsFile();

        const functionArgs = [];
        const workingDir   = process.cwd();

        functionArgs.push('--runtime=nodejs16');
        functionArgs.push(`--trigger-topic=${process.env["PUBSUB_TOPIC"]}`);
        functionArgs.push(`--entry-point=${process.env["FUNCTION_NAME"]}`);
        functionArgs.push('--region=europe-west1');
        functionArgs.push('--env-vars-file=env.yaml');
        functionArgs.push('--source=.');

        process.chdir(super.getTempDeployDirectory());

        console.log(chalk.yellow('Deploying with the following args'));
        console.log(chalk.yellow(functionArgs.toString()));
        (new GCloudCli()).exec(['beta', 'functions', 'deploy', `${process.env["FUNCTION_NAME_REST"]}`, ...functionArgs]);

        process.chdir(workingDir);
    }
}

deployCloudFunctions.js

import DeploymentScript from './DeploymentScript.js';

(new DeploymentScript()).handle();

Background events

To help with background events spawned by Google Cloud you can use the PubSubBackgroundEventController class. By extending this class you can easily access the data from the event, and flag the event as failed or aborted. See the src/Functions/PubSubBackgroundEventController.js file for all the functionality.