Skip to content

Commit

Permalink
feat: add --exclude-from-clean option
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Dec 3, 2023
1 parent 13a9c7a commit a5ec99b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,22 @@ Options:
-V, --version output the version number
-d, --dist <dist> The dist directory to target
-b, --build-script [buildScript] The build script to call after cleaning your dist directory
-nb, --no-build [noBuild] When enabled (default: false) the build step will not be called. Useful if you want to only bundle types and handle
building yourself.
-nc, --no-clean [noClean] When enabled (default: false) the clean step will not be called. Useful if you want to only bundle types and handle
cleaning yourself.
-ob, --only-bundle [onlyBundle] A shortcut to enabling both `--no-build` and `--no-clean`. This essentially makes it so rollup-type-bundler only
deals with bundling types and nothing else.
-t, --typings-file-extension [typingsFileExtension] The file extension for your typings files. Useful if you want to set `.cts` or `.mts`. If you forego adding a
prefixing dot (`.`), it will be added for you.
-nb, --no-build [noBuild] When enabled (default: false) the build step will not be called. Useful if you want to only bundle types and handle building
yourself.
-nc, --no-clean [noClean] When enabled (default: false) the clean step will not be called. Useful if you want to only bundle types and handle cleaning
yourself.
-ob, --only-bundle [onlyBundle] A shortcut to enabling both `--no-build` and `--no-clean`. This essentially makes it so rollup-type-bundler only deals with
bundling types and nothing else.
-t, --typings-file-extension [typingsFileExtension] The file extension for your typings files. Useful if you want to set `.cts` or `.mts`. If you forego adding a prefixing dot
(`.`), it will be added for you.
-v, --verbose Print verbose information
-e, --external [external...] Repeatable, each will be treated as a new entry. Library or libraries to treat as external in Rollup (see:
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency)
-ec, --exclude-from-clean [excludeFromClean...] Repeatable, each will be treated as a new entry.
Files to be excluded from the clean step, useful if you want to process those files manually yourself later.
This is in particular useful if you have multiple entrypoints.
Note that a String#endsWith check is used to check if an entry in this array matches a path of a file to delete. So you can
either use the full relative path, or just the file name.
-h, --help display help for command
```
Expand All @@ -131,6 +136,7 @@ package). It should be named `.rollup-type-bundlerrc`, optionally suffixed with
- `--typings-file-extension` maps to `typingsFileExtension`
- `--verbose` maps to `verbose`
- `--external` maps to `external`
- `--exclude-from-clean` maps to `excludeFromClean`
When using `.rollup-type-bundlerrc` or `.rollup-type-bundlerrc.json` as your
config file you can also use the JSON schema to get schema validation. To do so,
Expand Down Expand Up @@ -178,6 +184,7 @@ This library has opinionated defaults for its options. These are as follows:
- `--typings-file-extension` will default to `undefined`.
- `--verbose` will default to `false`.
- `--external` will default to `[]`.
- `--exclude-from-clean` will default to `[]`.
## Buy us some doughnuts
Expand Down
8 changes: 8 additions & 0 deletions assets/rollup-type-bundler.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
},
"default": []
},
"excludeFromClean": {
"description": "Files to be excluded from the clean step, useful if you want to process those files manually yourself later.\nThis is in particular useful if you have multiple entrypoints.\nNote that a String#endsWith check is used to check if an entry in this array matches a path of a file to delete. So you can either use the full relative path, or just the file name.",
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"noBuild": {
"description": "When enabled (default: false) the build step will not be called. Useful if you want to only bundle types and handle building yourself.",
"type": "boolean",
Expand Down
11 changes: 11 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ const command = new Command()
'https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency'
)})`,
(value: string, previous: string[]) => (previous ?? []).concat([value])
)
.option(
'-ec, --exclude-from-clean [excludeFromClean...]',
[
'Repeatable, each will be treated as a new entry.',
'Files to be excluded from the clean step, useful if you want to process those files manually yourself later.',
'This is in particular useful if you have multiple entrypoints.',
'Note that a String#endsWith check is used to check if an entry in this array matches a path of a file to delete. So you can either use the full relative path, or just the file name.'
].join('\n'),
(value: string, previous: string[]) => (previous ?? []).concat([value])
);

