Changed workflow to merge samples more betterer
This commit is contained in:
parent
3022177839
commit
8ddb366250
|
@ -0,0 +1,93 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const https = require('https');
|
||||
|
||||
const repoRoot = process.cwd();
|
||||
const samplesDir = path.join(repoRoot, 'samples');
|
||||
const outputDir = path.join(repoRoot, '.metadata');
|
||||
const outputFile = path.join(outputDir, 'samples.json');
|
||||
const externalSamplesUrl = 'https://raw.githubusercontent.com/pnp/sp-dev-fx-extensions/main/.metadata/samples.json';
|
||||
const externalSamplesFile = path.join(outputDir, 'extension-samples.json');
|
||||
|
||||
function downloadFile(url, dest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = fs.createWriteStream(dest);
|
||||
https.get(url, (response) => {
|
||||
if (response.statusCode === 200) {
|
||||
response.pipe(file);
|
||||
} else {
|
||||
file.close();
|
||||
fs.unlink(dest, () => { }); // Delete the file async. (Ignore errors)
|
||||
reject(`Server responded with ${response.statusCode}: ${response.statusMessage}`);
|
||||
}
|
||||
|
||||
file.on('finish', () => {
|
||||
file.close(resolve);
|
||||
});
|
||||
|
||||
file.on('error', (err) => {
|
||||
fs.unlink(dest, () => { }); // Delete the file async. (Ignore errors)
|
||||
reject(err.message);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function readSampleJson(filePath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(filePath, 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
try {
|
||||
const jsonData = JSON.parse(data);
|
||||
resolve(jsonData);
|
||||
} catch (parseErr) {
|
||||
console.error(`Invalid JSON in ${filePath}`);
|
||||
resolve(null); // Return null if JSON is invalid
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function mergeSamples() {
|
||||
try {
|
||||
// Ensure metadata directory exists
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
fs.mkdirSync(outputDir);
|
||||
}
|
||||
|
||||
// Download the external samples.json
|
||||
await downloadFile(externalSamplesUrl, externalSamplesFile);
|
||||
|
||||
let samples = [];
|
||||
// Include external samples if available
|
||||
if (fs.existsSync(externalSamplesFile)) {
|
||||
const externalSamples = await readSampleJson(externalSamplesFile);
|
||||
if (externalSamples) {
|
||||
samples = samples.concat(externalSamples);
|
||||
}
|
||||
}
|
||||
|
||||
const directories = fs.readdirSync(samplesDir, { withFileTypes: true });
|
||||
for (const dir of directories) {
|
||||
if (dir.isDirectory()) {
|
||||
const samplePath = path.join(samplesDir, dir.name, 'assets', 'sample.json');
|
||||
if (fs.existsSync(samplePath)) {
|
||||
const sampleData = await readSampleJson(samplePath);
|
||||
if (sampleData) { // Check if the data is not null (valid JSON)
|
||||
samples = samples.concat(sampleData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(outputFile, JSON.stringify(samples, null, 2));
|
||||
console.log('Samples merged successfully.');
|
||||
} catch (error) {
|
||||
console.error('Failed to merge samples:', error);
|
||||
}
|
||||
}
|
||||
|
||||
mergeSamples();
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "github-actions-scripts",
|
||||
"version": "1.0.0",
|
||||
"description": "Merge all sample.json files into one",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
name: Merge and Distribute Samples
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
types: [closed]
|
||||
|
||||
jobs:
|
||||
process_samples:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.pull_request.merged == true || github.event_name == 'push'
|
||||
steps:
|
||||
- name: Checkout main branch
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
ref: 'main'
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '16'
|
||||
|
||||
- name: Merge samples
|
||||
run: node .github/scripts/merge-samples/index.js
|
||||
|
||||
- name: Commit merged samples.json on main
|
||||
run: |
|
||||
git config --global user.email "action@github.com"
|
||||
git config --global user.name "GitHub Action"
|
||||
git add -A
|
||||
git diff --staged --quiet || git commit -m "Automated samples.json update"
|
||||
git push
|
||||
|
||||
- name: Checkout Docs branch
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
ref: 'Docs'
|
||||
path: 'docs-branch'
|
||||
|
||||
- name: Copy samples.json to Docs branch
|
||||
run: |
|
||||
cp .metadata/samples.json docs-branch/docs/samples.json
|
||||
cd docs-branch
|
||||
git add -A
|
||||
git diff --staged --quiet || git commit -m "Update samples.json in Docs branch"
|
||||
git push
|
||||
|
||||
- name: Checkout gh-pages branch
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
ref: 'gh-pages'
|
||||
path: 'gh-pages-branch'
|
||||
|
||||
- name: Copy samples.json to gh-pages branch
|
||||
run: |
|
||||
cp .metadata/samples.json gh-pages-branch/samples.json
|
||||
cd gh-pages-branch
|
||||
git add -A
|
||||
git diff --staged --quiet || git commit -m "Update samples.json in gh-pages branch"
|
||||
git push
|
|
@ -1,78 +0,0 @@
|
|||
name: Merge JSON
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "0 0 * * *"
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches:
|
||||
# When someone push to `main` branch
|
||||
- main
|
||||
paths:
|
||||
- 'samples/**/assets/sample.json'
|
||||
|
||||
jobs:
|
||||
merge:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout main branch
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
ref: main
|
||||
path: main
|
||||
|
||||
- name: Checkout gh-pages branch
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
ref: gh-pages
|
||||
path: gh-pages
|
||||
|
||||
- name: Checkout Docs branch
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
ref: Docs
|
||||
path: Docs
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm install lodash
|
||||
|
||||
- name: Download Extension samples
|
||||
run: |
|
||||
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
||||
-H "Accept: application/vnd.github.v3.raw" \
|
||||
-o ./main/.metadata/extension-samples.json \
|
||||
https://raw.githubusercontent.com/pnp/sp-dev-fx-extensions/main/.metadata/samples.json
|
||||
|
||||
- name: Join samples
|
||||
run: |
|
||||
jq -s '[.[][]]' ./main/samples/**/assets/sample.json ./main/.metadata/extension-samples.json > ./Docs/docs/samples.json
|
||||
|
||||
- name: Copy samples.json to gh-pages branch
|
||||
run: cp ./Docs/docs/samples.json ./gh-pages/samples.json
|
||||
|
||||
- name: Commit samples.json to gh-pages branch
|
||||
run: |
|
||||
cd gh-pages
|
||||
git config --local user.email "${{ secrets.USER_EMAIL }}"
|
||||
git config --local user.name "${{ secrets.USER_NAME }}"
|
||||
git add samples.json
|
||||
git commit -m "Update samples.json in gh-pages branch"
|
||||
git push origin gh-pages
|
||||
|
||||
- name: Copy samples.json to Docs branch
|
||||
run: cp ./Docs/docs/samples.json ./Docs/docs/samples.json
|
||||
|
||||
- name: Commit samples.json to Docs branch
|
||||
run: |
|
||||
cd Docs
|
||||
git config --local user.email "${{ secrets.USER_EMAIL }}"
|
||||
git config --local user.name "${{ secrets.USER_NAME }}"
|
||||
git add docs/samples.json
|
||||
git commit -m "Update samples.json in Docs branch"
|
||||
git push origin Docs
|
|
@ -1,36 +0,0 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const _ = require('lodash');
|
||||
|
||||
// Initialize an empty array to store the merged nodes
|
||||
let mergedNodes = [];
|
||||
|
||||
function getDirectories(dirPath) {
|
||||
return fs.readdirSync(dirPath, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
}
|
||||
|
||||
const baseDirectory = 'samples';
|
||||
const directories = getDirectories(baseDirectory);
|
||||
|
||||
directories.forEach(directory => {
|
||||
const assetsPath = path.join(baseDirectory, directory, 'assets');
|
||||
if (fs.existsSync(assetsPath)) {
|
||||
const files = fs.readdirSync(assetsPath);
|
||||
files.forEach(file => {
|
||||
if (file === 'sample.json') {
|
||||
const data = JSON.parse(fs.readFileSync(path.join(assetsPath, file), 'utf8'));
|
||||
// Assuming data is an array of nodes, merge it into the mergedNodes array
|
||||
Array.prototype.push.apply(mergedNodes, data);
|
||||
// Or using spread operator
|
||||
// mergedNodes.push(...data);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Write the merged nodes to a new JSON file
|
||||
fs.writeFileSync('sample.json', JSON.stringify(mergedNodes, null, 2));
|
||||
|
||||
console.log('Merged nodes saved to samples_merged.json');
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue