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

@labzdjee/about-from-json-package

v1.2.0

Published

Extract data from package.json to make an about json file with a build sequence

Downloads

2

Readme

Make an About JSON File from package.json

This package helps extract data from package.json and copy them to a JSON file which can be bundled by Webpack and also manages a build sequence (increased after each build)

The Problem

It appears interesting to expose version, writer, dependencies, etc which are contained into package.json of an Electron app. For example: package, env.npm_package_version gives the project version and this information can be displayed in an about box. However this only works in dev mode: when app is build this information is not defined anymore

The solution we found is to add a node pre-process in npm run build (or equivalent) which creates a JSON file which will be part of the build and only takes some information from package.json

This solution has the advantage this JSON file does not expose the entire information from package.json which can be a plus in case of a Webapp bundled from source file or even not bundled at all

What it Does

Package consists of aboutFileFromPackageJson function which takes the object read from a package.json, creates/rewrite a JSON file which contains parts of this package.json. This package also manages a build property in the exported object which is increased call after call to aboutFileFromPackageJson

Purpose is essentially to use this library as a preprocessor which updates this JSON file before the actual build

The Output JSON File

This JSON file consists of two properties:

  • build: a string that is based on date and a letter defining which build within the same day. See description below
  • packageJson: sub-object that is extracted from the package.json file

The build Property

This field is a string defined as the number of days since Wednesday, 2020/01/01, followed by a letter. This letter is defined as 0 to 9, then A to Z, then a to z (10+26+26, i.e. 62 values). Within the same day, it is increased in this order if already found in the output ECMAScript file

Example: "5623", means build was done on Wednesday, 2021/07/15, i.e. 562 days after 2020/01/01 and 3 means this is the fourth build within that day. If instead of "3" we had "b", it would mean 38th build (10+26+2)

In the unlikely case of more than 62 builds were made on the same day, we would have a dot '.' plus as many letters as needed. For example, "562.3Af", means 12,194th build (62²x3 + 62x10 + 42) on the same day (Wednesday, 2021/07/15)

Suggested Usage

The idea is to run a script which calls aboutFileFromPackageJson before calling the actual build script

One way to achieve this is to install npm-run-all package as dev dependency

Then in the scripts section of package.json, rename the normal build script (for example call it actual-build) and replace the normal build script with run-s pre-run actual-build, pre-run being the script which calls aboutFileFromPackageJson

API

aboutFileFromPackageJson

Parameters:

  • packageJsonObject - object imported from package.json. For example as a result of require("./package.json")
  • pattern - an object which strictly mimics parts of the package.json file. Only its structure is used, its terminal values are ignored as they are taken from packageJsonObject instead
  • jsonFilePath - path and file name of the JSON file to create or update
  • buildFunction - a function, if defined will replace the default build property handler. It takes the present build property string possibly found in previously written JSON file (or null if no such file existed) and is expected to return the next one. The default built function is makeBuildStr (see below) acting on Date.now()

lowLevelStuff

These are exports specially intended for unit tests

incrementBuildString

Takes a string defined as in the build property section (without the leading dot if length of more than one character) and returns a string with value increased by one. Examples: "9" will return "A", "f" will return "10", "aZz" will return "aa0"

makeBuildStr

Reminder: default build property consists of two parts: the number of days since Wednesday, 2020/01/01 and a letter (0-9A-Za-z) representing the number of builds in the same day. If that letter overflows it is replaced by a string consisting of a dot plus two or more letters. In this section, let's call d the number of days and n the number of strings within the same day (for example: "3", "B", or ".1H")

makeBuildStr takes a Date type and a build property and will return the next build property. This means that if given date corresponds d, the additional letter(s) n appended to d will by increased if d has the same value as the provided build property. If build property is not correct (e.g. null) or its d part does not match d, then d with an appended 0 will be returned

Examples where given date 2021/07/15 which corresponds to d equal to "562":

  • "foo", "5558", "561.abc", null will return "5620"
  • "562a" will return "562b"
  • "562z" will return "562.10"