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

signed-code

v1.0.3

Published

A template system for code generation.

Downloads

16

Readme

Signed-Code

A template system for code generation.

Installation

Install this module with the following command:

npm install signed-code

Add the module to your package.json dependencies:

npm install --save signed-code

Add the module to your package.json dev-dependencies:

npm install --save-dev signed-code

Import a module as follows:

Quick Start

import signedCode from "signed-code";
import SignedCode from "signed-code/SignedCode";
import SignedCodeBuilder from "signed-code/SignedCodeBuilder";

// Define the code template through the template function `signedCode`
const codeBuilder = (values): SignedCodeBuilder => signedCode`/**
 * Some sample code
 *
 * // Explain how to re-generate the code
 * ${signedCode.commandAnnotation("npm run gencode")}
 * 
 * // Add required hash to recognize when code is out of date
 * ${signedCode.hashAnnotation()}
 * 
 * // Add version to force re-generation when needed
 * ${signedCode.versionAnnotation(1)}
 */

// Define named manual sections for partially generated code.
// Code written here won't be calculated in the hash.
// Multiple of these are allowed each with a different name.
${signedCode.manualSection("import")}

// Write any value give into the template
const values = ${JSON.stringify(values)};

export default values;
`;

// Load already existing code
const previousCode = await SignedCode.load("/tmp/export.js");

// Get the hash from the old code
const previousHash = previousCode.getDerivedHash();

// Re-generate new code
const codeBuilder = codeGenTemplate([1, 2, 3, 4]);

// Add the manual sections from the old code to the newly generated code
codeBuilder.addPreviousCode(previousCode);

// Serialize the newly generated code into a string
const signedCode = codeBuilder.getCode();

// Get the hash from the new code
const newHash = signedCode.getDerivedHash();

// Avoid writing to disk if nothing changed
if (previousHash !== newHash) {
    await signedCode.save(apiRoutesFilePath);
    console.log("Done updating code.");
} else {
    console.log("Nothing to update.");
}

This code generates:

/**
 * Some sample code
 *
 * @generated npm run gencode
 * @signed-hash 70541b0db6e93343bc6569604e0c4585
 * @signed-version 1
 */

// Define named manual sections for partially generated code.
// Code written here won't be calculated in the hash.
// Multiple of these are allowed each with a different name.
// <Manual-Section START import>

// <Manual-Section END>

// Write any value give into the template
const values = [1, 2, 3, 4];

export default values;