autotag/src/lib/regex.js

37 lines
922 B
JavaScript
Raw Normal View History

2022-10-15 00:10:47 +07:00
import { statSync, readFileSync } from 'fs'
import { resolve } from 'path'
export default class Regex {
2020-04-03 11:38:10 +07:00
constructor (root = './', pattern) {
2022-10-15 00:10:47 +07:00
root = resolve(root)
2022-10-15 00:10:47 +07:00
if (statSync(root).isDirectory()) {
throw new Error(`${root} is a directory. The Regex tag identification strategy requires a file.`)
}
2022-10-15 00:10:47 +07:00
if (!existsSync(root)) {
throw new Error(`"${root}" does not exist.`)
}
2022-10-15 00:10:47 +07:00
this.content = readFileSync(root).toString()
let content = pattern.exec(this.content)
if (!content) {
this._version = null
// throw new Error(`Could not find pattern matching "${pattern.toString()}" in "${root}".`)
} else if (content.groups && content.groups.version) {
this._version = content.groups.version
} else {
this._version = content[1]
}
}
get version () {
return this._version
}
get versionFound () {
return this._version !== null
}
}