Skip to content

Commit

Permalink
create a widget bundle for insertion into grist-core via npm packaging (
Browse files Browse the repository at this point in the history
gristlabs#109)

This adds a few more flags to the bundling script, and then
uses them in a `prepare` script to assemble a bundle for insertion
into grist-core. The bundle can be previewed as a tarball with:
  yarn pack
or published to the npm registry as @gristlabs/grist-widget with:
  yarn publish --access public
  • Loading branch information
paulfitz authored Nov 28, 2023
1 parent 0605aeb commit cc049b5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
56 changes: 40 additions & 16 deletions buildtools/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@
* describe the widgets as a plugin to Grist. The
* clash in file names is a little unfortunate.
*
* Call without any arguments. Run from the root of the
* repository. Places results in:
* dist/grist-widget-bundle
* Run from the root of the repository. Places results
* the dist/plugins directory. Call as:
* node buildtools/bundle.js
* To make all widgets unlisted in the UI when bundled
* with Grist do:
* node buildtools/bundle.js --unlisted
* To set the name of the bundle directory, do:
* node buildtools/bundle.js --name the-bundle-name
*
* Will run a temporary server on port 9990.
*
Expand All @@ -41,12 +46,20 @@
*/

const { spawn, spawnSync } = require('child_process');
const { program } = require('commander');
const fs = require('fs');
const fetch = require('node-fetch');
const path = require('path');

program
.option('--unlisted')
.option('-n, --name <string>');

program.parse();
const { unlisted, name } = program.opts();

// This is where we will place our output.
const TARGET_DIR = 'dist/grist-widget-bundle';
const TARGET_DIR = `dist/plugins/${ name ?? 'grist-widget-bundle' }`;

// This is a temporary port number.
const TMP_PORT = 9990;
Expand Down Expand Up @@ -136,6 +149,27 @@ class Bundler {
for (const url of (widget.archive.entrypoints || [])) {
this.downloadUrl(url, widget);
}
// Fix up the URL in the manifest to be relative to where
// the widget material will be moved to.
widget.url = widget.url.replace(
this.assetUrl,
'./' + this.renamedHost
);
// Do same for entrypoint URLs - not really necessary, but feels
// a bit cleaner to keep consistent.
if (widget.archive?.entrypoints) {
widget.archive.entrypoints = widget.archive.entrypoints.map(
e => e.replace(
this.assetUrl,
'./' + this.renamedHost
)
);
}
// Set a timestamp.
widget.bundledAt = this.bundledAt.toISOString();
if (unlisted) {
widget.published = false;
}
this.widgets.push(widget);
}

Expand All @@ -152,7 +186,7 @@ class Bundler {
this.reviseManifest();

fs.writeFileSync(path.join(targetDir, 'manifest.yml'),
'name: Grist Widget Bundle\n' +
`name: ${name}\n` +
'components:\n' +
' widgets: archive/manifest.json\n');
}
Expand Down Expand Up @@ -195,18 +229,8 @@ class Bundler {
// Run the wget command.
const result = spawnSync(cmd, {shell: true, stdio: 'inherit'});
if (result.status !== 0) {
throw new Error('failure');
throw new Error(`failure running: ${cmd}`);
}

// Fix up the URL in the manifest to be relative to where
// the widget material will be moved to.
widget.url = widget.url.replace(
this.assetUrl,
'./' + this.renamedHost
);

// Set a timestamp.
widget.bundledAt = this.bundledAt.toISOString();
}

// Quick sanity check on domains, since we'll be inserting
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grist-widget",
"version": "0.0.2",
"name": "@gristlabs/grist-widget",
"version": "0.0.4",
"description": "A repository of grist custom widgets that have no back-end requirements.",
"scripts": {
"build": "tsc --build && node ./buildtools/publish.js manifest.json",
Expand All @@ -12,14 +12,15 @@
"test": "docker image inspect gristlabs/grist --format 'gristlabs/grist image present' && NODE_PATH=_build SELENIUM_BROWSER=chrome mocha -g \"${GREP_TESTS}\" _build/test/*.js",
"test:ci": "MOCHA_WEBDRIVER_HEADLESS=1 npm run test",
"pretest": "docker pull gristlabs/grist && tsc --build && node ./buildtools/publish.js manifest.json http://localhost:9998",
"bundle": "node ./buildtools/bundle.js"
"prepack": "node ./buildtools/bundle.js --name grist-bundled --unlisted"
},
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "18.11.9",
"@types/node-fetch": "^2.6.4",
"@types/selenium-webdriver": "^4.1.15",
"commander": "^11.1.0",
"live-server": "^1.2.1",
"mocha": "^10.2.0",
"mocha-webdriver": "0.3.1",
Expand All @@ -33,5 +34,10 @@
"require": [
"_build/test/init-mocha-webdriver"
]
}
},
"files": [
"dist/plugins",
"LICENSE",
"README.md"
]
}
2 changes: 1 addition & 1 deletion test/flashcards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('flashcards', function() {
const docId = await grist.upload('test/fixtures/docs/SchoolsSample.grist');
await grist.openDoc(docId);
await grist.toggleSidePanel('right', 'open');
await grist.addNewSection(/Custom/, /School/);
await grist.addNewSection(/Custom/, /School/, {dismissTips: true});
await grist.clickWidgetPane();
await grist.selectCustomWidget(/Flash/);
await grist.setCustomWidgetAccess('full');
Expand Down
2 changes: 1 addition & 1 deletion test/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('inspect', function() {
const docId = await grist.upload('test/fixtures/docs/SchoolsSample.grist');
await grist.openDoc(docId);
await grist.toggleSidePanel('right', 'open');
await grist.addNewSection(/Custom/, /School/);
await grist.addNewSection(/Custom/, /School/, {dismissTips: true});
await grist.clickWidgetPane();
await grist.selectCustomWidget('Inspect Record');
await grist.setCustomWidgetAccess('full');
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ combined-stream@^1.0.8:
dependencies:
delayed-stream "~1.0.0"

commander@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-11.1.0.tgz#62fdce76006a68e5c1ab3314dc92e800eb83d906"
integrity sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==

component-emitter@^1.2.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
Expand Down

0 comments on commit cc049b5

Please sign in to comment.