Skip to content

Commit

Permalink
feat(docs): add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 14, 2021
1 parent 59b9c3a commit 11a297e
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 11 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
lib
dist
out
__*__
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ yarn.lock
pnpm-lock.yaml
lib
dist
__*__
19 changes: 10 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
}
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off",
"typescript.tsdk": "node_modules/typescript/lib"
}
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,47 @@ In any file, simply invoke command `Generate Index` to generate a file list.
// @endindex
```

- Example 4: exports all `./*.tsx?` and `./*/index.tsx?` files.

```js
// @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`

<!-- prettier-ignore -->
```js
export type IconName =
// @index(['./icons/*.svg'], (f, _, e) => `'${f.name}'${e.isLast ? '' : ' |'}`)
'arrow' |
'home' |
'pass' |
'picture' |
'user'
// @endindex
```

- Example 6: imports all scripts

<!-- prettier-ignore -->
```html
<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:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"ts-jest": "^25.3.1",
"ts-node": "^8.8.2",
"typescript": "^3",
"typescript-snapshots-plugin": "^1.7.0",
"vscode": "^1.1.28"
},
"engines": {
Expand Down
3 changes: 3 additions & 0 deletions src/IndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const indexFilePaths = [
join(__dirname, './__fixtures__/sort/index2.ts'),
join(__dirname, './__fixtures__/invalid/empty'),
join(__dirname, './__fixtures__/invalid/error'),
join(__dirname, './__fixtures__/all_ts_tsx/index.ts'),
join(__dirname, './__fixtures__/types/index.ts'),
join(__dirname, './__fixtures__/scripts/index.html'),
]

test('ok', async () => {
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions src/__fixtures__/all_ts_tsx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @index(['./*.{ts,tsx}', './*/index.{ts,tsx}'], f => `export * from '${f.path.replace(/\/index$/, '')}'`)

// @endindex
Empty file.
9 changes: 9 additions & 0 deletions src/__fixtures__/scripts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<!-- @index('./*.js', f => `<script type="text/javascript" src="${f.path}${f.ext}"></script>`) -->
<!-- @endindex -->
</head>
<body>
...
</body>
</html>
Empty file.
Empty file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/__fixtures__/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type IconName =
// @index(['./icons/*.svg'], (f, _, e) => `'${f.name}'${e.isLast ? '' : ' |'}`)

// @endindex
67 changes: 67 additions & 0 deletions src/__snapshots__/IndexGenerator.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ok: generated content of /all_ts_tsx/index.ts 1`] = `
"// @index(['./*.{ts,tsx}', './*/index.{ts,tsx}'], f => \`export * from '\${f.path.replace(/\\\\/index$/, '')}'\`)
export * from './a'
export * from './b'
export * from './c'
export * from './d'
export * from './j'
// @endindex
"
`;

exports[`ok: generated content of /cross/index.txt 1`] = `
"# @index('../**/*', f => \`\${f.path}\${f.ext}\`, { onlyFiles: true })
../all_ts_tsx/a.ts
../all_ts_tsx/b.ts
../all_ts_tsx/c.tsx
../all_ts_tsx/d/index.ts
../all_ts_tsx/e/hello.ts
../all_ts_tsx/index.ts
../all_ts_tsx/j/index.tsx
../invalid/empty
../invalid/error
../scripts/index.html
../scripts/jssdk.js
../scripts/polyfill.js
../scss/a.scss
../scss/b.scss
../scss/c.scss
Expand All @@ -20,13 +41,26 @@ exports[`ok: generated content of /cross/index.txt 1`] = `
../sort/index.ts
../sort/index2.ts
../sort/国家.ts
../types/icons/arrow.svg
../types/icons/home.svg
../types/icons/pass.svg
../types/icons/picture.svg
../types/icons/user.svg
../types/index.ts
# @endindex
# @index('../**/*', f => \`\${f.path}\${f.ext}\`, { onlyDirectories: true })
../all_ts_tsx
../all_ts_tsx/d
../all_ts_tsx/e
../all_ts_tsx/j
./
../invalid
../scripts
../scss
../sort
../types
../types/icons
# @endindex
! @index('../**/*.scss', (f, _, {isFirst}) => \`\${isFirst ? '****' : ''}\${f.path}\${f.ext}\`)
Expand All @@ -45,6 +79,21 @@ exports[`ok: generated content of /invalid/error 1`] = `
"
`;

exports[`ok: generated content of /scripts/index.html 1`] = `
"<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>
"
`;
exports[`ok: generated content of /scss/index.scss 1`] = `
"// @index('./*', f => \`@import '\${f.path}\${f.ext}';\`)
@import './a.scss';
Expand Down Expand Up @@ -87,6 +136,20 @@ export * from './国家'
"
`;
exports[`ok: generated content of /types/index.ts 1`] = `
"export type IconName =
// @index(['./icons/*.svg'], (f, _, e) => \`'\${f.name}'\${e.isLast ? '' : ' |'}\`)
'arrow' |
'home' |
'pass' |
'picture' |
'user'
// @endindex
"
`;
exports[`ok: messages of /all_ts_tsx/index.ts 1`] = `Array []`;
exports[`ok: messages of /cross/index.txt 1`] = `Array []`;
exports[`ok: messages of /invalid/empty 1`] = `
Expand All @@ -102,8 +165,12 @@ Array [
]
`;
exports[`ok: messages of /scripts/index.html 1`] = `Array []`;
exports[`ok: messages of /scss/index.scss 1`] = `Array []`;
exports[`ok: messages of /sort/index.ts 1`] = `Array []`;
exports[`ok: messages of /sort/index2.ts 1`] = `Array []`;
exports[`ok: messages of /types/index.ts 1`] = `Array []`;
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const argv = yargs
.demandCommand(1, '').argv

generateManyIndex({
patterns: argv._,
patterns: argv._.map(String),
replaceFile: true,
cwd: argv.cwd,
watch: argv.watch as any,
Expand Down
12 changes: 11 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
"resolveJsonModule": true,
"preserveSymlinks": true,
"strict": true,
"newLine": "lf"
"newLine": "lf",
"plugins": [
{
"name": "typescript-snapshots-plugin",
"snapshotCallIdentifiers": [
"toMatchSnapshot",
"toThrowErrorMatchingSnapshot",
"toMatchDiffSnapshot"
]
}
]
}
}

0 comments on commit 11a297e

Please sign in to comment.