@niche-works/dev 0.2.0 → 0.3.0
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 +31 -23
- package/src/addExports/addExports.ts +98 -0
- package/src/addExports/index.ts +2 -0
- package/src/addExports/types.ts +33 -0
- package/src/addJsExtensions/addJsExtensions.ts +31 -0
- package/src/addJsExtensions/index.ts +1 -0
- package/src/checkExistence/checkExistence.ts +16 -0
- package/src/checkExistence/index.ts +1 -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 +10 -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/_types.ts +65 -0
- package/src/isMatchingPath/index.ts +2 -0
- package/src/isMatchingPath/isMatchingPath.ts +88 -0
- package/src/isMatchingPath/types.ts +21 -0
- package/src/moveFilesToSubdir/index.ts +1 -0
- package/src/moveFilesToSubdir/moveFilesToSubdir.ts +23 -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/tsup.config.ts +36 -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
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import { posix as path } from 'path';
|
|
3
|
+
import isMatchingPath from '../isMatchingPath';
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_EXCLUDE,
|
|
6
|
+
DEFAULT_EXPORT_ALL,
|
|
7
|
+
DEFAULT_EXPORT_ALL_AS,
|
|
8
|
+
DEFAULT_EXPORT_DEFAULT,
|
|
9
|
+
DEFAULT_EXPORT_TYPE_ALL,
|
|
10
|
+
DEFAULT_INCLUDE,
|
|
11
|
+
} from './constants';
|
|
12
|
+
import type { IndexesOptions } from './types';
|
|
13
|
+
|
|
14
|
+
const EXPORTS = {
|
|
15
|
+
exportAll: {
|
|
16
|
+
type: 'named',
|
|
17
|
+
generate: (name: string) => `export * from './${name}';`,
|
|
18
|
+
},
|
|
19
|
+
exportAllAs: {
|
|
20
|
+
type: 'named',
|
|
21
|
+
generate: (name: string) => `export * as ${name} from './${name}';`,
|
|
22
|
+
},
|
|
23
|
+
exportDefault: {
|
|
24
|
+
type: 'default',
|
|
25
|
+
generate: (name: string) => `export { default } from './${name}';`,
|
|
26
|
+
},
|
|
27
|
+
exportDefaultAs: {
|
|
28
|
+
type: 'named',
|
|
29
|
+
generate: (name: string) =>
|
|
30
|
+
`export { default as ${name} } from './${name}';`,
|
|
31
|
+
},
|
|
32
|
+
exportTypeAll: {
|
|
33
|
+
type: 'type',
|
|
34
|
+
generate: (name: string) => `export type * from './${name}';`,
|
|
35
|
+
},
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 対象のディレクトリ配下のindexファイルを作成する\
|
|
40
|
+
* 対象ファイルで出力形式が特に指定されていない場合は下記の形式で出力\
|
|
41
|
+
* `export { default as ${name} } from './${name}';`
|
|
42
|
+
*/
|
|
43
|
+
export default function indexes(options: IndexesOptions = {}) {
|
|
44
|
+
const {
|
|
45
|
+
srcPath = 'src',
|
|
46
|
+
indexFileName = 'index.ts',
|
|
47
|
+
include = DEFAULT_INCLUDE,
|
|
48
|
+
exclude = DEFAULT_EXCLUDE,
|
|
49
|
+
exportAll = DEFAULT_EXPORT_ALL,
|
|
50
|
+
exportAllAs = DEFAULT_EXPORT_ALL_AS,
|
|
51
|
+
exportDefault = DEFAULT_EXPORT_DEFAULT,
|
|
52
|
+
exportTypeAll = DEFAULT_EXPORT_TYPE_ALL,
|
|
53
|
+
...rest
|
|
54
|
+
} = options;
|
|
55
|
+
const indexRegex = _createRegex(indexFileName);
|
|
56
|
+
|
|
57
|
+
// indexファイルの作成処理を実行
|
|
58
|
+
_indexes(srcPath, indexRegex, {
|
|
59
|
+
indexFileName,
|
|
60
|
+
include,
|
|
61
|
+
exclude,
|
|
62
|
+
exportAll,
|
|
63
|
+
exportAllAs,
|
|
64
|
+
exportDefault,
|
|
65
|
+
exportTypeAll,
|
|
66
|
+
...rest,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function _indexes(
|
|
71
|
+
targetPath: string,
|
|
72
|
+
indexRegex: RegExp,
|
|
73
|
+
options: IndexesOptions,
|
|
74
|
+
) {
|
|
75
|
+
const {
|
|
76
|
+
indexFileName = 'index.ts',
|
|
77
|
+
include,
|
|
78
|
+
exclude,
|
|
79
|
+
includeNamedWithDefault,
|
|
80
|
+
exportAll,
|
|
81
|
+
exportAllAs,
|
|
82
|
+
exportDefault,
|
|
83
|
+
exportDefaultAs,
|
|
84
|
+
exportTypeAll,
|
|
85
|
+
dryRun,
|
|
86
|
+
} = options;
|
|
87
|
+
const exportTargets = {
|
|
88
|
+
exportAll,
|
|
89
|
+
exportAllAs,
|
|
90
|
+
exportDefault,
|
|
91
|
+
exportDefaultAs,
|
|
92
|
+
exportTypeAll,
|
|
93
|
+
};
|
|
94
|
+
const stat = fs.statSync(targetPath);
|
|
95
|
+
if (!stat.isDirectory()) {
|
|
96
|
+
console.error(`"${targetPath}" is not directory.`);
|
|
97
|
+
}
|
|
98
|
+
const items = fs.readdirSync(targetPath);
|
|
99
|
+
items.sort((a, b) => (a.toLowerCase() > b.toLowerCase() ? 1 : -1));
|
|
100
|
+
const exportCodes = [];
|
|
101
|
+
let hasDefaultExport = false;
|
|
102
|
+
let hasNamedExport = false;
|
|
103
|
+
|
|
104
|
+
for (const item of items) {
|
|
105
|
+
const itemPath = path.join(targetPath, item);
|
|
106
|
+
const { name } = path.parse(itemPath);
|
|
107
|
+
const stat = fs.statSync(itemPath);
|
|
108
|
+
|
|
109
|
+
let children;
|
|
110
|
+
if (stat.isDirectory()) {
|
|
111
|
+
// ディレクトリの場合は先に子要素を処理
|
|
112
|
+
children = _indexes(itemPath, indexRegex, options);
|
|
113
|
+
}
|
|
114
|
+
const isMatchingPathOptions = {
|
|
115
|
+
conditionOptions: { indexRegex, children },
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
if (
|
|
119
|
+
!indexRegex.test(item) &&
|
|
120
|
+
isMatchingPath(itemPath, include, isMatchingPathOptions) &&
|
|
121
|
+
!isMatchingPath(itemPath, exclude, isMatchingPathOptions)
|
|
122
|
+
) {
|
|
123
|
+
// indexではない場合
|
|
124
|
+
let isExported = false;
|
|
125
|
+
for (const type in EXPORTS) {
|
|
126
|
+
const exportType = type as keyof typeof EXPORTS;
|
|
127
|
+
const exportTarget = exportTargets[exportType];
|
|
128
|
+
if (
|
|
129
|
+
exportTarget &&
|
|
130
|
+
isMatchingPath(itemPath, exportTarget, isMatchingPathOptions)
|
|
131
|
+
) {
|
|
132
|
+
// 条件に一致した場合はexportのコードを生成
|
|
133
|
+
const EXPORT = EXPORTS[exportType];
|
|
134
|
+
exportCodes.push({
|
|
135
|
+
type: EXPORT.type,
|
|
136
|
+
code: EXPORT.generate(name),
|
|
137
|
+
});
|
|
138
|
+
// exportの種類に応じたフラグを立てる
|
|
139
|
+
if (EXPORT.type === 'named') {
|
|
140
|
+
hasNamedExport = true;
|
|
141
|
+
} else if (EXPORT.type === 'default') {
|
|
142
|
+
hasDefaultExport = true;
|
|
143
|
+
}
|
|
144
|
+
isExported = true;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!isExported) {
|
|
149
|
+
// 未指定の場合はexportDefaultAs
|
|
150
|
+
exportCodes.push({
|
|
151
|
+
type: EXPORTS.exportDefaultAs.type,
|
|
152
|
+
code: EXPORTS.exportDefaultAs.generate(name),
|
|
153
|
+
});
|
|
154
|
+
hasNamedExport = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (exportCodes.length) {
|
|
160
|
+
let index;
|
|
161
|
+
if (hasDefaultExport && hasNamedExport && !includeNamedWithDefault) {
|
|
162
|
+
// デフォルトエクスポートと名前付きエクスポートの混在を許さない場合は名前付きエクスポートを除外
|
|
163
|
+
index = exportCodes.reduce<string[]>((result, exportCode) => {
|
|
164
|
+
if (exportCode.type !== 'named') {
|
|
165
|
+
result.push(exportCode.code);
|
|
166
|
+
}
|
|
167
|
+
return result;
|
|
168
|
+
}, []);
|
|
169
|
+
} else {
|
|
170
|
+
index = exportCodes.map((exportCode) => exportCode.code);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const indexPath = path.join(targetPath, indexFileName);
|
|
174
|
+
if (!dryRun) {
|
|
175
|
+
// indexファイルの出力
|
|
176
|
+
fs.writeFileSync(indexPath, index.join('\r\n') + '\r\n', {
|
|
177
|
+
encoding: 'utf8',
|
|
178
|
+
});
|
|
179
|
+
console.info(indexPath);
|
|
180
|
+
} else {
|
|
181
|
+
console.info(indexPath + '-------------------------------------');
|
|
182
|
+
console.info(index.join('\r\n') + '\r\n');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return items;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function _createRegex(str: string) {
|
|
190
|
+
return new RegExp(
|
|
191
|
+
`^${str.replace(/[.*+?^=!:${}()|\[\]\/\\]/g, '\\$&')}$`,
|
|
192
|
+
'i',
|
|
193
|
+
);
|
|
194
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { IsMatchingPathCondition } from '../isMatchingPath';
|
|
2
|
+
|
|
3
|
+
export type IndexesOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* 処理対象のパス\
|
|
6
|
+
* @default `src`
|
|
7
|
+
*/
|
|
8
|
+
srcPath?: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* indexのファイル名\
|
|
12
|
+
* @default `index.ts`
|
|
13
|
+
*/
|
|
14
|
+
indexFileName?: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* indexファイルを作る対象\
|
|
18
|
+
* 未指定の場合は下記の条件がデフォルトで適用される
|
|
19
|
+
*
|
|
20
|
+
* - ファイル名が`_`で始まらず拡張子が`.ts`,`.tsx`,`.js`,`.jsx`のもの
|
|
21
|
+
* - ディレクトリ名が`_`で始まらないもの
|
|
22
|
+
*/
|
|
23
|
+
include?: IsMatchingPathCondition[];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* indexファイルを作る対象から除外するもの
|
|
27
|
+
*/
|
|
28
|
+
exclude?: IsMatchingPathCondition[];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 同じindexファイル内にデフォルトエクスポートがある場合でも名前付きエクスポートをする\
|
|
32
|
+
* デフォルトエクスポートしたモジュールをcjsへ変換したものへアクセスする際に、\
|
|
33
|
+
* defaultという名前付きでアクセスしても問題ない場合はtrue
|
|
34
|
+
*/
|
|
35
|
+
includeNamedWithDefault?: boolean;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* exportの形式を下記にしたいもの
|
|
39
|
+
*
|
|
40
|
+
* ```
|
|
41
|
+
* export * from './abc';
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
exportAll?: IsMatchingPathCondition[];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* exportの形式を下記にしたいもの
|
|
48
|
+
*
|
|
49
|
+
* ```
|
|
50
|
+
* export * as abc from './abc';
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
exportAllAs?: IsMatchingPathCondition[];
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* exportの形式を下記にしたいもの
|
|
57
|
+
*
|
|
58
|
+
* ```
|
|
59
|
+
* export { default } from './abc';
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
exportDefault?: IsMatchingPathCondition[];
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* exportの形式を下記にしたいもの
|
|
66
|
+
*
|
|
67
|
+
* ```
|
|
68
|
+
* export { default as abc } from './abc';
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
exportDefaultAs?: IsMatchingPathCondition[];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* exportの形式を下記にしたいもの
|
|
75
|
+
*
|
|
76
|
+
* ```
|
|
77
|
+
* export type * from './abc';
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
exportTypeAll?: IsMatchingPathCondition[];
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* お試し\
|
|
84
|
+
* ファイルの操作は行わない
|
|
85
|
+
*/
|
|
86
|
+
dryRun?: boolean;
|
|
87
|
+
};
|
|
@@ -1,48 +1,63 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import path from
|
|
3
|
-
import { reduce } from
|
|
4
|
-
import checkExistence from
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { reduce } from 'remeda';
|
|
4
|
+
import checkExistence from '../checkExistence';
|
|
5
|
+
import type { InstallFromLocalOptions } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ローカルに配置されたパッケージを差し替える
|
|
9
|
+
* @param localNodeModulesPath ローカルのnode_modulesディレクトリのパス
|
|
10
|
+
* @param options オプション
|
|
11
|
+
*/
|
|
12
|
+
export default function installFromLocal(
|
|
13
|
+
localNodeModulesPath: string,
|
|
14
|
+
options: InstallFromLocalOptions = {},
|
|
15
|
+
) {
|
|
16
|
+
const {
|
|
17
|
+
packageJsonPath = './package.json',
|
|
18
|
+
nodeModulesPath = './node_modules',
|
|
19
|
+
dryRun,
|
|
20
|
+
} = options;
|
|
21
|
+
const resolvedLocalNodeModulesPath = path.resolve(localNodeModulesPath);
|
|
22
|
+
const resolvedPackageJsonPath = path.resolve(packageJsonPath);
|
|
23
|
+
|
|
24
|
+
if (!checkExistence(resolvedLocalNodeModulesPath, resolvedPackageJsonPath)) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
console.info(`${localNodeModulesPath} -> ${nodeModulesPath}`);
|
|
29
|
+
|
|
30
|
+
// package.jsonからローカルのnode_modulesにあるパッケージを取得
|
|
31
|
+
const packageJson = fs.readJSONSync(resolvedPackageJsonPath);
|
|
32
|
+
const localPackages = reduce(
|
|
33
|
+
{ ...packageJson.dependencies, ...packageJson.devDependencies },
|
|
34
|
+
(result: Record<string, string>, value: string, key) => {
|
|
35
|
+
if (value.startsWith(localNodeModulesPath)) {
|
|
36
|
+
result[key] = value;
|
|
37
|
+
} else if (value.startsWith(`file:${localNodeModulesPath}`)) {
|
|
38
|
+
result[key] = value.substring(5);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
},
|
|
42
|
+
{},
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
for (const packageName in localPackages) {
|
|
46
|
+
// ローカルパッケージの確認
|
|
47
|
+
const localPackagePath = path.resolve(localPackages[packageName]);
|
|
48
|
+
if (fs.existsSync(localPackagePath)) {
|
|
49
|
+
// ローカルパッケージがある場合のみ処理
|
|
50
|
+
const installedPackagePath = path.resolve(nodeModulesPath, packageName);
|
|
51
|
+
if (!dryRun) {
|
|
52
|
+
// コピーを行う
|
|
53
|
+
if (fs.existsSync(installedPackagePath)) {
|
|
54
|
+
// 古いパッケージは削除
|
|
55
|
+
fs.removeSync(installedPackagePath);
|
|
56
|
+
}
|
|
57
|
+
// パッケージをコピー
|
|
58
|
+
fs.copySync(localPackagePath, installedPackagePath);
|
|
59
|
+
}
|
|
60
|
+
console.info(`${packageName} is being copied...`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type InstallFromLocalOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* package.jsonのパス
|
|
4
|
+
* @default './package.json'
|
|
5
|
+
*/
|
|
6
|
+
packageJsonPath?: string;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* node_modulesのパス
|
|
10
|
+
* @default './node_modules'
|
|
11
|
+
*/
|
|
12
|
+
nodeModulesPath?: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* お試し
|
|
16
|
+
* ファイルの操作は行わない
|
|
17
|
+
*/
|
|
18
|
+
dryRun?: boolean;
|
|
19
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
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;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import { posix as path } from 'path';
|
|
3
|
+
import { isFunction, isString } from 'remeda';
|
|
4
|
+
import type { ConditionValues } from './_types';
|
|
5
|
+
import type { IsMatchingPathCondition, IsMatchingPathOptions } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 処理対象のパスか判定する
|
|
9
|
+
* @param targetPath 検査対象のパス
|
|
10
|
+
* @param conditions 一致条件
|
|
11
|
+
* @param options オプション
|
|
12
|
+
* @returns 一致するか
|
|
13
|
+
*/
|
|
14
|
+
export default function isMatchingPath(
|
|
15
|
+
targetPath: string,
|
|
16
|
+
conditions:
|
|
17
|
+
| IsMatchingPathCondition
|
|
18
|
+
| IsMatchingPathCondition[]
|
|
19
|
+
| null
|
|
20
|
+
| undefined,
|
|
21
|
+
options: IsMatchingPathOptions = {},
|
|
22
|
+
) {
|
|
23
|
+
if (!conditions) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const conditionList = Array.isArray(conditions) ? conditions : [conditions];
|
|
27
|
+
// パスは'/'区切り
|
|
28
|
+
const posixPath = path.normalize(targetPath);
|
|
29
|
+
const parsedPath = path.parse(posixPath);
|
|
30
|
+
const parsedDir = path.parse(parsedPath.dir);
|
|
31
|
+
const values = {
|
|
32
|
+
path: posixPath,
|
|
33
|
+
...parsedPath,
|
|
34
|
+
dirpath: parsedPath.dir,
|
|
35
|
+
dirbase: parsedDir.base,
|
|
36
|
+
dirname: parsedDir.name,
|
|
37
|
+
dirext: parsedDir.ext,
|
|
38
|
+
};
|
|
39
|
+
values.path = posixPath;
|
|
40
|
+
|
|
41
|
+
for (const condition of conditionList) {
|
|
42
|
+
if (_isMatching(values.path, values, condition, options)) {
|
|
43
|
+
// conditionの内の何れかがtrueならば一致
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function _isMatching(
|
|
51
|
+
value: string,
|
|
52
|
+
values: ConditionValues,
|
|
53
|
+
condition: IsMatchingPathCondition,
|
|
54
|
+
options: IsMatchingPathOptions,
|
|
55
|
+
): boolean {
|
|
56
|
+
if (isString(condition)) {
|
|
57
|
+
// 条件が文字列
|
|
58
|
+
if (value.includes(condition)) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
} else if (condition instanceof RegExp) {
|
|
62
|
+
// 条件が正規表現
|
|
63
|
+
if (condition.test(value)) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
} else if (isFunction(condition)) {
|
|
67
|
+
// 条件が関数
|
|
68
|
+
return condition(values, options.conditionOptions ?? {});
|
|
69
|
+
} else {
|
|
70
|
+
// 条件が設定
|
|
71
|
+
const { valueType = 'path', entryType = 'both', conditions } = condition;
|
|
72
|
+
if (entryType !== 'both') {
|
|
73
|
+
const stat = fs.statSync(values.path);
|
|
74
|
+
if (
|
|
75
|
+
(stat.isDirectory() && entryType !== 'dir') ||
|
|
76
|
+
(stat.isFile() && entryType !== 'file')
|
|
77
|
+
) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const conditionList = Array.isArray(conditions) ? conditions : [conditions];
|
|
82
|
+
// 設定配下のconditionsはand条件
|
|
83
|
+
return conditionList.every((cond) =>
|
|
84
|
+
_isMatching(values[valueType], values, cond, options),
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ConditionConfig, ConditionFn } from './_types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 一致条件
|
|
5
|
+
*
|
|
6
|
+
* - string: 指定された文字列がパスに含まれているものに一致する際にtrue
|
|
7
|
+
* - RegExp: 指定された正規表現にパスが一致する際にtrue
|
|
8
|
+
* - ConditionConfig: 条件設定に従って検査し一致したときにtrue
|
|
9
|
+
*/
|
|
10
|
+
export type IsMatchingPathCondition =
|
|
11
|
+
| string
|
|
12
|
+
| RegExp
|
|
13
|
+
| ConditionConfig
|
|
14
|
+
| ConditionFn;
|
|
15
|
+
|
|
16
|
+
export type IsMatchingPathOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* ConditionFnに渡すオプション
|
|
19
|
+
*/
|
|
20
|
+
conditionOptions?: Record<string, any>;
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './moveFilesToSubdir';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 対象のディレクトリ直下のファイルを、拡張子を除いた同名のサブディレクトリ直下に移動する
|
|
6
|
+
* @param dirPath 対象のディレクトリのパス
|
|
7
|
+
*/
|
|
8
|
+
export default async function moveFilesToSubdir(dirPath: string) {
|
|
9
|
+
const items = await fs.readdir(dirPath);
|
|
10
|
+
for (const item of items) {
|
|
11
|
+
const itemPath = path.join(dirPath, item);
|
|
12
|
+
const stat = fs.statSync(itemPath);
|
|
13
|
+
if (stat.isFile()) {
|
|
14
|
+
const fileName = path.parse(item).name;
|
|
15
|
+
// 移動先のディレクトリパスを生成
|
|
16
|
+
const subdirPath = path.join(dirPath, fileName);
|
|
17
|
+
// 移動先のディレクトリが存在しなければ作成
|
|
18
|
+
fs.ensureDirSync(subdirPath);
|
|
19
|
+
// 移動先のファイルパスを生成
|
|
20
|
+
fs.moveSync(itemPath, path.join(subdirPath, item));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/ver/index.ts
ADDED
package/src/ver/types.ts
ADDED
package/src/ver/ver.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import type { VerOptions } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ワークスペースのパッケージにバージョンを反映する
|
|
7
|
+
* @param version バージョン
|
|
8
|
+
* @param options オプション
|
|
9
|
+
*/
|
|
10
|
+
export default async function ver(version: string, options: VerOptions = {}) {
|
|
11
|
+
const {
|
|
12
|
+
workspacePackageJsonPath = 'package.json',
|
|
13
|
+
packagesPath = 'packages',
|
|
14
|
+
} = options;
|
|
15
|
+
|
|
16
|
+
if (fs.existsSync(workspacePackageJsonPath)) {
|
|
17
|
+
// ワークスペースのpackage.jsonを更新
|
|
18
|
+
_updatePkgJson(version, workspacePackageJsonPath);
|
|
19
|
+
}
|
|
20
|
+
const pkgs = fs.readdirSync(packagesPath);
|
|
21
|
+
for (const pkg of pkgs) {
|
|
22
|
+
const packagePath = path.join(packagesPath, pkg);
|
|
23
|
+
const packageJsonPath = path.join(packagePath, 'package.json');
|
|
24
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
25
|
+
// 各パッケージのpackage.jsonを更新
|
|
26
|
+
_updatePkgJson(version, packageJsonPath);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function _updatePkgJson(version: string, packageJsonPath: string) {
|
|
32
|
+
const packageJson = fs.readJsonSync(packageJsonPath, { encoding: 'utf8' });
|
|
33
|
+
packageJson.version = version;
|
|
34
|
+
fs.writeJsonSync(packageJsonPath, packageJson, {
|
|
35
|
+
encoding: 'utf8',
|
|
36
|
+
spaces: 2,
|
|
37
|
+
});
|
|
38
|
+
}
|