joshfarrant/shortcuts-js

Development Requirement

Closed this issue ยท 6 comments

I'm not an Apple guy other than Ipad and phone. What are you guys using for development - computer models and software? I've messed around with node.js - can this stuff be done on a windows machine?
How would the shortcut get back to the Ipad- phone.
Thanks

Hi @avery12, a Mac is not required to build Shortcuts with Shortcuts JS. You can try to reinstall NodeJS from here if your current installation has some issues.

To start using this module, create a folder on your Windows PC, open Terminal in that folder and write

npm install @joshfarrant/shortcuts-js

This will create a /node_modules subfolder. In the main folder, create an index.js file with this inside:

// Only used to create the .shortcut file
const fs = require('fs');

const {
  actionOutput,
  buildShortcut,
  withVariables,
} = require('@joshfarrant/shortcuts-js');
const {
  calculate,
  comment,
  number,
  showResult
} = require('@joshfarrant/shortcuts-js/actions');

// We'll use this later to reference the output of a calculation
const calcVar = actionOutput();

// Define our list of actions
const actions = [
  comment({
    text: 'Hello, world!',
  }),
  number({
    number: 42,
  }),
  calculate({
    operand: 3,
    operation: '/',
  }, calcVar),
  showResult({
    /**
     * We can use the result of the calculation in this Shortcuts's input
     * by passing the string to the 'withVariables' tag function
     */
    text: withVariables`Total is ${calcVar}!`,
  }),
];

// Generate the Shortcut data
const shortcut = buildShortcut(actions);

// Write the Shortcut to a file in the current directory
fs.writeFile('My Fancy Shortcut.shortcut', shortcut, (err) => {
  if (err) {
    console.error('Something went wrong :(', err);
    return;
  }
  console.log('Shortcut created!');
});

In Terminal write:

node index.js

This will execute the JS file and will create a Shortcut named "My Fancy Shortcut". To install it on your iOS devices you can send it via mail, or Telegram, or anything else. Once opened in Files, you can choose to open it in Shortcuts app.

To see more examples, read the Further Examples section in the README.

If you have any other related issue, feel free to ask.

I'm getting going on development, as well. To understand: doesn't npm install @joshfarrant/shortcuts-js install the module from the global NPM repository? Or, since in this case one would be sitting in the root of the cloned repo, does the local code get installed? Would I want to install the local module so that, as I make changes, I can test them immediately?

(sorry for 3 questions in a row ๐Ÿ˜†)

Doesn't npm install @joshfarrant/shortcuts-js install the module from the global NPM repository?

That's right - an npm install will always pull the latest published version (which could be a bit behind master) from npm.

It is possible to 'install' a package from a directory on your machine using npm link, although I've always found this to be a pain and generally avoid it.

The way I've been testing throughout development is by just adding a file named playground.js to the root of the cloned repo. playground.js is included in the .gitignore so it's never pushed to GitHub. You can then use that file to import your modified version of shortcuts-js and test with it. So all you need to do is to create a playground.js file in the root of the project and write something like:

const fs = require('fs');

const {
  buildShortcut,
} = require('./build');
const {
  comment,
} = require('./build/actions');

const actions = [
  comment({
    text: 'Hello, world!',
  }),
];

// Generate the Shortcut data
const shortcut = buildShortcut(actions);

// Write the Shortcut to a file in the current directory
fs.writeFile('My Fancy Shortcut.shortcut', shortcut, (err) => {
  if (err) {
    console.error('Something went wrong :(', err);
    return;
  }
  console.log('Shortcut created!');
});

You can then run node playground.js to run the file and test the modifications you've made.

Just remember, when you're making modifications to the project it's best to do so whilst you've got npm run build:watch running in a terminal. That will make sure that any changes you make are built to the build directory. This means that playground.js will always run the latest version of your code.

Hope that makes sense!

@joshfarrant Makes total sense; glad to see my ad-hoc learning wasn't way off base. Really appreciate you taking the time to help me get up to speed.

@bachya No problem at all - more than happy to help!

I'm going to close this for now, feel free to add any further questions and I'll reopen ๐Ÿ‘