Skip to content

Commit 4fbf41a

Browse files
authored
Merge pull request #227 from nitrictech/fix/build-function-cleanup-context-fixes
fix: clean build folder and apply correct context for it
2 parents 4b3ad01 + cc61989 commit 4fbf41a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

packages/common/src/task/build-function.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import execa from 'execa';
16+
import path from 'path';
1617
import { oneLine } from 'common-tags';
1718
import { Task } from './task';
1819
import { ContainerImage } from '../types';
@@ -52,8 +53,13 @@ export class BuildFunctionTask extends Task<ContainerImage> {
5253

5354
// Create a temporary default ignore file
5455
// and delete it when we're done
55-
await fs.promises.mkdir(`./${DEFAULT_BUILD_DIR}`, { recursive: true });
56-
await fs.promises.writeFile(`./${DEFAULT_BUILD_DIR}/${imageId}.toml`, TOML.stringify(DEFAULT_PROJECT_CONFIG));
56+
const contextDirectory = this.service.getDescriptor().context || '.';
57+
const contextBuildDirectory = `./${path.join(contextDirectory, DEFAULT_BUILD_DIR)}`;
58+
59+
await fs.promises.mkdir(contextBuildDirectory, {
60+
recursive: true,
61+
});
62+
await fs.promises.writeFile(`./${contextBuildDirectory}/${imageId}.toml`, TOML.stringify(DEFAULT_PROJECT_CONFIG));
5763

5864
let baseCmd = oneLine`
5965
build ${imageId}
@@ -99,6 +105,14 @@ export class BuildFunctionTask extends Task<ContainerImage> {
99105

100106
// wait for the process to finalize
101107
await packProcess;
108+
109+
// clean build files
110+
await fs.promises.unlink(`./${contextBuildDirectory}/${imageId}.toml`);
111+
112+
// remove build directory if empty
113+
if (fs.readdirSync(contextBuildDirectory).length === 0) {
114+
await fs.promises.rmdir(contextBuildDirectory);
115+
}
102116
} catch (e) {
103117
throw new Error(e.message);
104118
}

0 commit comments

Comments
 (0)