Skip to content

Commit

Permalink
fix: tolerate + prefix in collocation convention
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Jun 1, 2023
1 parent 94da087 commit 13a3545
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions telefunc/node/server/runTelefunc/assertNamingConvention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,28 @@ function assertCollocation(telefuncFilePath: string, appRootDir: string | null,
return
}

assertPosixPath(telefuncFilePath)
const getBasename = (fileNameOrPath: string): string => {
assertPosixPath(fileNameOrPath)
let basename = path.posix.basename(fileNameOrPath).split('.')[0]!
if (basename.startsWith('+')) basename = basename.slice(1)
return basename
}

const basename = path.posix.basename(telefuncFilePath).split('.')[0]!
assertPosixPath(telefuncFilePath)
const telefuncFileBasename = getBasename(telefuncFilePath)
const telefuncFileDir = path.posix.dirname(telefuncFilePath)
const telefuncFileDirAbsolute = path.posix.join(appRootDir, telefuncFileDir)
const collocatedFiles = fs.readdirSync(telefuncFileDirAbsolute)
const collocatedFilesMatchYes: string[] = []
const collocatedFilesMatchNot: string[] = []
collocatedFiles.forEach((file) => {
assertPosixPath(file) // file is a filename so it shouldn't contain any windows backslash
const sameBasename = file.startsWith(basename)
file = path.posix.join(telefuncFileDir, file)
if (sameBasename) {
collocatedFilesMatchYes.push(file)
collocatedFiles.forEach((fileName) => {
assertPosixPath(fileName) // fileName isn't a path so it shouldn't contain any backslash windows path separator
const fileBasename = getBasename(fileName)
fileName = path.posix.join(telefuncFileDir, fileName)
if (fileBasename === telefuncFileBasename) {
collocatedFilesMatchYes.push(fileName)
} else {
collocatedFilesMatchNot.push(file)
collocatedFilesMatchNot.push(fileName)
}
})
/* There seem to be a race condition: https://github.com/brillout/telefunc/issues/61
Expand All @@ -74,10 +80,10 @@ function assertCollocation(telefuncFilePath: string, appRootDir: string | null,
[
`We recommend to collocate ${telefuncFilePath} with a UI component file, see https://telefunc.com/event-based#naming-convention`,
' Your telefunction:',
` ${telefuncFilePath}`,
` ${telefuncFilePath} (base name: '${telefuncFileBasename}')`,
' Its collocated files:',
...collocatedFilesMatchNot.map((f) => ' ' + f),
` None of its collocated files share its base name '${basename}'.`
...collocatedFilesMatchNot.map((fileName) => ` ${fileName} (base name: '${getBasename(fileName)}'`),
` None of its collocated files share its base name '${telefuncFileBasename}'.`
].join('\n'),
{ onlyOnce: true }
)
Expand Down

0 comments on commit 13a3545

Please sign in to comment.