Skip to content

Commit 2135ca3

Browse files
authored
Enable programmatic installation (#33)
1. Ensure `path` aalways reflects the state of the filesystem. Previously it would be set once when the module is loaded and not updated if the installation is added or removed from the filesystem 2. Allow optional installation on `npm install` It may be easier for some projects to include this as a dependency if the chromium installation can be made conditional.
1 parent 2b07299 commit 2135ca3

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

README.MD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ export NODE_CHROMIUM_CACHE_DISABLE=true
143143
# node_chromium_cache_disable=true
144144
```
145145

146+
### Skip Automatic Chromium Install
147+
148+
Chromium will ordinarily be installed when you exectute `npm install` however you may wish to skip this step if you are going to defer installation and perform it programatically at a later stage. Below is an example of how to do so.
149+
150+
```bash
151+
export NODE_CHROMIUM_SKIP_INSTALL=true
152+
153+
# or in .npmrc like so:
154+
# node_chromium_skip_install=true
155+
```
156+
157+
Then install it programatically when you need it:
158+
159+
```js
160+
chromium.install().then(function() {
161+
// do stuff...
162+
});
163+
```
146164
## Contributors
147165
<table>
148166
<tr style="background: #ffec86">

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ function getBinaryPath() {
1515
}
1616

1717
module.exports = {
18-
path: getBinaryPath(),
18+
/*
19+
* The path property needs to use a getter because the binaries may not be present for any number of reasons.
20+
* Using a getter allows this property to update itself as needed and reflect the current state of the filesystem.
21+
*/
22+
get path() {
23+
return getBinaryPath();
24+
},
1925
install: require('./install')
2026
};

install.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ async function install() {
142142

143143
if (require.main === module) {
144144
// Module called directly, not via "require", so execute install...
145-
install();
145+
if (config.getEnvVar('NODE_CHROMIUM_SKIP_INSTALL').toLowerCase() === 'true') {
146+
console.info('Skipping chromium install');
147+
} else {
148+
install();
149+
}
146150
}
147151

148152
tmp.setGracefulCleanup(); // Ensure temporary files are cleaned up when process exits

test/test-install.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const testUtils = require('./_utils');
1111
const utils = require('../utils');
1212
const config = require('../config');
1313
const install = require('../install');
14+
const chromium = require('..');
1415

1516
test.before(t => {
1617
// Deleting output folder
@@ -36,13 +37,15 @@ test.serial('Canary Test', t => {
3637
test.serial('Before Install Process', t => {
3738
const binPath = utils.getOsChromiumBinPath();
3839
t.false(fs.existsSync(binPath), `Chromium binary is found in: [${binPath}]`);
40+
t.falsy(chromium.path, 'chromium.path should not be defined when there is no installation');
3941
});
4042

4143
test.serial('Chromium Install', async t => {
4244
await install();
4345
const binPath = utils.getOsChromiumBinPath();
4446
const isExists = fs.existsSync(binPath);
4547
t.true(isExists, `Chromium binary is not found in: [${binPath}]`);
48+
t.true(fs.existsSync(chromium.path), 'chromium.path should be defined after installation');
4649
});
4750

4851
test.serial('Different OS support', async t => {

0 commit comments

Comments
 (0)