Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix #2

Open
wants to merge 1 commit into
base: v0.0.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file modified LICENSE
100644 → 100755
Empty file.
84 changes: 59 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

## 简介

node2bat 是一个可以将 NodeJS 脚本编译为 Windows 批处理脚本的工具。编译后的批处理不再依赖 NodeJS,双击可直接运行。你可以使用 NodeJS 的 API 来编写一些轻量级的自动化 Windows 批处理脚本。
node2bat 是一个可以将 NodeJS 脚本编译为 Windows 批处理脚本的工具。编译后的批处理不再依赖 NodeJS,双击可直接运行。你可以使用 NodeJS 的 API 来编写 Windows 批处理脚本发布给其他人使用,而不需要安装 NodeJS 的环境。借助 js 强大的语法支持,你的批处理将异常强大。

## 原理

windows 中自带了 javascript 引擎支持 jscript,所以 node2bat 便是使用 jscript 封装了一层 nodejs 的 api,然后巧妙地将 js 包含在 bat 文件中。

## 安装

先安装 NodeJS 与 npm,然后执行:
先安装 NodeJS,然后执行:

$ npm install node2bat -g

Expand All @@ -18,52 +22,54 @@ node2bat 是一个可以将 NodeJS 脚本编译为 Windows 批处理脚本的工

## 运行示例

源码 test 目录是一个演示例子,其中 includeHTML.js 是基于 NodeJS 编写的自动化脚本,它的用途是给页面制作人员批量更新局部模板的。
源码 demo 目录是一个演示例子,其中 includeHTML.js 是基于 NodeJS 编写的自动化脚本,它的用途是给页面制作人员批量更新局部模板的。

这个脚本里面用到了 NodeJS 的文件与路径处理,我们可以编译 includeHTML.js 为 bat 文件,这样用户拿到我们的 bat 文件就可以直接双击运行了,无需安装 NodeJS 与学习命令行。

切换到源码目录,运行:

$ node2bat test/includeHTML.js
$ node2bat demo/includeHTML/includeHTML.js

运行完毕后会立刻生成独立的 includeHTML.bat,你也可以双击打开它试用一下。

## JS API

node2bat v0.0.1 支持的 JS API 是 NodeJS API 的子集,包含基本的文件与路径操作:
node2bat 支持 NodeJS API 的核心 API,并且支持大多数 ECMA5 的 API,这些足可以满足绝大多数任务。

### 全局变量

