-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from OUCC/ci-fix-update-blogmeta
Update actions/checkout from v3 to v4 and fix update-blogmeta
- Loading branch information
Showing
3 changed files
with
185 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,171 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
const ok = validateExistingFiles() | ||
if (!ok) process.exit(2) | ||
updateBlogMeta() | ||
type FileStatus = | ||
| [status: 'A' | 'C' | 'D' | 'M' | 'T', path: string] | ||
| [status: `R${number}`, fromPath: string, toPath: string] | ||
|
||
type BlogMeta = { | ||
postDate: string | ||
updateDate?: string | ||
} | ||
const gitDiffs = validateArgs(chunkArray(process.argv.slice(2), 2)) | ||
if (gitDiffs === null) process.exit(1) | ||
updateBlogMeta(gitDiffs) | ||
|
||
function validateExistingFiles() { | ||
function validateArgs(gitDiffs: string[][]): FileStatus[] | null { | ||
let ok = true | ||
for (const file of process.argv.slice(2)) { | ||
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) { | ||
console.log(`\u001b[31m[ERROR]\tThe file was not found: ${file}\u001b[m`) | ||
function validateFile(path: string) { | ||
if (!fs.existsSync(path) || !fs.statSync(path).isFile()) { | ||
console.log(`\u001b[31m[ERROR]\tThe file was not found: ${path}\u001b[m`) | ||
ok = false | ||
return false | ||
} | ||
return true | ||
} | ||
return ok | ||
|
||
const result: FileStatus[] = [] | ||
|
||
for (const status of gitDiffs) { | ||
if (status.length != 2 && status.length != 3) { | ||
console.log( | ||
`\u001b[31m[ERROR]\tArgument is invalid format.\n Please set results of \`git diff --name-status\`. \u001b[m`, | ||
) | ||
ok = false | ||
continue | ||
} | ||
|
||
if (status.length === 2) { | ||
if (!['A', 'C', 'D', 'M', 'T'].includes(status[0]!)) { | ||
console.log( | ||
`\u001b[33m[WARNING]\tThe status does not supported: ${status[0]}\t${status[1]}\u001b[m`, | ||
) | ||
continue | ||
} | ||
|
||
if (status[0] === 'D' || validateFile(status[1]!)) { | ||
result.push(status as ['A' | 'C' | 'D' | 'M' | 'T', string]) | ||
} | ||
} else if (status.length === 3) { | ||
if ( | ||
status[0]!.startsWith('R') && | ||
Number.isInteger(Number(status[0]!.slice(1))) | ||
) { | ||
if (validateFile(status[1]!) && validateFile(status[2]!)) { | ||
result.push(status as [`R${number}`, string, string]) | ||
} | ||
} else | ||
console.log( | ||
`\u001b[33m[WARNING]\tThe status does not supported: ${status[0]}\t${status[1]}\u001b[m`, | ||
) | ||
} | ||
} | ||
return ok ? result : null | ||
} | ||
|
||
/** | ||
* blog meta のファイルの日付を更新します | ||
*/ | ||
function updateBlogMeta() { | ||
const targetFiles = process.argv | ||
.slice(2) | ||
.map(path.parse) | ||
.filter( | ||
(parsedPath) => | ||
parsedPath.dir === 'src/content/blogs' && parsedPath.ext === '.md', | ||
function updateBlogMeta(gitDiffs: readonly FileStatus[]) { | ||
const targetFiles = gitDiffs | ||
.map(([status, filePath, toPath]) => | ||
toPath === undefined | ||
? ([status, path.parse(filePath)] as const) | ||
: ([status, path.parse(toPath)] as const), | ||
) | ||
if (targetFiles.length === 0) { | ||
console.log( | ||
'\u001b[31m[ERROR]\tSet more than one target files to args.\u001b[m', | ||
) | ||
process.exit(1) | ||
} | ||
.filter(predicateTargetFiles) | ||
const updatedMetas = gitDiffs | ||
.map(([status, filePath, toPath]) => [status, toPath ?? filePath] as const) | ||
.filter(([status, filePath]) => { | ||
const { dir, ext } = path.parse(filePath) | ||
return ( | ||
status !== 'D' && dir === 'src/content/blog-metas' && ext === '.json' | ||
) | ||
}) | ||
|
||
for (const [_, { dir, name, ext }] of targetFiles) { | ||
const updatedName = | ||
ext === '.md' || ext === '.mdx' | ||
? name | ||
: dir.slice('src/content/blogs/'.length).split('/')[0]! | ||
|
||
for (const { dir, name } of targetFiles) { | ||
const blogMetaPath = path.resolve(dir, `./blog-meta/${name}.json`) | ||
const blogMetaPath = `src/content/blog-metas/${updatedName}.json` | ||
|
||
let meta: BlogMeta | null = null | ||
if (fs.existsSync(blogMetaPath)) { | ||
meta = JSON.parse( | ||
fs.readFileSync(blogMetaPath, { encoding: 'utf-8' }), | ||
) as BlogMeta | ||
|
||
meta.updateDate = new Date().toISOString() | ||
if (updatedMetas.every(([_, filePath]) => filePath !== blogMetaPath)) | ||
meta.updateDate = new Date().toISOString() | ||
else { | ||
// 今回のPRで既に更新済み | ||
|
||
if (meta.updateDate) meta.updateDate = new Date().toISOString() | ||
else meta.postDate = new Date().toISOString() | ||
} | ||
} else { | ||
meta = { | ||
postDate: new Date().toISOString(), | ||
} | ||
} | ||
|
||
fs.writeFileSync(blogMetaPath, JSON.stringify(meta, undefined, 2)) | ||
fs.writeFileSync(blogMetaPath, JSON.stringify(meta, undefined, 2) + '\n') | ||
|
||
console.log(`[INFO]\tUpdate blog meta: ${blogMetaPath}`) | ||
} | ||
} | ||
|
||
function predicateTargetFiles([status, file]: readonly [ | ||
'A' | 'C' | 'D' | 'M' | 'T' | `R${number}`, | ||
path.ParsedPath, | ||
]): boolean { | ||
// markdown | ||
if ( | ||
(['A', 'C', 'M', 'T'].includes(status) || status.startsWith('R')) && | ||
file.dir === 'src/content/blogs' && | ||
(file.ext === '.md' || file.ext === '.mdx') | ||
) | ||
return true | ||
|
||
// 画像 | ||
if ( | ||
(['A', 'C', 'D', 'M', 'T'].includes(status) || status.startsWith('R')) && | ||
file.dir.startsWith('src/content/blogs/') && | ||
[ | ||
'.jpg', | ||
'.jpeg', | ||
'.jfif', | ||
'.pjpeg', | ||
'.pjp', | ||
'.png', | ||
'.svg', | ||
'.webp', | ||
'.gif', | ||
'.avif', | ||
'.apng', | ||
].includes(file.ext) | ||
) | ||
return true | ||
|
||
return false | ||
} | ||
|
||
function chunkArray<T>(array: T[], chunkSize: number) { | ||
const result: T[][] = [] | ||
let tempArray: T[] = [] | ||
for (const val of array) { | ||
if (tempArray.length < chunkSize) { | ||
tempArray.push(val) | ||
} else { | ||
result.push(tempArray) | ||
tempArray = [val] | ||
} | ||
} | ||
|
||
if (tempArray.length !== 0) { | ||
result.push(tempArray) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters