Azure / static-web-apps-cli

Azure Static Web Apps CLI ✨
https://aka.ms/swa/cli-local-development
MIT License
585 stars 112 forks source link

not able to use swa cli as node dependencies in typescript project #691

Open DariuszPorowski opened 1 year ago

DariuszPorowski commented 1 year ago

Before filing this issue, please ensure you're using the latest CLI by running swa --version and comparing to the latest version on npm.

1.1.1

Are you accessing the CLI from the default port :4280 ?

Make sure you are accessing the URL printed in the console when running swa start!

ℹ️ NOTE: Make sure to enable debug logs when running any swa commands using --verbose=silly

Describe the bug I'm trying to use swa cli programmable in typescript project, but @azure/static-web-apps-cli package itself does not export/expose all types.

image

To Reproduce Steps to reproduce the behavior:

  1. Add @azure/static-web-apps-cli as project dependencies
  2. Import config/deploy from the package, e.g.:
import { DEFAULT_CONFIG as swaConfig } from '@azure/static-web-apps-cli/dist/config.js'
import { deploy as swaDeploy } from '@azure/static-web-apps-cli/dist/cli/commands/deploy/index.js'
  1. Assign some properties to config, and run deploy e.g.:
    swaConfig.appLocation = 'myapp'
    swaConfig.deploymentToken = 'xxxxxx...'
    swaConfig.verbose = 'silly'
    await swaDeploy(swaConfig)
  1. Build the project tsc
  2. See error:
> tsc

node_modules/@azure/static-web-apps-cli/dist/cli/commands/deploy/deploy.d.ts:1:41 - error TS2304: Cannot find name 'SWACLIConfig'.

1 export declare function deploy(options: SWACLIConfig): Promise<void>;
                                          ~~~~~~~~~~~~

node_modules/@azure/static-web-apps-cli/dist/config.d.ts:1:38 - error TS2304: Cannot find name 'SWACLIConfig'.

1 export declare const DEFAULT_CONFIG: SWACLIConfig;
                                       ~~~~~~~~~~~~

Found 2 errors in 2 files.

Errors  Files
     1  node_modules/@azure/static-web-apps-cli/dist/cli/commands/deploy/deploy.d.ts:1
     1  node_modules/@azure/static-web-apps-cli/dist/config.d.ts:1

Expected behavior swa cli used in a typescript project as a dependencies package

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context After exploring content of node_modules/@azure/static-web-apps-cli/dist, I saw not all types have been exported in distribution, lack of types defined here: https://github.com/Azure/static-web-apps-cli/blob/main/src/swa.d.ts

khill-fbmc commented 9 months ago

I would like to revive this as I am trying to do something similar.

I would like to be able to get the deploy token during a CI build, but even though there is a way to do it

swa deploy --print-token

it produces a lot of unnecessary output.

Welcome to Azure Static Web Apps CLI (1.1.4)

Using configuration "attendance-tracker" from file:
  C:\Users\Kevin Hill\projects\Fortunaverse\attendance-app\swa-cli.config.json

Deploying front-end files from folder:
  C:\Users\Kevin Hill\projects\Fortunaverse\attendance-app\out

Deploying to environment: preview

Deployment token:
TOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKEN

I would prefer the --print-token flag to print just the token and exclude the extra output. Maybe there is a better way to get the value programmatically, but I didn't see a documented way to require/import the tool in a script and use it.

This is my little wrapper script as a work-around...

const { spawnSync } = require("node:child_process");

// Find the SWA CLI script
const binPath = require.resolve("@azure/static-web-apps-cli/dist/cli/bin.js");

// Run the script and capture the output
const { stdout } = spawnSync("node", [binPath, "deploy", "--print-token"], {
  stdio: "pipe",
});

// Remove empty lines from the output
const lines = `${stdout}`.split("\n").filter(Boolean);

// The last line is the token
const token = lines.pop();

process.stdout.write(token);

Drop that in a script and call like node .\scripts\get-azure-deploy-token.cjs to get your token printed to stdout for capturing into a variable during a CI build. 🙂