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

wp-astro

v1.1.7

Published

JavaScript api wrapper for wp-cli

Downloads

13

Readme

Astro

Astro is a JavaScript api wrapper for wp-cli.

var wp = require('wp-astro')

wp('core download')

Installation

$ npm install wp-astro

Prerequisite

Requires WP-cli to be installed correctly and coincidently access to mysql

Features

  • Call wp cli commands from JavaScript
  • Write your own automated WordPress scripting
  • Async makes for faster automation
  • Simple api

API

The api can be called a number of different ways for example:

// Standalone command
wp('core download')

// Flags as array
wp('config create', ['--dbname=example', '--dbuser=root'])

// command with config
wp('config create', {
    input: `define('WP_DEBUG', true);`,
    flags: {
        dbname: 'example',
        dbuser: 'root',
        'extra-php': true
    }
})

// Config object
wp({
    command: 'config create'
    flags: {
        dbname: 'example',
        dbuser: 'root'
    }
})

Config options

Command

Define the wp cli command to call. The initial wp is not required here as this is done behind the scenes.

wp({
    command: 'core download'
})

Custom working directory

Set the current working directory for the command by default this uses the current working directory process.cwd().

This example will download the WordPress core to a folder called example the folder must exist.

wp('core download', {
    cwd: path.join(__dirname, 'example')
})

Flags

Any flags needed for the command are added here. Flags can be passed as an array or object.

wp('post get 1', ['field=id', '--format=json'])

// OR

wp({
    command: 'post get 1',
    flags: {
        field: 'id',
        format: 'json'
    }
})

// OR

wp({
    command: 'post get 1',
    flags: ['field=id', '--format=json']
})

Async

By default astro is synchronous by enabling async, commands can be run in parallel making for faster scripts.

Note: Running async on commands which require a previous command will not work as the previous command may not have completed!

Astro returns back the child process object meaning we have access to the events that triggers

var plugin = wp('plugin install hello', {
    async: true,
    flags: ['--activate']
})

plugin.on('data', function (data) {
    console.log(data.toString())
})

plugin.on('close', function (code, signal) {
    console.log(code)
})

For commands where data is piped into the command like wp config create this would be more appropriate:

var config = wp('config create', {
    async: true,
    flags: {
        dbname: 'example',
        dbuser: 'root',
        dbpass: 'root',
        'extra-php': true
    }
})

config.stdin.write(`
define('WP_DEBUG', true);
`)

config.end()

Verbose

Enable verbose mode to log wp cli's output to the console useful for debugging.

wp('core download', {
    verbose: true
})

Other Options

Behind the scenes we are just calling the wp-cli command with node's child_process module.

Depending on if async is enabled or disabled depends on which method we use, by default (async set to false) we use execSync otherwise if async is enabled we use exec

fortunatly that means we can pass any of it's config values in like so:

wp({
    input: `define('WP_DEBUG', true);`, // Only avalible not async
    cwd: path.resolve(__dirname, 'example'),
    env: {},
    encoding: 'utf8',
    shell: '/bin/sh',
    timeout: 0,
    maxBuffer: 200*1024,
    killSignal: 'SIGTERM',
    uid: ,
    gid: ,
    callback: function (error, stdout, stderr) {} // Only on async
})

Coming soon!

  • Promise API support
  • Predefined scripts
    • Entire install procedure
    • Install multiple plugins & themes
  • Tests :(

Licence

MIT