According to /recipes/generators/creating-files
Generators provide an API for managing files within your workspace. You can use generators to do things such as create, update, move, and delete files.
it's possible to delete files in an NX generator.
I have the following code that generates a node application but I want to delete some of the generated files.
import { readProjectConfiguration, Tree } from '@nrwl/devkit';
import { applicationGenerator } from '@nrwl/node'
export interface Schema {
name: string
}
export default async function (tree: Tree, schema: Schema) {
// create node application with name `schema.name`
await applicationGenerator(tree, {
name: schema.name
})
const projectRoot = readProjectConfiguration(tree, schema.name).sourceRoot
if (!projectRoot) throw new Error(`${schema.name} is not a project found in project configuration`)
// here I want to delete generated files:
// apps/myapp/src/app/.gitkeep
// apps/myapp/src/assets/.gitkeep
// apps/myapp/src/environments/environment.prod.ts
// apps/myapp/src/environments/environment.ts
}
Output
CREATE apps/myapp/src/app/.gitkeep
CREATE apps/myapp/src/assets/.gitkeep
CREATE apps/myapp/src/environments/environment.prod.ts
CREATE apps/myapp/src/environments/environment.ts
CREATE apps/myapp/src/main.ts
CREATE apps/myapp/tsconfig.app.json
CREATE apps/myapp/tsconfig.json
CREATE apps/myapp/project.json
CREATE apps/myapp/.eslintrc.json
CREATE apps/myapp/jest.config.ts
CREATE apps/myapp/tsconfig.spec.json
What API is used for deleting files?
According to https://nx.dev/recipes/generators/creating-files
Generators provide an API for managing files within your workspace. You can use generators to do things such as create, update, move, and delete files.
it's possible to delete files in an NX generator.
I have the following code that generates a node application but I want to delete some of the generated files.
import { readProjectConfiguration, Tree } from '@nrwl/devkit';
import { applicationGenerator } from '@nrwl/node'
export interface Schema {
name: string
}
export default async function (tree: Tree, schema: Schema) {
// create node application with name `schema.name`
await applicationGenerator(tree, {
name: schema.name
})
const projectRoot = readProjectConfiguration(tree, schema.name).sourceRoot
if (!projectRoot) throw new Error(`${schema.name} is not a project found in project configuration`)
// here I want to delete generated files:
// apps/myapp/src/app/.gitkeep
// apps/myapp/src/assets/.gitkeep
// apps/myapp/src/environments/environment.prod.ts
// apps/myapp/src/environments/environment.ts
}
Output
CREATE apps/myapp/src/app/.gitkeep
CREATE apps/myapp/src/assets/.gitkeep
CREATE apps/myapp/src/environments/environment.prod.ts
CREATE apps/myapp/src/environments/environment.ts
CREATE apps/myapp/src/main.ts
CREATE apps/myapp/tsconfig.app.json
CREATE apps/myapp/tsconfig.json
CREATE apps/myapp/project.json
CREATE apps/myapp/.eslintrc.json
CREATE apps/myapp/jest.config.ts
CREATE apps/myapp/tsconfig.spec.json
What API is used for deleting files?
Share Improve this question edited Nov 14, 2022 at 13:36 Andrew Allen asked Nov 14, 2022 at 13:24 Andrew AllenAndrew Allen 8,1327 gold badges37 silver badges80 bronze badges1 Answer
Reset to default 9Turned out to be very simple.
tree.delete(filePath)
In my case,
...
// Delete generated files:
// apps/myapp/src/app/.gitkeep
// apps/myapp/src/assets/.gitkeep
// apps/myapp/src/environments/environment.prod.ts
// apps/myapp/src/environments/environment.ts
tree.delete(joinPathFragments(projectRoot, 'src/app'))
tree.delete(joinPathFragments(projectRoot, 'src/assets'))
tree.delete(joinPathFragments(projectRoot, 'src/environments'))