Difference between revisions of "Zblack npm integration"
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
| + | {{Short description|Integration of Node.js and npm libraries in ZAP and Zblack environments}} | ||
| + | {{Infobox software | ||
| + | | name = ZAP and Zblack | ||
| + | | developer = ZCubes, Inc. | ||
| + | | operating_system = Cross-platform | ||
| + | | genre = Scripting environment | ||
| + | | website = [http://www.zcubes.com ZCubes.com] | ||
| + | }} | ||
| + | |||
=ZAP and Zblack: Local npm Library Integration= | =ZAP and Zblack: Local npm Library Integration= | ||
| − | This article demonstrates how ZAP and Zblack enable seamless use of Node.js, npm libraries, and advanced file system/database operations. The workflow allows | + | This article demonstrates how ZAP and Zblack enable seamless use of Node.js, npm libraries, and advanced file system/database operations. The workflow allows users to save code, install npm packages, and run scripts interactively, leveraging the full power of server-side JavaScript in the ZAP/Zblack environment. |
==ZAP: Using Node.js and npm Libraries Overview== | ==ZAP: Using Node.js and npm Libraries Overview== | ||
| − | On ZAP, Node.js is embedded, allowing | + | On ZAP, Node.js is embedded, allowing users to: |
| − | + | * Save code files to a folder. | |
| − | Save code files to a folder. | + | * Use <code>npm install</code> to add external npm libraries to that folder. |
| − | + | * <code>require</code> and use those libraries in Node.js scripts. | |
| − | Use npm install to add external npm libraries to that folder. | + | * Access the full file system, databases, and more (unlike browser JavaScript). |
| − | |||
| − | require and use those libraries in | ||
| − | |||
| − | Access the full file system, databases, and more (unlike browser | ||
==Step 1: Create a .z3 Document and Install npm Libraries== | ==Step 1: Create a .z3 Document and Install npm Libraries== | ||
| − | Open ZAP | + | Open ZAP and create a new document. Save this document with the <code>.z3</code> extension in a directory of your choice (for example, <code>test.z3</code>). |
| − | + | [[File:Temp 1.png|thumb]] | |
Next, open a terminal in that same directory. For each npm package you want to use, run: | Next, open a terminal in that same directory. For each npm package you want to use, run: | ||
| − | + | <pre>npm install <library-name></pre> | |
| − | <pre> npm install <library-name> </pre> | + | This command will create a <code>node_modules</code> folder in your directory and install the specified libraries. For example, to install the underscore library, use: |
| − | This command will create a node_modules folder in your directory and install the specified libraries. For example, to install the underscore library, use: | + | <pre>npm install underscore</pre> |
| − | + | Your directory will now contain your <code>.z3</code> file and a <code>node_modules</code> folder with the installed npm packages, ready for use in your Zblack project. | |
| − | <pre> npm install underscore </pre> | ||
| − | Your directory will now contain your .z3 file and a node_modules folder with the installed npm packages, ready for use in your Zblack project. | ||
==Step 2: Write Code in the .z3 File Using Installed npm Libraries== | ==Step 2: Write Code in the .z3 File Using Installed npm Libraries== | ||
| + | [[File:Zcubes Doc.png|thumb]] | ||
| + | Open your <code>.z3</code> file in the ZAP Document editor. | ||
| − | + | Use <code>require()</code> to import any installed npm libraries and write your Node.js code as needed. | |
| − | |||
| − | Use require() to import any installed npm libraries and write your Node.js code as needed. | ||
==Step 3: Install Zblack and Set the Path Variable== | ==Step 3: Install Zblack and Set the Path Variable== | ||
| − | Download and install Zblack from | + | Download and install Zblack from [http://downloads.zcubes.com Zblack]. |
Add the Zblack executable to your system’s PATH environment variable so you can run it from any terminal window. | Add the Zblack executable to your system’s PATH environment variable so you can run it from any terminal window. | ||
| Line 41: | Line 44: | ||
==Step 4: Run .z3 Files Using Zblack== | ==Step 4: Run .z3 Files Using Zblack== | ||
| − | In your terminal, navigate to the directory containing your .z3 file. | + | In your terminal, navigate to the directory containing your <code>.z3</code> file. |
Run your script with the command: | Run your script with the command: | ||
| − | + | <pre>zblack yourfile.z3</pre> | |
| − | <pre> | + | To keep the process interactive, add the <code>-i</code> flag: |
| − | zblack yourfile.z3 | + | <pre>zblack -i yourfile.z3</pre> |
| − | </pre> | ||
| − | To keep the process interactive, add the -i flag: | ||
| − | |||
| − | |||
| − | zblack -i yourfile.z3 | ||
This process enables you to develop and execute JavaScript scripts with full npm support in the Zblack environment. | This process enables you to develop and execute JavaScript scripts with full npm support in the Zblack environment. | ||
==How It Works== | ==How It Works== | ||
| − | + | * '''Local npm packages''': Any npm package can be installed locally to your project and required in your scripts. This avoids global installs and makes it easy to copy all files under a folder to a different machine for deployment. | |
| − | Local npm packages: | + | * '''Server-side features''': Unlike browser JavaScript, you can use Node.js modules like <code>fs</code> for file/database access. |
| − | + | * '''Zblack execution''': Save your code as <code>.z3</code> files and run with <code>zblack -i yourfile.z3</code> for an interactive session. | |
| − | + | * '''Flexible scripting''': Switch between libraries (e.g., <code>lodash</code>, <code>underscore</code>, <code>csv-parse</code>, <code>chance</code>) as needed for your workflow. | |
| − | |||
| − | This | ||
| − | |||
| − | Server-side features: Unlike browser JavaScript, you can use Node.js modules like fs for file/database access. | ||
| − | |||
| − | Zblack execution: Save your code as .z3 files and run with zblack -i yourfile.z3 for an interactive session. | ||
| − | |||
| − | Flexible scripting: Switch between libraries (lodash, underscore, csv-parse, chance | ||
==Examples== | ==Examples== | ||
| − | Example 1: | + | '''Example 1:''' |
[[File:Temp 3.png|thumb]] | [[File:Temp 3.png|thumb]] | ||
| − | < | + | <syntaxhighlight lang="javascript"> |
const _ = require('lodash'); | const _ = require('lodash'); | ||
console.log(_.shuffle([1,2,3,4,5])); | console.log(_.shuffle([1,2,3,4,5])); | ||
| − | </ | + | </syntaxhighlight> |
| − | Example 2: | + | '''Example 2:''' |
[[File:Temp 5.png|thumb]] | [[File:Temp 5.png|thumb]] | ||
| − | < | + | <syntaxhighlight lang="javascript"> |
const _ = require('underscore'); | const _ = require('underscore'); | ||
| Line 96: | Line 86: | ||
console.log('Grouped and Shuffled:', groups); | console.log('Grouped and Shuffled:', groups); | ||
| − | </ | + | </syntaxhighlight> |
| − | Example 3: | + | '''Example 3:''' |
[[File:Temp 7.png|thumb]] | [[File:Temp 7.png|thumb]] | ||
| − | < | + | <syntaxhighlight lang="javascript"> |
const Chance = require('chance'); | const Chance = require('chance'); | ||
const chance = new Chance(); | const chance = new Chance(); | ||
| Line 112: | Line 102: | ||
console.log('Random Names:', names); | console.log('Random Names:', names); | ||
console.log('Shuffled Names:', shuffled); | console.log('Shuffled Names:', shuffled); | ||
| − | </ | + | </syntaxhighlight> |
| − | Example 4: | + | '''Example 4:''' |
[[File:Temp 10.png|thumb]] | [[File:Temp 10.png|thumb]] | ||
[[File:Temp 9.png|thumb]] | [[File:Temp 9.png|thumb]] | ||
| − | < | + | <syntaxhighlight lang="javascript"> |
const fs = require('fs'); | const fs = require('fs'); | ||
const { parse } = require('csv-parse'); | const { parse } = require('csv-parse'); | ||
| Line 132: | Line 122: | ||
console.error('Error:', err.message); | console.error('Error:', err.message); | ||
}); | }); | ||
| + | </syntaxhighlight> | ||
| − | + | ==See also== | |
| − | + | * [[ZAP]] | |
| − | ==See | + | * [[Zblack Example of Omniglot]] |
| − | *[[ZAP]] | ||
| − | *[[Zblack Example of Omniglot]] | ||
Latest revision as of 15:15, 9 June 2025
Template:Short description Template:Infobox
ZAP and Zblack: Local npm Library Integration
This article demonstrates how ZAP and Zblack enable seamless use of Node.js, npm libraries, and advanced file system/database operations. The workflow allows users to save code, install npm packages, and run scripts interactively, leveraging the full power of server-side JavaScript in the ZAP/Zblack environment.
ZAP: Using Node.js and npm Libraries Overview
On ZAP, Node.js is embedded, allowing users to:
- Save code files to a folder.
- Use
npm installto add external npm libraries to that folder. requireand use those libraries in Node.js scripts.- Access the full file system, databases, and more (unlike browser JavaScript).
Step 1: Create a .z3 Document and Install npm Libraries
Open ZAP and create a new document. Save this document with the .z3 extension in a directory of your choice (for example, test.z3).
Next, open a terminal in that same directory. For each npm package you want to use, run:
npm install <library-name>
This command will create a node_modules folder in your directory and install the specified libraries. For example, to install the underscore library, use:
npm install underscore
Your directory will now contain your .z3 file and a node_modules folder with the installed npm packages, ready for use in your Zblack project.
Step 2: Write Code in the .z3 File Using Installed npm Libraries
Open your .z3 file in the ZAP Document editor.
Use require() to import any installed npm libraries and write your Node.js code as needed.
Step 3: Install Zblack and Set the Path Variable
Download and install Zblack from Zblack.
Add the Zblack executable to your system’s PATH environment variable so you can run it from any terminal window.
Step 4: Run .z3 Files Using Zblack
In your terminal, navigate to the directory containing your .z3 file.
Run your script with the command:
zblack yourfile.z3
To keep the process interactive, add the -i flag:
zblack -i yourfile.z3
This process enables you to develop and execute JavaScript scripts with full npm support in the Zblack environment.
How It Works
- Local npm packages: Any npm package can be installed locally to your project and required in your scripts. This avoids global installs and makes it easy to copy all files under a folder to a different machine for deployment.
- Server-side features: Unlike browser JavaScript, you can use Node.js modules like
fsfor file/database access. - Zblack execution: Save your code as
.z3files and run withzblack -i yourfile.z3for an interactive session. - Flexible scripting: Switch between libraries (e.g.,
lodash,underscore,csv-parse,chance) as needed for your workflow.
Examples
Example 1:
const _ = require('lodash');
console.log(_.shuffle([1,2,3,4,5]));
Example 2:
const _ = require('underscore');
let people = [
{ name: 'Alice', group: 'A' },
{ name: 'Bob', group: 'B' },
{ name: 'Carol', group: 'A' },
{ name: 'Dave', group: 'B' },
{ name: 'Eve', group: 'A' }
];
let groups = _.groupBy(people, 'group');
for (let key in groups) {
groups[key] = _.shuffle(groups[key]);
}
console.log('Grouped and Shuffled:', groups);
Example 3:
const Chance = require('chance');
const chance = new Chance();
// Generate an array of 5 random names
let names = Array.from({ length: 5 }, () => chance.name());
// Shuffle the names
let shuffled = chance.shuffle(names);
console.log('Random Names:', names);
console.log('Shuffled Names:', shuffled);
Example 4:
const fs = require('fs');
const { parse } = require('csv-parse');
fs.createReadStream('sample.csv')
.pipe(parse({ columns: true, skip_empty_lines: true }))
.on('data', (row) => {
console.log(row);
})
.on('end', () => {
console.log('Finished parsing the file');
})
.on('error', (err) => {
console.error('Error:', err.message);
});