@niche-works/dev 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@niche-works/dev",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "keywords": [
5
5
  "utils"
6
6
  ],
@@ -18,15 +18,6 @@
18
18
  },
19
19
  "main": "./src/index.ts",
20
20
  "module": "./src/index.ts",
21
- "scripts": {
22
- "build": "tsup",
23
- "check": "tsc --noEmit",
24
- "dev": "tsup --watch",
25
- "format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json}\"",
26
- "indexes": "tsx scripts/indexes.ts",
27
- "lint": "tsup --dry-run",
28
- "tidy": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json}\" --config ./prettier.tidy.config.js"
29
- },
30
21
  "dependencies": {
31
22
  "fs-extra": "^11.3.4",
32
23
  "remeda": "^2.33.6",
@@ -39,10 +30,18 @@
39
30
  "prettier": "^3.8.1",
40
31
  "prettier-plugin-organize-imports": "^4.3.0",
41
32
  "prettier-plugin-packagejson": "^3.0.2",
42
- "tsup": "^8.5.1",
33
+ "tsdown": "^0.21.9",
43
34
  "tsx": "^4.21.0",
44
35
  "type-fest": "^5.5.0",
45
36
  "typescript": "^6.0.2"
46
37
  },
47
- "packageManager": "pnpm@10.32.1"
48
- }
38
+ "scripts": {
39
+ "build": "tsdown",
40
+ "check": "tsc --noEmit",
41
+ "dev": "tsdown --watch",
42
+ "format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json}\"",
43
+ "indexes": "tsx scripts/indexes.ts",
44
+ "lint": "tsdown --dry-run",
45
+ "tidy": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json}\" --config ./prettier.tidy.config.js"
46
+ }
47
+ }
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs-extra';
2
2
  import { posix as path } from 'path';
3
- import isMatchingPath from '../isMatchingPath';
3
+ import isTargetPath from '../isTargetPath';
4
4
  import type { AddExportsOptions } from './types';
5
5
 
6
6
  /**
@@ -61,7 +61,7 @@ function _addExports(
61
61
  options: AddExportsOptions,
62
62
  current: string = '',
63
63
  ) {
64
- const { include = [], exclude = [], dist = 'dist' } = options;
64
+ const { include, exclude, dist = 'dist' } = options;
65
65
 
66
66
  const stat = fs.statSync(targetPath);
67
67
  if (!stat.isDirectory()) {
@@ -81,11 +81,7 @@ function _addExports(
81
81
  exportsIndex.push(
82
82
  ..._addExports(itemPath, options, path.join(current, item)),
83
83
  );
84
- } else if (
85
- stat.isFile() &&
86
- isMatchingPath(itemPath, include) &&
87
- !isMatchingPath(itemPath, exclude)
88
- ) {
84
+ } else if (isTargetPath(itemPath, { include, exclude })) {
89
85
  // exportの対象の場合
90
86
  exportsIndex.push({
91
87
  key: current,
@@ -7,9 +7,8 @@ const PATTERNS = {
7
7
  };
8
8
 
9
9
  /**
10
- * JSONファイルを更新する
11
- * @param jsonFilePath
12
- * @param editor
10
+ * ローカルファイルのimportやrequireに拡張子を付与する
11
+ * @param dir
13
12
  */
