@niche-works/dev 0.2.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 +30 -23
- package/src/addExports/addExports.ts +94 -0
- package/src/addExports/index.ts +2 -0
- package/src/addExports/types.ts +33 -0
- package/src/addJsExtensions/addJsExtensions.ts +30 -0
- package/src/addJsExtensions/index.ts +1 -0
- package/src/checkExistence/checkExistence.ts +16 -0
- package/src/checkExistence/index.ts +1 -0
- package/src/createExternalOptionFunction/createExternalOptionFunction.ts +43 -0
- package/src/createExternalOptionFunction/index.ts +1 -0
- package/src/createExternalOptionFunction/types.ts +34 -0
- package/src/editJsonFile/editJsonFile.ts +30 -0
- package/src/editJsonFile/index.ts +1 -0
- package/src/generatePublishPackageJson/generatePublishPackageJson.ts +55 -0
- package/src/generatePublishPackageJson/index.ts +2 -0
- package/src/generatePublishPackageJson/types.ts +61 -0
- package/src/index.ts +11 -0
- package/src/indexes/constants.ts +135 -0
- package/src/indexes/index.ts +2 -0
- package/src/indexes/indexes.ts +194 -0
- package/src/indexes/types.ts +87 -0
- package/src/installFromLocal/index.ts +2 -0
- package/{installFromLocal/installFromLocal.js → src/installFromLocal/installFromLocal.ts} +63 -48
- package/src/installFromLocal/types.ts +19 -0
- package/src/isMatchingPath/index.ts +2 -0
- package/src/isMatchingPath/isMatchingPath.ts +94 -0
- package/src/isMatchingPath/types.ts +95 -0
- package/src/isTargetPath/index.ts +2 -0
- package/src/isTargetPath/isTargetPath.ts +30 -0
- package/src/isTargetPath/types.ts +38 -0
- package/src/moveFilesToSubdir/index.ts +2 -0
- package/src/moveFilesToSubdir/moveFilesToSubdir.ts +40 -0
- package/src/moveFilesToSubdir/types.ts +15 -0
- package/src/removeFiles/index.ts +1 -0
- package/src/removeFiles/removeFiles.ts +11 -0
- package/src/removeFiles/types.ts +5 -0
- package/src/ver/index.ts +2 -0
- package/src/ver/types.ts +13 -0
- package/src/ver/ver.ts +38 -0
- package/tsconfig.json +46 -0
- package/tsdown.config.ts +41 -0
- package/addExports/addExports.cjs +0 -71
- package/addExports/addExports.cjs.map +0 -1
- package/addExports/addExports.d.cts +0 -13
- package/addExports/addExports.d.ts +0 -13
- package/addExports/addExports.js +0 -71
- package/addExports/addExports.js.map +0 -1
- package/addExports/index.cjs +0 -5
- package/addExports/index.cjs.map +0 -1
- package/addExports/index.d.cts +0 -5
- package/addExports/index.d.ts +0 -5
- package/addExports/index.js +0 -5
- package/addExports/index.js.map +0 -1
- package/addExports/types.cjs +0 -1
- package/addExports/types.cjs.map +0 -1
- package/addExports/types.d.cts +0 -33
- package/addExports/types.d.ts +0 -33
- package/addExports/types.js +0 -1
- package/addExports/types.js.map +0 -1
- package/checkExistence/checkExistence.cjs +0 -15
- package/checkExistence/checkExistence.cjs.map +0 -1
- package/checkExistence/checkExistence.d.cts +0 -8
- package/checkExistence/checkExistence.d.ts +0 -8
- package/checkExistence/checkExistence.js +0 -15
- package/checkExistence/checkExistence.js.map +0 -1
- package/checkExistence/index.cjs +0 -5
- package/checkExistence/index.cjs.map +0 -1
- package/checkExistence/index.d.cts +0 -1
- package/checkExistence/index.d.ts +0 -1
- package/checkExistence/index.js +0 -5
- package/checkExistence/index.js.map +0 -1
- package/editJsonFile/editJsonFile.cjs +0 -22
- package/editJsonFile/editJsonFile.cjs.map +0 -1
- package/editJsonFile/editJsonFile.d.cts +0 -8
- package/editJsonFile/editJsonFile.d.ts +0 -8
- package/editJsonFile/editJsonFile.js +0 -22
- package/editJsonFile/editJsonFile.js.map +0 -1
- package/editJsonFile/index.cjs +0 -5
- package/editJsonFile/index.cjs.map +0 -1
- package/editJsonFile/index.d.cts +0 -1
- package/editJsonFile/index.d.ts +0 -1
- package/editJsonFile/index.js +0 -5
- package/editJsonFile/index.js.map +0 -1
- package/generatePublishPackageJson/generatePublishPackageJson.cjs +0 -54
- package/generatePublishPackageJson/generatePublishPackageJson.cjs.map +0 -1
- package/generatePublishPackageJson/generatePublishPackageJson.d.cts +0 -7
- package/generatePublishPackageJson/generatePublishPackageJson.d.ts +0 -7
- package/generatePublishPackageJson/generatePublishPackageJson.js +0 -54
- package/generatePublishPackageJson/generatePublishPackageJson.js.map +0 -1
- package/generatePublishPackageJson/index.cjs +0 -5
- package/generatePublishPackageJson/index.cjs.map +0 -1
- package/generatePublishPackageJson/index.d.cts +0 -4
- package/generatePublishPackageJson/index.d.ts +0 -4
- package/generatePublishPackageJson/index.js +0 -5
- package/generatePublishPackageJson/index.js.map +0 -1
- package/generatePublishPackageJson/types.cjs +0 -1
- package/generatePublishPackageJson/types.cjs.map +0 -1
- package/generatePublishPackageJson/types.d.cts +0 -58
- package/generatePublishPackageJson/types.d.ts +0 -58
- package/generatePublishPackageJson/types.js +0 -1
- package/generatePublishPackageJson/types.js.map +0 -1
- package/index.cjs +0 -21
- package/index.cjs.map +0 -1
- package/index.d.cts +0 -19
- package/index.d.ts +0 -19
- package/index.js +0 -21
- package/index.js.map +0 -1
- package/indexes/constants.cjs +0 -91
- package/indexes/constants.cjs.map +0 -1
- package/indexes/constants.d.cts +0 -66
- package/indexes/constants.d.ts +0 -66
- package/indexes/constants.js +0 -91
- package/indexes/constants.js.map +0 -1
- package/indexes/index.cjs +0 -5
- package/indexes/index.cjs.map +0 -1
- package/indexes/index.d.cts +0 -5
- package/indexes/index.d.ts +0 -5
- package/indexes/index.js +0 -5
- package/indexes/index.js.map +0 -1
- package/indexes/indexes.cjs +0 -161
- package/indexes/indexes.cjs.map +0 -1
- package/indexes/indexes.d.cts +0 -13
- package/indexes/indexes.d.ts +0 -13
- package/indexes/indexes.js +0 -161
- package/indexes/indexes.js.map +0 -1
- package/indexes/types.cjs +0 -1
- package/indexes/types.cjs.map +0 -1
- package/indexes/types.d.cts +0 -81
- package/indexes/types.d.ts +0 -81
- package/indexes/types.js +0 -1
- package/indexes/types.js.map +0 -1
- package/installFromLocal/index.cjs +0 -5
- package/installFromLocal/index.cjs.map +0 -1
- package/installFromLocal/index.d.cts +0 -2
- package/installFromLocal/index.d.ts +0 -2
- package/installFromLocal/index.js +0 -5
- package/installFromLocal/index.js.map +0 -1
- package/installFromLocal/installFromLocal.cjs +0 -48
- package/installFromLocal/installFromLocal.cjs.map +0 -1
- package/installFromLocal/installFromLocal.d.cts +0 -10
- package/installFromLocal/installFromLocal.d.ts +0 -10
- package/installFromLocal/installFromLocal.js.map +0 -1
- package/installFromLocal/types.cjs +0 -1
- package/installFromLocal/types.cjs.map +0 -1
- package/installFromLocal/types.d.cts +0 -19
- package/installFromLocal/types.d.ts +0 -19
- package/installFromLocal/types.js +0 -1
- package/installFromLocal/types.js.map +0 -1
- package/isMatchingPath/_types.cjs +0 -1
- package/isMatchingPath/_types.cjs.map +0 -1
- package/isMatchingPath/_types.d.cts +0 -52
- package/isMatchingPath/_types.d.ts +0 -52
- package/isMatchingPath/_types.js +0 -1
- package/isMatchingPath/_types.js.map +0 -1
- package/isMatchingPath/index.cjs +0 -5
- package/isMatchingPath/index.cjs.map +0 -1
- package/isMatchingPath/index.d.cts +0 -4
- package/isMatchingPath/index.d.ts +0 -4
- package/isMatchingPath/index.js +0 -5
- package/isMatchingPath/index.js.map +0 -1
- package/isMatchingPath/isMatchingPath.cjs +0 -57
- package/isMatchingPath/isMatchingPath.cjs.map +0 -1
- package/isMatchingPath/isMatchingPath.d.cts +0 -14
- package/isMatchingPath/isMatchingPath.d.ts +0 -14
- package/isMatchingPath/isMatchingPath.js +0 -57
- package/isMatchingPath/isMatchingPath.js.map +0 -1
- package/isMatchingPath/types.cjs +0 -1
- package/isMatchingPath/types.cjs.map +0 -1
- package/isMatchingPath/types.d.cts +0 -19
- package/isMatchingPath/types.d.ts +0 -19
- package/isMatchingPath/types.js +0 -1
- package/isMatchingPath/types.js.map +0 -1
- package/moveFilesToSubdir/index.cjs +0 -5
- package/moveFilesToSubdir/index.cjs.map +0 -1
- package/moveFilesToSubdir/index.d.cts +0 -1
- package/moveFilesToSubdir/index.d.ts +0 -1
- package/moveFilesToSubdir/index.js +0 -5
- package/moveFilesToSubdir/index.js.map +0 -1
- package/moveFilesToSubdir/moveFilesToSubdir.cjs +0 -19
- package/moveFilesToSubdir/moveFilesToSubdir.cjs.map +0 -1
- package/moveFilesToSubdir/moveFilesToSubdir.d.cts +0 -7
- package/moveFilesToSubdir/moveFilesToSubdir.d.ts +0 -7
- package/moveFilesToSubdir/moveFilesToSubdir.js +0 -19
- package/moveFilesToSubdir/moveFilesToSubdir.js.map +0 -1
- package/ver/index.cjs +0 -5
- package/ver/index.cjs.map +0 -1
- package/ver/index.d.cts +0 -2
- package/ver/index.d.ts +0 -2
- package/ver/index.js +0 -5
- package/ver/index.js.map +0 -1
- package/ver/types.cjs +0 -1
- package/ver/types.cjs.map +0 -1
- package/ver/types.d.cts +0 -14
- package/ver/types.d.ts +0 -14
- package/ver/types.js +0 -1
- package/ver/types.js.map +0 -1
- package/ver/ver.cjs +0 -31
- package/ver/ver.cjs.map +0 -1
- package/ver/ver.d.cts +0 -10
- package/ver/ver.d.ts +0 -10
- package/ver/ver.js +0 -31
- package/ver/ver.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@niche-works/dev",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"utils"
|
|
6
6
|
],
|
|
@@ -12,29 +12,36 @@
|
|
|
12
12
|
"author": "Mitsuhiro Seino",
|
|
13
13
|
"type": "module",
|
|
14
14
|
"exports": {
|
|
15
|
-
".":
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"type": "./index.d.ts"
|
|
19
|
-
},
|
|
20
|
-
"./*": {
|
|
21
|
-
"import": "./*/index.js",
|
|
22
|
-
"reqire": "./*/index.cjs",
|
|
23
|
-
"type": "./*/index.d.ts"
|
|
24
|
-
},
|
|
25
|
-
"./constants": {
|
|
26
|
-
"import": "./constants.js",
|
|
27
|
-
"reqire": "./constants.cjs",
|
|
28
|
-
"type": "./constants.d.ts"
|
|
29
|
-
},
|
|
30
|
-
"./*/constants": {
|
|
31
|
-
"import": "./*/constants.js",
|
|
32
|
-
"reqire": "./*/constants.cjs",
|
|
33
|
-
"type": "./*/constants.d.ts"
|
|
34
|
-
}
|
|
15
|
+
".": "./src/index.ts",
|
|
16
|
+
"./*": "./src/*/index.ts",
|
|
17
|
+
"./*/constants": "./src/*/constants.ts"
|
|
35
18
|
},
|
|
19
|
+
"main": "./src/index.ts",
|
|
20
|
+
"module": "./src/index.ts",
|
|
36
21
|
"dependencies": {
|
|
37
22
|
"fs-extra": "^11.3.4",
|
|
38
|
-
"remeda": "^2.33.6"
|
|
23
|
+
"remeda": "^2.33.6",
|
|
24
|
+
"tslib": "^2.8.1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/fs-extra": "^11.0.4",
|
|
28
|
+
"@types/node": "^25.5.0",
|
|
29
|
+
"fast-glob": "^3.3.3",
|
|
30
|
+
"prettier": "^3.8.1",
|
|
31
|
+
"prettier-plugin-organize-imports": "^4.3.0",
|
|
32
|
+
"prettier-plugin-packagejson": "^3.0.2",
|
|
33
|
+
"tsdown": "^0.21.9",
|
|
34
|
+
"tsx": "^4.21.0",
|
|
35
|
+
"type-fest": "^5.5.0",
|
|
36
|
+
"typescript": "^6.0.2"
|
|
37
|
+
},
|
|
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"
|
|
39
46
|
}
|
|
40
|
-
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import { posix as path } from 'path';
|
|
3
|
+
import isTargetPath from '../isTargetPath';
|
|
4
|
+
import type { AddExportsOptions } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* index.tsが配置されているモジュールをpackage.jsonのexportsに追加する
|
|
8
|
+
* @param targetPath
|
|
9
|
+
* @param options
|
|
10
|
+
*/
|
|
11
|
+
export default function addExports(
|
|
12
|
+
targetPath: string = 'src',
|
|
13
|
+
options: AddExportsOptions = {},
|
|
14
|
+
) {
|
|
15
|
+
let {
|
|
16
|
+
packageJsonPath = 'package.json',
|
|
17
|
+
include = [],
|
|
18
|
+
dist = 'dist',
|
|
19
|
+
dryRun,
|
|
20
|
+
...rest
|
|
21
|
+
} = options;
|
|
22
|
+
|
|
23
|
+
// 処理対象を設定
|
|
24
|
+
include = [
|
|
25
|
+
// indexファイルを対象
|
|
26
|
+
{
|
|
27
|
+
valueType: 'base',
|
|
28
|
+
entryType: 'file',
|
|
29
|
+
conditions: /^index.(ts|tsx)$/i,
|
|
30
|
+
},
|
|
31
|
+
...include,
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
// exportsをpackage.jsonに追加
|
|
35
|
+
const exportsIndex = _addExports(targetPath, {
|
|
36
|
+
packageJsonPath,
|
|
37
|
+
include,
|
|
38
|
+
dist,
|
|
39
|
+
...rest,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const packageJson = fs.readJsonSync(packageJsonPath, { encoding: 'utf8' });
|
|
43
|
+
packageJson.exports = {};
|
|
44
|
+
exportsIndex.sort((a, b) => (a.key > b.key ? 1 : -1));
|
|
45
|
+
for (const index of exportsIndex) {
|
|
46
|
+
packageJson.exports[index.key ? './' + index.key : '.'] =
|
|
47
|
+
'./' + index.path + '.js';
|
|
48
|
+
}
|
|
49
|
+
if (dryRun) {
|
|
50
|
+
console.log(packageJson);
|
|
51
|
+
} else {
|
|
52
|
+
fs.writeJsonSync(packageJsonPath, packageJson, {
|
|
53
|
+
encoding: 'utf8',
|
|
54
|
+
spaces: 2,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function _addExports(
|
|
60
|
+
targetPath: string,
|
|
61
|
+
options: AddExportsOptions,
|
|
62
|
+
current: string = '',
|
|
63
|
+
) {
|
|
64
|
+
const { include, exclude, dist = 'dist' } = options;
|
|
65
|
+
|
|
66
|
+
const stat = fs.statSync(targetPath);
|
|
67
|
+
if (!stat.isDirectory()) {
|
|
68
|
+
console.error(`"${targetPath}" is not directory.`);
|
|
69
|
+
}
|
|
70
|
+
const items = fs.readdirSync(targetPath);
|
|
71
|
+
items.sort();
|
|
72
|
+
const exportsIndex: { key: string; path: string }[] = [];
|
|
73
|
+
|
|
74
|
+
for (const item of items) {
|
|
75
|
+
const itemPath = path.join(targetPath, item);
|
|
76
|
+
const stat = fs.statSync(itemPath);
|
|
77
|
+
const { name } = path.parse(itemPath);
|
|
78
|
+
|
|
79
|
+
if (stat.isDirectory()) {
|
|
80
|
+
// ディレクトリの場合は先に子要素を処理
|
|
81
|
+
exportsIndex.push(
|
|
82
|
+
..._addExports(itemPath, options, path.join(current, item)),
|
|
83
|
+
);
|
|
84
|
+
} else if (isTargetPath(itemPath, { include, exclude })) {
|
|
85
|
+
// exportの対象の場合
|
|
86
|
+
exportsIndex.push({
|
|
87
|
+
key: current,
|
|
88
|
+
path: path.join(dist, current, name),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return exportsIndex;
|
|
94
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { IsMatchingPathCondition } from '../isMatchingPath';
|
|
2
|
+
|
|
3
|
+
export type AddExportsOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* package.jsonのパス
|
|
6
|
+
*/
|
|
7
|
+
packageJsonPath?: string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* ビルドしたパッケージが置かれたディレクトリの名称
|
|
11
|
+
*/
|
|
12
|
+
dist?: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* indexファイルを作る対象
|
|
16
|
+
* 未指定の場合は下記の条件がデフォルトで適用される
|
|
17
|
+
*
|
|
18
|
+
* - ファイル名が`_`で始まらず拡張子が`.ts`,`.tsx`,`.js`,`.jsx`のもの
|
|
19
|
+
* - ディレクトリ名が`_`で始まらないもの
|
|
20
|
+
*/
|
|
21
|
+
include?: IsMatchingPathCondition[];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* indexファイルを作る対象から除外するもの
|
|
25
|
+
*/
|
|
26
|
+
exclude?: IsMatchingPathCondition[];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* お試し
|
|
30
|
+
* ファイルの操作は行わない
|
|
31
|
+
*/
|
|
32
|
+
dryRun?: boolean;
|
|
33
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fg from 'fast-glob';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
|
|
4
|
+
const PATTERNS = {
|
|
5
|
+
'.js': /(?:from|import)\s*["'](\.\.?\/[^"']+)["']/g,
|
|
6
|
+
'.cjs': /require\(["'](\.\.?\/[^"']+)["']\)/g,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* ローカルファイルのimportやrequireに拡張子を付与する
|
|
11
|
+
* @param dir
|
|
12
|
+
*/
|
|
13
|
+
export default async function addJsExtensions(dir: string) {
|
|
14
|
+
const files = await fg(`${dir}/**/*.{js,cjs}`);
|
|
15
|
+
for (const file of files) {
|
|
16
|
+
const content = await fs.readFile(file, 'utf-8');
|
|
17
|
+
const ext = file.endsWith('.cjs') ? '.cjs' : '.js';
|
|
18
|
+
const fixed = content.replace(PATTERNS[ext], (match, pos1) => {
|
|
19
|
+
if (pos1.endsWith(ext)) {
|
|
20
|
+
// 既に拡張子あり
|
|
21
|
+
return match;
|
|
22
|
+
}
|
|
23
|
+
// 拡張子を付与
|
|
24
|
+
return match.replace(pos1, `${pos1}${ext}`);
|
|
25
|
+
});
|
|
26
|
+
if (content !== fixed) {
|
|
27
|
+
await fs.writeFile(file, fixed);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './addJsExtensions';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ディレクトリ、ファイルの存在チェック
|
|
5
|
+
* @param targetPaths
|
|
6
|
+
* @param options
|
|
7
|
+
*/
|
|
8
|
+
export default function checkExistence(...targetPaths: string[]) {
|
|
9
|
+
return targetPaths.every((targetPath) => {
|
|
10
|
+
if (!fs.existsSync(targetPath)) {
|
|
11
|
+
console.error(`${targetPath}が存在しません。`);
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './checkExistence';
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { isFunction, merge } from 'remeda';
|
|
4
|
+
import checkExistence from '../checkExistence';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* JSONファイルを更新する
|
|
8
|
+
* @param jsonFilePath
|
|
9
|
+
* @param editor
|
|
10
|
+
*/
|
|
11
|
+
function editJsonFile(
|
|
12
|
+
jsonFilePath: string,
|
|
13
|
+
editor: ((obj: any) => object) | object,
|
|
14
|
+
) {
|
|
15
|
+
const resolvedJsonFilePath = path.resolve(jsonFilePath);
|
|
16
|
+
if (checkExistence(resolvedJsonFilePath)) {
|
|
17
|
+
const obj = fs.readJSONSync(resolvedJsonFilePath);
|
|
18
|
+
let editedJson;
|
|
19
|
+
if (isFunction(editor)) {
|
|
20
|
+
// 関数で編集する場合
|
|
21
|
+
editedJson = editor(obj);
|
|
22
|
+
} else {
|
|
23
|
+
// 差分を反映する場合
|
|
24
|
+
editedJson = merge(obj, editor);
|
|
25
|
+
}
|
|
26
|
+
fs.writeJSONSync(resolvedJsonFilePath, editedJson, { spaces: 2 });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default editJsonFile;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './editJsonFile';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import type { GeneratePublishPackageJsonOptions } from './types';
|
|
3
|
+
|
|
4
|
+
async function generatePublishPackageJson(
|
|
5
|
+
options: GeneratePublishPackageJsonOptions = {},
|
|
6
|
+
) {
|
|
7
|
+
const {
|
|
8
|
+
packageJsonPath = './package.json',
|
|
9
|
+
outputPackageJsonPath = './dist/package.json',
|
|
10
|
+
exports = {
|
|
11
|
+
'.': {
|
|
12
|
+
import: './index.js',
|
|
13
|
+
reqire: './index.cjs',
|
|
14
|
+
type: './index.d.ts',
|
|
15
|
+
},
|
|
16
|
+
'./*': {
|
|
17
|
+
import: './*/index.js',
|
|
18
|
+
reqire: './*/index.cjs',
|
|
19
|
+
type: './*/index.d.ts',
|
|
20
|
+
},
|
|
21
|
+
'./constants': {
|
|
22
|
+
import: './constants.js',
|
|
23
|
+
reqire: './constants.cjs',
|
|
24
|
+
type: './constants.d.ts',
|
|
25
|
+
},
|
|
26
|
+
'./*/constants': {
|
|
27
|
+
import: './*/constants.js',
|
|
28
|
+
reqire: './*/constants.cjs',
|
|
29
|
+
type: './*/constants.d.ts',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
transform = (pkgJson) => pkgJson,
|
|
33
|
+
jsonReadOptions,
|
|
34
|
+
jsonWriteOptions,
|
|
35
|
+
} = options;
|
|
36
|
+
const packageJson = await fs.readJSON(packageJsonPath, jsonReadOptions);
|
|
37
|
+
const distPackageJson = {
|
|
38
|
+
name: packageJson.name,
|
|
39
|
+
version: packageJson.version,
|
|
40
|
+
keywords: packageJson.keywords,
|
|
41
|
+
repository: packageJson.repository,
|
|
42
|
+
license: packageJson.license,
|
|
43
|
+
author: packageJson.author,
|
|
44
|
+
type: packageJson.type,
|
|
45
|
+
exports,
|
|
46
|
+
dependencies: packageJson.dependencies,
|
|
47
|
+
};
|
|
48
|
+
await fs.writeJSON(
|
|
49
|
+
outputPackageJsonPath,
|
|
50
|
+
transform(distPackageJson),
|
|
51
|
+
jsonWriteOptions,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export default generatePublishPackageJson;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { JsonReadOptions, JsonWriteOptions } from 'fs-extra';
|
|
2
|
+
import type { PackageJson } from 'type-fest';
|
|
3
|
+
|
|
4
|
+
export type GeneratePublishPackageJsonOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* package.jsonのパス
|
|
7
|
+
* @default './package.json'
|
|
8
|
+
*/
|
|
9
|
+
packageJsonPath?: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 出力先package.jsonファイルのパス
|
|
13
|
+
* @default './dist/package.json'
|
|
14
|
+
*/
|
|
15
|
+
outputPackageJsonPath?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* exportsに設定する値
|
|
19
|
+
* @default
|
|
20
|
+
* {
|
|
21
|
+
* '.': {
|
|
22
|
+
* import: './index.js',
|
|
23
|
+
* reqire: './index.cjs',
|
|
24
|
+
* type: './index.d.ts',
|
|
25
|
+
* },
|
|
26
|
+
* './*': {
|
|
27
|
+
* import: './*\/index.js',
|
|
28
|
+
* reqire: './*\/index.cjs',
|
|
29
|
+
* type: './*\/index.d.ts',
|
|
30
|
+
* },
|
|
31
|
+
* './constants': {
|
|
32
|
+
* import: './constants.js',
|
|
33
|
+
* reqire: './constants.cjs',
|
|
34
|
+
* type: './constants.d.ts',
|
|
35
|
+
* },
|
|
36
|
+
* './*\/constants': {
|
|
37
|
+
* import: './*\/constants.js',
|
|
38
|
+
* reqire: './*\/constants.cjs',
|
|
39
|
+
* type: './*\/constants.d.ts',
|
|
40
|
+
* },
|
|
41
|
+
* }
|
|
42
|
+
*/
|
|
43
|
+
exports?: PackageJson['exports'];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 任意の編集を行う関数
|
|
47
|
+
* @param packageJson 既定の変換を行った後のpackage.jsonの内容
|
|
48
|
+
* @returns 任意の編集を行った後のpackage.jsonの内容
|
|
49
|
+
*/
|
|
50
|
+
transform?(packageJson: PackageJson): PackageJson;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* package.json読み込み時のオプション
|
|
54
|
+
*/
|
|
55
|
+
jsonReadOptions?: JsonReadOptions;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* package.json書き込み時のオプション
|
|
59
|
+
*/
|
|
60
|
+
jsonWriteOptions?: JsonWriteOptions;
|
|
61
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { default as addExports } from './addExports';
|
|
2
|
+
export { default as addJsExtensions } from './addJsExtensions';
|
|
3
|
+
export { default as checkExistence } from './checkExistence';
|
|
4
|
+
export { default as editJsonFile } from './editJsonFile';
|
|
5
|
+
export { default as generatePublishPackageJson } from './generatePublishPackageJson';
|
|
6
|
+
export { default as indexes } from './indexes';
|
|
7
|
+
export { default as installFromLocal } from './installFromLocal';
|
|
8
|
+
export { default as isMatchingPath } from './isMatchingPath';
|
|
9
|
+
export { default as moveFilesToSubdir } from './moveFilesToSubdir';
|
|
10
|
+
export { default as removeFiles } from './removeFiles';
|
|
11
|
+
export { default as ver } from './ver';
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import type { IsMatchingPathCondition } from '../isMatchingPath';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* types.ts
|
|
6
|
+
*/
|
|
7
|
+
export const TYPES: IsMatchingPathCondition = {
|
|
8
|
+
valueType: 'base',
|
|
9
|
+
entryType: 'file',
|
|
10
|
+
conditions: /^types.ts$/i,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* constants.ts
|
|
15
|
+
*/
|
|
16
|
+
export const CONSTANTS: IsMatchingPathCondition = {
|
|
17
|
+
valueType: 'base',
|
|
18
|
+
entryType: 'file',
|
|
19
|
+
conditions: /^constants.(ts|tsx)$/i,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* TypeScriptとJavaScript
|
|
24
|
+
*/
|
|
25
|
+
export const TS_JS: IsMatchingPathCondition = {
|
|
26
|
+
valueType: 'base',
|
|
27
|
+
entryType: 'file',
|
|
28
|
+
conditions: /.+\.(ts|tsx|js|jsx)$/i,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 配下にindexファイルのあるディレクトリ
|
|
33
|
+
*/
|
|
34
|
+
export const HAS_INDEX_DIR: IsMatchingPathCondition = {
|
|
35
|
+
entryType: 'dir',
|
|
36
|
+
conditions: (values, { indexRegex }) => {
|
|
37
|
+
const items = fs.readdirSync(values.path);
|
|
38
|
+
for (const item of items) {
|
|
39
|
+
if (indexRegex.test(item)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* テストディレクトリ配下
|
|
49
|
+
*/
|
|
50
|
+
export const TEST_DIR: IsMatchingPathCondition = {
|
|
51
|
+
valueType: 'path',
|
|
52
|
+
conditions: /.+\/__test__\/.+/i,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* ファイル名またはディレクトリ名が`_`で始まる
|
|
57
|
+
*/
|
|
58
|
+
export const TEST_FILE: IsMatchingPathCondition = {
|
|
59
|
+
valueType: 'base',
|
|
60
|
+
conditions: /.*\.test\.(ts|tsx|js|jsx)$/i,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* ファイル名またはディレクトリ名が`_`で始まる
|
|
65
|
+
*/
|
|
66
|
+
export const PRIVATE: IsMatchingPathCondition = {
|
|
67
|
+
valueType: 'base',
|
|
68
|
+
conditions: /^_/,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 拡張子を除いたファイル名が親ディレクトリ名と同じ
|
|
73
|
+
*/
|
|
74
|
+
export const MAIN_FILE: IsMatchingPathCondition = {
|
|
75
|
+
entryType: 'file',
|
|
76
|
+
conditions: (values) => {
|
|
77
|
+
return values.name === values.dirbase;
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* ツール類の入ったディレクトリ
|
|
83
|
+
*/
|
|
84
|
+
export const TOOLS_DIR: IsMatchingPathCondition = {
|
|
85
|
+
entryType: 'dir',
|
|
86
|
+
conditions: (values, { children }) => {
|
|
87
|
+
const { base } = values;
|
|
88
|
+
return (
|
|
89
|
+
// 先頭が小文字
|
|
90
|
+
base[0] === base[0].toLowerCase() &&
|
|
91
|
+
// 子要素にディレクトリと同じ名称のファイルが無い
|
|
92
|
+
children?.every((child: string) => child.split('.')[0] !== base)
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* includeのデフォルト値
|
|
99
|
+
*/
|
|
100
|
+
export const DEFAULT_INCLUDE = [
|
|
101
|
+
// TypeScript,JavaScriptのファイルを対象
|
|
102
|
+
TS_JS,
|
|
103
|
+
// 配下にindex.tsを持つディレクトリを対象
|
|
104
|
+
HAS_INDEX_DIR,
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* excludeのデフォルト値
|
|
109
|
+
*/
|
|
110
|
+
export const DEFAULT_EXCLUDE = [
|
|
111
|
+
// __test__フォルダ配下の全てを除外
|
|
112
|
+
TEST_DIR,
|
|
113
|
+
// ディレクトリ名、ファイル名が_で始まるものを除外
|
|
114
|
+
PRIVATE,
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* exportTypeAllのデフォルト値
|
|
119
|
+
*/
|
|
120
|
+
export const DEFAULT_EXPORT_TYPE_ALL = [TYPES];
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* exportAllのデフォルト値
|
|
124
|
+
*/
|
|
125
|
+
export const DEFAULT_EXPORT_ALL = [CONSTANTS];
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* exportDefaultのデフォルト値
|
|
129
|
+
*/
|
|
130
|
+
export const DEFAULT_EXPORT_DEFAULT = [MAIN_FILE];
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* exportAllAsのデフォルト値
|
|
134
|
+
*/
|
|
135
|
+
export const DEFAULT_EXPORT_ALL_AS = [TOOLS_DIR];
|