Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 29, 2020
1 parent 1a289c3 commit 944fb74
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 42 deletions.
80 changes: 38 additions & 42 deletions cli/run/watch.ts
Expand Up @@ -32,50 +32,46 @@ export default async function watch(command: any) {
}

if (configFile) {
if (configFile.startsWith('node:')) {
({ options: configs, warnings } = await loadAndParseConfigFile(configFile, command));
} else {
let reloadingConfig = false;
let aborted = false;
let configFileData: string | null = null;

configWatcher = fs.watch(configFile, (event: string) => {
if (event === 'change') reloadConfigFile();
});

await reloadConfigFile();

async function reloadConfigFile() {
try {
const newConfigFileData = fs.readFileSync(configFile!, 'utf-8');
if (newConfigFileData === configFileData) {
return;
}
if (reloadingConfig) {
aborted = true;
return;
}
if (configFileData) {
stderr(`\nReloading updated config...`);
}
configFileData = newConfigFileData;
reloadingConfig = true;
({ options: configs, warnings } = await loadAndParseConfigFile(configFile!, command));
reloadingConfig = false;
if (aborted) {
aborted = false;
reloadConfigFile();
} else {
if (watcher) {
watcher.close();
}
start(configs);
let reloadingConfig = false;
let aborted = false;
let configFileData: string | null = null;

configWatcher = fs.watch(configFile, (event: string) => {
if (event === 'change') reloadConfigFile();
});

await reloadConfigFile();

async function reloadConfigFile() {
try {
const newConfigFileData = fs.readFileSync(configFile!, 'utf-8');
if (newConfigFileData === configFileData) {
return;
}
if (reloadingConfig) {
aborted = true;
return;
}
if (configFileData) {
stderr(`\nReloading updated config...`);
}
configFileData = newConfigFileData;
reloadingConfig = true;
({ options: configs, warnings } = await loadAndParseConfigFile(configFile!, command));
reloadingConfig = false;
if (aborted) {
aborted = false;
reloadConfigFile();
} else {
if (watcher) {
watcher.close();
}
} catch (err) {
configs = [];
reloadingConfig = false;
handleError(err, true);
start(configs);
}
} catch (err) {
configs = [];
reloadingConfig = false;
handleError(err, true);
}
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions test/cli/samples/watch/bundle-error/_config.js
@@ -0,0 +1,22 @@
const fs = require('fs');
const path = require('path');

let mainFile;

module.exports = {
description: 'recovers from errors during bundling',
command: 'rollup -cw',
before() {
mainFile = path.resolve(__dirname, 'main.js');
fs.writeFileSync(mainFile, '<=>');
},
abortOnStderr(data) {
if (data.includes('Error: Unexpected token')) {
fs.writeFileSync(mainFile, 'export default 42;');
return false;
}
if (data.includes('created _actual')) {
return true;
}
},
};
3 changes: 3 additions & 0 deletions test/cli/samples/watch/bundle-error/_expected/main.js
@@ -0,0 +1,3 @@
var main = 42;

export default main;
1 change: 1 addition & 0 deletions test/cli/samples/watch/bundle-error/main.js
@@ -0,0 +1 @@
export default 42;
7 changes: 7 additions & 0 deletions test/cli/samples/watch/bundle-error/rollup.config.js
@@ -0,0 +1,7 @@
export default {
input: 'main.js',
output: {
dir: "_actual",
format: "es"
}
};
9 changes: 9 additions & 0 deletions test/cli/samples/watch/no-config-file/_config.js
@@ -0,0 +1,9 @@
module.exports = {
description: 'watches without a config file',
command: 'rollup main.js --watch --format es --file _actual/main.js',
abortOnStderr(data) {
if (data.includes('created _actual/main.js')) {
return true;
}
},
};
3 changes: 3 additions & 0 deletions test/cli/samples/watch/no-config-file/_expected/main.js
@@ -0,0 +1,3 @@
const foo = 42;

export { foo };
1 change: 1 addition & 0 deletions test/cli/samples/watch/no-config-file/main.js
@@ -0,0 +1 @@
export const foo = 42;
10 changes: 10 additions & 0 deletions test/cli/samples/watch/node-config-file/_config.js
@@ -0,0 +1,10 @@
module.exports = {
solo: true,
description: 'watches using a node_modules config files',
command: 'rollup --watch --config node:custom',
abortOnStderr(data) {
if (data.includes('created _actual/main.js')) {
return true;
}
},
};
3 changes: 3 additions & 0 deletions test/cli/samples/watch/node-config-file/_expected/main.js
@@ -0,0 +1,3 @@
const foo = 42;

export { foo };
1 change: 1 addition & 0 deletions test/cli/samples/watch/node-config-file/main.js
@@ -0,0 +1 @@
export const foo = 42;

0 comments on commit 944fb74

Please sign in to comment.