@w5s/configurator-core 1.22.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/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 { __exists } from './__exists.js';
7
- import { __existsSync } from './__existsSync.js';
8
- import { __toMode } from './__toMode.js';
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 __exists(path);
47
+ const isPresent = await fileExists(path);
48
48
  if (state === 'present') {
49
- const newMode = __toMode(mode);
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 = __existsSync(path);
81
+ const isPresent = fileExistsSync(path);
82
82
  if (state === 'present') {
83
- const newMode = __toMode(mode);
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 { __exists } from './__exists.js';
7
- import { __existsSync } from './__existsSync.js';
8
- import { __toMode } from './__toMode.js';
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 __exists(path);
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 = __toMode(mode);
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 = __existsSync(path);
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 = __toMode(mode);
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';
@@ -1,7 +1,7 @@
1
1
  import { constants } from 'node:fs';
2
2
  import { access } from 'node:fs/promises';
3
3
 
4
- export async function __exists(path: string) {
4
+ export async function fileExists(path: string) {
5
5
  try {
6
6
  await access(path, constants.F_OK);
7
7
  return true;
@@ -1,6 +1,6 @@
1
1
  import { accessSync, constants } from 'node:fs';
2
2
 
3
- export function __existsSync(path: string) {
3
+ export function fileExistsSync(path: string) {
4
4
  try {
5
5
  accessSync(path, constants.F_OK);
6
6
  return true;
@@ -47,6 +47,7 @@ export function resolveCommentStyle(path: string): ResolvedCommentStyle {
47
47
  if (LINE_EXTENSIONS.has(extension)) {
48
48
  return '//';
49
49
  }
50
+
50
51
  if (BLOCK_EXTENSIONS.has(extension)) {
51
52
  return '/*';
52
53
  }
@@ -1,6 +1,6 @@
1
- import type { FileMode, FilePermissionSet } from './FileMode.js';
1
+ import type { FileMode, FilePermissionSet } from '../FileMode.js';
2
2
 
3
- export function __toMode(mode: FileMode | undefined): number | undefined {
3
+ export function toMode(mode: FileMode | undefined): number | undefined {
4
4
  return mode == null
5
5
  ? mode
6
6
  : (
@@ -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 { __exists } from './__exists.js';
6
- import { __existsSync } from './__existsSync.js';
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 (__existsSync(candidatePath)) {
55
+ if (fileExistsSync(candidatePath)) {
56
56
  return true;
57
57
  }
58
58
  }
59
59
 
60
- if (__existsSync(packageJsonPath)) {
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) => __exists(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 __exists(packageJsonPath))) {
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
+ }