Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better regex #1496

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- master
tags:
- v*
- ^v*
pull_request:
branches:
- master
Expand All @@ -25,6 +25,15 @@ jobs:
with:
fetch-depth: 0

- name: Check Git tag format
run: |
_tag="${{ github.ref_name }}"

if ! printf "%s\n" "$_tag" | grep -q -P '^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-(alpha|beta|rc)\.(0|[1-9][0-9]*))?$'; then
printf '[ERROR]: Git tag (%s) wrong format\n' "$_tag"
exit 1
fi

- name: Read package.json version
uses: sergeysova/jq-action@v2
id: version_package
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"scripts": {
"prebuild": "npm run clean:build && npm run check:version",
"build": "npx tsc --build ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.typings.json",
"postbuild": "npx shx rm ./build/typings/browser-shim.d.ts && npx shx cp ./src/browser-shim.ts ./build/typings && npx ts-node ./scripts/package.json.ts --on root",
"postbuild": "npx shx rm ./build/typings/browser-shim.d.ts && npx shx cp ./src/browser-shim.ts ./build/typings && npx ts-node ./scripts/package.json.ts",
"prebuild:benchmarks": "npm run clean:build:benchmarks",
"build:benchmarks": "npx tsc --build ./benchmarks/tsconfig.json",
"check": "npx npm-run-all --npm-path npm \"check:*\"",
Expand Down
30 changes: 18 additions & 12 deletions scripts/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

const enum ANALYZE {
const enum Analyze {
DOCS = "docs",
README = "readme",
}
Expand All @@ -30,31 +30,37 @@ const rootPath = path.resolve(`${__dirname}/..`);
const argv = yargs(hideBin(process.argv))
.strict()
.env("TYPE_GRAPHQL")
.usage("Markdown\n\nUsage: $0 [options]")
.usage("Markdown\n\nUsage: $0 --ref <REF> [options]")
.example([
["$0", "Use 'master' as Git reference"],
["$0 --ref v1.2.3", "Use 'v1.2.3' as Git reference"],
["TYPE_GRAPHQL_REF=v1.2.3 $0", "Use 'v1.2.3' as Git reference"],
[`$0 --on ${ANALYZE.README}`, `Analyze '${ANALYZE.README}'`],
[
`$0 --on ${ANALYZE.README} ${ANALYZE.DOCS}`,
`Analyze '${ANALYZE.README}' and '${ANALYZE.DOCS}'`,
`$0 --ref v1.2.3 --on ${Analyze.README}`,
`Use 'v1.2.3' as Git reference and analyze '${Analyze.README}'`,
],
[
`$0 --ref v1.2.3 --on ${Analyze.README} ${Analyze.DOCS}`,
`Use 'v1.2.3' as Git reference and analyze '${Analyze.README}' and '${Analyze.DOCS}'`,
],
])
.option("ref", {
type: "string",
default: "master",
demandOption: true,
description: "Git reference",
})
.option("on", {
type: "array",
default: [] as ANALYZE[],
default: [] as Analyze[],
requiresArg: true,
choices: [ANALYZE.DOCS, ANALYZE.README],
choices: [Analyze.DOCS, Analyze.README],
description: "Analysis to be performed",
})
.check(({ ref, on }) => {
if (!/^v[0-9]+.[0-9]+.[0-9]+(-(alpha|beta|rc)\.[0-9]+)*$/.test(ref)) {
if (
!/^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-(alpha|beta|rc)\.(0|[1-9][0-9]*))?$/.test(
ref,
)
) {
throw new Error(`Invalid Git reference '${ref}'`);
}
if (on.length === 0) {
Expand All @@ -68,7 +74,7 @@ const gitHubUrlRef = `${gitHubUrl}/${argv.ref}`;
const gitHubUrlRawRef = `${gitHubUrlRaw}/${argv.ref}`;

// README.md
if (argv.on.includes(ANALYZE.README)) {
if (argv.on.includes(Analyze.README)) {
const readmeFile = path.resolve(`${rootPath}/README.md`);

const readme = fs
Expand Down Expand Up @@ -98,7 +104,7 @@ if (argv.on.includes(ANALYZE.README)) {
}

// docs/**/*.md
if (argv.on.includes(ANALYZE.DOCS)) {
if (argv.on.includes(Analyze.DOCS)) {
const docFiles = glob.globSync(`${rootPath}/docs/**/*.md`, { absolute: true });
const gitHubUrlRefMasterEscaped = escapeRegExp(`${gitHubUrl}/master`);

Expand Down