{"id":15390299,"url":"https://github.com/gajus/create-index","last_synced_at":"2025-05-16T12:04:30.974Z","repository":{"id":54192213,"uuid":"49138118","full_name":"gajus/create-index","owner":"gajus","description":"Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.","archived":false,"fork":false,"pushed_at":"2023-03-05T16:40:58.000Z","size":75,"stargazers_count":278,"open_issues_count":12,"forks_count":87,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-29T22:26:29.676Z","etag":null,"topics":["es-modules"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gajus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-01-06T13:53:32.000Z","updated_at":"2024-10-16T20:04:21.000Z","dependencies_parsed_at":"2023-02-09T22:16:08.095Z","dependency_job_id":"9564478d-232f-4349-99f2-8302252037b0","html_url":"https://github.com/gajus/create-index","commit_stats":{"total_commits":76,"total_committers":11,"mean_commits":6.909090909090909,"dds":0.513157894736842,"last_synced_commit":"929754cc91a207cf3557a1cd9762be565d0b5755"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gajus%2Fcreate-index","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gajus%2Fcreate-index/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gajus%2Fcreate-index/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gajus%2Fcreate-index/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gajus","download_url":"https://codeload.github.com/gajus/create-index/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247994120,"owners_count":21030050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["es-modules"],"created_at":"2024-10-01T15:05:17.178Z","updated_at":"2025-04-09T07:04:26.919Z","avatar_url":"https://github.com/gajus.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# create-index\n\n[![NPM version](http://img.shields.io/npm/v/create-index.svg?style=flat-square)](https://www.npmjs.org/package/create-index)\n[![Travis build status](http://img.shields.io/travis/gajus/create-index/master.svg?style=flat-square)](https://travis-ci.org/gajus/create-index)\n[![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)\n\n`create-index` program creates (and maintains) ES6 `./index.js` file in target directories that imports and exports sibling files and directories.\n\n## Example\n\n```sh\n\u003e tree ./\n./\n├── bar.js\n└── foo.js\n\n0 directories, 2 files\n\n\u003e create-index ./\n[13:17:34] Target directories [ './' ]\n[13:17:34] Update index: false\n[13:17:34] ./index.js [created index]\n[13:17:34] Done\n\n\u003e tree\n.\n├── bar.js\n├── foo.js\n└── index.js\n\n0 directories, 3 files\n```\n\nThis created `index.js` with:\n\n```js\n// @create-index\n\nexport { default as bar } from './bar.js';\nexport { default as foo } from './foo.js';\n\n```\n\nLets create a new file and re-run `create-index`:\n\n```js\n\u003e touch baz.js\n\u003e tree ./\n./\n├── bar.js\n├── baz.js\n├── foo.js\n└── index.js\n\n0 directories, 4 files\n\n\u003e create-index ./\n[13:21:55] Target directories [ './' ]\n[13:21:55] Update index: false\n[13:21:55] ./index.js [updated index]\n[13:21:55] Done\n```\n\nThis have updated `index.js` file:\n\n```js\n// @create-index\n\nexport { default as bar } from './bar.js';\nexport { default as baz } from './baz.js';\nexport { default as foo } from './foo.js';\n\n```\n\n## Usage\n\n### Using CLI Program\n\n```sh\nnpm install create-index\n\ncreate-index --help\n\nOptions:\n  --recursive, -r          Create/update index files recursively. Halts on any\n                           unsafe \"index.js\" files.   [boolean] [default: false]\n  --ignoreUnsafe, -i       Ignores unsafe \"index.js\" files instead of halting.\n                                                      [boolean] [default: false]\n  --ignoreDirectories, -d  Ignores importing directories into the index file,\n                           even if they have a safe \"index.js\".\n                                                      [boolean] [default: false]\n  --update, -u             Updates only previously created index files\n                           (recursively).             [boolean] [default: false]\n  --banner                 Add a custom banner at the top of the index file\n                                                                        [string]\n  --extensions, -x         Allows some extensions to be parsed as valid source.\n                           First extension will always be preferred to homonyms\n                           with another allowed extension.\n                                                       [array] [default: [\"js\"]]\n  --outputFile, -o         Output file            [string] [default: \"index.js\"]                                                      [array] [default: [\"js\"]]\n\nExamples:\n  create-index ./src ./src/utilities      Creates or updates an existing\n                                          create-index index file in the target\n                                          (./src, ./src/utilities) directories.\n  create-index --update ./src ./tests     Finds all create-index index files in\n                                          the target directories and descending\n                                          directories. Updates found index\n                                          files.\n  create-index ./src --extensions js jsx  Creates or updates an existing\n                                          create-index index file in the target\n                                          (./src) directory for both .js and\n                                          .jsx extensions.\n```\n\n### Using `create-index` Programmatically\n\n```js\nimport {\n    writeIndex\n} from 'create-index';\n\n/**\n * @type {Function}\n * @param {Array\u003cstring\u003e} directoryPaths\n * @throws {Error} Directory \"...\" does not exist.\n * @throws {Error} \"...\" is not a directory.\n * @throws {Error} \"...\" unsafe index.\n * @returns {boolean}\n */\nwriteIndex;\n```\n\nNote that the `writeIndex` function is synchronous.\n\n```js\nimport {\n    findIndexFiles\n} from 'create-index';\n\n/**\n * @type {Function}\n * @param {string} directoryPath\n * @returns {Array\u003cstring\u003e} List of directory paths that have create-index index file.\n */\nfindIndexFiles;\n```\n\n### Gulp\n\nSince [Gulp](http://gulpjs.com/) can ran arbitrary JavaScript code, there is no need for a separate plugin. See [Using `create-index` Programmatically](#using-create-index-programmatically).\n\n```js\nimport {\n    writeIndex\n} from 'create-index';\n\ngulp.task('create-index', () =\u003e {\n    writeIndex(['./target_directory']);\n});\n```\n\nNote that the `writeIndex` function is synchronous.\n\n## Implementation\n\n`create-index` program will look into the target directory.\n\nIf there is no `./index.js`, it will create a new file, e.g.\n\n```js\n// @create-index\n```\n\nCreated index file must start with `// @create-index\\n\\n`. This is used to make sure that `create-index` does not accidentally overwrite your local files.\n\nIf there are sibling files, index file will `import` them and `export`, e.g.\n\n```sh\nchildren-directories-and-files git:(master) ✗ ls -lah\ntotal 0\ndrwxr-xr-x   5 gajus  staff   170B  6 Jan 15:39 .\ndrwxr-xr-x  10 gajus  staff   340B  6 Jan 15:53 ..\ndrwxr-xr-x   2 gajus  staff    68B  6 Jan 15:29 bar\ndrwxr-xr-x   2 gajus  staff    68B  6 Jan 15:29 foo\n-rw-r--r--   1 gajus  staff     0B  6 Jan 15:29 foo.js\n```\n\nGiven the above directory contents, `./index.js` will be:\n\n```js\n// @create-index\n\nimport { default as bar } from './bar';\nimport { default as foo } from './foo.js';\n\nexport {\n    bar,\n    foo\n};\n```\n\nWhen file has the same name as a sibling directory, file `import` takes precedence.\n\nDirectories that do not have `./index.js` in themselves will be excluded.\n\nWhen run again, `create-index` will update existing `./index.js` if it starts with `// @create-index\\n\\n`.\n\nIf `create-index` is executed against a directory that contains `./index.js`, which does not start with `// @create-index\\n\\n`, an error will be thrown.\n\n## Ignore files on `--update`\n\n`create-index` can ignore files in a directory if `./index.js` contains special object with defined `ignore` property which takes `an array` of `regular expressions` defined as `strings`, e.g.\n\n```js\n\u003e cat index.js\n// @create-index {\"ignore\": [\"/baz.js$/\"]}\n```\n\n```js\n\u003e tree ./\n./\n├── bar.js\n├── baz.js\n├── foo.js\n└── index.js\n\n0 directories, 4 files\n```\n\nGiven the above directory contents, after running `create-index` with `--update` flag, `./index.js` will be:\n\n```js\n// @create-index {\"ignore\": [\"/baz.js$/\"]}\n\nimport { default as bar } from './bar.js';\nimport { default as foo } from './foo.js';\n\nexport {\n    bar,\n    foo\n};\n```\n\n## Generate `index.tsx` for TypeScript\n\n```bash\ncreate-index src/components/Icons --extensions tsx --outputFile index.tsx\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgajus%2Fcreate-index","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgajus%2Fcreate-index","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgajus%2Fcreate-index/lists"}