简体   繁体   English

如何在 angular.json 的脚本部分使用全局模式?

[英]How to use a glob pattern in the scripts section of angular.json?

I have a hybrid AngularJS/Angular application that will take some time to complete migration to fully be an Angular app.我有一个混合 AngularJS/Angular 应用程序,需要一些时间才能完成迁移以完全成为 Angular 应用程序。 While this process occurs, I'd like to move away from the previous build system to using the CLI and webpack to manage all of the old AngularJS scripts as well.当这个过程发生时,我想从以前的构建系统转移到使用 CLI 和 webpack 来管理所有旧的 AngularJS 脚本。 This is possible as I've done it before by adding all of my scripts to the scripts section in angular.json like the following:这是可能的,因为我以前通过将所有脚本添加到angular.json中的scripts部分来完成它,如下所示:

"scripts": [              
  "src/app/angularjs/app.js",
  "src/app/angularjs/controllers/main.js",
  "src/app/angularjs/services/someService.js",
  "src/app/angularjs/controllers/someController.js"           
],

This works well and the CLI builds via ng serve and ng build continue to work for the hybrid bootstrapped app as needed.这很好用,并且通过ng serveng build的 CLI 继续根据需要为混合引导应用程序工作。 The problem I'm running into now is manually listing each file for the current application I'm migrating is not ideal.我现在遇到的问题是手动列出我正在迁移的当前应用程序的每个文件并不理想。 I have hundreds of scripts that need to be added, and what I need is to be able to use a globbing pattern like the following:我有数百个脚本需要添加,我需要的是能够使用如下通配模式:

"scripts": [              
  "src/app/angularjs/**/*.js"
],

The problem is this syntax from what I can tell is not supported.问题是不支持这种语法。 The glob pattern is supported in the assets section of angular.json as stated here but not in the scripts section: https://angular.io/guide/workspace-config#assets-configuration全局模式angular.jsonassets部分中受支持,如此处所述,但在scripts部分中 不受支持:https://angular-configuration.io/guide/workspace-config#assets

In the scripts section I can't find a similar solution.scripts部分我找不到类似的解决方案。 It does have an expanded object API, but nothing that solves the problem I can tell to select all .js files from a particular directory as listed here: https://angular.io/guide/workspace-config#styles-and-scripts-configuration It does have an expanded object API, but nothing that solves the problem I can tell to select all .js files from a particular directory as listed here: https://angular.io/guide/workspace-config#styles-and-scripts -配置

Is it possible by some means to use a glob pattern or similar approach to select all files of a directory for the scripts section in angular.json so I don't have to manually list out hundreds of individual .js files?是否可以通过某种方式对angular.json中的scripts部分的目录的所有文件使用 glob 模式或类似方法,所以我不必手动列出数百个单独的.js文件。

The Bad News坏消息

The scripts section does not support the same glob patterns that the assets section does. scripts部分不支持与assets部分相同的 glob 模式。

The Good News(?)好消息(?)

Since you're transitioning away from AngularJS, you hopefully won't have any new files to import in the future, so you could just generate the list of all the files you need to import.由于您正在从 AngularJS 过渡,因此您希望将来不会有任何新文件要导入,因此您可以生成需要导入的所有文件的列表。

Make your way to the src/app/angular directory and run the following:进入src/app/angular目录并运行以下命令:

find . -iregex '.*\.\(js\)' -printf '"%p",\n'

That will give you your list, already quoted for your convenience.这将为您提供您的清单,为了您的方便已经引用了。 You may need to do a quick search/replace (changing "." to "src/app/angularjs"), and don't forget to remove the last comma, but once you've done that once you should be all set.您可能需要进行快速搜索/替换(将“.”更改为“src/app/angularjs”),并且不要忘记删除最后一个逗号,但是一旦您完成了该操作,您就应该准备就绪。

The Extra News额外新闻

You can further filter out unwanted files with -not , so (per your comment) you might do:您可以使用-not进一步过滤掉不需要的文件,因此(根据您的评论)您可以这样做:

find . -iregex '^.*\.js$' -not -iregex '^.*_test\.js$' -printf '"%p",\n'

And that should give you all your.js files without your _test.js files.这应该会给你所有你的 .js 文件,而没有你的 _test.js 文件。

KISS

Of course, this isn't a complex pattern, so as @atconway points out below, this will work just as well:当然,这不是一个复杂的模式,所以正如@atconway 在下面指出的那样,这同样适用:

find . -iname "*.js" -not -iname "*_test.js" -printf '"%p",\n'

I'll keep the above, though, for use in situations where the full power of regex might come in handy.不过,我将保留上述内容,以便在正则表达式的全部功能可能派上用场的情况下使用。

I wanted to extend an anser of @JasonVerber and here is a Node.JS code and therefore (I believe) cross-platform.我想扩展 @JasonVerber 的分析器,这是一个 Node.JS 代码,因此(我相信)是跨平台的。

Firstly install find package and then save contents from the snippet in some file.js .首先安装find package 然后将代码段中的内容保存在一些file.js中。 Afterwards, specify paths so that they resolve to where you wan't to get your files from and where to put the resulting file to.之后,指定路径,以便它们解析到您不想从哪里获取文件以及将结果文件放在哪里。

After that node file-name.js and this will save all found file paths to the resultPath in result.txt ready to Ctrl+A , Ctrl+C , Ctrl+V .在该node file-name.js之后,这会将所有找到的文件路径保存到result.txt中的resultPath ,准备好Ctrl+ACtrl+CCtrl+V

const find = require('find');
const path = require('path');
const fs = require('fs');

// BEFORE USAGE INSTALL `find` package

// Path to the folder where to look for files
const sourcePath = path.resolve(path.join(__dirname, 'cordova-app', 'src'));
// Path that will be removed from absolute path to files
const pathToRemove = path.resolve(path.join(__dirname, 'cordova-app'));
// Path where to put result.txt
const resultPath = path.resolve(path.join(__dirname, './result.txt'));
// Collects the file paths
const res = [];
// Path with replaced \ onto /
const pathToRemovehReplaced = pathToRemove.replace(/\\/g, '/');

// Get all fils that match a regex
find.eachfile(/\.js$/, sourcePath, file => {
    // First remove all \ with / and then remove the path from root to source so that only relative path is left
    const fileReplaced = file.replace(/\\/g, '/').replace(`${pathToRemovehReplaced}/`, '');
    // Surround with quoutes
    res.push(`"${fileReplaced}"`);
}).end(() => {
    // Write file and concatenate results with newline and commas
    fs.writeFileSync(resultPath, res.join(',\r\n'), 'utf8');
    console.log('DONE!');
});

The result I got while testing ( /\.ts$/ for regex)我在测试时得到的结果( /\.ts$/用于正则表达式)

"src/app/app.component.spec.ts",
"src/app/app.component.ts",
"src/app/app.module.ts",
"src/environments/environment.prod.ts",
"src/environments/environment.ts",
"src/main.ts",
"src/polyfills.ts",
"src/test.ts"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM