Skip to content

Commit

Permalink
feat(node): add deleteOutputPath option to @nrwl/node:webpack executor
Browse files Browse the repository at this point in the history
    ISSUES CLOSED: #9167
  • Loading branch information
jaytavares committed Apr 29, 2022
1 parent bef8240 commit edea1f2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/node.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@
"type": "string",
"description": "The output path of the generated files."
},
"deleteOutputPath": {
"type": "boolean",
"description": "Delete the output path before building.",
"default": true
},
"watch": {
"type": "boolean",
"description": "Run build when files change.",
Expand Down
37 changes: 37 additions & 0 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import {
createFile,
checkFilesDoNotExist,
checkFilesExist,
killPorts,
Expand Down Expand Up @@ -285,6 +286,42 @@ describe('Build Node apps', () => {
);
}, 300000);

it('should remove previous output before building with the --deleteOutputPath option set', async () => {
const appName = uniq('app');
const libName = uniq('lib');

runCLI(
`generate @nrwl/node:app ${appName} --no-interactive --compiler swc`
);
runCLI(
`generate @nrwl/node:lib ${libName} --buildable --no-interactive --compiler swc`
);

createFile(`dist/apps/${appName}/_should_remove.txt`);
createFile(`dist/libs/${libName}/_should_remove.txt`);
createFile(`dist/apps/_should_not_remove.txt`);
checkFilesExist(
`dist/apps/${appName}/_should_remove.txt`,
`dist/apps/_should_not_remove.txt`
);
runCLI(`build ${appName} --outputHashing none`);
runCLI(`build ${libName}`);
checkFilesDoNotExist(
`dist/apps/${appName}/_should_remove.txt`,
`dist/libs/${libName}/_should_remove.txt`
);
checkFilesExist(`dist/apps/_should_not_remove.txt`);

// `delete-output-path`
createFile(`dist/apps/${appName}/_should_keep.txt`);
runCLI(`build ${appName} --delete-output-path=false --outputHashing none`);
checkFilesExist(`dist/apps/${appName}/_should_keep.txt`);

createFile(`dist/libs/${libName}/_should_keep.txt`);
runCLI(`build ${libName} --delete-output-path=false --outputHashing none`);
checkFilesExist(`dist/libs/${libName}/_should_keep.txt`);
}, 120000);

describe('NestJS', () => {
it('should have plugin output if specified in `tsPlugins`', async () => {
newProject();
Expand Down
5 changes: 5 additions & 0 deletions packages/node/src/executors/webpack/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"type": "string",
"description": "The output path of the generated files."
},
"deleteOutputPath": {
"type": "boolean",
"description": "Delete the output path before building.",
"default": true
},
"watch": {
"type": "boolean",
"description": "Run build when files change.",
Expand Down
7 changes: 7 additions & 0 deletions packages/node/src/executors/webpack/webpack.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getNodeWebpackConfig } from '../../utils/node.config';
import { BuildNodeBuilderOptions } from '../../utils/types';
import { normalizeBuildOptions } from '../../utils/normalize';
import { generatePackageJson } from '../../utils/generate-package-json';
import { deleteOutputDir } from '../../utils/fs';
import { runWebpack } from '../../utils/run-webpack';

export type NodeBuildEvent = {
Expand Down Expand Up @@ -81,6 +82,12 @@ export async function* webpackExecutor(
if (options.generatePackageJson) {
generatePackageJson(context.projectName, projGraph, options);
}

// Delete output path before bundling
if (options.deleteOutputPath) {
deleteOutputDir(context.root, options.outputPath);
}

const config = await options.webpackConfig.reduce(
async (currentConfig, plugin) => {
return require(plugin)(await currentConfig, {
Expand Down
14 changes: 14 additions & 0 deletions packages/node/src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as path from 'path';
import { removeSync } from 'fs-extra';

/**
* Delete an output directory, but error out if it's the root of the project.
*/
export function deleteOutputDir(root: string, outputPath: string) {
const resolvedOutputPath = path.resolve(root, outputPath);
if (resolvedOutputPath === root) {
throw new Error('Output path MUST not be project root directory!');
}

removeSync(resolvedOutputPath);
}
1 change: 1 addition & 0 deletions packages/node/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface BuildNodeBuilderOptions extends BuildBuilderOptions {
externalDependencies: 'all' | 'none' | string[];
buildLibsFromSource?: boolean;
generatePackageJson?: boolean;
deleteOutputPath?: boolean;
}

export interface NormalizedBuildNodeBuilderOptions
Expand Down

0 comments on commit edea1f2

Please sign in to comment.