index.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 gitCommitId(repositoryPath: string): Promise<string> {
  37. const commitId = (await runGetOutput('git rev-parse HEAD', { cwd: repositoryPath })).trim();
  38. return commitId;
  39. }
  40. export async function gitShallowClone(
  41. targetPath: string,
  42. repositoryUrl: string,
  43. ref: string
  44. ): Promise<{ commitId: string }> {
  45. await mkdir(targetPath, { recursive: true });
  46. const options: RunOptions = { cwd: targetPath };
  47. await run('git init', options);
  48. await run(`git remote add origin ${repositoryUrl}`, options);
  49. await run(`git fetch --depth 1 origin ${ref}`, options);
  50. await run(`git checkout ${ref}`, options);
  51. const commitId = await gitCommitId(targetPath);
  52. return { commitId };
  53. }
  54. export async function group(name: string, body: () => Promise<void>): Promise<void> {
  55. console.log(`##[group]${name}`);
  56. try {
  57. await body();
  58. } catch (e) {
  59. console.error(e);
  60. throw e;
  61. } finally {
  62. console.log('##[endgroup]');
  63. }
  64. }
  65. export async function writeJsonFile(filePath: string, jsonData: unknown): Promise<void> {
  66. await writeFile(filePath, JSON.stringify(jsonData, null, '\t') + '\n');
  67. }
  68. export function getNightlyVersion(version: string, prerelease: string): string {
  69. const pieces = version.split('.');
  70. const minor = parseInt(pieces[1], 10);
  71. const date = new Date();
  72. const yyyy = date.getUTCFullYear();
  73. const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
  74. const dd = String(date.getUTCDate()).padStart(2, '0');
  75. prerelease = prerelease.replace('${today}', `${yyyy}${mm}${dd}`);
  76. return `0.${minor + 1}.0-${prerelease}`;
  77. }
  78. export interface PackageJson {
  79. version: string;
  80. vscodeRef?: string;
  81. vscodeCommitId?: string;
  82. monacoCommitId?: string;
  83. devDependencies: Record<string, string>;
  84. }