Skip to content

Commit b4e4859

Browse files
feat: implement create-index-file cli v1
0 parents  commit b4e4859

23 files changed

+9348
-0
lines changed

.github/workflows/main.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}
6+
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
node: ['10.x', '12.x', '14.x']
11+
os: [ubuntu-latest, windows-latest, macOS-latest]
12+
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v2
16+
17+
- name: Use Node ${{ matrix.node }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node }}
21+
22+
- name: Install deps and build (with cache)
23+
uses: bahmutov/npm-install@v1
24+
25+
- name: Lint
26+
run: yarn lint
27+
28+
- name: Test
29+
run: yarn test --ci --coverage --maxWorkers=2
30+
31+
- name: Build
32+
run: yarn build

.github/workflows/size.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: size
2+
on: [pull_request]
3+
jobs:
4+
size:
5+
runs-on: ubuntu-latest
6+
env:
7+
CI_JOB_NUMBER: 1
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: andresz1/size-limit-action@v1
11+
with:
12+
github_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.log
2+
.DS_Store
3+
node_modules
4+
dist

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Giancarlos Isasi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# TSDX User Guide
2+
3+
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.
4+
5+
> This TSDX setup is meant for developing libraries (not apps!) that can be published to NPM. If you’re looking to build a Node app, you could use `ts-node-dev`, plain `ts-node`, or simple `tsc`.
6+
7+
> If you’re new to TypeScript, checkout [this handy cheatsheet](https://devhints.io/typescript)
8+
9+
## Commands
10+
11+
TSDX scaffolds your new library inside `/src`.
12+
13+
To run TSDX, use:
14+
15+
```bash
16+
npm start # or yarn start
17+
```
18+
19+
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`.
20+
21+
To do a one-off build, use `npm run build` or `yarn build`.
22+
23+
To run tests, use `npm test` or `yarn test`.
24+
25+
## Configuration
26+
27+
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.
28+
29+
### Jest
30+
31+
Jest tests are set up to run with `npm test` or `yarn test`.
32+
33+
### Bundle Analysis
34+
35+
[`size-limit`](https://github.com/ai/size-limit) is set up to calculate the real cost of your library with `npm run size` and visualize the bundle with `npm run analyze`.
36+
37+
#### Setup Files
38+
39+
This is the folder structure we set up for you:
40+
41+
```txt
42+
/src
43+
index.tsx # EDIT THIS
44+
/test
45+
blah.test.tsx # EDIT THIS
46+
.gitignore
47+
package.json
48+
README.md # EDIT THIS
49+
tsconfig.json
50+
```
51+
52+
### Rollup
53+
54+
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
55+
56+
### TypeScript
57+
58+
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
59+
60+
## Continuous Integration
61+
62+
### GitHub Actions
63+
64+
Two actions are added by default:
65+
66+
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
67+
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit)
68+
69+
## Optimizations
70+
71+
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations:
72+
73+
```js
74+
// ./types/index.d.ts
75+
declare var __DEV__: boolean;
76+
77+
// inside your code...
78+
if (__DEV__) {
79+
console.log('foo');
80+
}
81+
```
82+
83+
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.
84+
85+
## Module Formats
86+
87+
CJS, ESModules, and UMD module formats are supported.
88+
89+
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
90+
91+
## Named Exports
92+
93+
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.
94+
95+
## Including Styles
96+
97+
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
98+
99+
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.
100+
101+
## Publishing to NPM
102+
103+
We recommend using [np](https://github.com/sindresorhus/np).

package.json

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"version": "0.1.0",
3+
"license": "MIT",
4+
"main": "dist/index.js",
5+
"typings": "dist/index.d.ts",
6+
"files": [
7+
"dist",
8+
"src"
9+
],
10+
"engines": {
11+
"node": ">=12"
12+
},
13+
"scripts": {
14+
"start": "tsdx watch",
15+
"build": "tsdx build --target node",
16+
"test": "tsdx test",
17+
"lint": "tsdx lint",
18+
"prepare": "tsdx build",
19+
"size": "size-limit",
20+
"analyze": "size-limit --why"
21+
},
22+
"peerDependencies": {},
23+
"husky": {
24+
"hooks": {
25+
"pre-commit": "tsdx lint"
26+
}
27+
},
28+
"prettier": {
29+
"printWidth": 80,
30+
"semi": true,
31+
"singleQuote": true,
32+
"trailingComma": "es5"
33+
},
34+
"name": "create-index-file",
35+
"author": "Giancarlos Isasi",
36+
"module": "dist/create-index-file.esm.js",
37+
"size-limit": [
38+
{
39+
"path": "dist/create-index-file.cjs.production.min.js",
40+
"limit": "10 KB"
41+
},
42+
{
43+
"path": "dist/create-index-file.esm.js",
44+
"limit": "10 KB"
45+
}
46+
],
47+
"devDependencies": {
48+
"@size-limit/preset-small-lib": "^4.10.0",
49+
"del": "4.1.1",
50+
"husky": "^5.1.3",
51+
"size-limit": "^4.10.0",
52+
"tsdx": "^0.14.1",
53+
"tslib": "^2.1.0",
54+
"typescript": "^4.2.3"
55+
},
56+
"dependencies": {
57+
"@types/mkdirp": "^1.0.1",
58+
"@types/node": "^14.14.31",
59+
"@types/rimraf": "^3.0.0",
60+
"@types/yargs": "^16.0.0",
61+
"chalk": "^4.1.0",
62+
"mkdirp": "^1.0.4",
63+
"rimraf": "^3.0.2",
64+
"yargs": "^16.2.0"
65+
}
66+
}

src/cli.ts

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// import fs from 'fs';
2+
3+
import yargs from 'yargs'
4+
5+
// import generateIndexFromFiles from './generateIndexFromFiles'
6+
// import generateIndexFromFolders from './generateIndexFromFolders'
7+
8+
// import {
9+
// sourceFolder,
10+
// } from './utils'
11+
12+
export function runCli() {
13+
const argv = yargs(process.argv.slice(2))
14+
.option('from', {
15+
alias: 'from',
16+
demandOption: false,
17+
default: 'folder',
18+
describe: 'specify if you want to use folders or files to generate the index file.',
19+
type: 'string',
20+
choices: ['files', 'folder'],
21+
})
22+
.argv
23+
24+
const sourceFolder = argv._[0];
25+
26+
console.log({ sourceFolder })
27+
28+
console.log({ argv })
29+
30+
// const content =
31+
// argvs[0] === '--from' && argvs[1] === 'files'
32+
// ? generateIndexFromFiles()
33+
// : generateIndexFromFolders();
34+
35+
// fs.writeFile(indexFilePath, content, err => {
36+
// if (err) {
37+
// console.error(err);
38+
// return;
39+
// }
40+
// console.log('> Successfully to generate the index file.');
41+
// });
42+
43+
}
44+
45+
// export const mkdir = promisify(fs.mkdir)
46+
// export const writeFile = promisify(fs.writeFile)
47+
// export const readFile = promisify(fs.readFile);
48+
// export const readdir = promisify(fs.readdir)
49+
50+
// export function generateIndexFromFolders(srcFolder: string) {
51+
// const fileList = getFileList(srcFolder);
52+
// const folders = fileList.filter(isDirectory).filter(folderIncludesIndexFile);
53+
54+
// const indexContentArr = folders.map((currentFolder: string) => {
55+
// const files = fs.readdirSync(path.join(sourceFolder, currentFolder));
56+
57+
// // the folder can have an index.ts or an index.tsx
58+
// const hasIndexWithTsExtension = files.includes('index.ts');
59+
// const fileContent = fs.readFileSync(
60+
// path.join(
61+
// sourceFolder,
62+
// `${currentFolder}/index.${hasIndexWithTsExtension ? 'ts' : 'tsx'}`,
63+
// ),
64+
// 'utf8',
65+
// );
66+
67+
// const hasExportDefault = fileContent.includes('export { default } from');
68+
69+
// return hasExportDefault
70+
// ? `export { default as ${currentFolder} } from './${currentFolder}';\n`
71+
// : `export * as ${currentFolder} from './${currentFolder}';\n`;
72+
// });
73+
74+
// return indexContentArr.join('');
75+
// }
76+
77+
// export function generateIndexFromFiles(srcFolder: string) {
78+
// const fileList = getFileList(srcFolder);
79+
// const files = fileList
80+
// .filter(isFile)
81+
// .filter(f => f !== 'index.ts' && f !== 'index.tsx');
82+
83+
// const indexContentArr = files.map((file: string) => {
84+
// const name = file.split('.tsx')[0];
85+
86+
// return `export { default as ${name} } from './${name}';\n`;
87+
// });
88+
89+
// return indexContentArr.join('');
90+
// }
91+
92+
// const content =
93+
// argvs[0] === '--from' && argvs[1] === 'files'
94+
// ? generateIndexFromFiles()
95+
// : generateIndexFromFolders();
96+
97+
// fs.writeFile(indexFilePath, content, err => {
98+
// if (err) {
99+
// console.error(err);
100+
// return;
101+
// }
102+
// console.log('> Successfully to generate the index file.');
103+
// });

src/generateIndexFromFiles.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from 'path';
2+
3+
import {
4+
getFileList,
5+
isFile,
6+
} from './utils'
7+
8+
function generateIndexFromFiles(srcFolder: string): string {
9+
const fileList = getFileList(srcFolder);
10+
const files = fileList
11+
.map(f => path.join(srcFolder, f))
12+
.filter(isFile)
13+
.filter(filepath => {
14+
const filename = path.basename(filepath);
15+
return filename !== 'index.ts' && filename !== 'index.tsx'
16+
});
17+
18+
const indexContentArr = files.map((filePath: string) => {
19+
const filename = path.basename(filePath)
20+
const isTsxExtension = filename.includes('.tsx');
21+
const name = filename.split(isTsxExtension ? '.tsx' : '.ts')[0];
22+
23+
return `export { default as ${name} } from './${name}';\n`;
24+
});
25+
26+
return indexContentArr.join('');
27+
}
28+
29+
export default generateIndexFromFiles

0 commit comments

Comments
 (0)