Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Expose APIs for build/dev/export #272

Closed
Rich-Harris opened this issue May 22, 2018 · 1 comment
Closed

Expose APIs for build/dev/export #272

Rich-Harris opened this issue May 22, 2018 · 1 comment

Comments

@Rich-Harris
Copy link
Member

I'm building Sapper Studio, a GUI for creating Sapper apps (it'll make more sense when I share it soon, I promise), and it's revealed a limitation of the current setup — we can only get information about what Sapper is actually doing (e.g. the server is listening, the client is being rebuilt etc) via the process' stdout, which isn't very useful.

If there was an API that the CLI used internally, it would make it much easier to build clients like Sapper Studio.

Something like this:

Dev

import { dev } from 'sapper/api.js';

const watcher = dev({
  // TODO not sure what options we need off the top of my head
});

watcher.on('invalidate', event => {
  const toRebuild = [
    event.invalid.client && 'client',
    event.invalid.server && 'server',
    event.invalid.serviceworker && 'service worker'
  ].filter(Boolean);

  console.log(`${event.file} changed. rebuilding ${toRebuild.join(', ')}`);
});

watcher.on('build', event => {
  console.log(`rebuilt ${event.type} in ${prettyMs(event.duration)}`);

  if (event.warnings.length) {
    console.log(`${event.warnings.length} warnings:`);
    event.warnings.forEach(warning => {
      console.log(warning.toString()); 
    });
  }
});

watcher.on('error', event => {
  console.log(`an error occurred building the ${event.type}`:);
  console.log(event.error.message);
});

watcher.on('ready', event => {
  console.log(`server listening on localhost:${event.port}`);
});

// later...
watcher.close();

Build

import { build } from 'sapper/api.js';

const emitter = build({
  dest: 'build'
  // TODO see above
});

emitter.on('done', event => {
  console.log(`built to ${event.dest} in ${prettyMs(event.duration)}`);

  // future version maybe
  console.log(prettyPrintStats(event.stats));
});

emitter.on('build', event => {
  // fires for each individual build, so you can build a progress UI
  console.log(`built ${event.type} in ${prettyMs(event.duration)}`);

  if (event.warnings.length) {
    console.log(`${event.warnings.length} warnings:`);
    event.warnings.forEach(warning => {
      console.log(warning.toString()); 
    });
  }
});

emitter.on('error', event => {
  // do something with the error
});

Export

import { exporter } from 'sapper/api.js';

// basically the same as build

const emitter = exporter({
  dest: 'www'
  // TODO see above
});

// ...
@Rich-Harris
Copy link
Member Author

Oh and I suppose the dev watcher needs to expose the child process' stdout/stderr. Two possibilities:

watcher.on('ready', event => {
  console.log(`server listening on localhost:${event.port}`);

  event.process.stdout.on('data', data => {
    process.stdout.write(data);
  });

  event.process.stderr.on('data', data => {
    process.stderr.write(data);
  });
});
watcher.stdout.on('data', data => {
  process.stdout.write(data);
});

watcher.stderr.on('data', data => {
  process.stderr.write(data);
});

The second is probably more convenient.

Rich-Harris added a commit that referenced this issue May 28, 2018
[WIP] expose dev, build, export APIs
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant