|
| 1 | +/* eslint-disable no-console */ |
| 2 | +const path = require('path'); |
| 3 | +const { execSync: exec } = require('child_process'); |
| 4 | +const rollup = require('rollup'); |
| 5 | +const resolvePlugin = require('@rollup/plugin-node-resolve'); |
| 6 | +const babelPlugin = require('rollup-plugin-babel'); |
| 7 | +const externalsPlugin = require('rollup-plugin-auto-external'); |
| 8 | + |
| 9 | +console.customInfo = args => { |
| 10 | + console.info( |
| 11 | + '\x1b[44m\x1b[37m', |
| 12 | + '\u2139', |
| 13 | + '\x1b[0m', |
| 14 | + '\x1b[36m', |
| 15 | + args, |
| 16 | + '\x1b[0m', |
| 17 | + ); |
| 18 | +}; |
| 19 | + |
| 20 | +const exampleFolder = path.resolve(process.argv[2]); |
| 21 | + |
| 22 | +// 1. BUILD EASY-STATE BUNDLE |
| 23 | +console.customInfo('Building react-easy-state in watch mode.'); |
| 24 | +const watchOptions = { |
| 25 | + input: path.resolve('src/index.js'), |
| 26 | + plugins: [ |
| 27 | + resolvePlugin(), |
| 28 | + babelPlugin({ exclude: 'node_modules/**' }), |
| 29 | + externalsPlugin({ dependencies: true, peerDependecies: true }), |
| 30 | + ], |
| 31 | + output: { |
| 32 | + format: 'es', |
| 33 | + dir: 'dist', |
| 34 | + entryFileNames: 'es.es6.js', |
| 35 | + sourcemap: true, |
| 36 | + }, |
| 37 | + watch: { |
| 38 | + include: 'src/**', |
| 39 | + }, |
| 40 | +}; |
| 41 | +const watcher = rollup.watch(watchOptions); |
| 42 | +watcher.on('event', ({ code }) => { |
| 43 | + if (code === 'START') { |
| 44 | + console.customInfo('Building bundle.'); |
| 45 | + } else if (code === 'END') { |
| 46 | + console.customInfo('Bundle built!'); |
| 47 | + } else if (code === 'ERROR') { |
| 48 | + console.customInfo('Encountered an error while bundling!'); |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +// 2. LINK EASY-STATE |
| 53 | +console.customInfo('Create link to react-easy-state.'); |
| 54 | +exec('npm link', { stdio: 'inherit' }); |
| 55 | +console.customInfo('Installing dependencies for example.'); |
| 56 | +exec('npm i', { |
| 57 | + cwd: exampleFolder, |
| 58 | + stdio: 'inherit', |
| 59 | +}); |
| 60 | +console.customInfo('Deleting react dependency duplicates.'); |
| 61 | +exec( |
| 62 | + ` |
| 63 | +rm -rf ./node_modules/react-easy-state && |
| 64 | +rm -rf ./node_modules/react && |
| 65 | +rm -rf ./node_modules/react-dom |
| 66 | + `, |
| 67 | + { |
| 68 | + cwd: exampleFolder, |
| 69 | + stdio: 'inherit', |
| 70 | + }, |
| 71 | +); |
| 72 | +console.customInfo('Linking react-easy-state.'); |
| 73 | +exec('npm link react-easy-state', { |
| 74 | + cwd: exampleFolder, |
| 75 | + stdio: 'inherit', |
| 76 | +}); |
| 77 | + |
| 78 | +// 3. START EXAMPLE |
| 79 | +console.customInfo(`Starting ${process.argv[2]} in the background.`); |
| 80 | +exec("nohup bash -c 'npm run start' >/dev/null 2>&1 &", { |
| 81 | + cwd: exampleFolder, |
| 82 | + stdio: 'inherit', |
| 83 | +}); |
| 84 | + |
| 85 | +const gracefullShutdown = () => { |
| 86 | + console.customInfo('Killing nodes.'); |
| 87 | + exec('killall node', { stdio: 'inherit' }); |
| 88 | + console.customInfo('Unlinking react-easy-state.'); |
| 89 | + exec('npm unlink --no-save react-easy-state', { |
| 90 | + cwd: exampleFolder, |
| 91 | + stdio: 'inherit', |
| 92 | + }); |
| 93 | + exec('npm unlink', { stdio: 'inherit' }); |
| 94 | + console.customInfo('Stoping bundle builder.'); |
| 95 | + watcher.close(); |
| 96 | +}; |
| 97 | + |
| 98 | +process.on('SIGINT', gracefullShutdown); |
| 99 | +process.on('SIGTERM', gracefullShutdown); |
0 commit comments