sezna / nps

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json
MIT License
1.43k stars 93 forks source link

[Feature Req.] Typescript support? #217

Open jrmyio opened 4 years ago

jrmyio commented 4 years ago

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

sezna commented 4 years ago

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

jrmyio commented 4 years ago

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).

carddamom commented 4 years ago

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;
}
kirkstrobeck commented 2 years ago

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