Skip to content

Commit

Permalink
Merge pull request #37 from mapado/jd-feat-useDefaultConfigurationIfK…
Browse files Browse the repository at this point in the history
…eyIsNotSet
  • Loading branch information
jdeniau authored Jan 16, 2023
2 parents 03d78fd + bbbb7ac commit 7db405d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ npx watch-module /path/to/my/module ../my-other-module

### Configuration

On first launch, watch-module creates an empty configuration file to `{HOME_FOLDER}/.config/watch-module/config.json`
On first launch, watch-module creates an empty configuration file to `{HOME_FOLDER}/.config/watch-module/watch-module.json`
In order to force a different configuration for a specific module, you can add "per module" entries to this file :

```jsonc
Expand All @@ -44,6 +44,7 @@ In order to force a different configuration for a specific module, you can add "
// do not watch the files in the "dist" directory
// do not call any command before copying the files
"my-other-module": {
"command": null,
"includes": [""], // use "" or "." to watch all files
"excludes": ["dist"]
}
Expand All @@ -60,7 +61,7 @@ You can override this global configuration by configuring the targeted module's
},
"watch-module": {
"command": "yarn run build:prod",
"includes": ["src"] // if "includes" is not defined, watch-module will use "src" for retro compatibility
"includes": ["src"]
}
}
```
Expand All @@ -69,11 +70,16 @@ If no configuration is found for a module, watch-module falls back to the defaul

```jsonc
{
"command": "yarn|npm run build", // default configs tries to detect yarn or npm
"includes": ["src"],
"command": "yarn|npm run build" // default configs tries to detect yarn or npm
"excludes": []
}
```

#### Partial configuration

If you overrides only some parts of the configuration, then the keys that are not overiden will use the default configuration.

## Alternatives

[npm link | yarn link] : it does work fine until you have dependencies, etc. in your package.
Expand Down
3 changes: 2 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export const cwd = nodeProcess.cwd();
/**
* get the command to call for the package
*/
function getModuleCommandForPath(path: string): string | void {
function getModuleCommandForPath(path: string): string | null | undefined {
const moduleConfig = getModuleConfigEntry(path);

return moduleConfig.command;
}

Expand Down
37 changes: 24 additions & 13 deletions src/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Config = {
type ConfigEntry = {
includes?: string[];
excludes?: string[];
command?: string;
command?: string | null;
};

function getGlobalConfigPath(): string | void {
Expand Down Expand Up @@ -88,34 +88,45 @@ export function getModuleConfigEntry(modulePath: string): ConfigEntry {
fs.readFileSync(`${getModuleFullPath(modulePath)}/package.json`).toString()
);

const yarnOrNpm = hasYarn(modulePath) ? 'yarn' : 'npm';
const defaultConfig: ConfigEntry = {
includes: ['src'],
command: `${yarnOrNpm} run build`,
};

if (packageJson['watch-module']) {
// a watch-module config is found in the package
const packageJsonConfig = packageJson['watch-module'];
if (typeof packageJsonConfig.includes === 'undefined') {
packageJsonConfig.includes = ['src'];
}
const packageJsonConfig: ConfigEntry = {
...defaultConfig,
...packageJson['watch-module'],
};
moduleConfigCache[moduleName] = packageJsonConfig;

log(moduleName, chalk.hex(Theme.info)('using package.json config'));

return packageJsonConfig;
}

const globalConfig = getGlobalConfig();
if (globalConfig[moduleName]) {
// a config for this module is found in the global config
moduleConfigCache[moduleName] = globalConfig[moduleName];

const globalConfigWithDefault: ConfigEntry = {
...defaultConfig,
...globalConfig[moduleName],
};

moduleConfigCache[moduleName] = globalConfigWithDefault;
log(moduleName, chalk.hex(Theme.info)('using global config'));
return globalConfig[moduleName];

return globalConfigWithDefault;
}

// no config was found, return default config
const yarnOrNpm = hasYarn(modulePath) ? 'yarn' : 'npm';
const defaultConfig = {
includes: ['src'],
command: `${yarnOrNpm} run build`,
};

moduleConfigCache[moduleName] = defaultConfig;

log(moduleName, chalk.hex(Theme.info)('using default config'));

return defaultConfig;
}

Expand Down

0 comments on commit 7db405d

Please sign in to comment.