const program = command.parse(process.argv);
Expand Down Expand Up @@ -73,6 +83,7 @@ logVerboseInfo(
`${indent}onlyBundle: ${JSON.stringify(options.onlyBundle)}`,
`${indent}verbose: ${JSON.stringify(options.verbose)}`,
`${indent}external: ${JSON.stringify(options.external)}`,
`${indent}excludeFromClean: ${JSON.stringify(options.excludeFromClean)}`,
''
],
options.verbose
Expand Down
8 changes: 6 additions & 2 deletions src/commands/clean-extraneous-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ export async function cleanExtraneousTypes(options: Options): Promise<void> {
try {
const regexp = /(?:\.d\.[cm]?ts(?:\.map)?|\.tsbuildinfo)$/;

const inputFileName = `${basename(fileURLToPath(options.dist))}${sep}${getTypingsInputFileName(options)}`;

for await (const path of findFilesRecursivelyRegex(options.dist, regexp)) {
if (!path.endsWith(`${basename(fileURLToPath(options.dist))}${sep}${getTypingsInputFileName(options)}`)) {
await rm(path);
if (path.endsWith(inputFileName) || options.excludeFromClean?.some((filePath) => path.endsWith(filePath))) {
continue;
}

await rm(path);
}
} catch (err) {
const typedError = err as Error;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ declare module 'commander' {
* External packages to pass to rollup as external
*/
external?: string[];
/**
* Repeatable, each will be treated as a new entry.
*
* Files to be excluded from the clean step, useful if you want to process those files manually yourself later.
*
* This is in particular useful if you have multiple entrypoints.
*
* Note that a `String#endsWith` check is used to check if an entry in this array matches a path of a file to delete. So you can either use the full relative path, or just the file name.
*/
excludeFromClean?: string[];
/**
* When enabled the build step will not be called. Useful if you want to only bundle types and handle building yourself.
* @default false
Expand Down
12 changes: 8 additions & 4 deletions src/lib/optionsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export async function parseOptionsFile(cliOptions: Options) {
options = {
...fileOptions,
...options,
external: [...(fileOptions.external ?? []), ...(options.external ?? [])]
external: [...(fileOptions.external ?? []), ...(options.external ?? [])],
excludeFromClean: [...(fileOptions.excludeFromClean ?? []), ...(options.excludeFromClean ?? [])]
};
} catch (err) {
const typedError = err as Error;
Expand All @@ -58,7 +59,8 @@ export async function parseOptionsFile(cliOptions: Options) {
options = {
...fileOptions,
...options,
external: [...(fileOptions.external ?? []), ...(options.external ?? [])]
external: [...(fileOptions.external ?? []), ...(options.external ?? [])],
excludeFromClean: [...(fileOptions.excludeFromClean ?? []), ...(options.excludeFromClean ?? [])]
};
} catch (err) {
const typedError = err as Error;
Expand All @@ -83,21 +85,23 @@ export async function parseOptionsFile(cliOptions: Options) {

/**
* Transforms the {@link Options} object to have a `dist` directory relative to the current working directory and as a file {@link URL}
* Also hydrates the object with defaults in case `buildScript` and `external` were not yet set
* Also hydrates the object with defaults in case `buildScript`, `external`, and `excludeFromClean` were not yet set
* @param options The options to parse
* @returns The same options object, with the `dist` transformed and `buildScript` and `external` set.
* @returns The same options object, with the `dist` transformed and `buildScript`, `external`, and `excludeFromClean` set.
*/
function transformOptionsDistPathToFileUrl(options: Options): Options {
const distPath = Reflect.get(options, 'dist') ?? `.${sep}dist`;
const buildScript = options.buildScript ?? 'build';
const external = options.external ?? [];
const excludeFromClean = options.excludeFromClean ?? [];
const verbose = options.verbose ?? false;

return {
...options,
dist: pathToFileURL(join(packageCwd, cast<string>(distPath))),
buildScript,
external,
excludeFromClean,
verbose
};
}

0 comments on commit a5ec99b

Please sign in to comment.