Flexible configuration loading from package.json, dedicated config files, and CLI arguments with comprehensive validation.
Loads configuration from multiple sources with automatic discovery and validation.
/**
* Loads configuration from various sources with validation
* @returns Promise resolving to merged configuration object
* @throws Error for invalid configuration with descriptive messages
*/
function getConfig(): Promise<Config>;
interface Config {
/** Keep only main section of README.md */
cleanDocs?: boolean;
/** Clean inline comments from JS files */
cleanComments?: boolean;
/** List of files to delete before publishing (supports regex and glob patterns) */
files?: Array<string | RegExp>;
/** List of package.json fields to delete before publishing */
fields?: string[];
/** Clean project without npm publish (tmp directory not deleted automatically) */
withoutPublish?: boolean;
/** Name of package manager to use */
packageManager?: PackageManager;
/** Whether npm registry publishes this package as public or restricted */
access?: PackageAccess;
/** Create temporary directory with given name */
tempDir?: string;
/** Options passed directly to package manager during publish */
packageManagerOptions?: string[];
/** Reports details of what would have been published */
dryRun?: boolean;
/** Registers package with given tag */
tag?: string;
/** Run script on to-release dir before npm publish */
beforeScript?: string;
}
type PackageManager = 'npm' | 'pnpm' | 'yarn' | string;
type PackageAccess = 'public' | 'restricted' | string;Configuration is loaded from multiple sources in order of precedence:
Configuration Files Searched:
.clean-publish (JSON).clean-publish.js (CommonJS module).clean-publish.json (JSON)package.json ("clean-publish" section)Usage Examples:
import { getConfig } from 'clean-publish/get-config.js';
// Load merged configuration
const config = await getConfig();
console.log(config.packageManager); // 'npm' (default)
console.log(config.files); // Custom file patterns if configuredConfiguration can be embedded in package.json under the "clean-publish" key:
{
"name": "my-package",
"scripts": {
"publish": "clean-publish"
},
"clean-publish": {
"files": ["*.test.js", "coverage/"],
"fields": ["devDependencies", "scripts.test"],
"packageManager": "pnpm",
"cleanDocs": true,
"cleanComments": true
}
}{
"files": ["test/", "*.spec.js"],
"fields": ["devDependencies", "scripts.test", "husky"],
"packageManager": "yarn",
"access": "public",
"cleanDocs": true,
"beforeScript": "npm run build"
}module.exports = {
files: [
'test/',
/\.test\.js$/,
'coverage/',
'docs/internal/'
],
fields: [
'devDependencies',
'scripts.test',
'scripts.dev'
],
packageManager: 'pnpm',
access: 'public',
cleanComments: true,
packageManagerOptions: ['--no-git-checks']
};All configuration is validated with descriptive error messages:
import { getConfig } from 'clean-publish/get-config.js';
try {
const config = await getConfig();
} catch (error) {
// Examples of validation errors:
// "The `packageManager` in Clean Publish config must be `an string`"
// "The `files` in Clean Publish config must be `an array of strings or RegExps`"
// "Clean Publish config must `not be empty`"
console.error(error.message);
}{
// Files to exclude (strings, RegExp, or glob patterns)
"files": [
"*.test.js", // String pattern
/\.spec\./, // RegExp pattern
"coverage/**", // Glob pattern
"docs/development/" // Directory
],
// Package.json fields to remove
"fields": [
"devDependencies",
"scripts.test",
"husky",
"lint-staged"
]
}{
// Package manager configuration
"packageManager": "pnpm",
"packageManagerOptions": ["--no-git-checks", "--access=public"],
// Publishing metadata
"access": "public",
"tag": "beta",
"dryRun": true,
// Workflow control
"withoutPublish": false,
"beforeScript": "npm run build:production"
}{
// README cleaning
"cleanDocs": true,
// JavaScript comment removal
"cleanComments": true,
// Custom temporary directory
"tempDir": "dist-clean"
}{
packageManager: 'npm',
cleanDocs: false,
cleanComments: false,
withoutPublish: false,
dryRun: false
}Configuration errors include:
All errors include examples of correct configuration format to help with debugging.