14
13
  export default async function addJsExtensions(dir: string) {
15
14
  const files = await fg(`${dir}/**/*.{js,cjs}`);
@@ -0,0 +1,43 @@
1
+ import isMatchingPath from '../isMatchingPath';
2
+ import type {
3
+ CreateExternalOptionFunctionOptions,
4
+ ExternalOptionFunction,
5
+ } from './types';
6
+
7
+ /**
8
+ * rolldownやrollupなどで内部モジュールのimport以外をexternalとして扱うための関数を作る関数
9
+ * @param options
10
+ */
11
+ export default function createExternalOptionFunction(
12
+ options: CreateExternalOptionFunctionOptions = {},
13
+ ): ExternalOptionFunction {
14
+ const { isExternal, isInternal } = options;
15
+ return (
16
+ source: string,
17
+ _importer: string | undefined,
18
+ isResolved: boolean,
19
+ ) => {
20
+ const isMatchingPathOptions = {
21
+ conditionOptions: {
22
+ source,
23
+ importer: _importer,
24
+ isResolved,
25
+ },
26
+ };
27
+ if (isMatchingPath(source, isExternal, isMatchingPathOptions)) {
28
+ // 外部モジュール扱い
29
+ return true;
30
+ } else if (isMatchingPath(source, isInternal, isMatchingPathOptions)) {
31
+ // 内部モジュール扱い
32
+ return;
33
+ } else if (!isResolved) {
34
+ // 相対パスでもなく、srcディレクトリ配下でもないものは外部モジュール扱い
35
+ return (
36
+ !source.startsWith('.') &&
37
+ !source.startsWith('/') &&
38
+ !source.startsWith('src/')
39
+ );
40
+ }
41
+ return;
42
+ };
43
+ }
@@ -0,0 +1 @@
1
+ export { default } from './createExternalOptionFunction';
@@ -0,0 +1,34 @@
1
+ import type { IsMatchingPathCondition } from '../isMatchingPath';
2
+
3
+ /**
4
+ * オプション
5
+ */
6
+ export type CreateExternalOptionFunctionOptions = {
7
+ /**
8
+ * 外部モジュールとする条件
9
+ */
10
+ isExternal?: IsMatchingPathCondition<ConditionOptions>[];
11
+
12
+ /**
13
+ * 内部モジュールとする条件
14
+ */
15
+ isInternal?: IsMatchingPathCondition<ConditionOptions>[];
16
+ };
17
+
18
+ /**
19
+ * rolldownやrollupのexternalオプションに設定可能な関数
20
+ */
21
+ export type ExternalOptionFunction = (
22
+ source: string,
23
+ importer: string | undefined,
24
+ isResolved: boolean,
25
+ ) => boolean | null | undefined | void;
26
+
27
+ /**
28
+ * include,excludeが関数だった場合のオプション
29
+ */
30
+ export type ConditionOptions = {
31
+ source: string;
32
+ importer: string | undefined;
33
+ isResolved: boolean;
34
+ };
package/src/index.ts CHANGED
@@ -7,4 +7,5 @@ export { default as indexes } from './indexes';
7
7
  export { default as installFromLocal } from './installFromLocal';
8
8
  export { default as isMatchingPath } from './isMatchingPath';
9
9
  export { default as moveFilesToSubdir } from './moveFilesToSubdir';
10
+ export { default as removeFiles } from './removeFiles';
10
11
  export { default as ver } from './ver';
@@ -1,8 +1,12 @@
1
1
  import fs from 'fs-extra';
2
2
  import { posix as path } from 'path';
3
3
  import { isFunction, isString } from 'remeda';
4
- import type { ConditionValues } from './_types';
5
- import type { IsMatchingPathCondition, IsMatchingPathOptions } from './types';
4
+ import type {
5
+ ConditionFnOptions,
6
+ ConditionValues,
7
+ IsMatchingPathCondition,
8
+ IsMatchingPathOptions,
9
+ } from './types';
6
10
 
7
11
  /**
8
12
  * 処理対象のパスか判定する
@@ -11,14 +15,16 @@ import type { IsMatchingPathCondition, IsMatchingPathOptions } from './types';
11
15
  * @param options オプション
12
16
  * @returns 一致するか
13
17
  */
14
- export default function isMatchingPath(
18
+ export default function isMatchingPath<
19
+ O extends ConditionFnOptions = ConditionFnOptions,
20
+ >(
15
21
  targetPath: string,
16
22
  conditions:
17
- | IsMatchingPathCondition
18
- | IsMatchingPathCondition[]
23
+ | IsMatchingPathCondition<O>
24
+ | IsMatchingPathCondition<O>[]
19
25
  | null
20
26
  | undefined,
21
- options: IsMatchingPathOptions = {},
27
+ options: IsMatchingPathOptions<O> = {},
22
28
  ) {
23
29
  if (!conditions) {
24
30
  return false;
@@ -47,11 +53,11 @@ export default function isMatchingPath(
47
53
  return false;
48
54
  }
49
55
 
50
- function _isMatching(
56
+ function _isMatching<O extends ConditionFnOptions = ConditionFnOptions>(
51
57
  value: string,
52
58
  values: ConditionValues,
53
- condition: IsMatchingPathCondition,
54
- options: IsMatchingPathOptions,
59
+ condition: IsMatchingPathCondition<O>,
60
+ options: IsMatchingPathOptions<O>,
55
61
  ): boolean {
56
62
  if (isString(condition)) {
57
63
  // 条件が文字列
@@ -65,7 +71,7 @@ function _isMatching(
65
71
  }
66
72
  } else if (isFunction(condition)) {
67
73
  // 条件が関数
68
- return condition(values, options.conditionOptions ?? {});
74
+ return condition(values, (options.conditionOptions ?? {}) as O);
69
75
  } else {
70
76
  // 条件が設定
71
77
  const { valueType = 'path', entryType = 'both', conditions } = condition;
@@ -1,4 +1,4 @@
1
- import type { ConditionConfig, ConditionFn } from './_types';
1
+ import type { ParsedPath } from 'path';
2
2
 
3
3
  /**
4
4
  * 一致条件
@@ -7,15 +7,89 @@ import type { ConditionConfig, ConditionFn } from './_types';
7
7
  * - RegExp: 指定された正規表現にパスが一致する際にtrue
8
8
  * - ConditionConfig: 条件設定に従って検査し一致したときにtrue
9
9
  */
10
- export type IsMatchingPathCondition =
11
- | string
12
- | RegExp
13
- | ConditionConfig
14
- | ConditionFn;
10
+ export type IsMatchingPathCondition<
11
+ O extends ConditionFnOptions = ConditionFnOptions,
12
+ > = string | RegExp | ConditionConfig<O> | ConditionFn<O>;
15
13
 
16
- export type IsMatchingPathOptions = {
14
+ export type IsMatchingPathOptions<
15
+ O extends ConditionFnOptions = ConditionFnOptions,
16
+ > = {
17
17
  /**
18
18
  * ConditionFnに渡すオプション
19
19
  */
20
- conditionOptions?: Record<string, any>;
20
+ conditionOptions?: O;
21
21
  };
22
+
23
+ /**
24
+ * 条件設定
25
+ */
26
+ export type ConditionConfig<O extends ConditionFnOptions = ConditionFnOptions> =
27
+ {
28
+ /**
29
+ * 検査対象の値
30
+ *
31
+ * - path: '/'で区切られたフルパス
32
+ * - base: 拡張子付きのディレクトリ名 or ファイル名
33
+ * - name: 拡張子を除いたディレクトリ名 or ファイル名
34
+ * - ext: '.'を含む拡張子。拡張子が無い場合は空文字
35
+ * - dirpath: '/'で区切られた親ディレクトリのフルパス
36
+ * - base: 拡張子付きの親ディレクトリ名
37
+ * - name: 拡張子を除いた親ディレクトリ名
38
+ * - ext: '.'を含む親ディレクトリの拡張子。拡張子が無い場合は空文字
39
+ */
40
+ valueType?:
41
+ | 'path'
42
+ | 'base'
43
+ | 'name'
44
+ | 'ext'
45
+ | 'dirpath'
46
+ | 'dirbase'
47
+ | 'dirname'
48
+ | 'dirext';
49
+
50
+ /**
51
+ * 検査対象の種別
52
+ *
53
+ * - dir: ディレクトリ
54
+ * - file: ファイル
55
+ * - both: ディレクトリ & ファイル
56
+ */
57
+ entryType?: 'dir' | 'file' | 'both';
58
+
59
+ /**
60
+ * 一致条件
61
+ * 配列で指定した場合はand条件とする
62
+ *
63
+ * - string: 指定された文字列を含むものに一致
64
+ * - RegExp: 指定された正規表現に一致
65
+ */
66
+ conditions:
67
+ | string
68
+ | RegExp
69
+ | ConditionFn<O>
70
+ | (string | RegExp | ConditionFn<O>)[];
71
+ };
72
+
73
+ /**
74
+ * カスタム条件関数
75
+ */
76
+ export type ConditionFn<O extends ConditionFnOptions = ConditionFnOptions> = (
77
+ values: ConditionValues,
78
+ options: O,
79
+ ) => boolean;
80
+
81
+ /**
82
+ * カスタム条件関数のオプション
83
+ */
84
+ export type ConditionFnOptions = Record<string, any>;
85
+
86
+ /**
87
+ * 検証対象の値
88
+ */
89
+ export type ConditionValues = {
90
+ path: string;
91
+ dirpath: string;
92
+ dirbase: string;
93
+ dirname: string;
94
+ dirext: string;
95
+ } & ParsedPath;
@@ -0,0 +1,2 @@
1
+ export { default } from './isTargetPath';
2
+ export type * from './types';
@@ -0,0 +1,30 @@
1
+ import fs, { Stats } from 'fs-extra';
2
+ import type { ConditionFnOptions } from '../isMatchingPath';
3
+ import isMatchingPath from '../isMatchingPath';
4
+ import type { IsTargetPathOptions } from './types';
5
+
6
+ const IS_TARGET_TYPE = {
7
+ file: (stat: Stats) => stat.isFile(),
8
+ dir: (stat: Stats) => stat.isDirectory(),
9
+ both: (stat: Stats) => stat.isFile() || stat.isDirectory(),
10
+ } as const;
11
+
12
+ export default async function isTargetPath<
13
+ O extends ConditionFnOptions = ConditionFnOptions,
14
+ >(itemPath: string, options: IsTargetPathOptions<O> = {}) {
15
+ const {
16
+ itemType = 'file',
17
+ defaultInclude,
18
+ defaultExclude,
19
+ include = defaultInclude,
20
+ exclude = defaultExclude,
21
+ ...rest
22
+ } = options;
23
+ const stat = fs.statSync(itemPath);
24
+
25
+ return (
26
+ IS_TARGET_TYPE[itemType](stat) &&
27
+ (!include || isMatchingPath(itemPath, include, rest)) &&
28
+ (!exclude || !isMatchingPath(itemPath, exclude, rest))
29
+ );
30
+ }
@@ -0,0 +1,38 @@
1
+ import type {
2
+ ConditionFnOptions,
3
+ IsMatchingPathCondition,
4
+ } from '../isMatchingPath';
5
+
6
+ export type IsTargetPathOptions<
7
+ O extends ConditionFnOptions = ConditionFnOptions,
8
+ > = {
9
+ /**
10
+ * 対象の種別
11
+ * @default 'file'
12
+ */
13
+ itemType?: 'file' | 'dir' | 'both';
14
+
15
+ /**
16
+ * 対象とする条件
17
+ * 未指定の場合は`defaultInclude`が適用される
18
+ */
19
+ include?: IsMatchingPathCondition<O>[];
20
+
21
+ /**
22
+ * 対象から除外する条件
23
+ * 未指定の場合は`defaultExclude`が適用される
24
+ */
25
+ exclude?: IsMatchingPathCondition<O>[];
26
+
27
+ /**
28
+ * 対象とする条件のデフォルト
29
+ * 未指定の場合は条件なし
30
+ */
31
+ defaultInclude?: IsMatchingPathCondition<O>[];
32
+
33
+ /**
34
+ * 対象から除外する条件のデフォルト
35
+ * 未指定の場合は除外なし
36
+ */
37
+ defaultExclude?: IsMatchingPathCondition<O>[];
38
+ };
@@ -1 +1,2 @@
1
1
  export { default } from './moveFilesToSubdir';
2
+ export type * from './types';
@@ -1,23 +1,40 @@
1
1
  import fs from 'fs-extra';
2
2
  import path from 'path';
3
+ import isTargetPath from '../isTargetPath';
4
+ import type { MoveFilesToSubdirOptions } from './types';
3
5
 
4
6
  /**
5
7
  * 対象のディレクトリ直下のファイルを、拡張子を除いた同名のサブディレクトリ直下に移動する
6
8
  * @param dirPath 対象のディレクトリのパス
7
9
  */
8
- export default async function moveFilesToSubdir(dirPath: string) {
10
+ export default async function moveFilesToSubdir(
11
+ dirPath: string,
12
+ options: MoveFilesToSubdirOptions = {},
13
+ ) {
14
+ const { process, index, ...rest } = options;
9
15
  const items = await fs.readdir(dirPath);
10
16
  for (const item of items) {
11
17
  const itemPath = path.join(dirPath, item);
12
- const stat = fs.statSync(itemPath);
13
- if (stat.isFile()) {
14
- const fileName = path.parse(item).name;
18
+ if (isTargetPath(itemPath, rest)) {
19
+ const { name, ext } = path.parse(item);
15
20
  // 移動先のディレクトリパスを生成
16
- const subdirPath = path.join(dirPath, fileName);
21
+ const subdirPath = path.join(dirPath, name);
17
22
  // 移動先のディレクトリが存在しなければ作成
18
23
  fs.ensureDirSync(subdirPath);
19
- // 移動先のファイルパスを生成
24
+ // 移動
20
25
  fs.moveSync(itemPath, path.join(subdirPath, item));
26
+ if (index) {
27
+ fs.writeFileSync(
28
+ path.join(subdirPath, `index.${ext}`),
29
+ `export { default } from ${name}`,
30
+ {
31
+ encoding: 'utf8',
32
+ },
33
+ );
34
+ }
35
+ if (process) {
36
+ process(subdirPath, item);
37
+ }
21
38
  }
22
39
  }
23
40
  }
@@ -0,0 +1,15 @@
1
+ import type { IsTargetPathOptions } from '../isTargetPath';
2
+
3
+ export type MoveFilesToSubdirOptions = Omit<IsTargetPathOptions, 'itemType'> & {
4
+ /**
5
+ * ファイル毎の後処理
6
+ * @param subdirPath サブディレクトリのパス
7
+ * @param fileName ファイル名
8
+ */
9
+ process?: (subdirPath: string, fileName: string) => void;
10
+
11
+ /**
12
+ * indexファイルの作成
13
+ */
14
+ index?: boolean;
15
+ };
@@ -0,0 +1 @@
1
+ export { default } from './removeFiles';
@@ -0,0 +1,11 @@
1
+ import type { Options, Pattern } from 'fast-glob';
2
+ import fg from 'fast-glob';
3
+ import fs from 'fs-extra';
4
+
5
+ export default async function removeFiles(
6
+ source: Pattern | Pattern[],
7
+ options: Options = {},
8
+ ) {
9
+ const files = await fg(source, options);
10
+ await Promise.all(files.map((file) => fs.remove(file)));
11
+ }
@@ -0,0 +1,5 @@
1
+ import type { Options } from 'fast-glob';
2
+
3
+ export type RemoveFilesOptions = {
4
+ fastGlobOptions?: Options;
5
+ };
@@ -0,0 +1,41 @@
1
+ import fs from 'fs-extra';
2
+ import path from 'path';
3
+ import { defineConfig } from 'tsdown';
4
+ import addJsExtensions from './src/addJsExtensions';
5
+ import generatePublishPackageJson from './src/generatePublishPackageJson';
6
+ import removeFiles from './src/removeFiles';
7
+
8
+ export default defineConfig((options) => ({
9
+ entry: ['./src/**/*.ts'],
10
+ unbundle: true,
11
+ sourcemap: true,
12
+ clean: true,
13
+ dts: true,
14
+ format: ['cjs', 'esm'],
15
+ minify: false,
16
+ plugins: [
17
+ {
18
+ name: 'post-build',
19
+ async buildEnd() {
20
+ const opts = options ?? {};
21
+ if (opts.watch) {
22
+ return;
23
+ }
24
+
25
+ const outDir = opts.outDir ?? 'dist';
26
+ await Promise.all([
27
+ // import,requireの補完
28
+ addJsExtensions(outDir),
29
+ // package.jsonの編集
30
+ generatePublishPackageJson({
31
+ jsonWriteOptions: { spaces: 2 },
32
+ }),
33
+ // その他のファイルのコピー
34
+ fs.copyFile('LICENSE', path.join(outDir, 'LICENSE')),
35
+ fs.copyFile('README.md', path.join(outDir, 'README.md')),
36
+ ]);
37
+ await removeFiles(`${outDir}/**/*.d.cts`);
38
+ },
39
+ },
40
+ ],
41
+ }));
@@ -1,65 +0,0 @@
1
- import type { ParsedPath } from 'path';
2
-
3
- /**
4
- * 条件設定
5
- */
6
- export type ConditionConfig = {
7
- /**
8
- * 検査対象の値
9
- *
10
- * - path: '/'で区切られたフルパス
11
- * - base: 拡張子付きのディレクトリ名 or ファイル名
12
- * - name: 拡張子を除いたディレクトリ名 or ファイル名
13
- * - ext: '.'を含む拡張子。拡張子が無い場合は空文字
14
- * - dirpath: '/'で区切られた親ディレクトリのフルパス
15
- * - base: 拡張子付きの親ディレクトリ名
16
- * - name: 拡張子を除いた親ディレクトリ名
17
- * - ext: '.'を含む親ディレクトリの拡張子。拡張子が無い場合は空文字
18
- */
19
- valueType?:
20
- | 'path'
21
- | 'base'
22
- | 'name'
23
- | 'ext'
24
- | 'dirpath'
25
- | 'dirbase'
26
- | 'dirname'
27
- | 'dirext';
28
-
29
- /**
30
- * 検査対象の種別
31
- *
32
- * - dir: ディレクトリ
33
- * - file: ファイル
34
- * - both: ディレクトリ & ファイル
35
- */
36
- entryType?: 'dir' | 'file' | 'both';
37
-
38
- /**
39
- * 一致条件
40
- * 配列で指定した場合はand条件とする
41
- *
42
- * - string: 指定された文字列を含むものに一致
43
- * - RegExp: 指定された正規表現に一致
44
- */
45
- conditions: string | RegExp | ConditionFn | (string | RegExp | ConditionFn)[];
46
- };
47
-
48
- /**
49
- * カスタム条件関数
50
- */
51
- export type ConditionFn = (
52
- values: ConditionValues,
53
- options: Record<string, any>,
54
- ) => boolean;
55
-
56
- /**
57
- * 検証対象の値
58
- */
59
- export type ConditionValues = {
60
- path: string;
61
- dirpath: string;
62
- dirbase: string;
63
- dirname: string;
64
- dirext: string;
65
- } & ParsedPath;
package/tsup.config.ts DELETED
@@ -1,36 +0,0 @@
1
- import fs from 'fs-extra';
2
- import path from 'path';
3
- import { defineConfig } from 'tsup';
4
- import addJsExtensions from './src/addJsExtensions';
5
- import generatePublishPackageJson from './src/generatePublishPackageJson';
6
-
7
- export default defineConfig((options) => ({
8
- entry: ['./src/**/*.ts'],
9
- bundle: false,
10
- splitting: true,
11
- sourcemap: true,
12
- clean: true,
13
- dts: true,
14
- format: ['cjs', 'esm'],
15
- minify: false,
16
- external: [/node_modules/],
17
- async onSuccess() {
18
- const opts = options ?? {};
19
- if (opts.watch) {
20
- return;
21
- }
22
-
23
- const outDir = opts.outDir ?? 'dist';
24
- await Promise.all([
25
- // import,requireの補完
26
- addJsExtensions(outDir),
27
- // package.jsonの編集
28
- generatePublishPackageJson({
29
- jsonWriteOptions: { spaces: 2 },
30
- }),
31
- // その他のファイルのコピー
32
- fs.copyFile('LICENSE', path.join(outDir, 'LICENSE')),
33
- fs.copyFile('README.md', path.join(outDir, 'README.md')),
34
- ]);
35
- },
36
- }));