From abf5d9aee8315953504177145cacef18ae4354aa Mon Sep 17 00:00:00 2001 From: amcdnl Date: Tue, 14 May 2024 04:54:04 -0400 Subject: [PATCH] export blocks --- package.json | 8 ++++--- scripts/blocks.cjs | 36 ++++++++++++++++++++++++++++ scripts/{rewrite.cjs => stories.cjs} | 0 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 scripts/blocks.cjs rename scripts/{rewrite.cjs => stories.cjs} (100%) diff --git a/package.json b/package.json index 905eed74..60e7e0f3 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,14 @@ "version": "7.6.0", "description": "Component library for React", "scripts": { - "build": "npm run build:js && npm run build:styles && npm run rewrite && npm run build:stories && npm run build:docs", + "build": "npm run build:js && npm run build:styles && npm run rewrite:stories && npm run rewrite:blocks && npm run build:stories && npm run build:docs", "build-storybook": "storybook build", "build:js": "vite build --mode library", "build:stories": "tsc dist/stories/*.tsx --module esnext --jsx react-jsx --skipLibCheck true || exit 0;", "build:styles": "tailwindcss -i ./src/index.css -o ./dist/index.css", "build:docs": "node scripts/docs.js", - "rewrite": "node scripts/rewrite.cjs", + "rewrite:stories": "node scripts/stories.cjs", + "rewrite:blocks": "node scripts/blocks.cjs", "lint": "eslint --ext js,ts,tsx", "lint:fix": "eslint --ext js,ts,tsx --fix src", "lint:prettier": "prettier --loglevel warn --write 'src/**/*.{ts,tsx,js,jsx}'", @@ -30,7 +31,8 @@ "require": "./dist/index.umd.cjs", "types": "./dist/index.d.ts" }, - "./stories/*": "./dist/stories/*" + "./stories/*": "./dist/stories/*", + "./blocks/*": "./dist/blocks/*" }, "browser": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/scripts/blocks.cjs b/scripts/blocks.cjs new file mode 100644 index 00000000..78ec3175 --- /dev/null +++ b/scripts/blocks.cjs @@ -0,0 +1,36 @@ +const fs = require('fs'); +const fg = require('fast-glob'); +const { rewritePaths } = require('typescript-rewrite-paths'); +const path = require('path'); + +/** + * Replace all the paths in the stories from `./Block` to `reablocks` + */ +function replacePaths() { + // Grep all the stories + const files = fg.sync('docs/blocks/**/*.tsx'); + + files.forEach((file) => { + const code = fs.readFileSync(file, { encoding: 'utf-8' }); + + const output = rewritePaths(code, path => { + if (path.startsWith('./') || path.startsWith('../')) { + console.info(`Replacing ${path} with reablocks`); + return 'reablocks'; + } + + return path; + }); + + const newPath = path.resolve('dist', 'blocks', path.basename(file)); + const dir = path.dirname(newPath); + + if (!fs.existsSync(dir)){ + fs.mkdirSync(dir); + } + + fs.writeFileSync(newPath, output); + }); +} + +replacePaths(); diff --git a/scripts/rewrite.cjs b/scripts/stories.cjs similarity index 100% rename from scripts/rewrite.cjs rename to scripts/stories.cjs