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

vscode-generate-index-standalone

v1.7.1

Published

Generating file indexes easily.

Downloads

2,748

Readme

Generate Index Test

Generating file indexes easily.


Usage

In any file, simply invoke command Generate Index to generate a file list.

To display the command palette, use the following keyboard shortcut, based on your installed operating system:

  • MacOS: Command+Shift+P
  • Windows: Ctrl+Shift+P
  • Example 1: src/components/index.js

    // @index('./**/*.jsx', f => `export * from '${f.path}'`)
    export * from './Button'
    export * from './Card'
    export * from './Modal'
    export * from './Modal/Alert'
    // @endindex
  • Example 2: src/styles/components.scss

    // @index(['../components/**/*.scss', '!../components/**/_*.scss'], f => `@import '${f.path}';`)
    @import '../components/Button';
    @import '../components/Card';
    @import '../components/Modal';
    @import '../components/Modal/Alert';
    // @endindex
  • Example 3: src/assets/index.ts

    // @index('./*.{png,jpg,svg}', (f, _) => `export { default as img${_.pascalCase(f.name)} } from '${f.path}${f.ext}'`)
    export { default as imgHomeBanner } from './home-banner.png'
    export { default as imgPlaceholder } from './placeholder.jpg'
    export { default as imgDivider } from './divider.svg'
    // @endindex
    
    // @index('./*.{mp3,aac,m4a}', (f, _) => `export { default as audio${_.pascalCase(f.name)} } from '${f.path}${f.ext}'`)
    export { default as audioSailing } from './sailing.mp3'
    export { default as audioSummerWine } from './summer-wine.aac'
    export { default as audioThankYou } from './thank-you.m4a'
    // @endindex
  • Example 4: exports all ./*.tsx? and ./*/index.tsx? files.

    // @index(['./*.{ts,tsx}', './*/index.{ts,tsx}'], f => `export * from '${f.path.replace(/\/index$/, '')}'`)
    export * from './components'
    export * from './types'
    export * from './utils'
    // @endindex
  • Example 5: produces the type of the IconName

    export type IconName =
      // @index(['./icons/*.svg'], (f, _, e) => `'${f.name}'${e.isLast ? '' : ' |'}`)
      'arrow' |
      'home' |
      'pass' |
      'picture' |
      'user'
      // @endindex
  • Example 6: imports all scripts

    <html>
      <head>
        <!-- @index('./*.js', f => `<script type="text/javascript" src="${f.path}${f.ext}"></script>`) -->
        <script type="text/javascript" src="./jssdk.js"></script>
        <script type="text/javascript" src="./polyfill.js"></script>
        <!-- @endindex -->
      </head>
      <body>
        ...
      </body>
    </html>

@index()

index is a function, used for producing index:

function index(
  patterns: Patterns,
  codeGenerator: CodeGenerator,
  globbyOptions?: GlobbyOptions,
): string {}
  • Patterns

    type Patterns = string | string[]

    See supported minimatch patterns.

  • CodeGenerator

    type CodeGenerator = (
      parsedPath: ParsedPath,
      changeCase: ChangeCase,
      extraInfo: ExtraInfo,
    ) => string
    
    interface ParsedPath {
      /** The relative file path without extension, such as `./api` */
      path: string
      /** The file name without extension, such as `api` */
      name: string
      /** The file extension, such as `.js`*/
      ext: string
    }
    
    interface ChangeCase {
      // See https://github.com/blakeembrey/change-case#usage
    }
    
    interface ExtraInfo {
      /** total number of items */
      total: number
      /** index of current item */
      index: number
      /** if current item is the first */
      isFirst: boolean
      /** if current item is the last */
      isLast: boolean
      /** if current item is a directory */
      isDir: boolean
      /** if current item is a file */
      isFile: boolean
    }

    See all changeCase methods.

  • GlobbyOptions

    See https://github.com/sindresorhus/globby#options.

Indentation

You can make an index indented by indenting the start marker, e.g.

module.exports = {
  // @index('./*.js', (f, _) => `${_.constantCase(f.name)}: require('${f.path}'),`)
  // @endindex
}

The produced index like as:

module.exports = {
  // @index('./*.js', (f, _) => `${_.constantCase(f.name)}: require('${f.path}'),`)
  MODULE1: require('./module1'),
  MODULE2: require('./module2'),
  // @endindex
}

RegExp pattern

You can use regular expressions to filter the matched files, e.g.

// export all button components except starting with _

// @index(['./components/*', /button/, /\/_/g], f => `export * from '${f.name}${f.ext}'`)
export * from './components/button.js'
export * from './components/button_small.js'
export * from './components/button_large.js'
// @endindex

In this case, the g flag means negative rather than global.

Standalone version

There is a standalone version here that allows you to use this feature without the help of VSCode.

Install

# npm
npm i vscode-generate-index-standalone -D

# yarn
yarn add vscode-generate-index-standalone -D

# pnpm
pnpm add vscode-generate-index-standalone -D

Usage

CLI

# npm
npx vscode-generate-index-standalone src/ scripts/

# yarn
yarn vscode-generate-index-standalone src/ scripts/

# pnpm
pnpx vscode-generate-index-standalone src/ scripts/

API

import { generateIndex, generateManyIndex } from 'vscode-generate-index-standalone'
import { join } from 'path'

const generateResult = await generateIndex({
  filePath: join(__dirname, '../src/index.ts'),
  replaceFile: true,
}

const generateManyResult = await generateManyIndex({
  patterns: ['../src/**/index.ts', '!**/ignore/index.ts'],
  cwd: __dirname,
  replaceFile: true,
})

License

Jay Fong (c) MIT