nps icon indicating copy to clipboard operation
nps copied to clipboard

[Feature Req.] Typescript support?

Open jrmyio opened this issue 4 years ago • 4 comments

Are there any plans to support Typescript so one can use package-scripts.ts instead of package-scripts.js?

jrmyio avatar Feb 05 '20 09:02 jrmyio

Not currently,although it wouldn't be impossible. What advantages would you say having a typed package-scripts file has?

sezna avatar Mar 02 '20 15:03 sezna

It makes it possible to import typed utility functions written in ts that abstract the generation of the configuration.

Also it would allow to write modern javascript without the hassle of using requires, module.export etc. (using babel would do the same more or less).

jrmyio avatar Mar 02 '20 21:03 jrmyio

I'm also looking for this, and as a shortcut I have installed ts-node and used the following:

nps --config ./package-scripts.ts --require ts-node/register ${script_name}

To run your scripts using typescript...

If you use nps_utils.concurrent.nps or nps_utils.series.nps you may need to replace them by:

function concurrentNPS(...scriptNames: Array<string>): string {
  return npsUtils.concurrent(
    scriptNames.map(mapNPSScripts).reduce(reduceNPSScripts, {}),
  );

  function mapNPSScripts(scriptName: boolean | string | any): any {
    if (!Boolean(scriptName)) {
      return undefined;
    } else if (typeof scriptName === 'string') {
      return { script: scriptName};
    } else {
      return scriptName;
    }
  }

  function quoteScript(script: string, escaped?: string): string {
    const quote = escaped ? '\\"' : '"';
    const shouldQuote = script.indexOf(' ') !== -1;
    return shouldQuote ? `${quote}${script}${quote}` : script;
  }

  function reduceNPSScripts(scripts: any, scriptObj: any): string {
    if (!scriptObj) {
      return scripts;
    }
    const { color, script } = scriptObj;
    const [name] = script.split(' ');
    scripts[name] = {
      script: `nps --config ./package-scripts.ts --require ts-node/register ${quoteScript(script.trim())}`,
      color,
    };
    return scripts;
  }
};

For nps_utils.concurrent.nps and:

function seriesNPS(...scriptNames: Array<string>): string {
  function quoteScript(script: string, escaped?: string): string {
    const quote = escaped ? '\\"' : '"';
    const shouldQuote = script.indexOf(' ') !== -1;
    return shouldQuote ? `${quote}${script}${quote}` : script;
  }

  return npsUtils.series(
    ...scriptNames
      .filter(Boolean)
      .map(scriptName => scriptName.trim())
      .filter(Boolean)
      .map(scriptName => `nps --config ./package-scripts.ts --require ts-node/register {quoteScript(scriptName)}`),
  );
}; 

For nps_utils.series.nps.

Where the functions are similarly the same, but use nps --config ./package-scripts.ts --require ts-node/register instead of just nps

Also the definition for the package_script file is:

interface Scripts {
  [index: string]: string | {
    script: string;
    description?: string;
    hiddenFromHelp?: boolean;
  } | Scripts;
}

interface PackageScripts {
  scripts: Scripts;
}

carddamom avatar Apr 22 '20 12:04 carddamom

Not currently,although it wouldn't be impossible. What advantages would you say having a typed package-scripts file has?

Type safety and all other things TS has to offer

kirkstrobeck avatar Oct 01 '22 19:10 kirkstrobeck