【发布时间】:2019-04-27 18:39:00
【问题描述】:
我有多个以 cmets 开头的文件,例如:
/*
* @title Force email verification
* @overview Only allow access to users with verified emails.
* @gallery true
* @category access control
*
* This rule will only allow access users that have verified their emails.
*
* > Note: It might be a better UX to make this verification from your application.
*
* If you are using [Lock](https://auth0.com/docs/lock), the default behavior is to log in a user immediately after they have signed up.
* To prevent this from immediately displaying an error to the user, you can pass the following option to `lock.show()` or similar: `loginAfterSignup: false`.
*
* If you are using [auth0.js](https://auth0.com/docs/libraries/auth0js), the equivalent option is `auto_login: false`.
*
*/
//jshint -W025
function (user, context, callback) {
if (!user.email_verified) {
return callback(new UnauthorizedError('Please verify your email before logging in.'));
} else {
return callback(null, user, context);
}
}
所有文件都包含两种类型的 cmets,即 /**/ 和 // 现在我正在我的 javascript 代码中读取此文件,并希望删除 cmets 并获取变量中的实际代码,例如
function (user, context, callback) {
if (!user.email_verified) {
return callback(new UnauthorizedError('Please verify your email before logging in.'));
} else {
return callback(null, user, context);
}
}
我尝试过使用 strip-cmets 和 parse-cmets npm,但这些都不起作用。代码如下:
const fs = require('fs');
const path = require('path');
const strip = require('strip-comments');
module.exports = function (ruleFileName, globals, stubs) {
globals = globals || {};
stubs = stubs || {};
const fileName = path.join(__dirname, '../src/rules', ruleFileName + '.js');
const data = fs.readFileSync(fileName, 'utf8');
const code = strip(data);
console.log(code);
return compile(code, globals, stubs);
}
我尝试使用 parse-cmets:
const parsed = parseComments(data)[0];
const code = data.split('\n').slice(parsed.comment.end).join('\n').trim();
我认为条形注释不起作用,因为它将字符串作为参数,但 fs.readFileSync 不返回字符串。我也试过data.toString()但这也没有用。那么如何从内容中去除 cmets 呢?有没有其他解决办法?
【问题讨论】:
-
已经看到了,但它不起作用,因为 strip-cmets 将字符串作为参数
-
如果您使用 Komodo Edit,您可以跨多个文件使用 RegEx。
cntrl+h. -
"但是 fs.readFileSync 不返回字符串" - 实际上,it does return a string 因为你传递了一个编码选项。
标签: javascript node.js npm ecmascript-6 auth0