Skip to content

Commit

Permalink
fix(nx-heroku): replace hardcoded apps by nx config value
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Mar 1, 2023
1 parent fd37841 commit f542c48
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
28 changes: 16 additions & 12 deletions packages/nx-heroku/src/executors/deploy/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ const options: DeployExecutorSchema = {
};

class MockHerokuAppService extends HerokuAppService {
constructor(options: DeployExecutorSchema, logger: LoggerInterface) {
super(options, logger);
constructor(
options: DeployExecutorSchema,
context: ExecutorContext,
logger: LoggerInterface
) {
super(options, context, logger);
}
}

Expand All @@ -63,16 +67,6 @@ describe('Deploy Executor', () => {
let herokuDeployService: MockHerokuDeployService;

beforeEach(() => {
logger = new ConsoleLogger();
herokuAppService = new MockHerokuAppService(options, logger);
herokuDeployService = new MockHerokuDeployService(
options,
context,
herokuAppService,
logger
);
Container.set(HerokuDeployService, herokuDeployService);

context = {
isVerbose: true,
cwd: process.cwd(),
Expand All @@ -89,6 +83,16 @@ describe('Deploy Executor', () => {
},
};

logger = new ConsoleLogger();
herokuAppService = new MockHerokuAppService(options, context, logger);
herokuDeployService = new MockHerokuDeployService(
options,
context,
herokuAppService,
logger
);
Container.set(HerokuDeployService, herokuDeployService);

jest.spyOn(logger, 'info');
jest.spyOn(logger, 'error');
jest.spyOn(logger, 'log').mockImplementation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-lines */
import { ExecutorContext } from '@nrwl/devkit';
import axios from 'axios';
import {
ChildProcessWithoutNullStreams,
Expand All @@ -12,6 +13,7 @@ import { Inject, Service } from 'typedi';

import {
APTFILE,
EXECUTOR_CONTEXT,
HEROKU_BUILDPACK_APT,
HEROKU_BUILDPACK_STATIC,
PROCFILE,
Expand Down Expand Up @@ -48,6 +50,7 @@ export class HerokuAppService {
constructor(
@Inject(DEPLOY_EXECUTOR_SCHEMA)
private options: DeployExecutorSchema,
@Inject(EXECUTOR_CONTEXT) private readonly context: ExecutorContext,
@Logger() private logger: LoggerInterface
) {
this.logger.debug = options.debug;
Expand Down Expand Up @@ -77,7 +80,7 @@ export class HerokuAppService {
...options,
};
this.logger.info(`Deploying ${projectName} on ${appName} Heroku app...`);
await new HerokuApp(extendedOptions, this.logger).run();
await new HerokuApp(extendedOptions, this.context, this.logger).run();
}
}

Expand All @@ -98,12 +101,16 @@ export class HerokuAppService {
* 13. Run healthcheck (optional)
*/
class HerokuApp {
// TODO: add property appsDir => context.nxJsonConfiguration.workspaceLayout?.appsDir ??= 'apps';
private readonly appsDir: string;

constructor(
public options: ExtendedDeployExecutorSchema,
public context: ExecutorContext,
public logger: LoggerInterface
) {}
) {
this.appsDir =
context.nxJsonConfiguration?.workspaceLayout?.appsDir || 'apps';
}

private async addAndCommit(
projectName: string,
Expand All @@ -128,7 +135,7 @@ class HerokuApp {
private async createProcfile(): Promise<void> {
const { procfile, projectName } = this.options;
if (procfile) {
const procfilePath = `apps/${projectName}/${PROCFILE}`;
const procfilePath = `${this.appsDir}/${projectName}/${PROCFILE}`;
await writeFile(join(process.cwd(), procfilePath), procfile);
await this.addAndCommit(projectName, PROCFILE);
}
Expand All @@ -140,10 +147,9 @@ class HerokuApp {
): Promise<void> {
const { buildPacks, projectName } = this.options;
if (buildPacks.includes(buildPackName)) {
// TODO: check nxConfig.appsDir
const srcPath = join(
process.cwd(),
`apps/${projectName}/${buildPackFile}`
`${this.appsDir}/${projectName}/${buildPackFile}`
);
const destPath = join(process.cwd(), buildPackFile);
const srcBuildPackFile = await readFile(srcPath, 'utf-8');
Expand Down Expand Up @@ -325,11 +331,11 @@ class HerokuApp {
//? if data contains `Everything up-to-date`, should we still restart the app
push.stdout
.setEncoding('utf-8')
.on('data', (data) => this.logger.info(data));
.on('data', (data) => this.logger.info(data?.trim()));

push.stderr
.setEncoding('utf-8')
.on('data', (data) => this.logger.info(data));
.on('data', (data) => this.logger.info(data?.trim()));
return push;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { DEPLOY_EXECUTOR_SCHEMA } from './tokens';
@Service()
export class HerokuDeployService extends HerokuBaseService<DeployExecutorSchema> {
private previousGitConfig = { name: '', email: '' };
private appsDir: string;

constructor(
@Inject(DEPLOY_EXECUTOR_SCHEMA)
Expand All @@ -29,6 +30,8 @@ export class HerokuDeployService extends HerokuBaseService<DeployExecutorSchema>
) {
super(options, logger);
this.logger.debug = options.debug;
this.appsDir =
context.nxJsonConfiguration?.workspaceLayout?.appsDir || 'apps';
}

/*
Expand All @@ -45,7 +48,7 @@ export class HerokuDeployService extends HerokuBaseService<DeployExecutorSchema>
* in an Nx monorepo, there should be more that one app so more than one Procfile is needed,
* which requires to use the buildpack 'heroku-community/multi-procfile' and to set the config var 'PROCFILE' to the path of the Procfile
*/
process.env.HD_PROCFILE = `apps/${projectName}/Procfile`;
process.env.HD_PROCFILE = `${this.appsDir}/${projectName}/Procfile`;
}

private async setupHeroku(): Promise<void> {
Expand Down

0 comments on commit f542c48

Please sign in to comment.