javascript - How to delete a file in NX generator - Stack Overflow

According to recipesgeneratorscreating-filesGenerators provide an API for managing files within your

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 badges
Add a ment  | 

1 Answer 1

Reset to default 9

Turned 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'))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745565819a4633386.html

相关推荐

  • javascript - How to delete a file in NX generator - Stack Overflow

    According to recipesgeneratorscreating-filesGenerators provide an API for managing files within your

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信