@niche-works/dev 0.5.3 → 0.5.5
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/indexes/indexes.cjs +12 -7
- package/indexes/indexes.cjs.map +1 -1
- package/indexes/indexes.mjs +11 -7
- package/indexes/indexes.mjs.map +1 -1
- package/indexes/types.d.cts +16 -0
- package/indexes/types.d.mts +16 -0
- package/package.json +2 -2
package/indexes/indexes.cjs
CHANGED
|
@@ -5,6 +5,8 @@ const require_indexes_constants = require('./constants.cjs');
|
|
|
5
5
|
let fs_extra = require("fs-extra");
|
|
6
6
|
fs_extra = require_runtime.__toESM(fs_extra, 1);
|
|
7
7
|
let path = require("path");
|
|
8
|
+
let os = require("os");
|
|
9
|
+
os = require_runtime.__toESM(os, 1);
|
|
8
10
|
|
|
9
11
|
//#region src/indexes/indexes.ts
|
|
10
12
|
const EXPORTS = {
|
|
@@ -35,7 +37,7 @@ const EXPORTS = {
|
|
|
35
37
|
* `export { default as ${name} } from './${name}';`
|
|
36
38
|
*/
|
|
37
39
|
function indexes(options = {}) {
|
|
38
|
-
const { srcPath = "src", indexFileName = "index.ts", include = require_indexes_constants.DEFAULT_INCLUDE, exclude = require_indexes_constants.DEFAULT_EXCLUDE, exportAll = require_indexes_constants.DEFAULT_EXPORT_ALL, exportAllAs = require_indexes_constants.DEFAULT_EXPORT_ALL_AS, exportDefault = require_indexes_constants.DEFAULT_EXPORT_DEFAULT, exportTypeAll = require_indexes_constants.DEFAULT_EXPORT_TYPE_ALL, ...rest } = options;
|
|
40
|
+
const { srcPath = "src", indexFileName = "index.ts", include = require_indexes_constants.DEFAULT_INCLUDE, exclude = require_indexes_constants.DEFAULT_EXCLUDE, exportAll = require_indexes_constants.DEFAULT_EXPORT_ALL, exportAllAs = require_indexes_constants.DEFAULT_EXPORT_ALL_AS, exportDefault = require_indexes_constants.DEFAULT_EXPORT_DEFAULT, exportTypeAll = require_indexes_constants.DEFAULT_EXPORT_TYPE_ALL, eol = os.default.EOL, encoding = "utf8", ...rest } = options;
|
|
39
41
|
_indexes(srcPath, _createRegex(indexFileName), {
|
|
40
42
|
indexFileName,
|
|
41
43
|
include,
|
|
@@ -44,11 +46,13 @@ function indexes(options = {}) {
|
|
|
44
46
|
exportAllAs,
|
|
45
47
|
exportDefault,
|
|
46
48
|
exportTypeAll,
|
|
49
|
+
eol,
|
|
50
|
+
encoding,
|
|
47
51
|
...rest
|
|
48
52
|
});
|
|
49
53
|
}
|
|
50
54
|
function _indexes(targetPath, indexRegex, options) {
|
|
51
|
-
const { indexFileName
|
|
55
|
+
const { indexFileName, include, exclude, includeNamedWithDefault, exportAll, exportAllAs, exportDefault, exportDefaultAs, exportTypeAll, dryRun, transform, eol, encoding } = options;
|
|
52
56
|
const exportTargets = {
|
|
53
57
|
exportAll,
|
|
54
58
|
exportAllAs,
|
|
@@ -99,19 +103,20 @@ function _indexes(targetPath, indexRegex, options) {
|
|
|
99
103
|
}
|
|
100
104
|
}
|
|
101
105
|
if (exportCodes.length) {
|
|
102
|
-
let
|
|
103
|
-
if (hasDefaultExport && hasNamedExport && !includeNamedWithDefault)
|
|
106
|
+
let exports;
|
|
107
|
+
if (hasDefaultExport && hasNamedExport && !includeNamedWithDefault) exports = exportCodes.reduce((result, exportCode) => {
|
|
104
108
|
if (exportCode.type !== "named") result.push(exportCode.code);
|
|
105
109
|
return result;
|
|
106
110
|
}, []);
|
|
107
|
-
else
|
|
111
|
+
else exports = exportCodes.map((exportCode) => exportCode.code);
|
|
112
|
+
if (transform) exports = transform(exports);
|
|
108
113
|
const indexPath = path.posix.join(targetPath, indexFileName);
|
|
109
114
|
if (!dryRun) {
|
|
110
|
-
fs_extra.default.writeFileSync(indexPath,
|
|
115
|
+
fs_extra.default.writeFileSync(indexPath, exports.join(eol) + eol, { encoding });
|
|
111
116
|
console.info(indexPath);
|
|
112
117
|
} else {
|
|
113
118
|
console.info(indexPath + "-------------------------------------");
|
|
114
|
-
console.info(
|
|
119
|
+
console.info(exports.join(eol) + eol);
|
|
115
120
|
}
|
|
116
121
|
}
|
|
117
122
|
return items;
|
package/indexes/indexes.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"indexes.cjs","names":["DEFAULT_INCLUDE","DEFAULT_EXCLUDE","DEFAULT_EXPORT_ALL","DEFAULT_EXPORT_ALL_AS","DEFAULT_EXPORT_DEFAULT","DEFAULT_EXPORT_TYPE_ALL","fs","isMatchingPath"],"sources":["../../src/indexes/indexes.ts"],"sourcesContent":["import fs from 'fs-extra';\r\nimport { posix as path } from 'path';\r\nimport isMatchingPath from '../isMatchingPath';\r\nimport {\r\n DEFAULT_EXCLUDE,\r\n DEFAULT_EXPORT_ALL,\r\n DEFAULT_EXPORT_ALL_AS,\r\n DEFAULT_EXPORT_DEFAULT,\r\n DEFAULT_EXPORT_TYPE_ALL,\r\n DEFAULT_INCLUDE,\r\n} from './constants';\r\nimport type { IndexesOptions } from './types';\r\n\r\nconst EXPORTS = {\r\n exportAll: {\r\n type: 'named',\r\n generate: (name: string) => `export * from './${name}';`,\r\n },\r\n exportAllAs: {\r\n type: 'named',\r\n generate: (name: string) => `export * as ${name} from './${name}';`,\r\n },\r\n exportDefault: {\r\n type: 'default',\r\n generate: (name: string) => `export { default } from './${name}';`,\r\n },\r\n exportDefaultAs: {\r\n type: 'named',\r\n generate: (name: string) =>\r\n `export { default as ${name} } from './${name}';`,\r\n },\r\n exportTypeAll: {\r\n type: 'type',\r\n generate: (name: string) => `export type * from './${name}';`,\r\n },\r\n} as const;\r\n\r\n/**\r\n * 対象のディレクトリ配下のindexファイルを作成する\\\r\n * 対象ファイルで出力形式が特に指定されていない場合は下記の形式で出力\\\r\n * `export { default as ${name} } from './${name}';`\r\n */\r\nexport default function indexes(options: IndexesOptions = {}) {\r\n const {\r\n srcPath = 'src',\r\n indexFileName = 'index.ts',\r\n include = DEFAULT_INCLUDE,\r\n exclude = DEFAULT_EXCLUDE,\r\n exportAll = DEFAULT_EXPORT_ALL,\r\n exportAllAs = DEFAULT_EXPORT_ALL_AS,\r\n exportDefault = DEFAULT_EXPORT_DEFAULT,\r\n exportTypeAll = DEFAULT_EXPORT_TYPE_ALL,\r\n ...rest\r\n } = options;\r\n const indexRegex = _createRegex(indexFileName);\r\n\r\n // indexファイルの作成処理を実行\r\n _indexes(srcPath, indexRegex, {\r\n indexFileName,\r\n include,\r\n exclude,\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportTypeAll,\r\n ...rest,\r\n });\r\n}\r\n\r\nfunction _indexes(\r\n targetPath: string,\r\n indexRegex: RegExp,\r\n options: IndexesOptions,\r\n) {\r\n const {\r\n indexFileName
|
|
1
|
+
{"version":3,"file":"indexes.cjs","names":["DEFAULT_INCLUDE","DEFAULT_EXCLUDE","DEFAULT_EXPORT_ALL","DEFAULT_EXPORT_ALL_AS","DEFAULT_EXPORT_DEFAULT","DEFAULT_EXPORT_TYPE_ALL","fs","isMatchingPath"],"sources":["../../src/indexes/indexes.ts"],"sourcesContent":["import fs from 'fs-extra';\r\nimport os from 'os';\r\nimport { posix as path } from 'path';\r\nimport isMatchingPath from '../isMatchingPath';\r\nimport {\r\n DEFAULT_EXCLUDE,\r\n DEFAULT_EXPORT_ALL,\r\n DEFAULT_EXPORT_ALL_AS,\r\n DEFAULT_EXPORT_DEFAULT,\r\n DEFAULT_EXPORT_TYPE_ALL,\r\n DEFAULT_INCLUDE,\r\n} from './constants';\r\nimport type { IndexesOptions } from './types';\r\n\r\nconst EXPORTS = {\r\n exportAll: {\r\n type: 'named',\r\n generate: (name: string) => `export * from './${name}';`,\r\n },\r\n exportAllAs: {\r\n type: 'named',\r\n generate: (name: string) => `export * as ${name} from './${name}';`,\r\n },\r\n exportDefault: {\r\n type: 'default',\r\n generate: (name: string) => `export { default } from './${name}';`,\r\n },\r\n exportDefaultAs: {\r\n type: 'named',\r\n generate: (name: string) =>\r\n `export { default as ${name} } from './${name}';`,\r\n },\r\n exportTypeAll: {\r\n type: 'type',\r\n generate: (name: string) => `export type * from './${name}';`,\r\n },\r\n} as const;\r\n\r\n/**\r\n * 対象のディレクトリ配下のindexファイルを作成する\\\r\n * 対象ファイルで出力形式が特に指定されていない場合は下記の形式で出力\\\r\n * `export { default as ${name} } from './${name}';`\r\n */\r\nexport default function indexes(options: IndexesOptions = {}) {\r\n const {\r\n srcPath = 'src',\r\n indexFileName = 'index.ts',\r\n include = DEFAULT_INCLUDE,\r\n exclude = DEFAULT_EXCLUDE,\r\n exportAll = DEFAULT_EXPORT_ALL,\r\n exportAllAs = DEFAULT_EXPORT_ALL_AS,\r\n exportDefault = DEFAULT_EXPORT_DEFAULT,\r\n exportTypeAll = DEFAULT_EXPORT_TYPE_ALL,\r\n eol = os.EOL,\r\n encoding = 'utf8',\r\n ...rest\r\n } = options;\r\n const indexRegex = _createRegex(indexFileName);\r\n\r\n // indexファイルの作成処理を実行\r\n _indexes(srcPath, indexRegex, {\r\n indexFileName,\r\n include,\r\n exclude,\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportTypeAll,\r\n eol,\r\n encoding,\r\n ...rest,\r\n });\r\n}\r\n\r\nfunction _indexes(\r\n targetPath: string,\r\n indexRegex: RegExp,\r\n options: IndexesOptions,\r\n) {\r\n const {\r\n indexFileName,\r\n include,\r\n exclude,\r\n includeNamedWithDefault,\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportDefaultAs,\r\n exportTypeAll,\r\n dryRun,\r\n transform,\r\n eol,\r\n encoding,\r\n } = options;\r\n const exportTargets = {\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportDefaultAs,\r\n exportTypeAll,\r\n };\r\n const stat = fs.statSync(targetPath);\r\n if (!stat.isDirectory()) {\r\n console.error(`\"${targetPath}\" is not directory.`);\r\n }\r\n const items = fs.readdirSync(targetPath);\r\n items.sort((a, b) => (a.toLowerCase() > b.toLowerCase() ? 1 : -1));\r\n const exportCodes = [];\r\n let hasDefaultExport = false;\r\n let hasNamedExport = false;\r\n\r\n for (const item of items) {\r\n const itemPath = path.join(targetPath, item);\r\n const { name } = path.parse(itemPath);\r\n const stat = fs.statSync(itemPath);\r\n\r\n let children;\r\n if (stat.isDirectory()) {\r\n // ディレクトリの場合は先に子要素を処理\r\n children = _indexes(itemPath, indexRegex, options);\r\n }\r\n const isMatchingPathOptions = {\r\n conditionOptions: { indexRegex, children },\r\n };\r\n\r\n if (\r\n !indexRegex.test(item) &&\r\n isMatchingPath(itemPath, include, isMatchingPathOptions) &&\r\n !isMatchingPath(itemPath, exclude, isMatchingPathOptions)\r\n ) {\r\n // indexではない場合\r\n let isExported = false;\r\n for (const type in EXPORTS) {\r\n const exportType = type as keyof typeof EXPORTS;\r\n const exportTarget = exportTargets[exportType];\r\n if (\r\n exportTarget &&\r\n isMatchingPath(itemPath, exportTarget, isMatchingPathOptions)\r\n ) {\r\n // 条件に一致した場合はexportのコードを生成\r\n const EXPORT = EXPORTS[exportType];\r\n exportCodes.push({\r\n type: EXPORT.type,\r\n code: EXPORT.generate(name),\r\n });\r\n // exportの種類に応じたフラグを立てる\r\n if (EXPORT.type === 'named') {\r\n hasNamedExport = true;\r\n } else if (EXPORT.type === 'default') {\r\n hasDefaultExport = true;\r\n }\r\n isExported = true;\r\n break;\r\n }\r\n }\r\n if (!isExported) {\r\n // 未指定の場合はexportDefaultAs\r\n exportCodes.push({\r\n type: EXPORTS.exportDefaultAs.type,\r\n code: EXPORTS.exportDefaultAs.generate(name),\r\n });\r\n hasNamedExport = true;\r\n }\r\n }\r\n }\r\n\r\n if (exportCodes.length) {\r\n let exports: string[];\r\n if (hasDefaultExport && hasNamedExport && !includeNamedWithDefault) {\r\n // デフォルトエクスポートと名前付きエクスポートの混在を許さない場合は名前付きエクスポートを除外\r\n exports = exportCodes.reduce<string[]>((result, exportCode) => {\r\n if (exportCode.type !== 'named') {\r\n result.push(exportCode.code);\r\n }\r\n return result;\r\n }, []);\r\n } else {\r\n exports = exportCodes.map((exportCode) => exportCode.code);\r\n }\r\n if (transform) {\r\n exports = transform(exports);\r\n }\r\n\r\n const indexPath = path.join(targetPath, indexFileName);\r\n if (!dryRun) {\r\n // indexファイルの出力\r\n fs.writeFileSync(indexPath, exports.join(eol) + eol, {\r\n encoding,\r\n });\r\n console.info(indexPath);\r\n } else {\r\n console.info(indexPath + '-------------------------------------');\r\n console.info(exports.join(eol) + eol);\r\n }\r\n }\r\n\r\n return items;\r\n}\r\n\r\nfunction _createRegex(str: string) {\r\n return new RegExp(\r\n `^${str.replace(/[.*+?^=!:${}()|\\[\\]\\/\\\\]/g, '\\\\$&')}$`,\r\n 'i',\r\n );\r\n}\r\n"],"mappings":";;;;;;;;;;;AAcA,MAAM,UAAU;CACd,WAAW;EACT,MAAM;EACN,WAAW,SAAiB,oBAAoB,KAAK;CACvD;CACA,aAAa;EACX,MAAM;EACN,WAAW,SAAiB,eAAe,KAAK,WAAW,KAAK;CAClE;CACA,eAAe;EACb,MAAM;EACN,WAAW,SAAiB,8BAA8B,KAAK;CACjE;CACA,iBAAiB;EACf,MAAM;EACN,WAAW,SACT,uBAAuB,KAAK,aAAa,KAAK;CAClD;CACA,eAAe;EACb,MAAM;EACN,WAAW,SAAiB,yBAAyB,KAAK;CAC5D;AACF;;;;;;AAOA,SAAwB,QAAQ,UAA0B,CAAC,GAAG;CAC5D,MAAM,EACJ,UAAU,OACV,gBAAgB,YAChB,UAAUA,2CACV,UAAUC,2CACV,YAAYC,8CACZ,cAAcC,iDACd,gBAAgBC,kDAChB,gBAAgBC,mDAChB,MAAM,WAAG,KACT,WAAW,QACX,GAAG,SACD;CAIJ,SAAS,SAHU,aAAa,aAGL,GAAG;EAC5B;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,GAAG;CACL,CAAC;AACH;AAEA,SAAS,SACP,YACA,YACA,SACA;CACA,MAAM,EACJ,eACA,SACA,SACA,yBACA,WACA,aACA,eACA,iBACA,eACA,QACA,WACA,KACA,aACE;CACJ,MAAM,gBAAgB;EACpB;E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ACtB,IAAI;EACJ,IAAI,oBAAoB,kBAAkB,CAAC,yBAEzC,UAAU,YAAY,QAAkB,QAAQ,eAAe;GAC7D,IAAI,WAAW,SAAS,SACtB,OAAO,KAAK,WAAW,IAAI;GAE7B,OAAO;EACT,GAAG,CAAC,CAAC;OAEL,UAAU,YAAY,KAAK,eAAe,WAAW,IAAI;EAE3D,IAAI,WACF,UAAU,UAAU,OAAO;EAG7B,MAAM,YAAY,WAAK,KAAK,YAAY,aAAa;EACrD,IAAI,CAAC,QAAQ;GAEX,iBAAG,cAAc,WAAW,QAAQ,KAAK,GAAG,IAAI,KAAK,EACnD,SACF,CAAC;GACD,QAAQ,KAAK,SAAS;EACxB,OAAO;GACL,QAAQ,KAAK,YAAY,uCAAuC;GAChE,QAAQ,KAAK,QAAQ,KAAK,GAAG,IAAI,GAAG;EACtC;CACF;CAEA,OAAO;AACT;AAEA,SAAS,aAAa,KAAa;CACjC,OAAO,IAAI,OACT,IAAI,IAAI,QAAQ,6BAA6B,MAAM,EAAE,IACrD,GACF;AACF"}
|
package/indexes/indexes.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import "../isMatchingPath/index.mjs";
|
|
|
3
3
|
import { DEFAULT_EXCLUDE, DEFAULT_EXPORT_ALL, DEFAULT_EXPORT_ALL_AS, DEFAULT_EXPORT_DEFAULT, DEFAULT_EXPORT_TYPE_ALL, DEFAULT_INCLUDE } from "./constants.mjs";
|
|
4
4
|
import fs from "fs-extra";
|
|
5
5
|
import { posix } from "path";
|
|
6
|
+
import os from "os";
|
|
6
7
|
|
|
7
8
|
//#region src/indexes/indexes.ts
|
|
8
9
|
const EXPORTS = {
|
|
@@ -33,7 +34,7 @@ const EXPORTS = {
|
|
|
33
34
|
* `export { default as ${name} } from './${name}';`
|
|
34
35
|
*/
|
|
35
36
|
function indexes(options = {}) {
|
|
36
|
-
const { srcPath = "src", indexFileName = "index.ts", include = DEFAULT_INCLUDE, exclude = DEFAULT_EXCLUDE, exportAll = DEFAULT_EXPORT_ALL, exportAllAs = DEFAULT_EXPORT_ALL_AS, exportDefault = DEFAULT_EXPORT_DEFAULT, exportTypeAll = DEFAULT_EXPORT_TYPE_ALL, ...rest } = options;
|
|
37
|
+
const { srcPath = "src", indexFileName = "index.ts", include = DEFAULT_INCLUDE, exclude = DEFAULT_EXCLUDE, exportAll = DEFAULT_EXPORT_ALL, exportAllAs = DEFAULT_EXPORT_ALL_AS, exportDefault = DEFAULT_EXPORT_DEFAULT, exportTypeAll = DEFAULT_EXPORT_TYPE_ALL, eol = os.EOL, encoding = "utf8", ...rest } = options;
|
|
37
38
|
_indexes(srcPath, _createRegex(indexFileName), {
|
|
38
39
|
indexFileName,
|
|
39
40
|
include,
|
|
@@ -42,11 +43,13 @@ function indexes(options = {}) {
|
|
|
42
43
|
exportAllAs,
|
|
43
44
|
exportDefault,
|
|
44
45
|
exportTypeAll,
|
|
46
|
+
eol,
|
|
47
|
+
encoding,
|
|
45
48
|
...rest
|
|
46
49
|
});
|
|
47
50
|
}
|
|
48
51
|
function _indexes(targetPath, indexRegex, options) {
|
|
49
|
-
const { indexFileName
|
|
52
|
+
const { indexFileName, include, exclude, includeNamedWithDefault, exportAll, exportAllAs, exportDefault, exportDefaultAs, exportTypeAll, dryRun, transform, eol, encoding } = options;
|
|
50
53
|
const exportTargets = {
|
|
51
54
|
exportAll,
|
|
52
55
|
exportAllAs,
|
|
@@ -97,19 +100,20 @@ function _indexes(targetPath, indexRegex, options) {
|
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
102
|
if (exportCodes.length) {
|
|
100
|
-
let
|
|
101
|
-
if (hasDefaultExport && hasNamedExport && !includeNamedWithDefault)
|
|
103
|
+
let exports;
|
|
104
|
+
if (hasDefaultExport && hasNamedExport && !includeNamedWithDefault) exports = exportCodes.reduce((result, exportCode) => {
|
|
102
105
|
if (exportCode.type !== "named") result.push(exportCode.code);
|
|
103
106
|
return result;
|
|
104
107
|
}, []);
|
|
105
|
-
else
|
|
108
|
+
else exports = exportCodes.map((exportCode) => exportCode.code);
|
|
109
|
+
if (transform) exports = transform(exports);
|
|
106
110
|
const indexPath = posix.join(targetPath, indexFileName);
|
|
107
111
|
if (!dryRun) {
|
|
108
|
-
fs.writeFileSync(indexPath,
|
|
112
|
+
fs.writeFileSync(indexPath, exports.join(eol) + eol, { encoding });
|
|
109
113
|
console.info(indexPath);
|
|
110
114
|
} else {
|
|
111
115
|
console.info(indexPath + "-------------------------------------");
|
|
112
|
-
console.info(
|
|
116
|
+
console.info(exports.join(eol) + eol);
|
|
113
117
|
}
|
|
114
118
|
}
|
|
115
119
|
return items;
|
package/indexes/indexes.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"indexes.mjs","names":["path"],"sources":["../../src/indexes/indexes.ts"],"sourcesContent":["import fs from 'fs-extra';\r\nimport { posix as path } from 'path';\r\nimport isMatchingPath from '../isMatchingPath';\r\nimport {\r\n DEFAULT_EXCLUDE,\r\n DEFAULT_EXPORT_ALL,\r\n DEFAULT_EXPORT_ALL_AS,\r\n DEFAULT_EXPORT_DEFAULT,\r\n DEFAULT_EXPORT_TYPE_ALL,\r\n DEFAULT_INCLUDE,\r\n} from './constants';\r\nimport type { IndexesOptions } from './types';\r\n\r\nconst EXPORTS = {\r\n exportAll: {\r\n type: 'named',\r\n generate: (name: string) => `export * from './${name}';`,\r\n },\r\n exportAllAs: {\r\n type: 'named',\r\n generate: (name: string) => `export * as ${name} from './${name}';`,\r\n },\r\n exportDefault: {\r\n type: 'default',\r\n generate: (name: string) => `export { default } from './${name}';`,\r\n },\r\n exportDefaultAs: {\r\n type: 'named',\r\n generate: (name: string) =>\r\n `export { default as ${name} } from './${name}';`,\r\n },\r\n exportTypeAll: {\r\n type: 'type',\r\n generate: (name: string) => `export type * from './${name}';`,\r\n },\r\n} as const;\r\n\r\n/**\r\n * 対象のディレクトリ配下のindexファイルを作成する\\\r\n * 対象ファイルで出力形式が特に指定されていない場合は下記の形式で出力\\\r\n * `export { default as ${name} } from './${name}';`\r\n */\r\nexport default function indexes(options: IndexesOptions = {}) {\r\n const {\r\n srcPath = 'src',\r\n indexFileName = 'index.ts',\r\n include = DEFAULT_INCLUDE,\r\n exclude = DEFAULT_EXCLUDE,\r\n exportAll = DEFAULT_EXPORT_ALL,\r\n exportAllAs = DEFAULT_EXPORT_ALL_AS,\r\n exportDefault = DEFAULT_EXPORT_DEFAULT,\r\n exportTypeAll = DEFAULT_EXPORT_TYPE_ALL,\r\n ...rest\r\n } = options;\r\n const indexRegex = _createRegex(indexFileName);\r\n\r\n // indexファイルの作成処理を実行\r\n _indexes(srcPath, indexRegex, {\r\n indexFileName,\r\n include,\r\n exclude,\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportTypeAll,\r\n ...rest,\r\n });\r\n}\r\n\r\nfunction _indexes(\r\n targetPath: string,\r\n indexRegex: RegExp,\r\n options: IndexesOptions,\r\n) {\r\n const {\r\n indexFileName
|
|
1
|
+
{"version":3,"file":"indexes.mjs","names":["path"],"sources":["../../src/indexes/indexes.ts"],"sourcesContent":["import fs from 'fs-extra';\r\nimport os from 'os';\r\nimport { posix as path } from 'path';\r\nimport isMatchingPath from '../isMatchingPath';\r\nimport {\r\n DEFAULT_EXCLUDE,\r\n DEFAULT_EXPORT_ALL,\r\n DEFAULT_EXPORT_ALL_AS,\r\n DEFAULT_EXPORT_DEFAULT,\r\n DEFAULT_EXPORT_TYPE_ALL,\r\n DEFAULT_INCLUDE,\r\n} from './constants';\r\nimport type { IndexesOptions } from './types';\r\n\r\nconst EXPORTS = {\r\n exportAll: {\r\n type: 'named',\r\n generate: (name: string) => `export * from './${name}';`,\r\n },\r\n exportAllAs: {\r\n type: 'named',\r\n generate: (name: string) => `export * as ${name} from './${name}';`,\r\n },\r\n exportDefault: {\r\n type: 'default',\r\n generate: (name: string) => `export { default } from './${name}';`,\r\n },\r\n exportDefaultAs: {\r\n type: 'named',\r\n generate: (name: string) =>\r\n `export { default as ${name} } from './${name}';`,\r\n },\r\n exportTypeAll: {\r\n type: 'type',\r\n generate: (name: string) => `export type * from './${name}';`,\r\n },\r\n} as const;\r\n\r\n/**\r\n * 対象のディレクトリ配下のindexファイルを作成する\\\r\n * 対象ファイルで出力形式が特に指定されていない場合は下記の形式で出力\\\r\n * `export { default as ${name} } from './${name}';`\r\n */\r\nexport default function indexes(options: IndexesOptions = {}) {\r\n const {\r\n srcPath = 'src',\r\n indexFileName = 'index.ts',\r\n include = DEFAULT_INCLUDE,\r\n exclude = DEFAULT_EXCLUDE,\r\n exportAll = DEFAULT_EXPORT_ALL,\r\n exportAllAs = DEFAULT_EXPORT_ALL_AS,\r\n exportDefault = DEFAULT_EXPORT_DEFAULT,\r\n exportTypeAll = DEFAULT_EXPORT_TYPE_ALL,\r\n eol = os.EOL,\r\n encoding = 'utf8',\r\n ...rest\r\n } = options;\r\n const indexRegex = _createRegex(indexFileName);\r\n\r\n // indexファイルの作成処理を実行\r\n _indexes(srcPath, indexRegex, {\r\n indexFileName,\r\n include,\r\n exclude,\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportTypeAll,\r\n eol,\r\n encoding,\r\n ...rest,\r\n });\r\n}\r\n\r\nfunction _indexes(\r\n targetPath: string,\r\n indexRegex: RegExp,\r\n options: IndexesOptions,\r\n) {\r\n const {\r\n indexFileName,\r\n include,\r\n exclude,\r\n includeNamedWithDefault,\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportDefaultAs,\r\n exportTypeAll,\r\n dryRun,\r\n transform,\r\n eol,\r\n encoding,\r\n } = options;\r\n const exportTargets = {\r\n exportAll,\r\n exportAllAs,\r\n exportDefault,\r\n exportDefaultAs,\r\n exportTypeAll,\r\n };\r\n const stat = fs.statSync(targetPath);\r\n if (!stat.isDirectory()) {\r\n console.error(`\"${targetPath}\" is not directory.`);\r\n }\r\n const items = fs.readdirSync(targetPath);\r\n items.sort((a, b) => (a.toLowerCase() > b.toLowerCase() ? 1 : -1));\r\n const exportCodes = [];\r\n let hasDefaultExport = false;\r\n let hasNamedExport = false;\r\n\r\n for (const item of items) {\r\n const itemPath = path.join(targetPath, item);\r\n const { name } = path.parse(itemPath);\r\n const stat = fs.statSync(itemPath);\r\n\r\n let children;\r\n if (stat.isDirectory()) {\r\n // ディレクトリの場合は先に子要素を処理\r\n children = _indexes(itemPath, indexRegex, options);\r\n }\r\n const isMatchingPathOptions = {\r\n conditionOptions: { indexRegex, children },\r\n };\r\n\r\n if (\r\n !indexRegex.test(item) &&\r\n isMatchingPath(itemPath, include, isMatchingPathOptions) &&\r\n !isMatchingPath(itemPath, exclude, isMatchingPathOptions)\r\n ) {\r\n // indexではない場合\r\n let isExported = false;\r\n for (const type in EXPORTS) {\r\n const exportType = type as keyof typeof EXPORTS;\r\n const exportTarget = exportTargets[exportType];\r\n if (\r\n exportTarget &&\r\n isMatchingPath(itemPath, exportTarget, isMatchingPathOptions)\r\n ) {\r\n // 条件に一致した場合はexportのコードを生成\r\n const EXPORT = EXPORTS[exportType];\r\n exportCodes.push({\r\n type: EXPORT.type,\r\n code: EXPORT.generate(name),\r\n });\r\n // exportの種類に応じたフラグを立てる\r\n if (EXPORT.type === 'named') {\r\n hasNamedExport = true;\r\n } else if (EXPORT.type === 'default') {\r\n hasDefaultExport = true;\r\n }\r\n isExported = true;\r\n break;\r\n }\r\n }\r\n if (!isExported) {\r\n // 未指定の場合はexportDefaultAs\r\n exportCodes.push({\r\n type: EXPORTS.exportDefaultAs.type,\r\n code: EXPORTS.exportDefaultAs.generate(name),\r\n });\r\n hasNamedExport = true;\r\n }\r\n }\r\n }\r\n\r\n if (exportCodes.length) {\r\n let exports: string[];\r\n if (hasDefaultExport && hasNamedExport && !includeNamedWithDefault) {\r\n // デフォルトエクスポートと名前付きエクスポートの混在を許さない場合は名前付きエクスポートを除外\r\n exports = exportCodes.reduce<string[]>((result, exportCode) => {\r\n if (exportCode.type !== 'named') {\r\n result.push(exportCode.code);\r\n }\r\n return result;\r\n }, []);\r\n } else {\r\n exports = exportCodes.map((exportCode) => exportCode.code);\r\n }\r\n if (transform) {\r\n exports = transform(exports);\r\n }\r\n\r\n const indexPath = path.join(targetPath, indexFileName);\r\n if (!dryRun) {\r\n // indexファイルの出力\r\n fs.writeFileSync(indexPath, exports.join(eol) + eol, {\r\n encoding,\r\n });\r\n console.info(indexPath);\r\n } else {\r\n console.info(indexPath + '-------------------------------------');\r\n console.info(exports.join(eol) + eol);\r\n }\r\n }\r\n\r\n return items;\r\n}\r\n\r\nfunction _createRegex(str: string) {\r\n return new RegExp(\r\n `^${str.replace(/[.*+?^=!:${}()|\\[\\]\\/\\\\]/g, '\\\\$&')}$`,\r\n 'i',\r\n );\r\n}\r\n"],"mappings":";;;;;;;;AAcA,MAAM,UAAU;CACd,WAAW;EACT,MAAM;EACN,WAAW,SAAiB,oBAAoB,KAAK;CACvD;CACA,aAAa;EACX,MAAM;EACN,WAAW,SAAiB,eAAe,KAAK,WAAW,KAAK;CAClE;CACA,eAAe;EACb,MAAM;EACN,WAAW,SAAiB,8BAA8B,KAAK;CACjE;CACA,iBAAiB;EACf,MAAM;EACN,WAAW,SACT,uBAAuB,KAAK,aAAa,KAAK;CAClD;CACA,eAAe;EACb,MAAM;EACN,WAAW,SAAiB,yBAAyB,KAAK;CAC5D;AACF;;;;;;AAOA,SAAwB,QAAQ,UAA0B,CAAC,GAAG;CAC5D,MAAM,EACJ,UAAU,OACV,gBAAgB,YAChB,UAAU,iBACV,UAAU,iBACV,YAAY,oBACZ,cAAc,uBACd,gBAAgB,wBAChB,gBAAgB,yBAChB,MAAM,GAAG,KACT,WAAW,QACX,GAAG,SACD;CAIJ,SAAS,SAHU,aAAa,aAGL,GAAG;EAC5B;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,GAAG;CACL,CAAC;AACH;AAEA,SAAS,SACP,YACA,YACA,SACA;CACA,MAAM,EACJ,eACA,SACA,SACA,yBACA,WACA,aACA,eACA,iBACA,eACA,QACA,WACA,KACA,aACE;CACJ,MAAM,gBA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EACtB,IAAI;EACJ,IAAI,oBAAoB,kBAAkB,CAAC,yBAEzC,UAAU,YAAY,QAAkB,QAAQ,eAAe;GAC7D,IAAI,WAAW,SAAS,SACtB,OAAO,KAAK,WAAW,IAAI;GAE7B,OAAO;EACT,GAAG,CAAC,CAAC;OAEL,UAAU,YAAY,KAAK,eAAe,WAAW,IAAI;EAE3D,IAAI,WACF,UAAU,UAAU,OAAO;EAG7B,MAAM,YAAYA,MAAK,KAAK,YAAY,aAAa;EACrD,IAAI,CAAC,QAAQ;GAEX,GAAG,cAAc,WAAW,QAAQ,KAAK,GAAG,IAAI,KAAK,EACnD,SACF,CAAC;GACD,QAAQ,KAAK,SAAS;EACxB,OAAO;GACL,QAAQ,KAAK,YAAY,uCAAuC;GAChE,QAAQ,KAAK,QAAQ,KAAK,GAAG,IAAI,GAAG;EACtC;CACF;CAEA,OAAO;AACT;AAEA,SAAS,aAAa,KAAa;CACjC,OAAO,IAAI,OACT,IAAI,IAAI,QAAQ,6BAA6B,MAAM,EAAE,IACrD,GACF;AACF"}
|
package/indexes/types.d.cts
CHANGED
|
@@ -75,6 +75,22 @@ type IndexesOptions = {
|
|
|
75
75
|
* ファイルの操作は行わない
|
|
76
76
|
*/
|
|
77
77
|
dryRun?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* 出力前の編集
|
|
80
|
+
*/
|
|
81
|
+
transform?: (exports: string[]) => string[];
|
|
82
|
+
/**
|
|
83
|
+
* 出力の改行コード
|
|
84
|
+
*
|
|
85
|
+
* @default os.EOL
|
|
86
|
+
*/
|
|
87
|
+
eol?: string;
|
|
88
|
+
/**
|
|
89
|
+
* 出力のエンコード
|
|
90
|
+
*
|
|
91
|
+
* @default `utf8`
|
|
92
|
+
*/
|
|
93
|
+
encoding?: BufferEncoding;
|
|
78
94
|
};
|
|
79
95
|
//#endregion
|
|
80
96
|
export { IndexesOptions };
|
package/indexes/types.d.mts
CHANGED
|
@@ -75,6 +75,22 @@ type IndexesOptions = {
|
|
|
75
75
|
* ファイルの操作は行わない
|
|
76
76
|
*/
|
|
77
77
|
dryRun?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* 出力前の編集
|
|
80
|
+
*/
|
|
81
|
+
transform?: (exports: string[]) => string[];
|
|
82
|
+
/**
|
|
83
|
+
* 出力の改行コード
|
|
84
|
+
*
|
|
85
|
+
* @default os.EOL
|
|
86
|
+
*/
|
|
87
|
+
eol?: string;
|
|
88
|
+
/**
|
|
89
|
+
* 出力のエンコード
|
|
90
|
+
*
|
|
91
|
+
* @default `utf8`
|
|
92
|
+
*/
|
|
93
|
+
encoding?: BufferEncoding;
|
|
78
94
|
};
|
|
79
95
|
//#endregion
|
|
80
96
|
export { IndexesOptions };
|