Skip to content

Commit

Permalink
chore: added script to create new package (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhthinh388 authored Feb 1, 2024
1 parent a1a3bb5 commit fdfd78c
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
README.md
README.md
scripts/*
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@
"release": "yarn build --filter=!@react-awesome/docs && changeset publish",
"docs": "yarn workspace @react-awesome/docs",
"eslint": "yarn workspace @react-awesome/eslint-config",
"prepare": "husky"
"prepare": "husky",
"gen:repo": "node ./scripts/create-new-repo.js"
},
"devDependencies": {
"@changesets/cli": "2.26.2",
"@inquirer/prompts": "^3.3.2",
"@react-awesome/eslint-config": "*",
"@testing-library/jest-dom": "^6.3.0",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.2",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-istanbul": "^1.2.1",
"@vitest/ui": "^1.2.1",
"chalk": "^4.1.2",
"cli-spinner": "^0.2.10",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.56.0",
"figlet": "^1.7.0",
"husky": "^9.0.6",
"jsdom": "^24.0.0",
"lodash": "^4.17.21",
"prettier": "^3.1.1",
"rimraf": "^5.0.5",
"tty-table": "^4.2.3",
Expand Down
61 changes: 61 additions & 0 deletions scripts/create-new-repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const figlet = require("figlet")
const { input} = require("@inquirer/prompts")
const chalk = require("chalk")
const Spinner = require('cli-spinner').Spinner;
const fs = require("node:fs")
const path = require("node:path")
const _ = require("lodash")

const run = async () => {
console.log(figlet.textSync("React Awesome Components"))
const packageName = await input({ message: 'Enter package name (write in camel case): ' });
const description = await input({ message: 'Enter package description: ' });
const keywordStr = await input({ message: 'Enter package keywords (separate by whitespace): ' });

const keywords = keywordStr.split(" ").map(str => JSON.stringify(str)).join(",\n")

const spinner = new Spinner(chalk.yellow(`Creating new package ${packageName}... %s `));
spinner.setSpinnerString('⣾⣽⣻⢿⡿⣟⣯⣷');
spinner.start();

const kebabName = _.kebabCase(packageName)

const packageFolder = path.resolve(__dirname, "../packages/" + kebabName)

// Create new folder with kebab case
fs.mkdirSync(packageFolder)

// Copy contents to new folder
fs.cpSync(path.resolve(__dirname, "./template"), packageFolder, { recursive: true })

// Replace package name

const replaceFiles = [
path.resolve(packageFolder, "./README.md"),
path.resolve(packageFolder, "./package.json")
]

for(const filePath of replaceFiles) {
const content = fs.readFileSync(filePath).toString('utf8')
const kebabRegex = new RegExp("<package-kebab>", 'g')
const camelRegex = new RegExp("<package-camel>", 'g')
const descriptionRegex = new RegExp("<description>", "g")
const keywordRegex = new RegExp("\"<keywords>\"", "g")
let updatedContent = content.replace(kebabRegex, kebabName)
updatedContent = updatedContent.replace(camelRegex, packageName)
updatedContent = updatedContent.replace(descriptionRegex, description)
updatedContent = updatedContent.replace(keywordRegex, keywords)
fs.writeFileSync(filePath, updatedContent, 'utf8')
}

spinner.stop();

console.log(chalk.green(`\nYour new package is located at ${packageFolder}. Happy Hacking!!`))

console.log(chalk.yellow("Remember to do the following steps:"))
console.log(chalk.blue("- Ignore newly created package in vite.config.ts file."))
console.log(chalk.blue("- Add Codecov step for newly created package in test.yml workflow."))
console.log(chalk.blue("- Add new package to the @react-awesome/components's package.json dependencies."))
}

run()
8 changes: 8 additions & 0 deletions scripts/template/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
root: true,
extends: ["@react-awesome/eslint-config/library.js"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: true,
},
};
32 changes: 32 additions & 0 deletions scripts/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @react-awesome/<package-kebab>

<p align="center">
<img alt="version" src="https://img.shields.io/npm/v/%40react-awesome%2F<package-kebab>" />
<img alt="coverage" src="https://img.shields.io/codecov/c/github/trinhthinh388/react-awesome-components/master?token=VQ8VJ7OECQ&flag=<package-camel>" />
<img alt="license" src="https://img.shields.io/github/license/trinhthinh388/react-awesome-components" />
<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/trinhthinh388/react-awesome-components/release.yml" />
<img alt="size" src="https://img.shields.io/bundlejs/size/%40react-awesome/<package-kebab>" />
</p>

**<package-camel>** tracks the previous value of a variable.

Please refer to the [documentation](https://react-awesome-components.vercel.app/docs/<package-kebab>) for more information.

## Installation

```sh
yarn add @react-awesome/<package-kebab>
# or
npm i @react-awesome/<package-kebab>
```

## Contribution

Yes please! See the
[contributing guidelines](https://github.com/trinhthinh388/react-awesome-components/blob/master/CONTRIBUTING.md)
for details.

## Licence

This project is licensed under the terms of the
[MIT license](https://github.com/trinhthinh388/react-awesome-components/blob/master/LICENSE).
41 changes: 41 additions & 0 deletions scripts/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@react-awesome/<package-kebab>",
"version": "0.0.3",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"sideEffects": false,
"license": "MIT",
"description": "<description>",
"keywords": [
"react",
"<keywords>"
],
"repository": {
"url": "https://github.com/trinhthinh388/react-awesome-components"
},
"files": [
"dist/**"
],
"scripts": {
"build": "vite --config ../../vite.config.ts build",
"dev": "vite --watch --config ../../vite.config.ts build",
"lint": "eslint \"src/**/*.ts*\"",
"clean": "rimraf .turbo && rimraf node_modules && rimraf dist",
"test": "vitest --config ../../vite.config.ts --coverage --run",
"test:ui": "vitest --config ../../vite.config.ts --coverage --ui",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@react-awesome/eslint-config": "*",
"@react-awesome/tsconfig": "*",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"eslint": "^8.56.0",
"react": "^18.2.0",
"typescript": "^5.3.3"
},
"publishConfig": {
"access": "public"
}
}
Empty file added scripts/template/src/index.ts
Empty file.
8 changes: 8 additions & 0 deletions scripts/template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@react-awesome/tsconfig/react-library.json",
"compilerOptions": {
"types": ["vitest/globals"],
},
"include": ["."],
"exclude": ["dist", "build", "node_modules"],
}
Loading

0 comments on commit fdfd78c

Please sign in to comment.