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

gulp-concat-flatten

v3.0.1

Published

A Gulp plugin to recursively flatten directories and concatenate the files within

Downloads

488

Readme

gulp-concat-flatten NPM version NPM downloads Build Status Coverage Status Dependency Status Development Dependency Status

is a Gulp plugin that concatenates files based on their directory structure. Starting at a base directory of your choice, it recursively flattens out subdirectories and concatenates the files within these. Files in the base directory will get copied without concatenation.

Installation

To install gulp-concat-flatten as a development dependency, simply run:

npm install --save-dev gulp-concat-flatten

Usage

Add it to your gulpfile.js and use it like this:

const gulp      = require('gulp');
const concat    = require('gulp-concat-flatten');
const sort      = require('gulp-sort');

gulp.src('path/**/*.txt')
	.pipe(sort()) // Recommendation, see below
	.pipe(concat('path/to/assets', 'txt', {'newLine': '\n'}))
	.pipe(gulp.dest('out'));

Running the shown task on a source file structure like this:

path
`-- to
    |-- 00_outside.txt
    `-- assets
        |-- 01_first.txt
        |-- 02_second
        |   |-- 01_second_first.txt
        |   |-- 02_second_second
        |   |   `-- second_second_first.txt
        |   `-- 03_second_third.txt
        `-- 03_third.txt

would result in:

out
|-- 01_first.txt
|-- 02_second.txt
`-- 03_third.txt

In detail,

  • the file path/to/00_outside.txt would be skipped as it's not contained in the base directory path/to/assets,
  • the files path/to/assets/01_first.txt and path/to/assets/03_third.txt would be copied over without concatenation,
  • the directory path/to/assets/02_second would be recursively concatenated and appended with the file extension ".txt".

Generally, I recommend sorting the source files via gulp-sort prior to piping them through concat(). This way you can reliably control their order for concatenation using file and directory names.

Dependencies / topological sorting

As of version 1.0, you can also let concat() control the resource order based on dependencies:

path
`-- to
    |-- .dependencies.json
    `-- assets
        |-- 01_first.txt
        |-- 02_second
        |   |-- .dependencies.json
        |   |-- 01_second_first.txt
        |   `-- 02_second_second.txt
        `-- 03_third.txt

When flattening the directory path/to/assets/02_second, concat() will read path/to/assets/02_second/.dependencies.json and build a dependency graph based on the instructions inside that file. It will also collect and merge all .dependecies.json files in parent directories. The format for .dependencies.json files is a follows:

{
    "*.txt": [
        "path/to/assets/03_third.txt",
        "path/to/another/asset",
    ]
}

The example tells concat() the following:

Whenever the resulting filename of the flattened 02_second directory is going to match the glob pattern *.txt, then register the two dependencies path/to/assets/03_third.txt and path/to/another/asset for this resource . (The dependency path/to/another/asset will be ignored as no such resource exists in the example.)

Given the above file structure, concat() will result in the following resource order:

  • path/to/assets/03_third.txt: no dependency, but there is a dependent resources (02_second.txt), so this has to come before
  • path/to/assets/02_second.txt: concatenated folder; depends on 03_third.txt
  • path/to/assets/01_first.txt: no dependency, added at the end

By binding the dependency set to a glob pattern (*.txt) you can express multiple sets for different result file types (e.g. CSS and JavaScript files).

Signature

/**
 * Concatenation by directory structure
 *
 * @param {String} base    Base directory
 * @param {String} ext     Optional: File extension
 *                         Will be used as file extension for concatenated directories
 * @param {Object} opt     Optional: Options, defaulting to:
 *                         {
 *                             newLine: "\n" // Concatenation string, may be empty
 *                         }
 */
concat(base, ext, opt);

NOTE that the base argument also accepts a glob pattern to match multiple base directories.

Changelog

Please refer to the changelog for a complete release history.

Legal

Copyright © 2019 Joschi Kuphal [email protected] / @jkphl.

gulp-concat-flatten is licensed under the terms of the MIT license.