Skip to content

Commit

Permalink
Merge pull request #227 from nitrictech/fix/build-function-cleanup-co…
Browse files Browse the repository at this point in the history
…ntext-fixes

fix: clean build folder and apply correct context for it
  • Loading branch information
jyecusch authored Oct 26, 2021
2 parents 4b3ad01 + cc61989 commit 4fbf41a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/common/src/task/build-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import execa from 'execa';
import path from 'path';
import { oneLine } from 'common-tags';
import { Task } from './task';
import { ContainerImage } from '../types';
Expand Down Expand Up @@ -52,8 +53,13 @@ export class BuildFunctionTask extends Task<ContainerImage> {

// Create a temporary default ignore file
// and delete it when we're done
await fs.promises.mkdir(`./${DEFAULT_BUILD_DIR}`, { recursive: true });
await fs.promises.writeFile(`./${DEFAULT_BUILD_DIR}/${imageId}.toml`, TOML.stringify(DEFAULT_PROJECT_CONFIG));
const contextDirectory = this.service.getDescriptor().context || '.';
const contextBuildDirectory = `./${path.join(contextDirectory, DEFAULT_BUILD_DIR)}`;

await fs.promises.mkdir(contextBuildDirectory, {
recursive: true,
});
await fs.promises.writeFile(`./${contextBuildDirectory}/${imageId}.toml`, TOML.stringify(DEFAULT_PROJECT_CONFIG));

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

// wait for the process to finalize
await packProcess;

// clean build files
await fs.promises.unlink(`./${contextBuildDirectory}/${imageId}.toml`);

// remove build directory if empty
if (fs.readdirSync(contextBuildDirectory).length === 0) {
await fs.promises.rmdir(contextBuildDirectory);
}
} catch (e) {
throw new Error(e.message);
}
Expand Down

0 comments on commit 4fbf41a

Please sign in to comment.