-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-format.ts
More file actions
114 lines (102 loc) · 3.27 KB
/
Copy pathdetect-format.ts
File metadata and controls
114 lines (102 loc) · 3.27 KB
1
2
3
4
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @file `detectFormat(filename)` — maps a filename (or path) to its
* `FormatDescriptor` (ecosystem + manifest/lockfile + lockfile format), or
* returns `undefined` if nothing matched. On socket-btm's smol Node binary
* this routes to `node:smol-manifest`'s native `detectFormat`; on stock Node
* it does a basename switch over the format descriptors exported by each
* PM-specific dir under `src/eco/<eco>/<pm>/`. The two paths return
* byte-equivalent shapes so the swap is invisible. `supportedFiles` is
* exported alongside for callers that want to enumerate the recognized
* filenames (e.g. for file-globbing).
*/
import {
CARGO_LOCK_FILENAME,
CARGO_LOCK_FORMAT,
} from '../cargo/lockfile-format'
import {
CARGO_TOML_FILENAME,
CARGO_TOML_FORMAT,
} from '../cargo/manifest-format'
import {
PACKAGE_JSON_FILENAME,
PACKAGE_JSON_FORMAT,
} from '../npm/manifest-format'
import {
PACKAGE_LOCK_FILENAMES,
PACKAGE_LOCK_FORMAT,
} from '../npm/npm/lockfile-format'
import {
PNPM_LOCK_FILENAME,
PNPM_LOCK_FORMAT,
} from '../npm/pnpm/lockfile-format'
import {
YARN_LOCK_FILENAME,
YARN_LOCK_FORMAT,
} from '../npm/yarnpkg/yarn/lockfile-format'
import { ObjectFreeze } from '../../primordials/object'
import { getSmolManifest } from '../../smol/manifest'
import {
StringPrototypeLastIndexOf,
StringPrototypeSlice,
} from '../../primordials/string'
import type { FormatDescriptor, SupportedFiles } from './types'
// Composer descriptors stay inline until `src/eco/composer/` exists.
const COMPOSER_JSON_FORMAT = ObjectFreeze({
__proto__: null,
ecosystem: 'composer',
type: 'manifest',
}) as unknown as FormatDescriptor
const COMPOSER_LOCK_FORMAT = ObjectFreeze({
__proto__: null,
ecosystem: 'composer',
format: 'composer',
type: 'lockfile',
}) as unknown as FormatDescriptor
const MANIFEST_NAMES = ObjectFreeze([
PACKAGE_JSON_FILENAME,
CARGO_TOML_FILENAME,
'composer.json',
])
const LOCKFILE_NAMES = ObjectFreeze([
...PACKAGE_LOCK_FILENAMES,
YARN_LOCK_FILENAME,
PNPM_LOCK_FILENAME,
CARGO_LOCK_FILENAME,
'composer.lock',
])
const jsSupportedFiles: SupportedFiles = ObjectFreeze({
__proto__: null,
manifests: MANIFEST_NAMES,
lockfiles: LOCKFILE_NAMES,
}) as unknown as SupportedFiles
export function jsDetectFormat(filename: string): FormatDescriptor | undefined {
const lastSlash = StringPrototypeLastIndexOf(filename, '/')
const basename =
lastSlash === -1 ? filename : StringPrototypeSlice(filename, lastSlash + 1)
switch (basename) {
case PACKAGE_JSON_FILENAME:
return PACKAGE_JSON_FORMAT
case CARGO_TOML_FILENAME:
return CARGO_TOML_FORMAT
case 'composer.json':
return COMPOSER_JSON_FORMAT
case 'package-lock.json':
case 'npm-shrinkwrap.json':
return PACKAGE_LOCK_FORMAT
case YARN_LOCK_FILENAME:
return YARN_LOCK_FORMAT
case PNPM_LOCK_FILENAME:
return PNPM_LOCK_FORMAT
case CARGO_LOCK_FILENAME:
return CARGO_LOCK_FORMAT
case 'composer.lock':
return COMPOSER_LOCK_FORMAT
default:
return undefined
}
}
const smol = getSmolManifest()
export const detectFormat: (filename: string) => FormatDescriptor | undefined =
smol?.detectFormat ?? jsDetectFormat
export const supportedFiles: SupportedFiles =
smol?.supportedFiles ?? jsSupportedFiles