Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vscode-generate-index-standalone

fjc0k3.8kMIT1.7.1TypeScript support: included

Generating file indexes easily.

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 neg*ative 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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.7.1 (2022-12-08)

Bug Fixes

  • RegExp pattern generated result is wrong (c7d09e4)

1.7.0 (2022-12-08)

Features

1.6.0 (2021-08-28)

Features

  • ignore os hidden files by default (8bb6264)

1.5.3 (2021-07-21)

Bug Fixes

  • avoid to hoist fsevents require (86ed15f)
  • normalize paths before comparison (close: #8) (dd35f4d)

1.5.2 (2021-07-21)

Bug Fixes

  • bundle: set target to node (bca9ada)

1.5.1 (2021-07-20)

Bug Fixes

1.5.0 (2021-07-20)

Features

1.4.2 (2021-07-19)

Bug Fixes

  • move fsevents to optionalDependencies (f1ab602)

1.4.1 (2021-07-18)

Bug Fixes

  • lock fast-glob to 3.2.5 (580684d)

1.4.0 (2021-04-14)

Features

  • docs: add more examples (11a297e)

1.3.0 (2020-04-23)

Features

  • standalone: support watch (323ada0)

1.2.1 (2020-04-17)

Bug Fixes

  • extension: remove pkg deps (edb3830)

1.2.0 (2020-04-17)

Features

  • use bili to bundle extension (46cfbdc)

1.1.1 (2020-04-17)

Bug Fixes

  • api: write files only when changed (80e45c3)

1.1.0 (2020-04-17)

Features

  • api: add generateManyIndex (30f0320)

1.0.0 (2020-04-16)

Features

0.13.0 (2020-03-24)

Features

0.12.2 (2019-07-17)

Bug Fixes

  • temporarily set gitignore to false (4fb564d)

0.12.1 (2019-07-03)

Bug Fixes

  • keybindings only work when editorFocus (1747271)

0.12.0 (2019-07-03)

Features

  • add keybindings: ctrl+k i, cmd+k i (6560db3)

0.11.0 (2019-07-02)

Features

  • support custom globby options (9d1b443)

0.10.1 (2019-05-28)

0.10.0 (2019-05-23)

Features

  • rename first, last to isFirst, isLast (e0b02ce)

0.9.0 (2019-05-23)

Features

  • add isDir, isFile to ExtraInfo (b38d9e2)

0.8.0 (2019-05-20)

Features

0.7.0 (2019-03-18)

Features

  • add extraInfo parameter to codeGenerator (f698baf)

0.6.1 (2019-03-15)

Bug Fixes

  • docs: correct changeCase link (35340ef)

0.6.0 (2019-03-13)

Features

0.5.2 (2019-03-13)

Bug Fixes

0.5.1 (2019-03-13)

0.5.0 (2019-03-13)

Features

  • lodash --> changeCase (9142e6d)
  • use bili to bundle extension (338575f)

0.4.0 (2019-03-12)

Features

0.3.0 (2019-03-12)

Features

  • rename extension to ext (514a889)

0.2.1 (2019-03-12)

0.2.0 (2019-03-12)

Bug Fixes

  • globby: set onlyFiles to false (ded022b)

Features

0.1.0 (2019-03-12)

Features