1 | import { execSync } from 'child_process';
|
2 | import { join, resolve } from 'path';
|
3 |
|
4 | import * as fsExtra from 'fs-extra';
|
5 |
|
6 | export const ROOT_DIR = resolve(join(__dirname, '..'));
|
7 | const CI_TOOLS_EXECUTABLE = join(ROOT_DIR, 'dist', 'ci_tools.js');
|
8 |
|
9 | export function run(ciToolsCommand: string): string {
|
10 | return shell(`node ${CI_TOOLS_EXECUTABLE} ${ciToolsCommand}`);
|
11 | }
|
12 |
|
13 | export function shell(shellCommand: string): string {
|
14 | console.log(' shell:', shellCommand);
|
15 |
|
16 | const env = { ...process.env };
|
17 | delete env['GIT_BRANCH'];
|
18 | delete env['GITHUB_REF'];
|
19 |
|
20 | const output = execSync(`${shellCommand}`, { encoding: 'utf-8', env: env });
|
21 |
|
22 | return output;
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export async function inDir(directory: string, callback: () => Promise<void>): Promise<void> {
|
37 | const oldDir = process.cwd();
|
38 | try {
|
39 | process.chdir(directory);
|
40 | await callback.apply(null);
|
41 | } catch (err) {
|
42 | console.error(`inDir(${directory}): ${err}`);
|
43 | throw err;
|
44 | } finally {
|
45 | process.chdir(oldDir);
|
46 | }
|
47 | }
|
48 |
|
49 | export function inDirSync(directory: string, callback: () => void): void {
|
50 | const oldDir = process.cwd();
|
51 | try {
|
52 | process.chdir(directory);
|
53 | callback.apply(null);
|
54 | } catch (err) {
|
55 | console.error(`inDir(${directory}): ${err}`);
|
56 | throw err;
|
57 | } finally {
|
58 | process.chdir(oldDir);
|
59 | }
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | export function setupGitWorkingCopyForTest(): string {
|
66 | const gitRemoteForTest = createGitRemoteToCloneFrom();
|
67 | const gitTempWorkingCopy = cloneTestRepoFromRemote(gitRemoteForTest);
|
68 | return gitTempWorkingCopy;
|
69 | }
|
70 |
|
71 | function createGitRemoteToCloneFrom(): string {
|
72 |
|
73 | const gitFixtureDir = resolve(join(ROOT_DIR, 'test', 'fixtures', 'simple.git'));
|
74 | const newGitRemote = resolve(join(ROOT_DIR, 'tmp', 'origin'));
|
75 |
|
76 | fsExtra.removeSync(newGitRemote);
|
77 | fsExtra.copySync(gitFixtureDir, newGitRemote);
|
78 |
|
79 | return newGitRemote;
|
80 | }
|
81 |
|
82 | function cloneTestRepoFromRemote(remote: string): string {
|
83 | const workingCopyDir = resolve(join(ROOT_DIR, 'tmp', 'working-copy'));
|
84 |
|
85 | fsExtra.removeSync(workingCopyDir);
|
86 | shell(`git clone ${remote} ${workingCopyDir}`);
|
87 |
|
88 | return workingCopyDir;
|
89 | }
|