Skip to content

Commit

Permalink
docs: Move JSDoc type definitions to unified location
Browse files Browse the repository at this point in the history
pr npm#15

* docs: Move JSDoc type definitions to unified location (tats-u#1)

This moves the JSDoc type definitions to the top of the file.

* docs: Fix typos
  • Loading branch information
ExE-Boss authored and zkochan committed Mar 19, 2019
1 parent 154cdd0 commit 7d838d1
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ cmdShim.ifExists = cmdShimIfExists

/**
* @typedef {import('./index').Options} Options
*
* @typedef {object} RuntimeInfo Information of runtime and its arguments
* of the script `target`, defined in the shebang of it.
* @property {string|null} [program] If `program` is `null`, the program may
* be a binary executable and can be called from shells by just its path.
* (e.g. `.\foo.exe` in CMD or PowerShell)
* @property {string} additionalArgs Additional arguments embedded in the shebang and passed to `program`.
* `''` if nothing, unlike `program`.
*
* @callback ShimGenerator Callback functions to generate scripts for shims.
* @param {string} src Path to the executable or script.
* @param {string} to Path to the shim(s) that is going to be created.
* @param {Options} opts Options.
* @return {string} Generated script for shim.
*
* @typedef {object} ShimGenExtTuple
* @property {ShimGenerator} generator The shim generator function.
* @property {string} extension The file extension for the shim.
*/

const fs = require('mz/fs')
Expand Down Expand Up @@ -125,7 +143,7 @@ function writeShimsPreCommon (target) {
*/
function writeAllShims (src, to, srcRuntimeInfo, opts) {
opts = Object.assign({}, DEFAULT_OPTIONS, opts)
/** @type {Array<[ShimGenerator, string]>} */
/** @type {ShimGenExtTuple[]} */
const generatorAndExts = [{ generator: generateShShim, extension: '' }]
if (opts.createCmdFile) {
generatorAndExts.push({ generator: generateCmdShim, extension: '.cmd' })
Expand Down Expand Up @@ -158,17 +176,6 @@ function writeShimPost (target) {
return chmodShim(target)
}

/**
* Infomation of runtime and its arguments of the script `target`, defined in the shebang of it.
*
* @typedef {object} RuntimeInfo
* @property {string|null} [program] If `program` is `null`, the program may
* be a binary executable and can be called from shells by just its path.
* (e.g. `.\foo.exe` in CMD or PowerShell)
* @property {string} additionalArgs Additional arguments embedded in the shebang and passed to `program`.
* `''` if nothing, unlike `program`.
*/

/**
* Look into runtime (e.g. `node` & `sh` & `pwsh`) and its arguments
* of the target program (script or executable).
Expand All @@ -185,7 +192,7 @@ function searchScriptRuntime (target) {
const shebang = firstLine.match(shebangExpr)
if (!shebang) {
// If not, infer script type from its extension.
// If the inferrence fails, it's something that'll be compiled, or some other
// If the inference fails, it's something that'll be compiled, or some other
// sort of script, and just call it directly.
const targetExtension = path.extname(target).toLowerCase()
return Promise.resolve({
Expand Down Expand Up @@ -213,7 +220,7 @@ function searchScriptRuntime (target) {
*/
function writeShim (src, to, srcRuntimeInfo, generateShimScript, opts) {
const defaultArgs = opts.preserveSymlinks ? '--preserve-symlinks' : ''
// `Array.prototype.flter` removes ''.
// `Array.prototype.filter` removes ''.
// ['--foo', '--bar'].join(' ') and [].join(' ') returns '--foo --bar' and '' respectively.
const args = [srcRuntimeInfo.additionalArgs, defaultArgs].filter(arg => arg).join(' ')
opts = Object.assign({}, opts, {
Expand All @@ -226,16 +233,6 @@ function writeShim (src, to, srcRuntimeInfo, generateShimScript, opts) {
.then(() => writeShimPost(to))
}

/**
* Callback functions to generate scripts for shims.
* @callback ShimGenerator Callback functions to generate scripts for shims.
*
* @param {string} src Path to the executable or script.
* @param {string} to Path to the shim(s) that is going to be created.
* @param {Options} opts Options.
* @return {string} Generated script for shim.
*/

/**
* Generate the content of a shim for CMD.
*
Expand Down

0 comments on commit 7d838d1

Please sign in to comment.