From cbe3b27166504c7339d2cf255769b18c5b7e63c1 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Fri, 2 Apr 2021 15:27:19 -0400 Subject: [PATCH] doc,tools: allow stability table to be updated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep markers for the stability table so that it can be updated on subsequent runs of the doc tooling. Only overwrite the files if they have been changed. PR-URL: https://github.com/nodejs/node/pull/38048 Fixes: https://github.com/nodejs/node/issues/37886 Reviewed-By: Antoine du Hamel Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig Reviewed-By: Juan José Arboleda --- doc/api/documentation.md | 3 ++- tools/doc/stability.js | 29 ++++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/doc/api/documentation.md b/doc/api/documentation.md index d0a05262cc1f27..6277e3a869bfaf 100644 --- a/doc/api/documentation.md +++ b/doc/api/documentation.md @@ -50,7 +50,8 @@ modifications occur. To avoid surprises, use of an Experimental feature may need a command-line flag. Experimental features may also emit a [warning][]. ## Stability overview - + + ## JSON output '; +const markBegin = ''; +const markEnd = ''; +const mark = `${markBegin}(.*)${markEnd}`; const output = { json: path.join(source, 'stability.json'), @@ -84,16 +86,12 @@ function processStability() { function updateStabilityMark(file, value, mark) { const fd = fs.openSync(file, 'r+'); - const content = fs.readFileSync(fd); + const content = fs.readFileSync(fd, { encoding: 'utf8' }); - // Find the position of the `mark`. - const index = content.indexOf(mark); - - // Overwrite the mark with `value` parameter. - const offset = fs.writeSync(fd, value, index, 'utf-8'); - - // Re-write the end of the file after `value`. - fs.writeSync(fd, content, index + mark.length, undefined, index + offset); + const replaced = content.replace(mark, value); + if (replaced !== content) { + fs.writeSync(fd, replaced, 0, 'utf8'); + } fs.closeSync(fd); } @@ -101,11 +99,16 @@ const stability = collectStability(data); // add markdown const markdownTable = createMarkdownTable(stability); -updateStabilityMark(output.docMarkdown, markdownTable, mark); +updateStabilityMark(output.docMarkdown, + `${markBegin}\n${markdownTable}\n${markEnd}`, + new RegExp(mark, 's')); // add html table const html = createHTML(markdownTable); -updateStabilityMark(output.docHTML, html, mark); +updateStabilityMark(output.docHTML, `${markBegin}${html}${markEnd}`, + new RegExp(mark, 's')); // add json output -updateStabilityMark(output.docJSON, JSON.stringify(html), JSON.stringify(mark)); +updateStabilityMark(output.docJSON, + JSON.stringify(`${markBegin}${html}${markEnd}`), + new RegExp(JSON.stringify(mark), 's'));