2019年12月28日 星期六

[Nodejs] 用 PKG 將專案打包為可執行檔

PKG 可以將整個 NodeJS 專案打包為可執行檔,可以看到 PKG 的 Github 上追蹤的 star 數有 15k,但是網路上的範例少之又少。

尤其在使用 Express Generator 所建立出的專案要如何打包,在概念不清楚的情況下如同瞎子摸象般摸索了許久。

打包完成後也經過多台電腦(包含未安裝 NodeJS 環境的電腦)的測試下,終於成功執行了。在此記錄下相關筆記。

一、 安裝 PKG


npm install -g pkg

二、 實際跑跑看官方的範例


官方範例連結

將專案 clone 下後,如同官方範例下方說明所敘述,執行命令: npm install

pkg .
完成後會產生各平台的執行檔。


如果只是要產生 windows 下的執行檔,可以使用命令:
pkg . -t win


三、 Express Generator 建立出的專案如何打包?


我的專案目錄如下圖:


在 package.json 中加入資訊

{
  ....
  "bin": "bin/www",
  "pkg": {
    "scripts":[
      "models/**/*",
      "routes/**/*",
      "app.js"
    ],
    "assets": [
      "views/**/*",
      "public/**/*",
      "data.db",
      ".env"
    ]
  }
}

執行指令:

pkg . -t win

接著就等待完成產生的 exe 檔案吧!


四、 遇到的問題


由於專案使用到了 sqlite3 ,在該 module 中包含了 .node 檔案,沒辦法被包入 exe 中,目前找到的解法只有和 exe 放在同一個目錄下執行。

例如,我的專案用到了node_sqlite3.node,在包好 exe 後,要將該檔案一同複製到相同目錄中,exe 才能成功執行,如下圖:




2019年12月1日 星期日

[Ubuntu] 安裝紀錄

