admin管理员组文章数量:1431720
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'))
本文标签: javascriptHow to delete a file in NX generatorStack Overflow
版权声明:本文标题:javascript - How to delete a file in NX generator - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745565818a2663752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论