新手献丑,晒几个自己写的Nodejs小玩意,欢迎拍砖
新手献丑,晒几个自己写的Nodejs小玩意,欢迎拍砖
玩 Node.js 差不多三个月了,写了几个小玩意,不出意外是能用的,拿来晒晒找拍。
license-gen
这是我入门后写的,代码不堪入目,到昨天被另一个东西依赖才稍微改了下,囧。
开源许可生成器,一条命令生成 LICENSE 文件
$ license mit
直接读取 git 配置中的姓名,生成 mit 许可
$ license -a Luffy mit
手动置顶名字
$ license
会出现对话,经过几个简单问题,提供建议的许可
更多的可以看 README
gitignore-gen
.gitignore 文件生成器,大概是入门后第三个东西。
模板是直接拉取了 github/gitignore 项目转的。
$ ignore mac node sublimetext
就能生成适合「在mac下用sublimetext开发node」情况下大概要加的东西
$ ignore -c test.js
追加一条 test.js (这里好像有个bug,我忘了,囧)
--force
选项被加上后会直接覆盖已有的 .gitignore
未来计划加入删除某个模板的功能
gitignore-update
这个就是用来拉取 github/gitignore 弄成 json 给上面那个用的,囧。
modit
这是昨天无聊写的快速新建 npm 包的工具
$ modit path/name
然后,根据提示输入名字(默认切取参数中的 name),作者(自动获取 git 配置),giturl, license(提供此参数后,调用 license-gen) 生成 LICENSE 文件。
dep-docs
其实没做完,前几天无聊写的
在项目目录执行 $ docs
后,会在 node_modules
下生成 .docs 目录,然后收集 node_modules
下各个 module 的 readme,将 markdown 转成 html,最后生成 _toc.html 做目录。
在 mac 下使用的话,会直接用浏览器打开这个目录。囧。
gulp-cdn-ref
一个 gulp 插件,可以轻松替换 html,css,js 中图片等静态资源的引用为 cdn 链接,支持相对路径和绝对路径(PS:总觉得有bug,慎用)
好了,后面的比较无聊,非战斗人员可以撤离了 →_→
unicode-lorem
缘起公司里要测试 unicode 字符的兼容性,乱写的,入门后第二个应该。
lorem(10)
就会生成 10 个乱码一样的文字,以及响应的编码。 因为大多时候出现的字符都和麻将牌一样,被人叫做麻将牌模块 orz
random-date
var date = randomDate('1d', '2014-10-06');
就会在 2014-10-05 至 2014-10-07 之间随机一个时间戳,orz。。我也布吉岛能干嘛
差不多就这些,还有几个感觉还没做好,打算先憋着了(战斗人员可以在我的 npm 页面或者 github 找到),不然感觉会走火入魔,给大家带来麻烦。囧
各位大大请随意拍砖
新手献丑,晒几个自己写的Nodejs小玩意,欢迎拍砖
玩 Node.js 差不多三个月了,写了几个小玩意,不出意外是能用的,拿来晒晒找拍。
license-gen
开源许可生成器,一条命令生成 LICENSE 文件。
$ license mit
直接读取 git 配置中的姓名,生成 MIT 许可。
$ license -a Luffy mit
手动置顶名字。
$ license
会出现对话,经过几个简单问题,提供建议的许可。
示例代码:
const fs = require('fs');
const { execSync } = require('child_process');
function generateLicense(type, author) {
let name = author || execSync('git config user.name').toString().trim();
let content = `MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`;
fs.writeFileSync('LICENSE', content);
}
generateLicense('mit', 'Luffy');
gitignore-gen
.gitignore 文件生成器。
$ ignore mac node sublimetext
就能生成适合「在mac下用sublimetext开发node」情况下大概要加的东西。
$ ignore -c test.js
追加一条 test.js。
示例代码:
const fs = require('fs');
function generateGitIgnore(...args) {
let content = '';
args.forEach(item => {
if (item.startsWith('-c')) {
content += `${item.split('-c ')[1]}\n`;
} else {
content += `# ${item}\n${item}\n\n`;
}
});
fs.writeFileSync('.gitignore', content);
}
generateGitIgnore('mac', 'node', 'sublimetext', '-c test.js');
gitignore-update
这个就是用来拉取 github/gitignore 弄成 json 给上面那个用的。
modit
快速新建 npm 包的工具。
$ modit path/name
根据提示输入名字(默认切取参数中的 name),作者(自动获取 git 配置),giturl, license(提供此参数后,调用 license-gen) 生成 LICENSE 文件。
示例代码:
const readline = require('readline-sync');
function createNpmPackage(name, author, gitUrl, license) {
const packageJson = {
name: name || readline.question('Enter package name: '),
version: '1.0.0',
description: '',
main: 'index.js',
scripts: {
start: 'node index.js'
},
author: author || readline.question('Enter author name: '),
license: license || 'MIT',
dependencies: {}
};
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2));
if (license) {
generateLicense(license, author);
}
}
createNpmPackage('path/name', 'Author Name', 'git@github.com:user/repo.git', 'MIT');
dep-docs
在项目目录执行 $ docs
后,会在 node_modules
下生成 .docs
目录,然后收集 node_modules
下各个 module 的 readme,将 markdown 转成 html,最后生成 _toc.html
做目录。
gulp-cdn-ref
一个 gulp 插件,可以轻松替换 html,css,js 中图片等静态资源的引用为 cdn 链接,支持相对路径和绝对路径。
unicode-lorem
生成乱码一样的文字,以及响应的编码。
$ lorem(10)
就会生成 10 个乱码一样的文字。
示例代码:
function lorem(count) {
let result = [];
for (let i = 0; i < count; i++) {
result.push(String.fromCharCode(Math.floor(Math.random() * 65535)));
}
return result.join('');
}
console.log(lorem(10));
random-date
生成指定范围内的随机日期。
$ var date = randomDate('1d', '2014-10-06');
会在 2014-10-05
至 2014-10-07
之间随机一个时间戳。
示例代码:
function randomDate(range, baseDate) {
let startDate = new Date(baseDate);
let endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + parseInt(range));
return new Date(startDate.getTime() + Math.random() * (endDate.getTime() - startDate.getTime()));
}
console.log(randomDate('1d', '2014-10-06'));
差不多就这些,还有几个感觉还没做好,打算先憋着了(战斗人员可以在我的 npm 页面或者 GitHub 找到),不然感觉会走火入魔,给大家带来麻烦。
支持原创
dep-docs 的依赖没有全写到 package.json里面。npm 安装后报错。
楼主是怎么学习node,有推荐的吗。感觉可国内参考的资源有点少
当然,很高兴来分享一下这些有趣的 Node.js 小工具。
License Generator (license-gen
)
用途:一键生成开源许可证文件。
示例代码:
// license-gen.js
const program = require('commander');
const fs = require('fs');
const { execSync } = require('child_process');
program
.version('0.1.0')
.option('-a, --author [name]', 'Specify the author name')
.argument('<type>', 'License type (e.g., mit, apache)')
.action((type, options) => {
let authorName = options.author || execSync('git config user.name').toString().trim();
let content = generateLicense(type, authorName);
fs.writeFileSync('LICENSE', content);
console.log('LICENSE file created.');
});
function generateLicense(type, authorName) {
if (type === 'mit') {
return `MIT License
Copyright (c) ${new Date().getFullYear()} ${authorName}
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`;
}
// 可以扩展更多类型的许可证
}
program.parse(process.argv);
使用方法:
$ node license-gen.js mit
$ node license-gen.js -a Luffy mit
Gitignore Generator (gitignore-gen
)
用途:生成 .gitignore
文件。
示例代码:
// gitignore-gen.js
const program = require('commander');
const fs = require('fs');
const axios = require('axios');
program
.version('0.1.0')
.option('-f, --force', 'Force overwrite existing .gitignore file')
.arguments('<templates...>')
.action(async (templates, options) => {
const content = await fetchGitignoreTemplates(templates.join(' '));
const force = options.force;
const filename = '.gitignore';
if (!force && fs.existsSync(filename)) {
console.error(`File ${filename} already exists. Use --force to overwrite.`);
process.exit(1);
}
fs.writeFileSync(filename, content);
console.log(`${filename} generated.`);
});
async function fetchGitignoreTemplates(templates) {
const response = await axios.get(`https://raw.githubusercontent.com/github/gitignore/master/${templates}.gitignore`);
return response.data;
}
program.parse(process.argv);
使用方法:
$ node gitignore-gen.js mac node sublimetext
$ node gitignore-gen.js -f test.js
希望这些简单的例子能够帮到你!如果有任何问题,欢迎拍砖指正。