Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Req.] Typescript support? #217

Open
jrmyio opened this issue Feb 5, 2020 · 4 comments
Open

[Feature Req.] Typescript support? #217

jrmyio opened this issue Feb 5, 2020 · 4 comments
Labels

Comments

@jrmyio
Copy link

jrmyio commented Feb 5, 2020

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

@sezna
Copy link
Owner

sezna commented Mar 2, 2020

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

@sezna sezna added the question label Mar 2, 2020
@jrmyio
Copy link
Author

jrmyio commented Mar 2, 2020

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
Copy link

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
Copy link

kirkstrobeck commented Oct 1, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants