Skip to content

Commit beb74a8

Browse files
rolandszokesolkimicreb
authored andcommitted
ci(examples): start locally linked example with npm script
1 parent 877b1ab commit beb74a8

File tree

4 files changed

+154
-32
lines changed

4 files changed

+154
-32
lines changed

package-lock.json

Lines changed: 47 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"test-web": "jest --config ./jest.web.json",
1515
"test-web-no-hook": "NOHOOK=true jest --config ./jest.no-hook.json",
1616
"test": "npm run test-web && npm run test-web-no-hook && npm run test-native",
17+
"example-beer-finder": "node ./scripts/startExample.js ./examples/beer-finder",
18+
"example-clock": "node ./scripts/startExample.js ./examples/clock",
19+
"example-contacts": "node ./scripts/startExample.js ./examples/contacts",
20+
"example-stop-watch": "node ./scripts/startExample.js ./examples/stop-watch",
21+
"example-todo-mvc": "node ./scripts/startExample.js ./examples/todo-mvc",
22+
"example-native-clock": "node ./scripts/startExample.js ./examples/native-clock",
1723
"coveralls": "cat ./coverage/lcov.info | coveralls",
1824
"test-builds": "node ./scripts/testBuilds.js",
1925
"lint": "eslint --max-warnings 0 --ext js,jsx src scripts",
@@ -76,6 +82,7 @@
7682
"@babel/preset-react": "^7.8.3",
7783
"@commitlint/cli": "^8.3.5",
7884
"@commitlint/config-angular": "^8.3.4",
85+
"@rollup/plugin-node-resolve": "^7.1.1",
7986
"@testing-library/jest-dom": "^5.1.1",
8087
"@testing-library/react": "^9.4.0",
8188
"@types/react": "^16.9.19",
@@ -108,7 +115,6 @@
108115
"rollup": "^1.31.0",
109116
"rollup-plugin-auto-external": "^2.0.0",
110117
"rollup-plugin-babel": "^4.3.3",
111-
"rollup-plugin-node-resolve": "^5.2.0",
112118
"sinon": "^8.1.1",
113119
"styled-components": "^5.0.1"
114120
},

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import resolvePlugin from 'rollup-plugin-node-resolve';
2+
import resolvePlugin from '@rollup/plugin-node-resolve';
33
import babelPlugin from 'rollup-plugin-babel';
44
import externalsPlugin from 'rollup-plugin-auto-external';
55

scripts/startExample.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)