02.Packaging

igorklopov edited this page Apr 6, 2015 · 9 revisions
Clone this wiki locally

EncloseJS packaging rules dont break your code, and preserve the ability to run the project without EncloseJS (in node environment).

Along with input javascript file (entry file of your project) you can include other files into the package, using several ways. "Include file into the package" means that the file is not needed to be on disk after compilation. It is placed into EncloseJS internal (in-package) file system, and accessible from code inside the package.

Types of files

EncloseJS distinguish:

  • Files with javascript code. They are compiled to machine code when included into package. EncloseJS implicitly includes such a file when require('script') is encountered in your code. 'script' must be a literal (not an expression) to make 'script' included. 'script' can be a node package name. So, that package main file will be included with it's further dependencies.

  • Files with raw content. They are included as is. EncloseJS implicitly includes such a file when path.join(__dirname, 'asset') is encountered in your code. 'asset' must be a literal (not an expression) to make 'asset' included. So, you are encouraged to use the multi-purpose expression fs.readFileSync(path.join(__dirname, 'asset')) to properly run in node environment, to store the file into the package, and to read it from there.

  • Directory listing. Included if you need to call fs.readdirSync from inside the package (for example to enumerate plugins, that are already included as javascript files). There is no way to include directory listing implicitly. See explicit packaging.

Explicit packaging

Explicit packaging means that you specify the files to include manually, using a config file: example06, example07

module.exports = {
  scripts: "./views/*.js",
  assets: "./templates/*.*",
  dirs: [ "./views",
          "./templates" ]
};

To include files with javascript code, scripts property of config is used. To include files with raw content, assets is used. To store directory listings, dirs is used. ./views/*.js is a glob - a mask to describe several files. Please refer to node-glob and globby docs.

List of included files

Use --loglevel info command line option to see what files were included.

Accessing files stored in package and on disk

In order to access files, enclosed inside the executable, you should use __dirname as a base of your path calculations. For example path.join(__dirname, './usage.txt') or path.join(__dirname, 'lib', 'plugins', name). You should NOT use __dirname to access files of real on-disk file system. In that case use process.cwd() and path.dirname(process.argv[1]). If you feel you need to access something via __dirname - probably you should package it as an asset. So, accessing it with __dirname becomes legal.

Notes

Only fs.statSync, fs.existsSync, fs.readFileSync, fs.readdirSync and fs.createReadStream can be used to access in-package files.