index.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { spawn } from 'child_process';
  2. import { mkdir, writeFile } from 'fs/promises';
  3. export interface RunOptions {
  4. cwd: string;
  5. }
  6. export async function run(command: string, options: RunOptions) {
  7. console.log(`Running ${command} in ${options.cwd}`);
  8. const process = spawn(command, { shell: true, cwd: options.cwd, stdio: 'inherit' });
  9. return new Promise<void>((resolve, reject) => {
  10. process.on('exit', (code) => {
  11. if (code !== 0) {
  12. reject(new Error(`Command ${command} exited with code ${code}`));
  13. } else {
  14. resolve();
  15. }
  16. });
  17. });
  18. }
  19. export async function runGetOutput(command: string, options: RunOptions): Promise<string> {
  20. console.log(`Running ${command} in ${options.cwd}`);
  21. return new Promise<string>((resolve, reject) => {
  22. const process = spawn(command, { shell: true, cwd: options.cwd, stdio: 'pipe' });
  23. let output = '';
  24. process.stdout.on('data', (data) => {
  25. output += data;
  26. });
  27. process.on('exit', (code) => {
  28. if (code !== 0) {
  29. reject(new Error(`Command ${command} exited with code ${code}`));
  30. } else {
  31. resolve(output);
  32. }
  33. });
  34. });
  35. }
  36. export async function gitShallowClone(
  37. targetPath: string,
  38. repositoryUrl: string,
  39. ref: string
  40. ): Promise<{ commitId: string }> {
  41. await mkdir(targetPath, { recursive: true });
  42. const options: RunOptions = { cwd: targetPath };
  43. await run('git init', options);
  44. await run(`git remote add origin ${repositoryUrl}`, options);
  45. await run(`git fetch --depth 1 origin ${ref}`, options);
  46. await run(`git checkout ${ref}`, options);
  47. const commitId = await runGetOutput('git rev-parse HEAD', options);
  48. return { commitId };
  49. }
  50. export async function group(name: string, body: () => Promise<void>): Promise<void> {
  51. console.log(`##[group]${name}`);
  52. try {
  53. await body();
  54. } catch (e) {
  55. console.error(e);
  56. throw e;
  57. } finally {
  58. console.log('##[endgroup]');
  59. }
  60. }
  61. export async function writeJsonFile(filePath: string, jsonData: unknown): Promise<void> {
  62. await writeFile(filePath, JSON.stringify(jsonData, null, '\t') + '\n');
  63. }
  64. export function getNightlyVersion(version: string): string {
  65. const pieces = version.split('.');
  66. const minor = parseInt(pieces[1], 10);
  67. const date = new Date();
  68. const yyyy = date.getUTCFullYear();
  69. const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
  70. const dd = String(date.getUTCDate()).padStart(2, '0');
  71. return `0.${minor + 1}.0-dev.${yyyy}${mm}${dd}`;
  72. }