2022-10-15 00:10:47 +07:00
|
|
|
import { statSync, readFileSync } from 'fs'
|
|
|
|
import { resolve } from 'path'
|
2020-04-02 00:27:19 +07:00
|
|
|
|
|
|
|
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)
|
2020-04-02 00:27:19 +07:00
|
|
|
|
2022-10-15 00:10:47 +07:00
|
|
|
if (statSync(root).isDirectory()) {
|
2020-04-02 00:27:19 +07:00
|
|
|
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)) {
|
2020-04-02 00:27:19 +07:00
|
|
|
throw new Error(`"${root}" does not exist.`)
|
|
|
|
}
|
|
|
|
|
2022-10-15 00:10:47 +07:00
|
|
|
this.content = readFileSync(root).toString()
|
2020-04-02 00:27:19 +07:00
|
|
|
|
|
|
|
let content = pattern.exec(this.content)
|
|
|
|
if (!content) {
|
|
|
|
this._version = null
|
|
|
|
// throw new Error(`Could not find pattern matching "${pattern.toString()}" in "${root}".`)
|
2020-09-11 06:27:44 +07:00
|
|
|
} else if (content.groups && content.groups.version) {
|
|
|
|
this._version = content.groups.version
|
2020-04-02 00:27:19 +07:00
|
|
|
} else {
|
|
|
|
this._version = content[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get version () {
|
|
|
|
return this._version
|
|
|
|
}
|
|
|
|
|
|
|
|
get versionFound () {
|
|
|
|
return this._version !== null
|
|
|
|
}
|
|
|
|
}
|