node.js - Generating a json for a icon cheatsheet
问题描述
I'm trying to generate a json file containing the filenames of all the files in a certain directory. I need this to create a cheatsheet for icons.
Currently I'm trying to run a script locally via terminal, to generate the json. That json will be the input for a react component that will display icons. That component works, the create json script doesn't.
Code for generating the json
const fs = require('fs');
const path = require('path');
/**
* Create JSON file
*/
const CreateJson = () => {
const files = [];
const dir = '../icons';
fs.readdirSync(dir).forEach(filename => {
const name = path.parse(filename);
const filepath = path.resolve(dir, filename);
const stat = fs.statSync(filepath);
const isFile = stat.isFile();
if (isFile) files.push({ name });
});
const data = JSON.stringify(files, null, 2);
fs.writeFileSync('../Icons.json', data);
};
module.exports = CreateJson;
I run it in terminal using "create:json": "NODE_ENV=build node ./scripts/CreateJson.js"
I expect a json file to be created/overridden. But terminal returns:
$ NODE_ENV=build node ./scripts/CreateJson.js ✨ Done in 0.16s.
Any pointers?
解决方案
You are creating a function CreateJson
and exporting it, but you are actually never calling it.
You can get rid of the module.exports
and replace it with CreateJson()
.
When you'll execute the file with node, it will see the function declaration, and a call to it, whereas with your current code there is no call.
推荐阅读
- r - 将R中一组点的边界作为多边形返回?
- javascript - 您如何在 jest 上执行重复的 jQuery ajax 调用以进行单元测试?
- javascript - 当任何 cypress 方法不起作用时如何处理 Cypress 中的拖放
- node.js - Nodejs,在所有功能或事件中为我的应用程序创建日志的更好方法是什么?
- flutter - 如何在文本字段上方显示提示文本
- javascript - 是否可以从 FIREBASE 存储中同步下载图像?并将其传递给 PNG.sync.read?
- python - 6.moves 中的关键错误,返回 sys.modules[fullname]
- antlr4 - ANTLR - 解析时如何在双引号字符串中接受双引号
- postgresql - 恢复 Postgres 数据库失败:用户密码验证失败
- javascript - 无法在 Windows 10 中执行脚本 docker-compose YML 文件