CircleCI-Public / circleci-config-sdk-ts

Generate CircleCI Configuration YAML from JavaScript or TypeScript. Use Dynamic Configuration and the Config SDK together for live generative config.
https://circleci-public.github.io/circleci-config-sdk-ts/
Apache License 2.0
82 stars 29 forks source link

Request: Add save to file functionality to config #153

Closed timo-reymann closed 2 years ago

timo-reymann commented 2 years ago

Is there an existing issue that is already proposing this?

Is your feature request related to a problem? Please describe it

To use the SDK together with the continuation orb, you need a configuration file written to disk. This also stated in the README and getting started guide.

Saving the file to disk is something that I think a lot (if not most) of the users would find useful.

Describe the solution you'd like

Provide a way to save to disk with a method directly on the CircleCI configuration object.

The ideal solution would look like this:

const config = new CircleCI.Config();
// ... build configuration
try {
  await config.saveToDisk("fileName.yml");
} catch(e) {
  console.error("Failed to generate configuration", e);
  process.exit(1);
}

Under the hood, it just uses the fs module from Node.js to write the file to disk and wrapping the logic into a promise, making it easy to use.

Teachability, documentation, adoption, migration strategy

When the functionality is implemented, adjusting the getting started guide and the example should be sufficient.

What is the motivation / use case for changing the behavior?

Currently, it is required to copy & paste the logic block to all projects where dynamic pipelines are used, or otherwise an internal library is required.

To reduce this effort, it would be convenient to include this in the regular SDK.

Additional information

If the feature is something you folks from CircleCI see as useful and worth to include into the SDK, I would also happily create a PR :)

KyleTryon commented 2 years ago

Hey folks,

While we could add the writing-to-file bit to the SDK, this would be a Node-exclusive feature, preventing us from using the SDK in the browser. The SDK is responsible for generating the config as a library but I think it may need to be on the user to decide what to do with that data, of course most users will in fact be writing to a file.

timo-reymann commented 2 years ago

Heya @KyleTryon,

understand that making it only work for Node might be only half of a solution.

Might it be a thing to also add some logic like "if running in browser, use FileSystem API"? Just thinking this could also be a nice feature for e.g. the visual config editor.

Imho it would be a really valuable feature. But if you don't see any value in also adding it for the browser world, lets close this request :)

Cheers, Timo

KyleTryon commented 2 years ago

A solution has been prepared. Thanks for the tip @timo-reymann ! https://github.com/CircleCI-Public/circleci-config-sdk-ts/pull/157

timo-reymann commented 2 years ago

Awesome, thank you @KyleTryon! 🚀