* [require(id)](http://nodejs.org/api/globals.html#globals_require)``(注意:不支持外部模块)``
* [process.argv](http://nodejs.org/api/process.html#process_process_argv)
* [process.env](http://nodejs.org/api/process.html#process_process_env)
* [process.exit([code])](http://nodejs.org/api/process.html#process_process_env)
* [process.cwd()](http://nodejs.org/api/process.html#process_process_cwd)
* [process.stdout.write(message)](http://nodejs.org/api/process.html#process_process_stdout)
* [console.log(message)](http://nodejs.org/api/console.html#console_console_log_data)
* [__filename](http://nodejs.org/api/globals.html#globals_filename)
* [__dirname](http://nodejs.org/api/globals.html#globals_dirname)

> 注:使用 process.argv.slice(2) 可返回拖拽到批处理程序图标上后所有的文件列表
* [require(id)](http://nodejs.org/api/globals.html#globals_require)``(注意:仅支持 node2bat 内置的系统模块或外部 json 文件)``
* process
* [process.argv](http://nodejs.org/api/process.html#process_process_argv)
* [process.env](http://nodejs.org/api/process.html#process_process_env)
* [process.exit([code])](http://nodejs.org/api/process.html#process_process_env)
* [process.cwd()](http://nodejs.org/api/process.html#process_process_cwd)
* [process.stdout.write(message)](http://nodejs.org/api/process.html#process_process_stdout)
* [process.stderr.write(message)](http://nodejs.org/api/process.html#process_process_stderr)
* console
* [console.log([data], [...])](http://nodejs.org/api/console.html#console_console_log_data)
* [console.info([data], [...])](http://nodejs.org/api/console.html#console_console_info_data)
* [console.error([data], [...])](http://nodejs.org/api/console.html#console_console_error_data)
* [console.warn([data], [...])](http://nodejs.org/api/console.html#console_console_warn_data)
* [console.dir(obj)](http://nodejs.org/api/console.html#console_console_dir_obj)
* [console.time(label)](http://nodejs.org/api/console.html#console_console_time_label)
* [console.timeEnd(label)](http://nodejs.org/api/console.html#console_console_timeend_label)
* [console.assert(expression, [message])](http://nodejs.org/api/console.html#console_console_assert_expression_message)

> 注:如果拖拽文件到 bat 图标上执行,process.argv.slice(2) 可以获取这些文件的列表

### 内置模块

* File System
* [fs.rename(oldPath, newPath, callback)](http://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback)
* [fs.renameSync(oldPath, newPath)](http://nodejs.org/api/fs.html#fs_fs_renamesync_oldpath_newpath)
* [fs.stat(path, callback)](http://nodejs.org/api/fs.html#fs_fs_stat_path_callback)
* [fs.statSync(path)](http://nodejs.org/api/fs.html#fs_fs_statsync_path)
* [fs.unlink(path, callback)](http://nodejs.org/api/fs.html#fs_fs_unlink_path_callback)
* [fs.statSync(path)](http://nodejs.org/api/fs.html#fs_fs_statsync_path)``只支 isFile()、isDirectory()、size、atime、mtime、ctime``
* [fs.unlinkSync(path)](http://nodejs.org/api/fs.html#fs_fs_unlinksync_path)
* [fs.rmdir(path, callback)](http://nodejs.org/api/fs.html#fs_fs_rmdir_path_callback)
* [fs.rmdirSync(path)](http://nodejs.org/api/fs.html#fs_fs_rmdirsync_path)
* [fs.mkdir(path, [mode], callback)](http://nodejs.org/api/fs.html#fs_fs_mkdir_path_mode_callback)
* [fs.mkdirSync(path, [mode])](http://nodejs.org/api/fs.html#fs_fs_mkdirsync_path_mode)
* [fs.readFile(filename, [options], callback)](http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback)
* [fs.readFileSync(filename, [options])](http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options)
* [fs.writeFile(filename, data, [options], callback)](http://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback)
* [fs.writeFileSync(filename, data, [options])](http://nodejs.org/api/fs.html#fs_fs_writefilesync_filename_data_options)
* [fs.exists(path, callback)](http://nodejs.org/api/fs.html#fs_fs_exists_path_callback)
* [fs.existsSync(path)](http://nodejs.org/api/fs.html#fs_fs_existssync_path)
* Path
* [path.normalize(p)](http://nodejs.org/api/path.html#path_path_normalize_p)
Expand All @@ -75,8 +81,36 @@ node2bat v0.0.1 支持的 JS API 是 NodeJS API 的子集,包含基本的文
* [path.extname(p)](http://nodejs.org/api/path.html#path_path_extname_p)
* [path.sep](http://nodejs.org/api/path.html#path_path_sep)
* [path.delimiter](http://nodejs.org/api/path.html#path_path_delimiter)

## 测试用例

node2bat test/test.js
start test/test.bat

## 更新日志

v0.0.3

* 修复``fs.writeFileSync()``写入``utf-8``文件会插入 bom 的问题
* 修复``this``指向为``global``对象的问题
* ``require()``方法支持载入 json 文件
* ``fs.statSync()``支持``size``、``atime``、``mtime``、``ctime``
* ``require(id)``支持读取外部 json 文件
* 完善``console``模块
* 完善``process.env``模块,完全与 NodeJS 保持一致
* 鉴于使用场景为批处理,所以取消异步方法的支持
* 提供测试用例

v0.0.2

* 修正``process.argv``的 BUG
* ``console.log``方法可以友好的显示``Object``数据结构

v0.0.1

* 支持 NodeJS 文件与路径操作等基础 API

## 开源节流
## License

正如您若见,这是一个奇葩的项目,如果有兴趣可参与进来将更多的 NodeJS 特性在 node2bat 上实现。
MIT

2 changes: 1 addition & 1 deletion bin/node2bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

var node2bat = require('../node2bat.js');
var node2bat = require('../src/node2bat.js');
var fs = require('fs');


Expand Down
39 changes: 39 additions & 0 deletions demo/includeHTML/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# HTML 局部模板更新工具

## 工具介绍

includeHTML 是一个简单易用的 HTML 静态文件局部模板批量更新工具,它通过注释的方式给 HTML 添加类似服务端模板``include``标签的功能,可用来分离页面的头部与底部模板。

### 特性

1. 无需 js 支持不会破坏 SEO
2. 无需服务端环境运行
3. 页面可被反复更新
4. 语法与普通注释无异
5. 绿色单文件,双击即可运行

本工具适用于 web 设计师或者 web 重构工程师。

## include 语句

编写页面的时候,在需要动态更新的地方按固定格式写好注释即可,例如:

```
<!--[include './public/header.html']-->
<div id="header">
<ul class="nav">
<li><a href="./index.html">Home</a></li>
<li><a href="./news.html">News</a></li>
<li><a href="./about.html">about</a></li>
</ul>
</div>
<!--[/include]-->
```

运行本工具后则会将内容替换为外部模板。

## 工具使用

将工具放在模板目录,双击 includeHTML.bat 即可。

> 处理单个页面拖拽到 includeHTML.bat 图标上运行即可。
14 changes: 7 additions & 7 deletions test/test/about.html → demo/includeHTML/demo/about.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<!doctype html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>About</title>
</head>
<body>
<!--[include './public/header.html']-->
<!--[include "./public/header.html"]-->
<div id="header">
<h1>Site</h1>
<ul class="nav">
<li><a href="./index.html">Home</a></li>
<li><a href="./news.html">News</a></li>
<li><a href="./about.html">about</a></li>
</ul>
</div>
</div>
<!--[/include]-->

<div id="content">
<h2>About</h2>
</div>

<!--[include './public/footer.html']-->
<!--[include "./public/footer.html"]-->
<div id="footer">
(c) 2013 <a href="https://github.com/aui">github.com/aui</a>
</div>

(c) 2014 <a href="https://github.com/aui">github.com/aui</a>
</div>
<!--[/include]-->
</body>
</html>
14 changes: 7 additions & 7 deletions test/test/index.html → demo/includeHTML/demo/index.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<!doctype html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<!--[include './public/header.html']-->
<!--[include "./public/header.html"]-->
<div id="header">
<h1>Site</h1>
<ul class="nav">
<li><a href="./index.html">Home</a></li>
<li><a href="./news.html">News</a></li>
<li><a href="./about.html">about</a></li>
</ul>
</div>
</div>
<!--[/include]-->

<div id="content">
<h2>Home</h2>
</div>

<!--[include './public/footer.html']-->
<!--[include "./public/footer.html"]-->
<div id="footer">
(c) 2013 <a href="https://github.com/aui">github.com/aui</a>
</div>

(c) 2014 <a href="https://github.com/aui">github.com/aui</a>
</div>
<!--[/include]-->
</body>
</html>
14 changes: 7 additions & 7 deletions test/test/news.html → demo/includeHTML/demo/news.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<!doctype html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>News</title>
</head>
<body>
<!--[include './public/header.html']-->
<!--[include "./public/header.html"]-->
<div id="header">
<h1>Site</h1>
<ul class="nav">
<li><a href="./index.html">Home</a></li>
<li><a href="./news.html">News</a></li>
<li><a href="./about.html">about</a></li>
</ul>
</div>
</div>
<!--[/include]-->

<div id="content">
<h2>News</h2>
</div>

<!--[include './public/footer.html']-->
<!--[include "./public/footer.html"]-->
<div id="footer">
(c) 2013 <a href="https://github.com/aui">github.com/aui</a>
</div>

(c) 2014 <a href="https://github.com/aui">github.com/aui</a>
</div>
<!--[/include]-->
</body>
</html>
3 changes: 3 additions & 0 deletions demo/includeHTML/demo/public/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
 <div id="footer">
(c) 2014 <a href="https://github.com/aui">github.com/aui</a>
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
 <div id="header">
<h1>Site</h1>
<ul class="nav">
<li><a href="./index.html">Home</a></li>
<li><a href="./news.html">News</a></li>
Expand Down