本文介绍了用node.js写一个简单的命令行工具,分享给大家,具体如下:
操作系统需要为linux
1. 目标
2. 代码部分
介绍: 生成一个文件,文件内容为当前日期和创建者
#! /usr/bin/env node console.log('command start'); const fs = require('fs'); let date = new date().tolocaledatestring(); let data = date + '\n\t' + '——create by karuru'; fs.writefile('./date.txt', data, 'utf8', (err) => { if (err) { console.log('sherryfile command wrong', err); return false; } console.log('writefile success!!!!'); console.log('command end'); });
command start
writefile success!!!!
command end
在该文件目录下,会有一个新的date.txt文件生成,内容如下
2/28/2018
create by karuru
将命令修改为全局有效
ln sherryfile /usr/local/bin/sherryfile
删除命令
rm /usr/local/bin/sherryfile
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
egg项目npm/cnpm出错Connect timeout
网友评论