microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/1873-devcontainer

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/linting/Format-MarkdownTables.ps1

131lines · modecode

1#!/usr/bin/env pwsh
2# Copyright (c) Microsoft Corporation.
3# SPDX-License-Identifier: MIT
4#Requires -Version 7.0
5<#
6.SYNOPSIS
7 Formats Markdown tables across the repository using markdown-table-formatter.
8
9.DESCRIPTION
10 Cross-platform wrapper around the markdown-table-formatter Node library.
11 Enumerates tracked Markdown files via 'git ls-files' (deterministic, respects
12 .gitignore, and includes dot-prefixed directories such as .github/) and
13 delegates formatting to the library API.
14
15 The upstream CLI uses 'glob' with the v13 default of dot:false, which
16 silently skips .github/** and other dot-prefixed paths on Windows. This
17 wrapper bypasses that bug by passing an explicit file list to the library.
18
19 File discovery includes tracked Markdown files only.
20
21.PARAMETER Check
22 Check only; exit with non-zero status if any tables would be reformatted.
23
24.EXAMPLE
25 ./scripts/linting/Format-MarkdownTables.ps1
26 Reformat Markdown tables in place across the repository.
27
28.EXAMPLE
29 ./scripts/linting/Format-MarkdownTables.ps1 -Check
30 Verify formatting without modifying files; exits non-zero on drift.
31#>
32
33[CmdletBinding()]
34param(
35 [Parameter(Mandatory = $false)]
36 [switch]$Check
37)
38
39$ErrorActionPreference = 'Stop'
40$script:MarkdownTableFormatterExitCode = 0
41
42#region Functions
43function Invoke-MarkdownTableFormatter {
44 [CmdletBinding()]
45 param(
46 [Parameter(Mandatory = $false)]
47 [switch]$Check
48 )
49
50 $repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..' '..')).Path
51 $emitVerbose = $VerbosePreference -ne 'SilentlyContinue'
52 $script:MarkdownTableFormatterExitCode = 0
53
54 Push-Location $repoRoot
55 try {
56 $gitOutput = & git ls-files -z --cached -- '*.md'
57 if ($LASTEXITCODE -ne 0) {
58 [System.Console]::Error.WriteLine('git ls-files failed; not running inside a git checkout?')
59 $script:MarkdownTableFormatterExitCode = 2
60 return
61 }
62
63 $files = if ($gitOutput) { $gitOutput -split "`0" | Where-Object { $_ } } else { @() }
64 if ($files.Count -eq 0) {
65 Write-Output 'No markdown files found.'
66 $script:MarkdownTableFormatterExitCode = 0
67 return
68 }
69
70 if ($emitVerbose) {
71 [System.Console]::Error.WriteLine("Formatting $($files.Count) markdown file(s).")
72 }
73
74 $tempList = New-TemporaryFile
75 try {
76 Set-Content -Path $tempList.FullName -Value $files -Encoding utf8
77
78 $nodeScript = @'
79import { readFileSync } from 'node:fs';
80import pkg from 'markdown-table-formatter/lib/markdown-table-formatter.js';
81const { MarkdownTableFormatter } = pkg;
82
83const files = readFileSync(process.env.MTF_FILE_LIST, 'utf8')
84 .split(/\r?\n/)
85 .filter(Boolean);
86const check = process.env.MTF_CHECK === '1';
87const verbose = process.env.MTF_VERBOSE === '1';
88
89const formatter = new MarkdownTableFormatter({ check });
90const result = await formatter.run(files, { verbose });
91for (const updated of result.updates) {
92 console.log(`${check ? 'needs-format' : 'formatted'}: ${updated}`);
93}
94process.exit(result.status);
95'@
96
97 $env:MTF_FILE_LIST = $tempList.FullName
98 $env:MTF_CHECK = $(if ($Check) { '1' } else { '0' })
99 $env:MTF_VERBOSE = $(if ($emitVerbose) { '1' } else { '0' })
100
101 & node --input-type=module -e $nodeScript
102 $script:MarkdownTableFormatterExitCode = $LASTEXITCODE
103 return
104 }
105 finally {
106 Remove-Item -Path $tempList.FullName -ErrorAction SilentlyContinue
107 Remove-Item Env:MTF_FILE_LIST, Env:MTF_CHECK, Env:MTF_VERBOSE -ErrorAction SilentlyContinue
108 }
109 }
110 finally {
111 Pop-Location
112 }
113}
114#endregion
115
116#region Main
117if ($MyInvocation.InvocationName -ne '.') {
118 try {
119 Invoke-MarkdownTableFormatter -Check:$Check
120 $exitCode = $script:MarkdownTableFormatterExitCode
121 if ($null -eq $exitCode) {
122 $exitCode = 0
123 }
124 exit $exitCode
125 }
126 catch {
127 Write-Error -ErrorAction Continue "Format-MarkdownTables failed: $($_.Exception.Message)"
128 exit 1
129 }
130}
131#endregion
132