Skip to content

Commit f542c48

Browse files
committed
fix(nx-heroku): replace hardcoded apps by nx config value
1 parent fd37841 commit f542c48

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

packages/nx-heroku/src/executors/deploy/executor.spec.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ const options: DeployExecutorSchema = {
4040
};
4141

4242
class MockHerokuAppService extends HerokuAppService {
43-
constructor(options: DeployExecutorSchema, logger: LoggerInterface) {
44-
super(options, logger);
43+
constructor(
44+
options: DeployExecutorSchema,
45+
context: ExecutorContext,
46+
logger: LoggerInterface
47+
) {
48+
super(options, context, logger);
4549
}
4650
}
4751

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

6569
beforeEach(() => {
66-
logger = new ConsoleLogger();
67-
herokuAppService = new MockHerokuAppService(options, logger);
68-
herokuDeployService = new MockHerokuDeployService(
69-
options,
70-
context,
71-
herokuAppService,
72-
logger
73-
);
74-
Container.set(HerokuDeployService, herokuDeployService);
75-
7670
context = {
7771
isVerbose: true,
7872
cwd: process.cwd(),
@@ -89,6 +83,16 @@ describe('Deploy Executor', () => {
8983
},
9084
};
9185

86+
logger = new ConsoleLogger();
87+
herokuAppService = new MockHerokuAppService(options, context, logger);
88+
herokuDeployService = new MockHerokuDeployService(
89+
options,
90+
context,
91+
herokuAppService,
92+
logger
93+
);
94+
Container.set(HerokuDeployService, herokuDeployService);
95+
9296
jest.spyOn(logger, 'info');
9397
jest.spyOn(logger, 'error');
9498
jest.spyOn(logger, 'log').mockImplementation();

packages/nx-heroku/src/executors/deploy/services/heroku-app.service.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable max-lines */
2+
import { ExecutorContext } from '@nrwl/devkit';
23
import axios from 'axios';
34
import {
45
ChildProcessWithoutNullStreams,
@@ -12,6 +13,7 @@ import { Inject, Service } from 'typedi';
1213

1314
import {
1415
APTFILE,
16+
EXECUTOR_CONTEXT,
1517
HEROKU_BUILDPACK_APT,
1618
HEROKU_BUILDPACK_STATIC,
1719
PROCFILE,
@@ -48,6 +50,7 @@ export class HerokuAppService {
4850
constructor(
4951
@Inject(DEPLOY_EXECUTOR_SCHEMA)
5052
private options: DeployExecutorSchema,
53+
@Inject(EXECUTOR_CONTEXT) private readonly context: ExecutorContext,
5154
@Logger() private logger: LoggerInterface
5255
) {
5356
this.logger.debug = options.debug;
@@ -77,7 +80,7 @@ export class HerokuAppService {
7780
...options,
7881
};
7982
this.logger.info(`Deploying ${projectName} on ${appName} Heroku app...`);
80-
await new HerokuApp(extendedOptions, this.logger).run();
83+
await new HerokuApp(extendedOptions, this.context, this.logger).run();
8184
}
8285
}
8386

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

103106
constructor(
104107
public options: ExtendedDeployExecutorSchema,
108+
public context: ExecutorContext,
105109
public logger: LoggerInterface
106-
) {}
110+
) {
111+
this.appsDir =
112+
context.nxJsonConfiguration?.workspaceLayout?.appsDir || 'apps';
113+
}
107114

108115
private async addAndCommit(
109116
projectName: string,
@@ -128,7 +135,7 @@ class HerokuApp {
128135
private async createProcfile(): Promise<void> {
129136
const { procfile, projectName } = this.options;
130137
if (procfile) {
131-
const procfilePath = `apps/${projectName}/${PROCFILE}`;
138+
const procfilePath = `${this.appsDir}/${projectName}/${PROCFILE}`;
132139
await writeFile(join(process.cwd(), procfilePath), procfile);
133140
await this.addAndCommit(projectName, PROCFILE);
134141
}
@@ -140,10 +147,9 @@ class HerokuApp {
140147
): Promise<void> {
141148
const { buildPacks, projectName } = this.options;
142149
if (buildPacks.includes(buildPackName)) {
143-
// TODO: check nxConfig.appsDir
144150
const srcPath = join(
145151
process.cwd(),
146-
`apps/${projectName}/${buildPackFile}`
152+
`${this.appsDir}/${projectName}/${buildPackFile}`
147153
);
148154
const destPath = join(process.cwd(), buildPackFile);
149155
const srcBuildPackFile = await readFile(srcPath, 'utf-8');
@@ -325,11 +331,11 @@ class HerokuApp {
325331
//? if data contains `Everything up-to-date`, should we still restart the app
326332
push.stdout
327333
.setEncoding('utf-8')
328-
.on('data', (data) => this.logger.info(data));
334+
.on('data', (data) => this.logger.info(data?.trim()));
329335

330336
push.stderr
331337
.setEncoding('utf-8')
332-
.on('data', (data) => this.logger.info(data));
338+
.on('data', (data) => this.logger.info(data?.trim()));
333339
return push;
334340
}
335341

packages/nx-heroku/src/executors/deploy/services/heroku-deploy.service.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { DEPLOY_EXECUTOR_SCHEMA } from './tokens';
1919
@Service()
2020
export class HerokuDeployService extends HerokuBaseService<DeployExecutorSchema> {
2121
private previousGitConfig = { name: '', email: '' };
22+
private appsDir: string;
2223

2324
constructor(
2425
@Inject(DEPLOY_EXECUTOR_SCHEMA)
@@ -29,6 +30,8 @@ export class HerokuDeployService extends HerokuBaseService<DeployExecutorSchema>
2930
) {
3031
super(options, logger);
3132
this.logger.debug = options.debug;
33+
this.appsDir =
34+
context.nxJsonConfiguration?.workspaceLayout?.appsDir || 'apps';
3235
}
3336

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

5154
private async setupHeroku(): Promise<void> {

0 commit comments

Comments
 (0)