-
Notifications
You must be signed in to change notification settings - Fork 66.6k
Expand file tree
/
Copy pathshared.ts
More file actions
204 lines (176 loc) · 6.34 KB
/
shared.ts
File metadata and controls
204 lines (176 loc) · 6.34 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import walk from 'walk-sync'
import path from 'path'
import { TokenizationError, TokenKind } from 'liquidjs'
import type { TagToken } from 'liquidjs'
import { getLiquidTokens } from '@/content-linter/lib/helpers/liquid-utils'
const __dirname = path.dirname(new URL(import.meta.url).pathname)
const repoRoot = path.resolve(__dirname, '../../../../')
const contentDirectory = path.resolve(__dirname, repoRoot, 'content/')
const dataDirectory = path.resolve(__dirname, repoRoot, 'data/')
const reusablesDirectory = path.resolve(dataDirectory, 'reusables/')
export type FilesWithLineNumbers = {
filePath: string
lineNumbers: number[]
reusableFile?: string
}[]
export type FilesWithSimilarity = {
filePath: string
similarityScore: number
reusableFile?: string
}[]
export function filterFiles(files: string[]) {
return files.filter(
(filePath) =>
filePath.endsWith('.md') || (filePath.endsWith('.yml') && !filePath.endsWith('README.md')),
)
}
export function getAllContentFilePaths() {
const allContentFiles = filterFiles(
walk(contentDirectory, {
includeBasePath: true,
directories: false,
}),
)
const allDataFiles = filterFiles(
walk(dataDirectory, {
includeBasePath: true,
directories: false,
}),
)
return [...allContentFiles, ...allDataFiles]
}
// Get the string that represents the reusable in the content files
export function getReusableLiquidString(reusablePath: string): string {
const relativePath = path.relative(reusablesDirectory, reusablePath)
return `reusables.${relativePath.slice(0, -3).split('/').join('.')}`
}
export function getIndicesOfLiquidVariable(liquidVariable: string, fileContents: string): number[] {
const indices: number[] = []
try {
const tokens = getLiquidTokens(fileContents).filter(
(token): token is TagToken => token.kind === TokenKind.Tag,
)
for (const token of tokens) {
if (token.name === 'data' && token.args.trim() === liquidVariable) {
indices.push(token.begin)
}
}
} catch (err) {
if (err instanceof TokenizationError) return []
throw err
}
return indices
}
// Find the path to a reusable file.
export function resolveReusablePath(reusablePath: string): string {
// Try .md if extension is not provided
if (!reusablePath.endsWith('.md') && !reusablePath.endsWith('.yml')) {
reusablePath += '.md'
}
// Allow user to just pass the name of the file. If it's not ambiguous, we'll find it.
const allReusableFiles = getAllReusablesFilePaths()
const foundPaths = []
for (const possiblePath of allReusableFiles) {
if (possiblePath.includes(reusablePath)) {
foundPaths.push(possiblePath)
}
}
if (foundPaths.length === 0) {
console.error(`Reusables file not found: ${reusablePath}`)
process.exit(1)
} else if (foundPaths.length === 1) {
return foundPaths[0]
} else {
console.error(`Multiple reusables found by name: ${reusablePath}`)
for (let i = 0; i < foundPaths.length; i++) {
console.error(` ${i + 1}: ${getRelativeReusablesPath(foundPaths[i])}`)
}
console.error('Please specify which reusable by passing the full path')
process.exit(1)
}
}
let paths: string[]
export function getAllReusablesFilePaths(): string[] {
if (paths) return paths!
paths = filterFiles(
walk(reusablesDirectory, {
includeBasePath: true,
directories: false,
ignore: ['**/README.md', 'enterprise_deprecation/**'],
}),
)
return paths
}
export function findIndicesOfSubstringInString(substr: string, str: string): number[] {
str = str.toLowerCase()
const result: number[] = []
let idx = str.indexOf(substr)
while (idx !== -1) {
result.push(idx)
idx = str.indexOf(substr, idx + 1)
}
return result
}
export function findSimilarSubStringInString(substr: string, str: string) {
// Take every sentence in the substr, lower case it, and compare it to every sentence in the str to get a similarity score
const substrSentences = substr.split('.').map((sentence) => sentence.toLowerCase())
const corpus = str.split('.').map((sentence) => sentence.toLowerCase())
let similarityScore = 0
// Find how similar every two strings are based on the words they share
for (const substrSentence of substrSentences) {
for (const sentence of corpus) {
const substrTokens = substrSentence.split(' ')
const tokens = sentence.split(' ')
const sharedWords = substrTokens.filter((token) => tokens.includes(token))
similarityScore += sharedWords.length / (substrTokens.length + tokens.length)
}
}
// Normalize the similarity score
return Math.round((similarityScore / substrSentences.length) * corpus.length)
}
export function printFindsWithLineNumbers(
absolute: boolean,
reusableFindings: { filePath: string; lineNumbers: number[]; reusableFile?: string }[],
similarityFindings?: { filePath: string; similarityScore: number; reusableFile?: string }[],
) {
for (const { filePath, lineNumbers, reusableFile } of reusableFindings) {
let printReusablePath = reusableFile
let printFilePath = filePath
if (!absolute) {
printReusablePath = getRelativeReusablesPath(printReusablePath as string)
printFilePath = path.relative(repoRoot, printFilePath)
}
if (reusableFile) {
console.log(`\nReusable ${printReusablePath} can be used`)
console.log(`In ${printFilePath} on:`)
} else {
console.log(`\nIn ${printFilePath} on:`)
}
for (const lineNumber of lineNumbers) {
console.log(` Line ${lineNumber}`)
}
}
if (similarityFindings?.length) {
console.log('\nFindings using "similar" algorithm:')
for (const { filePath, similarityScore, reusableFile } of similarityFindings) {
let printReusablePath = reusableFile
let printFilePath = filePath
if (!absolute) {
printReusablePath = getRelativeReusablesPath(printReusablePath as string)
printFilePath = path.relative(repoRoot, printFilePath)
}
if (reusableFile) {
console.log(`\nReusables ${printReusablePath} can be used`)
console.log(`In ${printFilePath} with similarity score: ${similarityScore}`)
} else {
console.log(`\nIn ${printFilePath} with similarity score: ${similarityScore}`)
}
}
}
}
export function getRelativeReusablesPath(reusablePath: string) {
if (!reusablePath) {
return ''
}
return path.relative(repoRoot, reusablePath)
}