Skip to content

Commit

Permalink
refactor: extract include and exclude logic to utils
Browse files Browse the repository at this point in the history
The logic for determining the include and exclude files for the package
has been extracted into separate utility functions. This makes the code
in bin.ts cleaner and more maintainable. The utility functions are
getInclude and getExclude, both of which take the package JSON as an
argument.
  • Loading branch information
ryoppippi committed Aug 7, 2024
1 parent 968a732 commit 7ec2950
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
22 changes: 3 additions & 19 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,19 @@ import typia from 'typia';
import { dirname, resolve } from 'pathe';
import { readPackageJSON, resolvePackageJSON } from 'pkg-types';
import type { JSR } from './type';
import { getExclude, getInclude } from './utils';

const pkgJSONPath = await resolvePackageJSON();
const pkgJSON = await readPackageJSON(pkgJSONPath);
const rootDir = dirname(pkgJSONPath);
const jsrPath = resolve(rootDir, 'jsr.json');

const isStartWithExclamation = typia.createIs<`!${string}`>();
const { files } = pkgJSON;
const include = files == null ? ['dist'] : files.filter(file => isStartWithExclamation(file));
const exclude = files == null
? undefined
: files
.filter(file => !isStartWithExclamation(file))
.map((file) => {
if (file.startsWith('!**/')) {
return file.slice(4);
}
if (file.startsWith('!')) {
return file.slice(1);
}
return file;
});

const jsr = {
name: pkgJSON.name as string,
version: pkgJSON.version as string,
publish: {
include,
exclude,
include: getInclude(pkgJSON),
exclude: getExclude(pkgJSON),
},
exports: pkgJSON.exports,
};
Expand Down
29 changes: 29 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import typia from 'typia';
import type { PackageJson } from 'pkg-types';
import { parse } from 'pathe';

const isStartWithExclamation = typia.createIs<`!${string}`>();

export function getInclude(pkgJSON: PackageJson): string[] | undefined {
const { files } = pkgJSON;
const include = files == null ? ['dist'] : files.filter(file => isStartWithExclamation(file));
return include;
}

export function getExclude(pkgJSON: PackageJson): string[] | undefined {
const { files } = pkgJSON;
const exclude = files == null
? undefined
: files
.filter(file => !isStartWithExclamation(file))
.map((file) => {
if (file.startsWith('!**/')) {
return file.slice(4);
}
if (file.startsWith('!')) {
return file.slice(1);
}
return file;
});
return exclude;
}

0 comments on commit 7ec2950

Please sign in to comment.