* 建立安裝 USB
(1). Download iso file ( https://www.ubuntu-tw.org/modules/tinyd0/ )
(2). Rufus 製作開機 USB (https://rufus.ie/)


* Install packages

(1). 取得更新
sudo apt-get update

(2). SSH
sudo apt-get install openssh-server

(3). VNC ( https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-18-04 )
sudo apt-get install xfce4 xfce4-goodies
sudo apt-get install tightvncserver

安裝完成後,輸入 vncserver 建立密碼


sudo apt install nginx

sudo systemctl status nginx
or 
sudo service nginx status

(5). 防火牆
sudo apt-get ufw

sudo ufw status

sudo ufw enable
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 5901
sudo ufw allow ssh




* To-do list

setting fixed ip for pppoe
ftp
sftp
domain
cloudflare

2019年10月11日 星期五

[Python] 編譯成exe

環境:
python 2.7

安裝軟體:
pyinstaller (https://pypi.org/project/PyInstaller/)
pywin32

流程:
使用pyinstaller進行打包

在cmd中輸入
python pyinstaller.py -F filename.py




• 參考
https://blog.csdn.net/lqzdreamer/article/details/77917493

* 其他
https://anthony-tuininga.github.io/cx_Freeze/
https://sourceforge.net/projects/pyinstaller/


====================================================
更簡單的方法:

pip install auto-py-to-exe

auto-py-to-exe filename.py

[Python] I/O

2.7

import glob,os
from os.path import isfile, join
from os import walk
import urllib

* Read

with open(filePath, 'r') as file:
            data = file.read()
            decodeData = urllib.unquote(data)
           
* Write

f1=open(newFilePath, 'w+')
f1.write(decodeData)
f1.close()

[Python] Tkinter

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
def hello():
   tkMessageBox.showinfo("Say Hello", "Hello World")

B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()

top.mainloop()

[Python] OCR文字辨識

Install:
pip install pytesseract
pip install pillow

Tesseract-OCR
https://github.com/UB-Mannheim/tesseract/wiki



Edit:
C:\Python27\Lib\site-packages\pytesseract\pytesseract.py

modify path
tesseract_cmd = 'F:/Program Files/Tesseract-OCR/tesseract.exe'

python script:
#-*-coding:utf-8-*-

from PIL import Image,ImageEnhance
import pytesseract
import time


def pic_to_word(filepath,filename,resize_num,b):
    """
    :param filepath: 檔案路徑
    :param filename:圖片名
    :param resize_num:縮放倍數
    :param b:對比度
    :return:返回圖片識別文字
    """
    try:
        time1 = time.time()
        im = Image.open(str(filepath)+str(filename))
        # 影象放大
        im = im.resize((im.width * int(resize_num), im.height * int(resize_num)))
        # 影象二值化
        imgry = im.convert('L')
        # 對比度增強
        sharpness = ImageEnhance.Contrast(imgry)
        sharp_img = sharpness.enhance(b)
        content = pytesseract.image_to_string(sharp_img, lang='chi_sim')
        time2 = time.time()
        print('total time%s s' % (time2 - time1))
    except Exception as e:
        print("{0}".format(str(e)))

    return content


if __name__ == '__main__':
    filepath="C:/Users/lc_lee/Desktop/"
    filename="a2.png"
    resize_num = 2
    b = 2.0
    content=pic_to_word(filepath,filename,resize_num,b)
    print(content.encode(encoding="utf-8", errors="strict"))
    # print(content.encode("utf8").decode("cp950", "ignore"))


# chcp 65001

• https://www.itread01.com/content/1547721909.html
• https://blog.csdn.net/wwj_748/article/details/78109680

2019年6月8日 星期六

[Nodejs] node-adodb 使用Electron建置執行檔錯誤

在使用Electron + node-adodb時,在local端運作正常,但編譯過後會執行時出現錯誤提示:
Spawn C:\Windows\SysWOW64\cscript.exe error

此程式是透過調用Windows下內建cscript.exe來進行解析mdb檔案,而調用的過程中所給的參數如下:

const args = [Proxy.adodb, '//E:JScript', '//Nologo', '//U', '//B', command];

其中 Proxy.adodb = require.resolve('./adodb')

這個路徑是造成此error最主要的原因,在調用cscript.exe時,要傳遞js腳本作為參數,而compile後的 adodb.js 這個檔案,被封裝於asar內部,無法讓外部程式cscript.exe取得內容。我的解決方法是程式內部讀取adodb.js文件,再進行轉存一份script.js到公開目錄下,供外部程式讀取。

fork了一份專案進行修改: https://github.com/lclee3390/node-adodb

npm install:
npm install --save https://github.com/lclee3390/node-adodb.git

[Nodejs] Electron建立可執行檔


一、Electron


可使用html作為GUI介面的設計,進行配置後也可支援Angular、React、Vue相應的環境,再透過electron packager 可以打包成exe,或者,使用electron builder建置安裝檔。

若是使用Angular,可直接clone angular-electron這個倉庫,裡面已將各版本的環境都建置模板了。

又或者,想要建置一個非常基礎的應用程式,clone 官方的quick start來進行修改會快速很多,但此專案並不包含electron packager,需另外進行配置。


二、Electron Packager


npm install electron-packager --save-dev

  • 在package.json中的script加入指令

"build" :  "electron-packager ./ appname --out ./dist --overwrite"


三、Electron Builder


npm install electron-builder --save-dev



  • 配置package.json


"build": {
    "appId": "com.xxx.app",
    "mac": {
      "target": ["dmg","zip"]
    },
    "win": {
      "target": ["nsis","zip"]
    }
},
"scripts": {
     "builder": "electron-builder --win --x64"
}


2019年6月7日 星期五

[Nodejs] node-adodb 讀取.mdb檔案

  • https://www.npmjs.org/package/node-adodb
  • Simple Query:

const ADODB = require('node-adodb');
const connection = ADODB.open('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=node-adodb.mdb;');

connection
  .query('SELECT * FROM TABLE_NAME') // query method return Promise
  .then(data => {
    console.log(JSON.stringify(data, null, 2));
  })
  .catch(error => {
    console.error(error);
  });

  • 依據Access版本不同,在ADODB.open時指定不同的連接字串
    • Access 2000-2003 (*.mdb): 
      • Provider=Microsoft.Jet.OLEDB.4.0;Data Source=node-adodb.mdb;
    • Access > 2007 (*.accdb): 
      • Provider=Microsoft.ACE.OLEDB.12.0;Data Source=adodb.accdb;Persist Security Info=False; 
      • Provider=Microsoft.ACE.OLEDB.15.0;Data Source=adodb.accdb;Persist Security Info=False;