-
Notifications
You must be signed in to change notification settings - Fork 2
BA-2259: improve dev mode #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import chalk from "chalk" | ||
| import chokidar from "chokidar" | ||
| import fs from "fs/promises" | ||
| import path from "path" | ||
| import { execa } from "execa" | ||
|
|
||
| export const cancelCurrentBuild = async ({ | ||
| currentBuildProcesses, | ||
| commandTag, | ||
| }) => { | ||
| console.log(chalk.red(`${commandTag} Canceling current build processes...`)) | ||
| for (const proc of currentBuildProcesses) { | ||
| try { | ||
| proc.kill() | ||
| } catch (err) { | ||
| console.error( | ||
| chalk.red( | ||
| `${commandTag} Error killing process ${proc.pid}: ${err.message}` | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| await execa("pnpm", ["clean:tmp"], { preferLocal: true }) | ||
| currentBuildProcesses = [] | ||
| } | ||
|
|
||
| export const getConsumerAppBasePath = () => { | ||
| const consumerPath = process.env.BASEAPP_FRONTEND_TEMPLATE_PATH | ||
| if (!consumerPath) { | ||
| console.error( | ||
| chalk.red( | ||
| " Error: Please set the environment variable BASEAPP_FRONTEND_TEMPLATE_PATH in your shell startup (e.g., in ~/.bashrc or ~/.zshrc) before running this command.\n", | ||
| "Example: export BASEAPP_FRONTEND_TEMPLATE_PATH=/path/to/the/baseapp-frontend-template\n", | ||
| `Note: Don't forget to restart your terminal after setting the new environment variable.` | ||
| ) | ||
| ) | ||
|
|
||
| process.exit(1) | ||
| } | ||
| return consumerPath | ||
| } | ||
|
|
||
| export const updateConsumerRsync = async ({ | ||
| consumerAppPath, | ||
| sourceDist, | ||
| packageName, | ||
| commandTag, | ||
| }) => { | ||
| const targetDist = | ||
| path.join( | ||
| consumerAppPath, | ||
| "node_modules", | ||
| "@baseapp-frontend", | ||
| packageName, | ||
| "dist" | ||
| ) + "/" | ||
|
|
||
| console.log( | ||
| chalk.cyan(`${commandTag} Syncing dist folder to consumer app...`) | ||
| ) | ||
| try { | ||
| await execa( | ||
| "rsync", | ||
| ["-av", "--delete", "--delay-updates", sourceDist, targetDist], | ||
| { | ||
| shell: true, | ||
| } | ||
| ) | ||
anicioalexandre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log(chalk.cyan(`${commandTag} Sync completed successfully.`)) | ||
| } catch (error) { | ||
| console.error(chalk.red(`${commandTag} Sync failed:`)) | ||
| console.error(chalk.red(error.stderr || error.message)) | ||
| } | ||
| } | ||
|
|
||
| export const waitForReadyFile = async ({ readyFileParentPath, commandTag }) => { | ||
| console.log( | ||
| chalk.yellow(`${commandTag} Waiting for other packages to start...`) | ||
| ) | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const watcher = chokidar.watch(readyFileParentPath, { | ||
| ignoreInitial: false, | ||
| usePolling: true, | ||
| interval: 100, | ||
| awaitWriteFinish: { | ||
| stabilityThreshold: 500, | ||
| pollInterval: 100, | ||
| }, | ||
| }) | ||
| watcher.on('add', (filePath) => { | ||
| if (path.basename(filePath) === 'build.ready') { | ||
| console.log(chalk.green(`${commandTag} Ready file detected.`)) | ||
| watcher.close() | ||
| resolve() | ||
| } | ||
| }) | ||
| watcher.on("error", (err) => { | ||
| watcher.close() | ||
| reject(err) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| export const cleanupReadyFile = async ({readyFilePath}) => { | ||
| try { | ||
| await fs.unlink(readyFilePath) | ||
| } catch (err) { | ||
| // pass | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { runBuild } from './build.mjs' | ||
|
|
||
| runBuild() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import chalk from 'chalk' | ||
| import { execa } from 'execa' | ||
|
|
||
| const commandTag = '[@baseapp-frontend/components]' | ||
|
|
||
| export const runBuild = async (currentBuildProcesses = []) => { | ||
| console.log(`${chalk.magenta(`${commandTag} Starting build process...`)}`) | ||
|
|
||
| try { | ||
| console.log(chalk.cyanBright(`${commandTag} Running Relay Compiler...`)) | ||
| const relayProc = execa('pnpm', ['relay'], { preferLocal: true }) | ||
| currentBuildProcesses?.push(relayProc) | ||
| await relayProc | ||
| console.log(chalk.cyanBright(`${commandTag} Relay compilation completed.`)) | ||
|
|
||
| console.log(chalk.yellowBright(`${commandTag} Running Babel transpiling...`)) | ||
| const babelProc = execa('pnpm', ['babel:transpile'], { preferLocal: true }) | ||
| currentBuildProcesses?.push(babelProc) | ||
| await babelProc | ||
| console.log(chalk.yellowBright(`${commandTag} Babel transpilation completed.`)) | ||
|
|
||
| await Promise.all([ | ||
| (async () => { | ||
| console.log(chalk.yellow(`${commandTag} Running tsup bundling...`)) | ||
| const tsupProc = execa('pnpm', ['tsup:bundle', '--silent'], { preferLocal: true }) | ||
| currentBuildProcesses?.push(tsupProc) | ||
| await tsupProc | ||
| console.log(chalk.yellow(`${commandTag} tsup Bundling completed.`)) | ||
| })(), | ||
| (async () => { | ||
| console.log(chalk.blue(`${commandTag} Running type declaration generation...`)) | ||
| const tscProc = execa('pnpm', ['tsc:declaration'], { preferLocal: true }) | ||
| currentBuildProcesses?.push(tscProc) | ||
| await tscProc | ||
| console.log(chalk.blue(`${commandTag} Type declarations generated.`)) | ||
|
|
||
| console.log(chalk.cyan(`${commandTag} Copying DTS files...`)) | ||
| const copyProc = execa('pnpm', ['copy:dts'], { preferLocal: true }) | ||
| currentBuildProcesses?.push(copyProc) | ||
| await copyProc | ||
| console.log(chalk.cyan(`${commandTag} DTS files copied.`)) | ||
| })(), | ||
| ]) | ||
|
|
||
| console.log(chalk.hex('#c86c2c')(`${commandTag} Cleaning temporary files...`)) | ||
| const cleanProc = execa('pnpm', ['clean:tmp'], { preferLocal: true }) | ||
| currentBuildProcesses?.push(cleanProc) | ||
| await cleanProc | ||
| console.log(chalk.hex('#c86c2c')(`${commandTag} Temporary files cleaned.`)) | ||
|
|
||
| console.log(chalk.green(`${commandTag} Build completed successfully.`)) | ||
| } catch (error) { | ||
| if (error.signal !== 'SIGTERM') { | ||
| console.error(chalk.red(`${commandTag} Build failed:`)) | ||
| console.error(chalk.red(error.stderr || error.message)) | ||
| } | ||
| throw error | ||
| } finally { | ||
| currentBuildProcesses = [] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import chalk from 'chalk' | ||
| import chokidar from 'chokidar' | ||
| import path from 'path' | ||
| import { fileURLToPath } from 'url' | ||
|
|
||
| import { | ||
| cancelCurrentBuild, | ||
| getConsumerAppBasePath, | ||
| updateConsumerRsync, | ||
| waitForReadyFile, | ||
| } from '../../../.scripts/command-utils.mjs' | ||
| import { runBuild } from './build.mjs' | ||
|
|
||
| const currentDir = path.dirname(fileURLToPath(import.meta.url)) | ||
| const rootDir = path.join(currentDir, '..') | ||
|
|
||
| const commandTag = '[@baseapp-frontend/components]' | ||
|
|
||
| let isBuilding = false | ||
| let needsRebuild = false | ||
| let buildTimeout = null | ||
| let currentBuildProcesses = [] | ||
|
|
||
| const runWatchBuild = async () => { | ||
| if (isBuilding) { | ||
| needsRebuild = true | ||
| await cancelCurrentBuild({ currentBuildProcesses, commandTag }) | ||
| return | ||
| } | ||
|
|
||
| isBuilding = true | ||
|
|
||
| try { | ||
| const consumerAppPath = getConsumerAppBasePath() | ||
|
|
||
| const designSystemPath = path.join(rootDir, '..', 'design-system') | ||
| const readyFileParentPath = path.join(designSystemPath, 'dist') | ||
anicioalexandre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await waitForReadyFile({ readyFileParentPath, commandTag }) | ||
|
|
||
| await runBuild(currentBuildProcesses) | ||
|
|
||
| await updateConsumerRsync({ | ||
| consumerAppPath, | ||
| sourceDist: path.join(rootDir, 'dist/'), | ||
| packageName: 'components', | ||
| commandTag, | ||
| }) | ||
|
|
||
| console.log(`${chalk.magenta(`${commandTag} Watching for file changes...`)}`) | ||
| } catch (error) { | ||
| if (error.signal !== 'SIGTERM') { | ||
| console.error(chalk.red(`${commandTag} Build failed:`)) | ||
| console.error(chalk.red(error.stderr || error.message)) | ||
| } | ||
| } finally { | ||
| isBuilding = false | ||
| currentBuildProcesses = [] | ||
| if (needsRebuild) { | ||
| needsRebuild = false | ||
| runWatchBuild() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const watchRegex = /^modules\/(?:.*\/)?(common|web|native)\/.*$/ | ||
|
|
||
| const watcher = chokidar.watch(rootDir, { | ||
| ignoreInitial: true, | ||
| usePolling: true, | ||
| interval: 100, | ||
| awaitWriteFinish: { | ||
| stabilityThreshold: 500, | ||
| pollInterval: 100, | ||
| }, | ||
| ignored: (filePath, stats) => { | ||
| if ( | ||
| filePath.includes('node_modules') || | ||
| filePath.includes('dist') || | ||
| filePath.includes('tmp') | ||
| ) { | ||
| return true | ||
| } | ||
| if (stats && stats.isFile()) { | ||
| const relative = path.relative(rootDir, filePath).replace(/\\/g, '/') | ||
| if (!watchRegex.test(relative)) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| }, | ||
| }) | ||
|
|
||
| watcher.on('all', (event, changedPath) => { | ||
| const relativePath = path.relative(rootDir, changedPath).replace(/\\/g, '/') | ||
| console.log(`${commandTag} Detected event "${event}" on: ${relativePath}`) | ||
| if (buildTimeout) clearTimeout(buildTimeout) | ||
| buildTimeout = setTimeout(runWatchBuild, 2000) | ||
| }) | ||
anicioalexandre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| runWatchBuild() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.