@w5s/configurator-core 1.21.0 → 1.23.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/dist/index.cjs +146 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -25
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +32 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +146 -59
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/block.ts +62 -12
- package/src/directory.ts +7 -7
- package/src/file.ts +7 -7
- package/src/index.ts +1 -0
- package/src/{__exists.ts → internal/fileExists.ts} +1 -1
- package/src/{__existsSync.ts → internal/fileExistsSync.ts} +1 -1
- package/src/internal/resolveCommentStyle.ts +57 -0
- package/src/{__toMode.ts → internal/toMode.ts} +2 -2
- package/src/isConfigPresent.ts +6 -6
- package/src/toolVersions.ts +54 -0
package/src/block.ts
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import { file, type FileOptions, fileSync } from './file.js';
|
|
2
|
+
import { resolveCommentStyle, type ResolvedCommentStyle } from './internal/resolveCommentStyle.js';
|
|
3
|
+
|
|
4
|
+
export type BlockCommentStyle = '#' | '/*' | '//' | '<!--' | 'auto';
|
|
5
|
+
|
|
6
|
+
export interface BlockMarkerOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Comment opener used for the marker lines: `'auto'` (from file name),
|
|
9
|
+
* `'#'`, `'//'`, slash-star, or `'<!--'`.
|
|
10
|
+
*
|
|
11
|
+
* @default 'auto'
|
|
12
|
+
*/
|
|
13
|
+
commentStyle?: BlockCommentStyle;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Identity after `BEGIN:` / `END:`, so multiple blocks in one file do not collide.
|
|
17
|
+
*
|
|
18
|
+
* @default 'managed-block'
|
|
19
|
+
*/
|
|
20
|
+
topic?: string;
|
|
21
|
+
}
|
|
2
22
|
|
|
3
23
|
export interface BlockOptions {
|
|
4
24
|
/**
|
|
@@ -12,11 +32,11 @@ export interface BlockOptions {
|
|
|
12
32
|
insertPosition?: ['after', 'EndOfFile' | RegExp] | ['before', 'BeginningOfFile' | RegExp];
|
|
13
33
|
|
|
14
34
|
/**
|
|
15
|
-
*
|
|
35
|
+
* Marker configuration, or a function that returns the full marker line.
|
|
16
36
|
*
|
|
17
|
-
* @default '
|
|
37
|
+
* @default `{ commentStyle: 'auto', topic: 'managed-block' }`
|
|
18
38
|
*/
|
|
19
|
-
marker?: (mark: 'Begin' | 'End') => string;
|
|
39
|
+
marker?: ((mark: 'Begin' | 'End') => string) | BlockMarkerOptions;
|
|
20
40
|
|
|
21
41
|
/**
|
|
22
42
|
* File path
|
|
@@ -31,6 +51,8 @@ export interface BlockOptions {
|
|
|
31
51
|
|
|
32
52
|
const EOF = 'EndOfFile';
|
|
33
53
|
const BOF = 'BeginningOfFile';
|
|
54
|
+
const DEFAULT_TOPIC = 'managed-block';
|
|
55
|
+
|
|
34
56
|
const insertAt = (str: string, index: number, toInsert: string) => str.slice(0, index) + toInsert + str.slice(index);
|
|
35
57
|
const matchLast = (string: string, regexp: RegExp) => {
|
|
36
58
|
const matcher = new RegExp(regexp.source, `${regexp.flags}g`);
|
|
@@ -75,18 +97,34 @@ export function blockSync(options: BlockOptions) {
|
|
|
75
97
|
return fileSync(toFileOptions(options));
|
|
76
98
|
}
|
|
77
99
|
|
|
100
|
+
function formatMarker(style: ResolvedCommentStyle, mark: 'Begin' | 'End', topic: string): string {
|
|
101
|
+
const inner = `${mark.toUpperCase()}:${topic}`;
|
|
102
|
+
switch (style) {
|
|
103
|
+
case '#': {
|
|
104
|
+
return `# ${inner}`;
|
|
105
|
+
}
|
|
106
|
+
case '/*': {
|
|
107
|
+
return `/* ${inner} */`;
|
|
108
|
+
}
|
|
109
|
+
case '//': {
|
|
110
|
+
return `// ${inner}`;
|
|
111
|
+
}
|
|
112
|
+
case '<!--': {
|
|
113
|
+
return `<!-- ${inner} -->`;
|
|
114
|
+
}
|
|
115
|
+
default: {
|
|
116
|
+
throw new Error(`Unsupported comment style ${String(style)}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
78
121
|
function toFileOptions(options: BlockOptions): FileOptions {
|
|
79
|
-
const {
|
|
80
|
-
block: blockName,
|
|
81
|
-
insertPosition = ['after', EOF],
|
|
82
|
-
marker = (mark) => `# ${mark.toUpperCase()} MANAGED BLOCK`,
|
|
83
|
-
path,
|
|
84
|
-
state = 'present',
|
|
85
|
-
} = options;
|
|
122
|
+
const { block: blockName, insertPosition = ['after', EOF], marker, path, state = 'present' } = options;
|
|
86
123
|
|
|
87
124
|
const EOL = '\n';
|
|
88
|
-
const
|
|
89
|
-
const
|
|
125
|
+
const markerFn = toMarkerFn(marker, path);
|
|
126
|
+
const beginBlock = markerFn('Begin');
|
|
127
|
+
const endBlock = markerFn('End');
|
|
90
128
|
|
|
91
129
|
/**
|
|
92
130
|
* @param content
|
|
@@ -151,3 +189,15 @@ function toFileOptions(options: BlockOptions): FileOptions {
|
|
|
151
189
|
update: (sourceContent) => apply(sourceContent, blockName),
|
|
152
190
|
};
|
|
153
191
|
}
|
|
192
|
+
|
|
193
|
+
function toMarkerFn(marker: BlockOptions['marker'], path: string): (mark: 'Begin' | 'End') => string {
|
|
194
|
+
if (typeof marker === 'function') {
|
|
195
|
+
return marker;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const commentStyle = marker?.commentStyle ?? 'auto';
|
|
199
|
+
const topic = marker?.topic ?? DEFAULT_TOPIC;
|
|
200
|
+
const resolvedStyle = commentStyle === 'auto' ? resolveCommentStyle(path) : commentStyle;
|
|
201
|
+
|
|
202
|
+
return (mark) => formatMarker(resolvedStyle, mark, topic);
|
|
203
|
+
}
|
package/src/directory.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { chmod, mkdir, rm } from 'node:fs/promises';
|
|
|
3
3
|
|
|
4
4
|
import type { FileMode } from './FileMode.js';
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { fileExists } from './internal/fileExists.js';
|
|
7
|
+
import { fileExistsSync } from './internal/fileExistsSync.js';
|
|
8
|
+
import { toMode } from './internal/toMode.js';
|
|
9
9
|
|
|
10
10
|
export interface DirectoryOptions {
|
|
11
11
|
/**
|
|
@@ -44,9 +44,9 @@ export interface DirectoryOptions {
|
|
|
44
44
|
*/
|
|
45
45
|
export async function directory(options: DirectoryOptions): Promise<void> {
|
|
46
46
|
const { mode, path, state } = options;
|
|
47
|
-
const isPresent = await
|
|
47
|
+
const isPresent = await fileExists(path);
|
|
48
48
|
if (state === 'present') {
|
|
49
|
-
const newMode =
|
|
49
|
+
const newMode = toMode(mode);
|
|
50
50
|
if (!isPresent) {
|
|
51
51
|
await mkdir(path, { mode: newMode, recursive: true });
|
|
52
52
|
}
|
|
@@ -78,9 +78,9 @@ export async function directory(options: DirectoryOptions): Promise<void> {
|
|
|
78
78
|
*/
|
|
79
79
|
export function directorySync(options: DirectoryOptions): void {
|
|
80
80
|
const { mode, path, state } = options;
|
|
81
|
-
const isPresent =
|
|
81
|
+
const isPresent = fileExistsSync(path);
|
|
82
82
|
if (state === 'present') {
|
|
83
|
-
const newMode =
|
|
83
|
+
const newMode = toMode(mode);
|
|
84
84
|
if (!isPresent) {
|
|
85
85
|
mkdirSync(path, { mode: newMode, recursive: true });
|
|
86
86
|
}
|
package/src/file.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { chmod, readFile, rm, writeFile } from 'node:fs/promises';
|
|
|
3
3
|
|
|
4
4
|
import type { FileMode } from './FileMode.js';
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { fileExists } from './internal/fileExists.js';
|
|
7
|
+
import { fileExistsSync } from './internal/fileExistsSync.js';
|
|
8
|
+
import { toMode } from './internal/toMode.js';
|
|
9
9
|
|
|
10
10
|
export interface FileOptions {
|
|
11
11
|
/**
|
|
@@ -57,10 +57,10 @@ export interface FileOptions {
|
|
|
57
57
|
export async function file(options: FileOptions): Promise<void> {
|
|
58
58
|
const { encoding = 'utf8', mode, path, state, update } = options;
|
|
59
59
|
if (state === 'present') {
|
|
60
|
-
const isPresent = await
|
|
60
|
+
const isPresent = await fileExists(path);
|
|
61
61
|
const previousContent = isPresent ? await readFile(path, encoding) : '';
|
|
62
62
|
const newContent = update == null ? (isPresent ? undefined : '') : update(previousContent);
|
|
63
|
-
const newMode =
|
|
63
|
+
const newMode = toMode(mode);
|
|
64
64
|
if (newContent != null) {
|
|
65
65
|
await writeFile(path, newContent, { encoding, mode: newMode });
|
|
66
66
|
}
|
|
@@ -94,10 +94,10 @@ export async function file(options: FileOptions): Promise<void> {
|
|
|
94
94
|
export function fileSync(options: FileOptions): void {
|
|
95
95
|
const { encoding = 'utf8', mode, path, state, update } = options;
|
|
96
96
|
if (state === 'present') {
|
|
97
|
-
const isPresent =
|
|
97
|
+
const isPresent = fileExistsSync(path);
|
|
98
98
|
const previousContent = isPresent ? readFileSync(path, encoding) : '';
|
|
99
99
|
const newContent = update == null ? (isPresent ? undefined : '') : update(previousContent);
|
|
100
|
-
const newMode =
|
|
100
|
+
const newMode = toMode(mode);
|
|
101
101
|
if (newContent != null) {
|
|
102
102
|
writeFileSync(path, newContent, { encoding, mode: newMode });
|
|
103
103
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './ignoreFile.js';
|
|
|
5
5
|
export * from './isConfigPresent.js';
|
|
6
6
|
export * from './json.js';
|
|
7
7
|
export * from './meta.js';
|
|
8
|
+
export * from './toolVersions.js';
|
|
8
9
|
export * from './yaml.js';
|
|
9
10
|
export * from './yarnConfig.js';
|
|
10
11
|
export * from './yarnVersion.js';
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import nodePath from 'node:path';
|
|
2
|
+
|
|
3
|
+
export type ResolvedCommentStyle = '#' | '/*' | '//' | '<!--';
|
|
4
|
+
|
|
5
|
+
/* cspell:ignore containerfile */
|
|
6
|
+
const HASH_BASENAMES = new Set(['containerfile', 'dockerfile', 'gemfile', 'makefile']);
|
|
7
|
+
|
|
8
|
+
const HTML_EXTENSIONS = new Set(['htm', 'html', 'md', 'mdx', 'svg', 'xml']);
|
|
9
|
+
const LINE_EXTENSIONS = new Set([
|
|
10
|
+
'c',
|
|
11
|
+
'cjs',
|
|
12
|
+
'cpp',
|
|
13
|
+
'cs',
|
|
14
|
+
'cts',
|
|
15
|
+
'go',
|
|
16
|
+
'h',
|
|
17
|
+
'java',
|
|
18
|
+
'js',
|
|
19
|
+
'jsx',
|
|
20
|
+
'kt',
|
|
21
|
+
'mjs',
|
|
22
|
+
'mts',
|
|
23
|
+
'rs',
|
|
24
|
+
'swift',
|
|
25
|
+
'ts',
|
|
26
|
+
'tsx',
|
|
27
|
+
]);
|
|
28
|
+
const BLOCK_EXTENSIONS = new Set(['css', 'less', 'sass', 'scss']);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Resolve comment style from the file path when `commentStyle` is `'auto'`.
|
|
32
|
+
*
|
|
33
|
+
* @param path File path used to infer the comment style.
|
|
34
|
+
*/
|
|
35
|
+
export function resolveCommentStyle(path: string): ResolvedCommentStyle {
|
|
36
|
+
const basename = nodePath.basename(path);
|
|
37
|
+
const basenameLower = basename.toLowerCase();
|
|
38
|
+
|
|
39
|
+
if (basename.startsWith('.') || HASH_BASENAMES.has(basenameLower)) {
|
|
40
|
+
return '#';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const extension = nodePath.extname(basename).slice(1).toLowerCase();
|
|
44
|
+
if (HTML_EXTENSIONS.has(extension)) {
|
|
45
|
+
return '<!--';
|
|
46
|
+
}
|
|
47
|
+
if (LINE_EXTENSIONS.has(extension)) {
|
|
48
|
+
return '//';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (BLOCK_EXTENSIONS.has(extension)) {
|
|
52
|
+
return '/*';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// py, rb, sh, toml, yaml, yml, json, no extension, and unknown → #
|
|
56
|
+
return '#';
|
|
57
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { FileMode, FilePermissionSet } from '
|
|
1
|
+
import type { FileMode, FilePermissionSet } from '../FileMode.js';
|
|
2
2
|
|
|
3
|
-
export function
|
|
3
|
+
export function toMode(mode: FileMode | undefined): number | undefined {
|
|
4
4
|
return mode == null
|
|
5
5
|
? mode
|
|
6
6
|
: (
|
package/src/isConfigPresent.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { readFileSync } from 'node:fs';
|
|
|
2
2
|
import { readFile } from 'node:fs/promises';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { fileExists } from './internal/fileExists.js';
|
|
6
|
+
import { fileExistsSync } from './internal/fileExistsSync.js';
|
|
7
7
|
|
|
8
8
|
const defaultExtensions: Array<string> = ['.json', '.yaml', '.yml', '.js', '.cjs', '.mjs', '.ts', '.cts', '.mts'];
|
|
9
9
|
|
|
@@ -52,12 +52,12 @@ export function isConfigPresentSync(options: IsConfigPresentOptions): boolean {
|
|
|
52
52
|
const { candidatePaths, packageConfig, packageJsonPath } = toCandidates(options);
|
|
53
53
|
|
|
54
54
|
for (const candidatePath of candidatePaths) {
|
|
55
|
-
if (
|
|
55
|
+
if (fileExistsSync(candidatePath)) {
|
|
56
56
|
return true;
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
if (
|
|
60
|
+
if (fileExistsSync(packageJsonPath)) {
|
|
61
61
|
try {
|
|
62
62
|
return hasPackageConfigKey(readFileSync(packageJsonPath, 'utf8'), packageConfig);
|
|
63
63
|
} catch {
|
|
@@ -69,13 +69,13 @@ export function isConfigPresentSync(options: IsConfigPresentOptions): boolean {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
async function hasAnyFile(paths: Array<string>): Promise<boolean> {
|
|
72
|
-
const results = await Promise.all(paths.map((candidatePath) =>
|
|
72
|
+
const results = await Promise.all(paths.map((candidatePath) => fileExists(candidatePath)));
|
|
73
73
|
|
|
74
74
|
return results.some(Boolean);
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
async function hasPackageConfigFile(packageJsonPath: string, packageConfig: string): Promise<boolean> {
|
|
78
|
-
if (!(await
|
|
78
|
+
if (!(await fileExists(packageJsonPath))) {
|
|
79
79
|
return false;
|
|
80
80
|
}
|
|
81
81
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { file, type FileOptions, fileSync } from './file.js';
|
|
2
|
+
|
|
3
|
+
const reToolVersion = /\s+/;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Regular expression to split tool and version
|
|
7
|
+
*/
|
|
8
|
+
export type ToolVersions = Record<string, string>;
|
|
9
|
+
|
|
10
|
+
export interface ToolVersionsOption extends Omit<FileOptions, 'path' | 'update'>, Partial<Pick<FileOptions, 'path'>> {
|
|
11
|
+
/**
|
|
12
|
+
* File content mapping function
|
|
13
|
+
*/
|
|
14
|
+
readonly update?: ((content: ToolVersions | undefined) => ToolVersions | undefined) | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function toolVersions(options: ToolVersionsOption) {
|
|
18
|
+
return file(toFileOption(options));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function toolVersionsSync(options: ToolVersionsOption) {
|
|
22
|
+
return fileSync(toFileOption(options));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function toFileOption({ update, ...otherOptions }: ToolVersionsOption): FileOptions {
|
|
26
|
+
return {
|
|
27
|
+
...otherOptions,
|
|
28
|
+
path: otherOptions.path ?? '.tool-versions',
|
|
29
|
+
update:
|
|
30
|
+
update == null
|
|
31
|
+
? update
|
|
32
|
+
: (content) => {
|
|
33
|
+
const eol = '\n';
|
|
34
|
+
const lines = content.split(eol);
|
|
35
|
+
const parsed = Object.fromEntries(
|
|
36
|
+
(function* () {
|
|
37
|
+
for (const line of lines) {
|
|
38
|
+
const [tool, value] = line.split(reToolVersion, 2);
|
|
39
|
+
if (tool) {
|
|
40
|
+
yield [tool, value ?? ''];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}()),
|
|
44
|
+
);
|
|
45
|
+
const updated = update ? update(parsed) : parsed;
|
|
46
|
+
if (updated === undefined) return updated;
|
|
47
|
+
|
|
48
|
+
const formatted = Object.entries(updated)
|
|
49
|
+
.map(([tool, value]) => `${tool} ${value}`)
|
|
50
|
+
.join(eol);
|
|
51
|
+
return formatted;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|