Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into jd-feat-renderWithInk
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Jan 16, 2023
2 parents 8746e56 + 7db405d commit 6fd9c5d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 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 @@ -11,8 +11,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
35 changes: 22 additions & 13 deletions src/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Config = {
type ConfigEntry = {
includes?: string[];
excludes?: string[];
command?: string;
command?: string | null;
};

function getGlobalConfigPath(): string | void {
Expand Down Expand Up @@ -90,34 +90,43 @@ 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, 'using package.json config', Theme.info);

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, 'using global config', Theme.info);
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, 'using default config', Theme.info);

return defaultConfig;
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"sourceMap": true,
"strict": true,
"moduleResolution": "node",
"module": "node16",
"module": "es2020", // can be changed to node16 when support for node 14 is dropped
"esModuleInterop": true,
"target": "es2020",
"allowJs": true,
Expand Down

0 comments on commit 6fd9c5d

Please sign in to comment.