Skip to content

Commit

Permalink
Cleanup code by switching from promise exec to execa
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ Higgins committed Jun 3, 2019
1 parent 3a4700b commit e42a6e5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 56 deletions.
55 changes: 47 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@oclif/plugin-help": "^2.1.2",
"@types/auth0": "^2.9.11",
"@types/dotenv": "^6.1.1",
"@types/execa": "^0.9.0",
"@types/fs-extra": "^7.0.0",
"@types/joi": "^14.3.3",
"@types/keytar": "^4.4.0",
Expand All @@ -24,6 +25,7 @@
"auth0": "^2.17.0",
"chalk": "^2.4.1",
"dotenv": "^8.0.0",
"execa": "^1.0.0",
"fs-extra": "^8.0.1",
"inquirer": "^6.2.0",
"joi": "^14.3.1",
Expand Down
27 changes: 10 additions & 17 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { flags } from '@oclif/command';
import chalk from 'chalk';
import { exec } from 'child_process';
import * as execa from 'execa';
import * as Listr from 'listr';
import * as path from 'path';

Expand Down Expand Up @@ -63,22 +63,15 @@ export default class Build extends Command {
const dockerfile_path = path.join(__dirname, '../../Dockerfile');
const tag_name = tag || `architect-${service_config.name}`;

// execSync caused the output logs to hang
await new Promise(resolve => {
const thread = exec([
'docker', 'build',
'--compress',
'--build-arg', `SERVICE_LANGUAGE=${service_config.language}`,
'-t', tag_name,
'-f', dockerfile_path,
'--label', `architect.json='${JSON.stringify(service_config)}'`,
service_path
].join(' '));

thread.on('close', () => {
resolve();
});
});
await execa.shell([
'docker', 'build',
'--compress',
'--build-arg', `SERVICE_LANGUAGE=${service_config.language}`,
'-t', tag_name,
'-f', dockerfile_path,
'--label', `architect.json='${JSON.stringify(service_config)}'`,
service_path
].join(' '));
}

async run() {
Expand Down
13 changes: 3 additions & 10 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { flags } from '@oclif/command';
import chalk from 'chalk';
import { exec, execSync } from 'child_process';
import * as execa from 'execa';
import * as Listr from 'listr';
import * as path from 'path';
import * as url from 'url';
Expand Down Expand Up @@ -78,14 +78,7 @@ export default class Push extends Command {

const user = await this.architect.getUser();
const repository_name = url.resolve(`${this.app_config.default_registry_host}/`, `${user.username}/${tag_name}`);
execSync(`docker tag ${tag_name} ${repository_name}`);
// execSync caused the output logs to hang
await new Promise(resolve => {
const thread = exec(`docker push ${repository_name}`);

thread.on('close', () => {
resolve();
});
});
await execa.shell(`docker tag ${tag_name} ${repository_name}`);
await execa.shell(`docker push ${repository_name}`);
}
}
34 changes: 13 additions & 21 deletions src/common/protoc-executor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec, execSync } from 'child_process';
import * as execa from 'execa';
import { copyFileSync, existsSync, mkdirSync, realpathSync, writeFileSync } from 'fs';
import * as os from 'os';
import * as path from 'path';
Expand Down Expand Up @@ -44,26 +44,18 @@ namespace ProtocExecutor {
const mount_dirname = '/opt/protoc';
const mounted_proto_path = path.posix.join(mount_dirname, ServiceConfig.convertServiceNameToFolderName(dependency_config.name), dependency_config.proto);

// execSync caused the output logs to hang
await new Promise(resolve => {
const thread = exec([
'docker', 'run',
'-v', `${target_path}:/defs`,
'-v', `${tmpRoot}:${mount_dirname}`,
'--user', process.platform === 'win32' ? '1000:1000' : '$(id -u):$(id -g)', // TODO figure out correct user for windows
'architectio/protoc-all',
'-f', `${mounted_proto_path}`,
'-i', mount_dirname,
'-l', target_language,
'-o', MANAGED_PATHS.DEPENDENCY_STUBS_DIRECTORY
].join(' '));

thread.on('close', () => {
resolve();
});
});

execSync(`rm -rf ${tmpDir}`);
await execa.shell([
'docker', 'run',
'-v', `${target_path}:/defs`,
'-v', `${tmpRoot}:${mount_dirname}`,
'--user', process.platform === 'win32' ? '1000:1000' : '$(id -u):$(id -g)', // TODO figure out correct user for windows
'architectio/protoc-all',
'-f', `${mounted_proto_path}`,
'-i', mount_dirname,
'-l', target_language,
'-o', MANAGED_PATHS.DEPENDENCY_STUBS_DIRECTORY
].join(' '));
await execa.shell(`rm -rf ${tmpDir}`);

_postHooks(stub_directory, target_language);
};
Expand Down

0 comments on commit e42a6e5

Please sign in to comment.