From ee5dda8343d1c104d079badef9106a0ca459bc42 Mon Sep 17 00:00:00 2001 From: siyuniu-ms Date: Wed, 10 Jan 2024 16:09:43 -0800 Subject: [PATCH 1/5] add script --- package.json | 3 +- .../injectScript.js | 59 +++++++++++++++++++ tools/github-page-script-injection/script.js | 18 ++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tools/github-page-script-injection/injectScript.js create mode 100644 tools/github-page-script-injection/script.js diff --git a/package.json b/package.json index 9b019aa49..3a390b4ea 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "npm-pack": "node common/scripts/install-run-rush.js npm-pack --verbose", "npm-publish": "node ./tools/release-tools/npm_publish.js", "npm-set-latest": "node ./tools/release-tools/npm_set_latest.js", - "gh-status": "node ./tools/status-tools/github-status.js" + "gh-status": "node ./tools/status-tools/github-status.js", + "script-inject": "node tools/github-page-script-injection/injectScript.js" }, "repository": { "type": "git", diff --git a/tools/github-page-script-injection/injectScript.js b/tools/github-page-script-injection/injectScript.js new file mode 100644 index 000000000..6e996f6f6 --- /dev/null +++ b/tools/github-page-script-injection/injectScript.js @@ -0,0 +1,59 @@ +const fs = require('fs'); +const path = require('path'); + +// Recursively process a folder and its subfolders to search for HTML files +const processFolder = (folderPath) => { + const files = fs.readdirSync(folderPath); + + files.forEach((file) => { + const filePath = path.join(folderPath, file); + console.log(` ${filePath}`); + + if (fs.statSync(filePath).isDirectory()) { + // If the current path is a directory, recursively process it + processFolder(filePath); + } else if (path.extname(file) === '.html') { + // If it's an HTML file, inject the script + console.log(`process ${filePath}`); + + injectScript(filePath); + } + }); +}; + + +// Start processing from the 'docs' folder +const docsFolder = path.join(__dirname, '../../docs'); +processFolder(docsFolder); + +function injectScript(filePath) { + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + console.error('Error reading file:', err); + return; + } + + // Read the script content from a file + const scriptFilePath = path.join(__dirname, 'script.js'); + const scriptContent = fs.readFileSync(scriptFilePath, 'utf8'); + + // Check if the script content is already present in the file + const fileContent = fs.readFileSync(filePath, 'utf8'); + + // Check if the script content is already present in the file + if (fileContent.includes(scriptContent)) { + console.log(`Script already present in ${filePath}`); + return; + } + // Create the modified content by inserting the script tag right before the closing head tag + const modifiedContent = data.replace(/(<\/head[^>]*)/i, `\n${scriptContent}\n$1`); + // Save the modified content back to the file + fs.writeFile(filePath, modifiedContent, (err) => { + if (err) { + console.error('Error writing to file:', err); + return; + } + console.log(`Script injected successfully into ${filePath}`); + }); + }); +} diff --git a/tools/github-page-script-injection/script.js b/tools/github-page-script-injection/script.js new file mode 100644 index 000000000..bcc1acf55 --- /dev/null +++ b/tools/github-page-script-injection/script.js @@ -0,0 +1,18 @@ + \ No newline at end of file From 1f0a85dd2f995bade29ef8544e0c9299b6889b98 Mon Sep 17 00:00:00 2001 From: siyuniu-ms Date: Thu, 11 Jan 2024 13:12:39 -0800 Subject: [PATCH 2/5] read from local file --- .../injectScript.js | 19 ++++++++++++------- tools/github-page-script-injection/script.js | 18 ------------------ 2 files changed, 12 insertions(+), 25 deletions(-) delete mode 100644 tools/github-page-script-injection/script.js diff --git a/tools/github-page-script-injection/injectScript.js b/tools/github-page-script-injection/injectScript.js index 6e996f6f6..760bb30f0 100644 --- a/tools/github-page-script-injection/injectScript.js +++ b/tools/github-page-script-injection/injectScript.js @@ -7,7 +7,6 @@ const processFolder = (folderPath) => { files.forEach((file) => { const filePath = path.join(folderPath, file); - console.log(` ${filePath}`); if (fs.statSync(filePath).isDirectory()) { // If the current path is a directory, recursively process it @@ -15,7 +14,6 @@ const processFolder = (folderPath) => { } else if (path.extname(file) === '.html') { // If it's an HTML file, inject the script console.log(`process ${filePath}`); - injectScript(filePath); } }); @@ -24,8 +22,19 @@ const processFolder = (folderPath) => { // Start processing from the 'docs' folder const docsFolder = path.join(__dirname, '../../docs'); + +// Prepare the script content to be injected +const scriptFilePath = path.join(__dirname, '../applicationinsights-web-snippet/build/output/snippet.min.js'); +let scriptContent = fs.readFileSync(scriptFilePath, 'utf8'); + +// Define the connection string to replace the placeholder +const connectionString = 'InstrumentationKey=814a172a-92fd-4950-9023-9cf13bb65696;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'; + +// Replace the placeholder string with the actual connection string +scriptContent = scriptContent.replace('YOUR_CONNECTION_STRING', connectionString); processFolder(docsFolder); + function injectScript(filePath) { fs.readFile(filePath, 'utf8', (err, data) => { if (err) { @@ -33,10 +42,6 @@ function injectScript(filePath) { return; } - // Read the script content from a file - const scriptFilePath = path.join(__dirname, 'script.js'); - const scriptContent = fs.readFileSync(scriptFilePath, 'utf8'); - // Check if the script content is already present in the file const fileContent = fs.readFileSync(filePath, 'utf8'); @@ -46,7 +51,7 @@ function injectScript(filePath) { return; } // Create the modified content by inserting the script tag right before the closing head tag - const modifiedContent = data.replace(/(<\/head[^>]*)/i, `\n${scriptContent}\n$1`); + const modifiedContent = data.replace(/(<\/head[^>]*)/i, `\n\n$1`); // Save the modified content back to the file fs.writeFile(filePath, modifiedContent, (err) => { if (err) { diff --git a/tools/github-page-script-injection/script.js b/tools/github-page-script-injection/script.js deleted file mode 100644 index bcc1acf55..000000000 --- a/tools/github-page-script-injection/script.js +++ /dev/null @@ -1,18 +0,0 @@ - \ No newline at end of file From 745aababbb48c9a826c7a581aed3d59893298bcb Mon Sep 17 00:00:00 2001 From: siyuniu-ms Date: Thu, 11 Jan 2024 16:17:27 -0800 Subject: [PATCH 3/5] also support markdown file --- _includes/snippet.html | 13 ++++++ package.json | 3 +- .../injectScript.js | 42 ++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 _includes/snippet.html diff --git a/_includes/snippet.html b/_includes/snippet.html new file mode 100644 index 000000000..a0f1598c7 --- /dev/null +++ b/_includes/snippet.html @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/package.json b/package.json index 3a390b4ea..fe7242f0b 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "npm-publish": "node ./tools/release-tools/npm_publish.js", "npm-set-latest": "node ./tools/release-tools/npm_set_latest.js", "gh-status": "node ./tools/status-tools/github-status.js", - "script-inject": "node tools/github-page-script-injection/injectScript.js" + "script-inject": "node tools/github-page-script-injection/injectScript.js", + "api-docs": "rush api-docs && npm run script-inject" }, "repository": { "type": "git", diff --git a/tools/github-page-script-injection/injectScript.js b/tools/github-page-script-injection/injectScript.js index 760bb30f0..37f196a24 100644 --- a/tools/github-page-script-injection/injectScript.js +++ b/tools/github-page-script-injection/injectScript.js @@ -9,12 +9,13 @@ const processFolder = (folderPath) => { const filePath = path.join(folderPath, file); if (fs.statSync(filePath).isDirectory()) { - // If the current path is a directory, recursively process it processFolder(filePath); } else if (path.extname(file) === '.html') { - // If it's an HTML file, inject the script console.log(`process ${filePath}`); injectScript(filePath); + } else if (path.extname(file) === '.md') { + console.log(`process ${filePath}`); + injectHtml(filePath); } }); }; @@ -27,13 +28,42 @@ const docsFolder = path.join(__dirname, '../../docs'); const scriptFilePath = path.join(__dirname, '../applicationinsights-web-snippet/build/output/snippet.min.js'); let scriptContent = fs.readFileSync(scriptFilePath, 'utf8'); -// Define the connection string to replace the placeholder -const connectionString = 'InstrumentationKey=814a172a-92fd-4950-9023-9cf13bb65696;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'; - // Replace the placeholder string with the actual connection string +const connectionString = 'InstrumentationKey=814a172a-92fd-4950-9023-9cf13bb65696;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'; scriptContent = scriptContent.replace('YOUR_CONNECTION_STRING', connectionString); +scriptContent = ``; + +// write this file into _include folder so that later github would reject it inside markdown files +const includeFolderFile = path.join(__dirname, '../../_includes/snippet.html'); +fs.writeFileSync(includeFolderFile, scriptContent, 'utf8'); + +// recursively process all html files under docs folder processFolder(docsFolder); +function injectHtml(filePath) { + // Read the content of the Markdown file + const markdownContent = fs.readFileSync(filePath, 'utf8'); + + // Specify the injection string + const injectionString = ` +
+ + + \`\`\`html + {% include script.html %} + \`\`\` + +
+ `; + + // Append the injection string to the end of the Markdown content + const updatedContent = `${markdownContent}\n\n${injectionString}`; + + // Write the updated content back to the file + fs.writeFileSync(filePath, updatedContent, 'utf8'); + + console.log(`Markdown file injection completed for ${filePath}`); +} function injectScript(filePath) { fs.readFile(filePath, 'utf8', (err, data) => { @@ -51,7 +81,7 @@ function injectScript(filePath) { return; } // Create the modified content by inserting the script tag right before the closing head tag - const modifiedContent = data.replace(/(<\/head[^>]*)/i, `\n\n$1`); + const modifiedContent = data.replace(/(<\/head[^>]*)/i, `\n${scriptContent}\n$1`); // Save the modified content back to the file fs.writeFile(filePath, modifiedContent, (err) => { if (err) { From 792abbe85bdac2840b559e142bac43bc5a04cd90 Mon Sep 17 00:00:00 2001 From: siyuniu-ms Date: Tue, 16 Jan 2024 20:58:53 -0800 Subject: [PATCH 4/5] use github action for snippet injection --- .github/workflows/jekyll-gh-pages.yml | 58 +++++++++++++++++++ package.json | 3 +- .../injectScript.js | 13 +---- 3 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/jekyll-gh-pages.yml diff --git a/.github/workflows/jekyll-gh-pages.yml b/.github/workflows/jekyll-gh-pages.yml new file mode 100644 index 000000000..26de92e5e --- /dev/null +++ b/.github/workflows/jekyll-gh-pages.yml @@ -0,0 +1,58 @@ +# Sample workflow for building and deploying a Jekyll site to GitHub Pages +name: Deploy Jekyll with GitHub Pages dependencies preinstalled + +on: + # Runs on pushes targeting the default branch + push: + branches: ["main"] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages +permissions: + contents: read + pages: write + id-token: write + +# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. +# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. +concurrency: + group: "pages" + cancel-in-progress: false + +jobs: + # Build job + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Build Snippet File + run: | + cd tools/applicationinsights-web-snippet + npm install + npm run build + - name: Inject Script + run: node ./tools/github-page-script-injection/injectScript.js + - name: Setup Pages + uses: actions/configure-pages@v4 + - name: Build with Jekyll + uses: actions/jekyll-build-pages@v1 + with: + source: ./ + destination: ./_site + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 + + # Deployment job + deploy: + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + runs-on: ubuntu-latest + needs: build + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 diff --git a/package.json b/package.json index fe7242f0b..95c672b74 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,7 @@ "npm-publish": "node ./tools/release-tools/npm_publish.js", "npm-set-latest": "node ./tools/release-tools/npm_set_latest.js", "gh-status": "node ./tools/status-tools/github-status.js", - "script-inject": "node tools/github-page-script-injection/injectScript.js", - "api-docs": "rush api-docs && npm run script-inject" + "api-docs": "rush api-docs" }, "repository": { "type": "git", diff --git a/tools/github-page-script-injection/injectScript.js b/tools/github-page-script-injection/injectScript.js index 37f196a24..c16d6d0ca 100644 --- a/tools/github-page-script-injection/injectScript.js +++ b/tools/github-page-script-injection/injectScript.js @@ -34,7 +34,7 @@ scriptContent = scriptContent.replace('YOUR_CONNECTION_STRING', connectionString scriptContent = ``; // write this file into _include folder so that later github would reject it inside markdown files -const includeFolderFile = path.join(__dirname, '../../_includes/snippet.html'); +const includeFolderFile = path.join(__dirname, '../../_includes/script.html'); fs.writeFileSync(includeFolderFile, scriptContent, 'utf8'); // recursively process all html files under docs folder @@ -45,16 +45,7 @@ function injectHtml(filePath) { const markdownContent = fs.readFileSync(filePath, 'utf8'); // Specify the injection string - const injectionString = ` -
- - - \`\`\`html - {% include script.html %} - \`\`\` - -
- `; + const injectionString = `{% include script.html %}`; // Append the injection string to the end of the Markdown content const updatedContent = `${markdownContent}\n\n${injectionString}`; From 579d98c83cdbb7d1fe6eacb8037af3d4943883bb Mon Sep 17 00:00:00 2001 From: siyuniu-ms Date: Wed, 17 Jan 2024 13:39:15 -0800 Subject: [PATCH 5/5] add rush update --- .github/workflows/jekyll-gh-pages.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/jekyll-gh-pages.yml b/.github/workflows/jekyll-gh-pages.yml index 26de92e5e..3fa261f51 100644 --- a/.github/workflows/jekyll-gh-pages.yml +++ b/.github/workflows/jekyll-gh-pages.yml @@ -31,6 +31,7 @@ jobs: - name: Build Snippet File run: | cd tools/applicationinsights-web-snippet + rush update npm install npm run build - name: Inject Script