From 50c879c3c66e49d7d82aa167e9ebe7fb7f4373c8 Mon Sep 17 00:00:00 2001 From: Dave Bouwman Date: Sun, 11 Feb 2018 12:36:26 -0700 Subject: [PATCH] docs(ago node-cli): add node-cli demo to search ago AFFECTS PACKAGES: node-cli --- demos/ago-node-cli/.gitignore | 0 demos/ago-node-cli/README.md | 29 +++ demos/ago-node-cli/ago.js | 32 ++++ demos/ago-node-cli/index.js | 11 ++ demos/ago-node-cli/lib/item-export-command.js | 48 +++++ demos/ago-node-cli/lib/item-search-command.js | 35 ++++ demos/ago-node-cli/package-lock.json | 172 ++++++++++++++++++ demos/ago-node-cli/package.json | 30 +++ 8 files changed, 357 insertions(+) create mode 100644 demos/ago-node-cli/.gitignore create mode 100644 demos/ago-node-cli/README.md create mode 100755 demos/ago-node-cli/ago.js create mode 100644 demos/ago-node-cli/index.js create mode 100644 demos/ago-node-cli/lib/item-export-command.js create mode 100644 demos/ago-node-cli/lib/item-search-command.js create mode 100644 demos/ago-node-cli/package-lock.json create mode 100644 demos/ago-node-cli/package.json diff --git a/demos/ago-node-cli/.gitignore b/demos/ago-node-cli/.gitignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/demos/ago-node-cli/README.md b/demos/ago-node-cli/README.md new file mode 100644 index 0000000000..efde3d7ff1 --- /dev/null +++ b/demos/ago-node-cli/README.md @@ -0,0 +1,29 @@ +# Node CLI Examples + +This demo shows how to create a command-line application that interacts with the ArcGIS Online API. + +At this point, the commands are very simple and intended to demonstrate how to build up tooling. + +This project uses the `commander` module, which streamlines the creation of node cli applications. Check out the [README](https://github.com/tj/commander.js/blob/master/Readme.md) for more details. + +### Installing + +Like all the other demo apps, run `npm run bootstrap` from the root. + +### Running +If you use this demo as a starting point for your own command line package, you would publish it to npm, then on the target systems run `npm install `, and it would be available as a command. + +But, this is demo code, and thus the package is not "installed" via `npm install ...`, before we can call it as `ago ` we need to run `npm link` in the `/demos/node-cli` folder. After you do that, the command should work. + +Here is a post with information on creating node command line tools: [A Guide to Creating a NodeJs Command](https://x-team.com/blog/a-guide-to-creating-a-nodejs-command/) + +### Commands + +| command | description | example | +| --- | --- | --- | +| `ago search ` | search for items | `ago search water` | +| `ago export ` | export an item to a json file | `ago export a62cb9d894f145cc89531c096d0012b1 pa.json` | + +## Building your own tooling + +If you want to build out some commands of your own, you can also clone a stand-alone version of this repo from https://github.com/dbouwman/arcgis-rest-js-commands diff --git a/demos/ago-node-cli/ago.js b/demos/ago-node-cli/ago.js new file mode 100755 index 0000000000..86637d21fe --- /dev/null +++ b/demos/ago-node-cli/ago.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node +require('isomorphic-fetch'); +require('isomorphic-form-data'); +/** + * This demo uses the commander module, which streamlines the creation of command-line-applications + */ +const program = require('commander'); + +program + .version('0.0.1'); + +/** + * Bring in the search command... + */ +const itemSearchCommand = require('./lib/item-search-command'); + +program + .command('search ') + .description('Search for items') + .action(function (query) { + itemSearchCommand.execute(query); + }); + + const itemExportCommand = require('./lib/item-export-command'); + program + .command('export ') + .description('Export an item to a json file') + .action(function (id, filename) { + itemExportCommand.execute(id, filename); + }); + +program.parse(process.argv); \ No newline at end of file diff --git a/demos/ago-node-cli/index.js b/demos/ago-node-cli/index.js new file mode 100644 index 0000000000..940ac79508 --- /dev/null +++ b/demos/ago-node-cli/index.js @@ -0,0 +1,11 @@ +/** + * Re-export the commands so they can be used in other contexts + */ +const itemSearchCommand = require('./lib/item-search-command'); +const itemExportCommand = require('./lib/item-export-command'); + +// just re-export the commands so they can be run in another scropt +module.exports = { + search: itemSearchCommand, + export: itemExportCommand +}; diff --git a/demos/ago-node-cli/lib/item-export-command.js b/demos/ago-node-cli/lib/item-export-command.js new file mode 100644 index 0000000000..c0cde38ce7 --- /dev/null +++ b/demos/ago-node-cli/lib/item-export-command.js @@ -0,0 +1,48 @@ +const { getItem, getItemData } = require("@esri/arcgis-rest-items"); +const jsonfile = require('jsonfile'); + +module.exports = { + /** + * Execute the command + */ + execute: function (id, fileName) { + console.info(`Exporting item ${id} to file ${fileName}`); + // construct the search call.. + let model = {}; + return getItem(id) + .then((item) => { + model.item = item; + if (this.shouldFetchData(item.type)){ + console.info(`...Fetching ./data for item of type ${item.type}`); + return getItemData(id); + } else { + console.info(`...Items of type ${item.type} do not have json ./data - skipping`); + return Promise.resolve(); + } + }) + .then((maybeData) => { + if (maybeData) { + model.data = maybeData; + } + // now write out the file... + jsonfile.writeFileSync(fileName, model, {spaces: 2}); + console.info(`Done. Exported "${model.item.title}" to ${fileName}`); + }) + .catch((err) => { + console.error(err); + }); + }, + + /** + * Not all item types have json /data + */ + shouldFetchData (type) { + let result = false; + let exportables = ['Web Map', 'Web Mapping Application', 'Hub Page', 'Hub Initiative', 'Hub Site Application']; + // if the type is in this list, we can export it... + if (exportables.indexOf(type) > -1) { + result = true; + } + return result; + } +} \ No newline at end of file diff --git a/demos/ago-node-cli/lib/item-search-command.js b/demos/ago-node-cli/lib/item-search-command.js new file mode 100644 index 0000000000..8b598c0a5c --- /dev/null +++ b/demos/ago-node-cli/lib/item-search-command.js @@ -0,0 +1,35 @@ + +/** + * Bring in searchItems fn + */ +const { searchItems } = require("@esri/arcgis-rest-items"); + +module.exports = { + /** + * Execute the command + */ + execute: function (query) { + // construct the search call.. + return searchItems({ + searchForm: { + q: query, + start: 1, + num: 10 + } + }) + .then((response) => { + // if we got results + if (Array.isArray(response.results) && response.results.length) { + console.info(`${response.total} items found for "${query}".`); + response.results.forEach((entry) => { + console.info(`${entry.id} | ${entry.title}`); + }) + } else { + console.info(`No results found for "${query}".`); + } + }) + .catch((err) => { + console.error(err); + }); + } +} \ No newline at end of file diff --git a/demos/ago-node-cli/package-lock.json b/demos/ago-node-cli/package-lock.json new file mode 100644 index 0000000000..5365708b33 --- /dev/null +++ b/demos/ago-node-cli/package-lock.json @@ -0,0 +1,172 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "ansi-styles": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.0.tgz", + "integrity": "sha512-NnSOmMEYtVR2JVMIGTzynRkkaxtiq1xnFBcdQD/DnNCYPoEPsVJhM98BDyaoNOQIi7p4okdi3E27eN7GQbsUug==", + "requires": { + "color-convert": "1.9.1" + } + }, + "async": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.0.tgz", + "integrity": "sha512-xAfGg1/NTLBBKlHFmnd7PlmUW9KhVQIUuSrYem9xzFUZy13ScvtyGGejaae9iAVRiRq9+Cx7DPFaAAhCpyxyPw==", + "requires": { + "lodash": "4.17.5" + } + }, + "chalk": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.3.1.tgz", + "integrity": "sha512-QUU4ofkDoMIVO7hcx1iPTISs88wsO8jA92RQIm4JAwZvFGGAV2hSAA1NX7oVj2Ej2Q6NDTcRDjPTFrMCRZoJ6g==", + "requires": { + "ansi-styles": "3.2.0", + "escape-string-regexp": "1.0.5", + "supports-color": "5.2.0" + } + }, + "color-convert": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.1.tgz", + "integrity": "sha512-mjGanIiwQJskCC18rPR6OmrZ6fm2Lc7PeGFYwCmy5J34wC6F1PzdGL6xeMfmgicfYcNLGuVFA3WzXtIDCQSZxQ==", + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + }, + "combined-stream": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz", + "integrity": "sha1-cj599ugBrFYTETp+RFqbactjKBg=", + "requires": { + "delayed-stream": "1.0.0" + } + }, + "commander": { + "version": "2.14.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.14.1.tgz", + "integrity": "sha512-+YR16o3rK53SmWHU3rEM3tPAh2rwb1yPcQX5irVn7mb0gXbwuCCrnkbV5+PBfETdfg1vui07nM6PCG1zndcjQw==" + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, + "encoding": { + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", + "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=", + "requires": { + "iconv-lite": "0.4.19" + } + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + }, + "form-data": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-1.0.1.tgz", + "integrity": "sha1-rjFduaSQf6BlUCMEpm13M0de43w=", + "requires": { + "async": "2.6.0", + "combined-stream": "1.0.6", + "mime-types": "2.1.17" + } + }, + "graceful-fs": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", + "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=", + "optional": true + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" + }, + "iconv-lite": { + "version": "0.4.19", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz", + "integrity": "sha512-oTZqweIP51xaGPI4uPa56/Pri/480R+mo7SeU+YETByQNhDG55ycFyNLIgta9vXhILrxXDmF7ZGhqZIcuN0gJQ==" + }, + "is-stream": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" + }, + "isomorphic-fetch": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz", + "integrity": "sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=", + "requires": { + "node-fetch": "1.7.3", + "whatwg-fetch": "2.0.3" + } + }, + "isomorphic-form-data": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isomorphic-form-data/-/isomorphic-form-data-1.0.0.tgz", + "integrity": "sha1-BEe+fg9rht7z05MDbPdmSpiRkQA=", + "requires": { + "form-data": "1.0.1" + } + }, + "jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", + "requires": { + "graceful-fs": "4.1.11" + } + }, + "lodash": { + "version": "4.17.5", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.5.tgz", + "integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw==" + }, + "mime-db": { + "version": "1.30.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.30.0.tgz", + "integrity": "sha1-dMZD2i3Z1qRTmZY0ZbJtXKfXHwE=" + }, + "mime-types": { + "version": "2.1.17", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.17.tgz", + "integrity": "sha1-Cdejk/A+mVp5+K+Fe3Cp4KsWVXo=", + "requires": { + "mime-db": "1.30.0" + } + }, + "node-fetch": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz", + "integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==", + "requires": { + "encoding": "0.1.12", + "is-stream": "1.1.0" + } + }, + "supports-color": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.2.0.tgz", + "integrity": "sha512-F39vS48la4YvTZUPVeTqsjsFNrvcMwrV3RLZINsmHo+7djCvuUzSIeXOnZ5hmjef4bajL1dNccN+tg5XAliO5Q==", + "requires": { + "has-flag": "3.0.0" + } + }, + "whatwg-fetch": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz", + "integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ=" + } + } +} diff --git a/demos/ago-node-cli/package.json b/demos/ago-node-cli/package.json new file mode 100644 index 0000000000..18b0490afe --- /dev/null +++ b/demos/ago-node-cli/package.json @@ -0,0 +1,30 @@ +{ + "name": "node-cli", + "version": "1.0.2", + "description": "arcgis-rest-js node command-line item search example", + "main": "ago.js", + "scripts": { + "start": "echo \"CLI - node index.js \" && exit 1" + }, + "bin": { + "ago":"./ago.js" + }, + "private": true, + "keywords": [ + "arcgis", + "node", + "arcgis-rest-js" + ], + "author": "Dave Bouwman ", + "license": "Apache-2.0", + "dependencies": { + "@esri/arcgis-rest-auth": "^1.0.2", + "@esri/arcgis-rest-request": "^1.0.2", + "@esri/arcgis-rest-items": "^1.0.2", + "isomorphic-fetch": "^2.2.1", + "isomorphic-form-data": "^1.0.0", + "chalk": "^2.3.0", + "jsonfile": "^4.0.0", + "commander": "^2.12.2" + } +}