-
Notifications
You must be signed in to change notification settings - Fork 13
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 #403 from st3phhays/testing-and-development
(#402) Enhance local choco-theme development
- Loading branch information
Showing
58 changed files
with
8,303 additions
and
566 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
/*! | ||
* Colors that are used in the terminal while running choco-theme commands. | ||
* Copyright 2020-2024 Chocolatey Software | ||
* Licensed under Apache License (https://github.com/chocolatey/choco-theme/blob/main/LICENSE) | ||
*/ | ||
|
||
export const revert = '\x1b[0m'; | ||
export const green = '\x1b[32m'; | ||
export const red = '\x1b[31m'; | ||
|
||
export const consoleColors = { | ||
revert, | ||
green, | ||
red | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
/*! | ||
* Configuration for repositories to be used by preview commands. | ||
* Copyright 2020-2024 Chocolatey Software | ||
* Licensed under Apache License (https://github.com/chocolatey/choco-theme/blob/main/LICENSE) | ||
*/ | ||
|
||
export interface FolderMapping { | ||
[key: string]: { | ||
folder: string; | ||
protocol?: string; | ||
port?: null | number; | ||
isStatic: boolean; | ||
root?: string | ||
}; | ||
} | ||
|
||
export const folderMapping: FolderMapping = { | ||
'--blog': { | ||
folder: 'blog', | ||
port: 5082, | ||
isStatic: true | ||
}, | ||
'--boxstarter': { | ||
folder: 'boxstarter.org', | ||
port: 5083, | ||
isStatic: true | ||
}, | ||
'--community': { | ||
folder: 'community.chocolatey.org', | ||
port: 55881, | ||
isStatic: false, | ||
root: '/chocolatey/Website' | ||
}, | ||
'--design': { | ||
folder: 'choco-design-system', | ||
port: 5085, | ||
isStatic: true | ||
}, | ||
'--docs': { | ||
folder: 'docs', | ||
port: 5086, | ||
isStatic: true | ||
}, | ||
'--fest': { | ||
folder: 'chocolateyfest', | ||
port: 5084, | ||
isStatic: true | ||
}, | ||
'--org': { | ||
folder: 'chocolatey.org', | ||
port: 5081, | ||
isStatic: true | ||
}, | ||
'--portal': { | ||
folder: 'licensing-services', | ||
protocol: 'https', | ||
port: 44362, | ||
isStatic: false, | ||
root: '/source/LicensingServices' | ||
}, | ||
'--zendesk': { | ||
folder: 'copenhagen_theme', | ||
isStatic: false | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
/*! | ||
* Loading animation to be used in the terminal when running choco-theme commands. | ||
* Copyright 2020-2024 Chocolatey Software | ||
* Licensed under Apache License (https://github.com/chocolatey/choco-theme/blob/main/LICENSE) | ||
*/ | ||
|
||
export const loadingAnimation = () => { | ||
const frames = ['-', '\\', '|', '/']; | ||
let i = 0; | ||
return setInterval(() => { | ||
process.stdout.write(`\r${frames[i]} Loading...`); | ||
i = (i + 1) % frames.length; | ||
}, 100); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
/*! | ||
* Script to start the preview server and monitor the ports of the repositories. | ||
* Copyright 2020-2024 Chocolatey Software | ||
* Licensed under Apache License (https://github.com/chocolatey/choco-theme/blob/main/LICENSE) | ||
*/ | ||
|
||
import net, { Socket } from 'net'; | ||
import { spawn } from 'child_process'; | ||
import WebSocket, { WebSocketServer } from 'ws'; | ||
import { folderMapping } from './data/preview-config'; | ||
|
||
// List of ports to monitor | ||
// Ensures if the folderMapping doesn't contain a port, that is excluded | ||
const portList: number[] = Object.values(folderMapping) | ||
.map(item => item.port) | ||
.filter(port => port !== undefined); | ||
|
||
const childProcess = spawn('yarn dlx http-server --cors -o', [], { | ||
stdio: 'inherit', // sends info over to preview.ts to be read | ||
shell: true | ||
}); | ||
|
||
// Create a WebSocket server | ||
const wss = new WebSocketServer({ port: 8081 }); | ||
|
||
// Function to check port status | ||
const checkPortStatus = (port: number): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
const socket: Socket = net.createConnection({ port }, () => { | ||
socket.end(); | ||
resolve('open'); | ||
}); | ||
|
||
socket.on('error', err => { | ||
const errorCodeMatch = err.message.match(/(EADDRINUSE|E.*):/); | ||
const errorCode = errorCodeMatch ? errorCodeMatch[1] : null; | ||
|
||
if (errorCode === 'ECONNREFUSED') { | ||
resolve('closed'); | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
|
||
socket.setTimeout(1000, () => { | ||
socket.destroy(); | ||
resolve('in use'); | ||
}); | ||
}); | ||
}; | ||
|
||
// Function to monitor ports and send status to client | ||
const monitorPorts = async (): Promise<void> => { | ||
const portStatus: { [key: number]: string } = {}; | ||
|
||
for (const port of portList) { | ||
try { | ||
const status: string = await checkPortStatus(port); | ||
portStatus[port] = status; | ||
console.log(`Port ${port} is ${status}`); | ||
} catch (err) { | ||
// console.error(`Error checking port ${port}: ${(err as Error).message}`); | ||
// Send port status to connected clients | ||
console.log(`Port ${port} is closed`); | ||
wss.clients.forEach(client => { | ||
if (client.readyState === WebSocket.OPEN) { | ||
const portStatus = JSON.stringify({ [port]: 'closed' }); | ||
client.send(portStatus); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
// Send port status to connected clients | ||
wss.clients.forEach(client => { | ||
if (client.readyState === WebSocket.OPEN) { | ||
client.send(JSON.stringify(portStatus)); | ||
} | ||
}); | ||
}; | ||
|
||
// Start monitoring ports | ||
setInterval(monitorPorts, 5000); // Check ports every 5 seconds |
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
/*! | ||
* Script to run choco-theme on specified linked folders. | ||
* Copyright 2020-2024 Chocolatey Software | ||
* Licensed under Apache License (https://github.com/chocolatey/choco-theme/blob/main/LICENSE) | ||
*/ | ||
|
||
import * as fs from 'fs/promises'; | ||
import * as path from 'path'; | ||
import { spawn } from 'child_process'; | ||
|
||
import { folderMapping } from './data/preview-config'; | ||
import { loadingAnimation } from './functions/loading-animation'; | ||
|
||
const init = async () => { | ||
// Parse command-line arguments | ||
let args = process.argv.slice(2); | ||
|
||
if (args[0] === '--all') { | ||
args = Object.keys(folderMapping); | ||
} | ||
|
||
const foldersToRun = args.filter(arg => folderMapping[arg]); | ||
|
||
if (foldersToRun.length === 0) { | ||
console.error('Please specify at least one valid option.'); | ||
process.exit(1); | ||
} else { | ||
console.log('🚀 Running choco-theme on folders...'); | ||
} | ||
|
||
// Array to store loading intervals for each script | ||
const loadingIntervals = foldersToRun.map(() => loadingAnimation()); | ||
|
||
await Promise.all(foldersToRun.map(async (option, index) => { | ||
const folderConfig = folderMapping[option]; | ||
const folderName = folderConfig.folder; | ||
const folderNamePath = folderConfig.root ? `${folderName}${folderConfig.root}` : folderName; | ||
const folderPath = path.join(__dirname, '../../', folderNamePath); | ||
|
||
// Check if folder exists | ||
try { | ||
await fs.access(folderPath); | ||
} catch (error) { | ||
clearInterval(loadingIntervals[index]); | ||
process.stdout.write('\r🟨 '); | ||
console.log(`🟨 ${folderName} does not exist. Skipping...`); | ||
return; // Skip to the next folder | ||
} | ||
|
||
try { | ||
const childProcess = spawn('yarn choco-theme', [], { | ||
// stdio: 'inherit', // Use 'inherit' to directly pipe the output to the parent process | ||
shell: true, // Needed for Windows to execute .sh files | ||
cwd: folderPath // Specify the working directory for the child process | ||
}); | ||
|
||
// Handle stdout data event | ||
childProcess.stdout.on('data', data => { | ||
const output = data.toString().trim(); | ||
if (output.includes('🎉 choco-theme complete' || output.startsWith('[nodemon] restarting due to changes...'))) { | ||
// Stop loading animation for this script | ||
clearInterval(loadingIntervals[index]); | ||
process.stdout.write('\r✅ '); | ||
console.log(`choco-theme complete on ${folderName}`); | ||
} | ||
}); | ||
|
||
// Handle exit event | ||
childProcess.on('exit', code => { | ||
if (code !== 0) { | ||
clearInterval(loadingIntervals[index]); | ||
process.stdout.write('\r⛔ '); | ||
console.error(`choco-theme not ran on ${folderName} and exited with code ${code}`); | ||
} | ||
}); | ||
} catch (error) { | ||
clearInterval(loadingIntervals[index]); | ||
process.stdout.write('\r⛔ '); | ||
console.error(`choco-theme not ran on ${folderName}. Error: ${error}`); | ||
} | ||
})); | ||
}; | ||
|
||
init(); |
Oops, something went wrong.